Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flag for scope-hoisting in serve/watch mode #5280

Open
sergey-shpak opened this issue Oct 26, 2020 · 9 comments
Open

Flag for scope-hoisting in serve/watch mode #5280

sergey-shpak opened this issue Oct 26, 2020 · 9 comments

Comments

@sergey-shpak
Copy link
Contributor

🐛 bug report

I'm trying to import node package into my app, package has 'main' and 'module' props in it's package.json:

"main": "dist/index.js",
"module": "src/index.js",

as result with parcel@v1 it works ok - package is imported as module,
but using parcel@next or parcel@nightly gives me following error:

@parcel/resolver-default:
"main": "dist/index.js",
>    |           ^^^^^^^^^^^^^^^^^^^^^ './dist/index.js' does not existundefined'

even though dist/index.js doesn't exist, why it's loaded instead of 'module' source?

🎛 Configuration (.babelrc, package.json, cli command)

no config

🤔 Expected Behavior

importing package as module

😯 Current Behavior

importing package main dist

@mischnic
Copy link
Member

In development mode, "main" is used because it has to be transpired to commonjs anyway.

@sergey-shpak
Copy link
Contributor Author

just tested and looks like parcel build works fine, it fails only with parcel watch

I had to walk through the core and found this piece of default-resolver:

// If scope hoisting is enabled, we can get smaller builds using esmodule input, so choose `module` over `main`.
// Otherwise, we'd be wasting time transforming esmodules to commonjs, so choose `main` over `module`.
if (dependency.env.scopeHoist) {
  mainFields.push('module', 'main');
} else {
  mainFields.push('main', 'module');
}

that means that scopeHoist is disabled for parcel watch as result order of entries is different (and that probably the root case of my issue)?

@sergey-shpak
Copy link
Contributor Author

ok, the issue comes from disabled scopeHoist option (--no-scope-hoist) for watch/serve as result using 'main' package property instead of 'module'. Any explanation why es module exports cannot be scope hosted with 'watch' or 'serve'?

@mischnic
Copy link
Member

mischnic commented Oct 26, 2020

Does the comment in the code snippet you posted still not answer your question?

Related issue: #4879

@sergey-shpak
Copy link
Contributor Author

@mischnic it answers about scope hoisting in general and its performance, but not why scope hosting (for es 'module') is disabled by default for watch/serve and there is no option to enable it

@mischnic
Copy link
Member

mischnic commented Oct 26, 2020

Ah, two reasons:

  • it's very slow
  • HMR (with scope-hoisting, individual files cannot be swapped out at runtime)

@sergey-shpak
Copy link
Contributor Author

sergey-shpak commented Oct 26, 2020

@mischnic ok, thanks! Anyway, it would be nice to have more transparency and control over scope hoisting, something opposite param to --no-scope-hoist, like --with-scope-hoist (default for build, and optional for watch/serve)

@mischnic mischnic changed the title Parcel@v2 es module import Flag for scope-hoisting in serve/watch mode Oct 26, 2020
@devongovett
Copy link
Member

Isn't this a bug in the package that there is a "main" field pointing to a non-existent file?

@sergey-shpak
Copy link
Contributor Author

@devongovett it's a main problem for sure, but it has raised what we just discussed here - --no-scope-hoist disables scope hoisting by silently switching package entry point, which looks not so obvious and is not expected.

Anyway, changing it to --with-scope-hoist and giving more control to user or not, it would be nice to mention about changing entry point somewhere in the docs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants