vercel / ncc

Compile a Node.js project into a single file. Supports TypeScript, binary addons, dynamic requires.
https://npmjs.com/@vercel/ncc
MIT License
9.27k stars 291 forks source link

Module parse failed when using `--asset-builds` with socket.io (ESM/CJS) #837

Open tmtron opened 2 years ago

tmtron commented 2 years ago

e.g. the newer socket.io packages contain esm and cjs .js files in their dist folder (4.4.0)

When we use --asset-builds (we need this, because of other dependencies, that must be compiled), we get an error:

ncc: Compiling file client-dist/socket.io.esm.min.js into CJS
Error: Module parse failed: 'import' and 'export' may appear only with 'sourceType: module' (6:33230)
File was processed with these loaders:
 * ../../../node_modules/@vercel/ncc/dist/ncc/loaders/empty-loader.js
 * ../../../node_modules/@vercel/ncc/dist/ncc/loaders/relocate-loader.js
 * ../../../node_modules/@vercel/ncc/dist/ncc/loaders/shebang-loader.js
You may need an additional loader to handle the result of these loaders.

Is there a way to only exclude the *.esm*.js files

ncc version: 0.33.1

Backreference: dm#915

styfle commented 2 years ago

I can't reproduce the issue.

mkdir example
cd example
npm init -y
npm install socket.io
echo "const s = require('http').createServer();const io = require('socket.io')(s);console.log(typeof io)" > index.js
ncc build index.js
rm -rf node_modules
node dist/index.js

Can you share how to reproduce?

tmtron commented 2 years ago

@styfle you must add the --asset-builds argument to see the issue:
ncc build --asset-builds index.js

styfle commented 2 years ago

I see, thanks!

The bug is likely somewhere in this if block: https://github.com/vercel/ncc/blob/f4b546e8078f31f1eed0cc69fd31603bb37a3386/src/index.js#L588

If you want to fix it, feel free to submit a PR, thanks! 👍

52flutter commented 2 years ago

any update?

the-homeless-god commented 9 months ago

any update?