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

esbuild minify unexpect transform #7179

Closed
7 tasks done
poyoho opened this issue Mar 5, 2022 · 10 comments · Fixed by #9105
Closed
7 tasks done

esbuild minify unexpect transform #7179

poyoho opened this issue Mar 5, 2022 · 10 comments · Fixed by #9105

Comments

@poyoho
Copy link
Member

poyoho commented Mar 5, 2022

Describe the bug

when I use config for vite

export default {
build: {
    target: 'chrome60'
  }
}

esbuild transform dynamic import to require.

like this:

import('./workerImport-a2c311d3.js')

to

require('./workerImport-a2c311d3.js')

may be chrome63 support dynamic import. chrome60 no support. But I don't think this conversion meets the expectations.

I think need to polyfill import in chrome63

Reproduction

https://stackblitz.com/edit/node-qfc2yz?devtoolsheight=33&file=index.js

System Info

any

Used Package Manager

pnpm

Logs

No response

Validations

@jplhomer
Copy link
Contributor

jplhomer commented Mar 8, 2022

But I don't think this conversion meets the expectations.

Can you describe what the expectations are?

@poyoho
Copy link
Member Author

poyoho commented Mar 8, 2022

polyfill?

@poyoho
Copy link
Member Author

poyoho commented Mar 8, 2022

I write a minirepo

https://stackblitz.com/edit/node-qfc2yz?devtoolsheight=33&file=index.js

@swandir
Copy link
Contributor

swandir commented Mar 13, 2022

If you are bundling an app, you might use @vitejs/plugin-legacy for that. It'll take care of dynamic imports.

For libraries, relying on Vite to handle dynamic imports, I believe, is not recommended.

Is build.target even intended for such use cases?

@bluwy
Copy link
Member

bluwy commented Jun 24, 2022

With esbuild supporting the supported option, I tried the repro with:

    supported: {
      'dynamic-import': true,
    },

and it seems to fix the issue. Perhaps we should do something similar to #8665 (this PR affects optimizeDeps only)

@sapphi-red
Copy link
Member

I think import needs to be preserved by esbuild, but it needs renamed in the final output. import is a keyword in js and to polyfill dynamic import, it needs to be a different name.
https://github.com/GoogleChromeLabs/dynamic-import-polyfill#:~:text=The%20name%20of%20the%20dynamic%20import%20polyfill%20function%20added%20to%20the%20global%20scope.

Note that renderDynamicImport/dynamicImportFunction cannot be used because Vite uses import in renderChunk by build-import-analysis which runs after them (#8588).

@bluwy
Copy link
Member

bluwy commented Jul 14, 2022

I think polyfilling import would be the area of plugin-legacy which uses systemjs alltogether. In any case, preserving import seems more correct than using require, unless someone is polyfilling require for some reason, which they should've used plugin-legacy in the first place 🤔

@swandir
Copy link
Contributor

swandir commented Jul 14, 2022

I think polyfilling import would be the area of plugin-legacy

Sounds perfectly reasonable since dynamic imports are the new baseline for Vite

@sapphi-red
Copy link
Member

I think polyfilling import would be the area of plugin-legacy

I think so too. But isn't preserving import confusing? If it does not work without plugin-legacy, I expect Vite to warn that.
That said, I think disabling import transformation will cause more understandable error (Unexpected token: import) than now (require is not defined). (Not sure if the error messages I wrote is same with the actual one.) So I'm for #9105.

@bluwy
Copy link
Member

bluwy commented Jul 14, 2022

Yeah I think the error would be more obvious if we preserve it. And this also helps to make sure that the modern bundle can be used by modern browsers too. If we downlevel to require it'll break even on chrome100.

I tried to auto issue a warning too but I'm not sure if there's a neat way of doing so with esbuild.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants