squirrellyjs / squirrelly

Semi-embedded JS template engine that supports helpers, filters, partials, and template inheritance. 4KB minzipped, written in TypeScript ⛺
https://squirrelly.js.org
MIT License
555 stars 81 forks source link

Uncaught ReferenceError: require is not defined in ViteJS #240

Open Kukunin opened 2 years ago

Kukunin commented 2 years ago

Describe the bug The package includes require that causes an exception in a browser bundle built by ViteJS. It happens because of these lines:

https://github.com/squirrellyjs/squirrelly/blob/master/src/file-utils.ts#L1-L2

That file should be imported only in nodejs build, not a browser. At least, it should not be included in ESM format

dahei commented 1 year ago

I was able to make it work via passing transformMixedEsModules: true to the commonJsOptions:

export default defineConfig({
  ...
  build: {
    commonjsOptions: {
      transformMixedEsModules: true
    },
  }
})

Background: https://stackoverflow.com/a/63099935/1574484

jnsprnw commented 1 year ago

To make this solution more visible for people with the same error, I want to add that this is happening with Sveltekit and the Netlify-adapter. @dahei’s solution added to vite.config.js worked for me.

lisilinhart commented 7 months ago

Another solution is to import the browser minified code instead of the regular export of the package when you're running this package in the browser :

import * as Sqrl from 'squirrelly/dist/browser/squirrelly.min.js'