parcel-bundler / parcel

The zero configuration build tool for the web. 📦🚀
https://parceljs.org
MIT License
43.37k stars 2.27k forks source link

Parcel 2: couldn't import mjs files #3432

Closed orlov-vo closed 4 years ago

orlov-vo commented 5 years ago

🐛 bug report

I got a problem in parcel@2.0.0-alpha.2.1 when I import a module which contain index.mjs and index.js. For example svelte@3.12.1 - check my example

-> % npm start

> parcel2-example-bug@ start /Users/vladislav/dev/example-svelte3
> parcel build src/index.html --no-cache --no-minify

× External modules are not supported when building for browser
at Object.generateExternalImport (./node_modules/@parcel/scope-hoisting/lib/formats/global.js:37:9)
at exit (./node_modules/@parcel/scope-hoisting/lib/link.js:540:23)
at NodePath._call (./node_modules/@babel/traverse/lib/path/context.js:53:20)
at NodePath.call (./node_modules/@babel/traverse/lib/path/context.js:40:17)
at NodePath.visit (./node_modules/@babel/traverse/lib/path/context.js:97:8)
at TraversalContext.visitQueue (./node_modules/@babel/traverse/lib/context.js:118:16)
at TraversalContext.visitSingle (./node_modules/@babel/traverse/lib/context.js:90:19)
at TraversalContext.visit (./node_modules/@babel/traverse/lib/context.js:146:19)
at Function.traverse.node (./node_modules/@babel/traverse/lib/index.js:94:17)
at traverse (./node_modules/@babel/traverse/lib/index.js:76:12)

When I manualy remove index.mjs from svelte it works fine

mischnic commented 5 years ago

I think this is rather related to the sub packages inside of svelte:

node_modules/svelte/
├── internal
│   ├── index.mjs
│   └── package.json
├── package.json
└── store
    ├── index.mjs
    └── package.json
orlov-vo commented 4 years ago

I updated the issue and the example with fresh Parcel version.

mischnic commented 4 years ago

The main/module/browser fields in package.json turn on library mode: node modules aren't bundled (as you would expect for a library...). It tried that in this case as well. But having imports to bare specifiers on a webpage wouldn't work, therefore the error.

To fix this:

diff --git package.json package.json
index 7570c2c..385ad5b 100644
--- package.json
+++ package.json
@@ -1,6 +1,5 @@
 {
   "name": "parcel2-example-bug",
-  "browser": "dist/index.html",
   "source": "src/index.html",
   "private": true,
   "scripts": {