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

minify unexpect transform #2089

Closed
poyoho opened this issue Mar 8, 2022 · 6 comments
Closed

minify unexpect transform #2089

poyoho opened this issue Mar 8, 2022 · 6 comments

Comments

@poyoho
Copy link

poyoho commented Mar 8, 2022

Describe the bug

run for this config

// run `node index.js` in the terminal
const esbuild = require('esbuild');

const code = `
const module = import('./a.js')
`;

esbuild
  .transform(code, {
    minify: true,
    target: 'chrome60',
  })
  .then((result) => {
    // {
    //   warnings: [],
    //   code: 'var i=Object.create;var c=Object.defineProperty;var l=Object.getOwnPropertyDescriptor;var n=Object.getOwnPropertyNames;var p=Object.getPrototypeOf,r=Object.prototype.hasOwnProperty;var s=o=>c(o,"__esModule",{value:!0});var u=(o,m,e,d)=>{if(m&&typeof m=="object"||typeof m=="function")for(let t of n(m))!r.call(o,t)&&(e||t!=="default")&&c(o,t,{get:()=>m[t],enumerable:!(d=l(m,t))||d.enumerable});return o},a=(o,m)=>u(s(c(o!=null?i(p(o)):{},"default",!m&&o&&o.__esModule?{get:()=>o.default,enumerable:!0}:{value:o,enumerable:!0})),o);const module=Promise.resolve().then(()=>a(require("./a.js")));\n',
    //   map: ''
    // }
    console.log(result);
  });

esbuild transform dynamic import to require.

I think need to polyfill import in chrome60

like this:

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

to

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

Reproduction

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

@poyoho
Copy link
Author

poyoho commented Mar 8, 2022

vitejs/vite#7179

@evanw
Copy link
Owner

evanw commented Mar 8, 2022

This is because import() expressions are only supported in Chrome 63+: https://caniuse.com/es6-module-dynamic-import.

@poyoho
Copy link
Author

poyoho commented Mar 8, 2022

yep!

But transform it to require can't to run too. Do you have a solution?

@evanw
Copy link
Owner

evanw commented Mar 9, 2022

The transformation of import to require just allows the code to be parsed without causing a syntax error. This must be done by esbuild because esbuild is in charge of syntax-related stuff.

From there, you can use that to write some code that loads the file however you'd like for it to be loaded. You could do this for example, which loads the file as a string:

window.require = function (what) {
  return {
    then: function (cb) {
      return fetch(what)
        .then(function (r) { return r.text() })
        .then(function (text) { return { 'default': text } })
        .then(cb)
    },
  }
}

@swandir
Copy link

swandir commented Mar 13, 2022

Dynamic imports can be polyfilled with reliance on a global function name.

Perhaps the function name should be configurable in ESBuild (can't seem to find an option in the docs)? And maybe substitution should not happen in transform mode if the option is not supplied — unless format is cjs probably.

@evanw
Copy link
Owner

evanw commented Jun 18, 2022

With the upcoming release you will be able to disable this transform with --supported:dynamic-import=true. This will tell esbuild to pretend it’s supported even though it’s actually not, which will leave import() in the output.

Closing as fixed because there is now a way to do this.

@evanw evanw closed this as completed Jun 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants