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
Dynamic imports don't work when exports
specified in package.json and aliases
#13865
Comments
Yep, bug, feel free to send a fix |
@alexander-akait can you give me some pointers on where to start looking? I'm clueless about what to do to fix this. |
I think you need to start debug here https://github.com/webpack/webpack/blob/main/lib/ContextModuleFactory.js#L155 |
This may is a bit more involved to fix... |
should this even work.. spec disallow requesting directories.. |
@vankop this problem is not for importing directories, it occurs when dynamically importing a file (which is present in the |
Not sure if related to this, but I get this error from my Next.js app when using Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/languages/arduino' is not defined by "exports" in /xxx/node_modules/highlight.js/package.json imported from /xxx/node_modules/lowlight/lib/common.js Then when trying to reproduce in code sandbox, I get a different error: https://codesandbox.io/s/interesting-fire-thu88?file=/pages/index.js |
@dominicarrojado the Next.js error you mention is the same that I faced. |
Bumps [highlight.js](https://github.com/highlightjs/highlight.js) from 10.7.3 to 11.3.1. - [Release notes](https://github.com/highlightjs/highlight.js/releases) - [Changelog](https://github.com/highlightjs/highlight.js/blob/main/CHANGES.md) - [Commits](highlightjs/highlight.js@10.7.3...11.3.1) --- updated-dependencies: - dependency-name: highlight.js dependency-type: direct:production update-type: version-update:semver-major ... Work around a bug in webpack with dynamic imports: webpack/webpack#13865 Signed-off-by: dependabot[bot] <support@github.com>
update: however this breaks the dynamic import and thus the chunks for these packages will not be build. I tried various things ( |
Bumps [highlight.js](https://github.com/highlightjs/highlight.js) from 10.7.3 to 11.3.1. - [Release notes](https://github.com/highlightjs/highlight.js/releases) - [Changelog](https://github.com/highlightjs/highlight.js/blob/main/CHANGES.md) - [Commits](highlightjs/highlight.js@10.7.3...11.3.1) --- updated-dependencies: - dependency-name: highlight.js dependency-type: direct:production update-type: version-update:semver-major ... Work around a bug in webpack with dynamic imports: webpack/webpack#13865 Signed-off-by: dependabot[bot] <support@github.com>
Bumps [highlight.js](https://github.com/highlightjs/highlight.js) from 10.7.3 to 11.3.1. - [Release notes](https://github.com/highlightjs/highlight.js/releases) - [Changelog](https://github.com/highlightjs/highlight.js/blob/main/CHANGES.md) - [Commits](highlightjs/highlight.js@10.7.3...11.3.1) --- updated-dependencies: - dependency-name: highlight.js dependency-type: direct:production update-type: version-update:semver-major ... Work around a bug in webpack with dynamic imports: webpack/webpack#13865 Signed-off-by: dependabot[bot] <support@github.com>
I ran the build again with Trace taken with `--profile`
|
The one entry in `dependencies` at that point
I think there should not be an attempts to resolve |
Trace of the inner error
|
#14598 was an attempt at fixing this by ignoring exports in context resolvers. While this fixes the build it's also too permissive. |
Use path to the node_modules folder to work around webpack/webpack#13865 Bumps [highlight.js](https://github.com/highlightjs/highlight.js) from 10.7.3 to 11.3.1. - [Release notes](https://github.com/highlightjs/highlight.js/releases) - [Changelog](https://github.com/highlightjs/highlight.js/blob/main/CHANGES.md) - [Commits](highlightjs/highlight.js@10.7.3...11.3.1) --- updated-dependencies: - dependency-name: highlight.js dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com>
Use path to the node_modules folder to work around webpack/webpack#13865 Bumps [highlight.js](https://github.com/highlightjs/highlight.js) from 10.7.3 to 11.3.1. - [Release notes](https://github.com/highlightjs/highlight.js/releases) - [Changelog](https://github.com/highlightjs/highlight.js/blob/main/CHANGES.md) - [Commits](highlightjs/highlight.js@10.7.3...11.3.1) --- updated-dependencies: - dependency-name: highlight.js dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com>
Use path to the node_modules folder to work around webpack/webpack#13865 Bumps [highlight.js](https://github.com/highlightjs/highlight.js) from 10.7.3 to 11.3.1. - [Release notes](https://github.com/highlightjs/highlight.js/releases) - [Changelog](https://github.com/highlightjs/highlight.js/blob/main/CHANGES.md) - [Commits](highlightjs/highlight.js@10.7.3...11.3.1) --- updated-dependencies: - dependency-name: highlight.js dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com>
Use path to the node_modules folder to work around webpack/webpack#13865 Bumps [highlight.js](https://github.com/highlightjs/highlight.js) from 10.7.3 to 11.3.1. - [Release notes](https://github.com/highlightjs/highlight.js/releases) - [Changelog](https://github.com/highlightjs/highlight.js/blob/main/CHANGES.md) - [Commits](highlightjs/highlight.js@10.7.3...11.3.1) --- updated-dependencies: - dependency-name: highlight.js dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com>
I got a temporary hack for this issue. import file normaly from package in a file then do dynamic import of that file. src/components/externalForm/fwkComponentOnticFormRenderer.tsx
|
@ichangela-ontic unfortunately it doesn't help with cases like the one mentioned in this issue, where the import string itself is dynamic:
|
If you add a webpack alias to // this uses webpack-chain, but the idea is the same
config.resolve.alias.set('highlight-js-alias', path.dirname(require.resolve('highlight.js/package.json'))); then you can do const file = await import(
/* webpackChunkName: "highlight-js-[request]" */
`highlight-js-alias/lib/languages/${file}`
); The benefit of this, is that in a monorepo setup for example, |
exports
specified in package.jsonexports
specified in package.json and aliases
Also need to fix #17737 |
Bug report
What is the current behavior?
The same problem in
require.context
andimport.meta.webpackContext
, so need to be fixed tooFor example,
highlight.js
has the following inexports
ofpackage.json
:I tried to import a language dynamically using the following code in a Vue 3 / TS 4 app:
This leads to the following error:
If the current behavior is a bug, please provide the steps to reproduce.
npm install
.npm run serve
.What is the expected behavior?
Replacing
${langName.value}
with a literal value works and imports the file as expected:Dynamic import should work equally well.
Workarounds
The comment highlightjs/highlight.js#3223 (comment) contains possible workarounds, if that helps to narrow the problem.
Other relevant information:
webpack version: 5
Node.js version: 14
Operating System: mac OS
Additional tools: TypeScript 4, Vue 3, Babel 7
The text was updated successfully, but these errors were encountered: