Infrasym / laravel-favicon-vite-plugin

MIT License
3 stars 0 forks source link

ESM export incorrectly uses require #1

Open vchernin opened 1 year ago

vchernin commented 1 year ago

laravel-vite-plugin recently added support for ESM, e.g. so one can set "type": "module" in the root package.json of a laravel project. This gives a hint to node that ESM should be assumed for all *.js files.

It seems tsup as configured for this project over bundles dependencies, and results in a ESM export that cannot be used in a ESM laravel project.

npm run build
failed to load config from /my-laravel-project/vite.config.js
error during build:
Error: Dynamic require of "events" is not supported
    at file:///my-laravel-project/node_modules/@infrasym/laravel-favicon-vite-plugin/dist/index.js:12:9
    at Object.<anonymous> (file:///my-laravel-project/node_modules/@infrasym/laravel-favicon-vite-plugin/dist/index.js:5595:16)
    at node_modules/xml2js/lib/parser.js (file:///my-laravel-project/node_modules/@infrasym/laravel-favicon-vite-plugin/dist/index.js:5951:8)
    at __require2 (file:///my-laravel-project/node_modules/@infrasym/laravel-favicon-vite-plugin/dist/index.js:15:50)
    at Object.<anonymous> (file:///my-laravel-project/node_modules/@infrasym/laravel-favicon-vite-plugin/dist/index.js:5975:16)
    at node_modules/xml2js/lib/xml2js.js (file:///my-laravel-project/node_modules/@infrasym/laravel-favicon-vite-plugin/dist/index.js:5990:8)
    at __require2 (file:///my-laravel-project/node_modules/@infrasym/laravel-favicon-vite-plugin/dist/index.js:15:50)
    at file:///my-laravel-project/node_modules/@infrasym/laravel-favicon-vite-plugin/dist/index.js:5996:29
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

As a workaround you can change tsup to tsup-node in the package.json build script. Since favicons is only a devDependency here that needed to be installed as well in my project.

This could just be a weird tsup bug, but at the same time wouldn't it be best to avoid bundling so many dependencies in the first place in the outputted dist? Right now the 2 outputted dist files index.cjs and index.js have a size of ~500 KB combined, instead of ~7 KB with tsup-node (of course this ignores the size of the favicons package that may need to be installed).

CallumBee commented 3 weeks ago

@DevJMD Is this a dead project as this error is still here and makes this useful script not usable.