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

exports in package.json break dynamic imports with webpack #3384

Closed
azul opened this issue Nov 2, 2021 · 8 comments
Closed

exports in package.json break dynamic imports with webpack #3384

azul opened this issue Nov 2, 2021 · 8 comments
Labels
autoclose Flag things to future autoclose. bug help welcome Could use help from community parser

Comments

@azul
Copy link

azul commented Nov 2, 2021

Describe the issue/behavior that seems buggy

Webpack currently does not understand wildcard exports when processing dynamic imports.
This is a bug in webpack.

Trying to build a package that uses dynamic imports from highlights.js from version 11 on will result in

Module not found: Error: Package path ./lib/languages is not exported from package <path_to_proj>/node_modules/highlight.js (see exports field in <path_to_proj>/node_modules/highlight.js/package.json)

Sample Code or Instructions to Reproduce

This was already reported here but i think it's unrelated to the bug report it was submitted to.

Attempting to build this code with webpack will result in the error emssage mentioned above:

await import(
  /*
   webpackInclude: /(java|javascript|latex|markdown|python|ruby|typescript|xml|yaml)/,
   webpackChunkName: "lang-[request]"
   */
   `highlight.js/lib/languages/${lang}.js`
)

Expected behavior
The build should succeed providing the languages as separate artifacts to be dynamically imported.

Additional context

It looks like webpack currently does not implement handling the combination of exports specified in package.json and dynamic imports at all. So it might take some time until this is fixed on the webpack side of things. That's why i am proposing to look into a workaround in highlight.js as dynamic imports seem very relevant given the large number of language files.

@azul azul added bug help welcome Could use help from community parser labels Nov 2, 2021
@joshgoebel
Copy link
Member

@azul What about your own easy workaround you posted in the other thread? Did you find an issue with it? I would have expected to have seen it attached to this issue if it's a work-around.

@joshgoebel
Copy link
Member

What are all other Webpack uses doing? We have been using exports since v11, seems hard to believe no one has successfully upgrade until now? As a work-around (if there are no others) I would suggest using a pre-built version of Highlight.js and just throwing that into the build system whole rather than trying to let webpack understand the entire structure of the library of 250 independent files.

Of course as mentioned this is truly a Webpack bug and needs to just be fixed on that side.

@joshgoebel
Copy link
Member

Ping.

@joshgoebel joshgoebel added the autoclose Flag things to future autoclose. label Nov 16, 2021
@azul
Copy link
Author

azul commented Nov 16, 2021

Thanks for the reminder.
I found a workaround for the builds by using relative paths in the import statement: nextcloud/text#1942 (review).
It's ugly but it did the trick.

There are other dependency issues between version 11 of highlight js and tiptap1 that mean we need to stick to version 10 until we managed to migrate to tiptap 2. But that's unrelated so i'll close this issue.

@azul azul closed this as completed Nov 16, 2021
@Hypnosphi
Copy link

Hypnosphi commented Dec 14, 2021

@azul unfortunately this doesn't work for libraries, because npm can flatten packages:

App
  node_modules
    library (depends on highlight.js)
      node_modules
        // no highlight.js here
    highlight.js

@Hypnosphi
Copy link

Hypnosphi commented Dec 14, 2021

We have been using exports since v11, seems hard to believe no one has successfully upgrade until now?

I can't upgrade because I use dynamic imports: https://github.com/JetBrains/ring-ui/blob/master/components/code/code.js#L15. It works with v10 but not with v11

@joshgoebel
Copy link
Member

joshgoebel commented Dec 14, 2021

@Hypnosphi Have you commented on the bug/issue over in webpack?

I'm also potentially assuming there is a good/reasonable workaround (since this issue is so quiet), but that no one has popped up yet to tell us what it is.

@Hypnosphi
Copy link

@joshgoebel no, but they weren't asking how people deal with it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autoclose Flag things to future autoclose. bug help welcome Could use help from community parser
Projects
None yet
Development

No branches or pull requests

3 participants