Skip to content

Output statically resolvable dynamic import expressions #15697

@jacobp100

Description

@jacobp100

Feature request

What is the expected behavior?

If I have the following,

import('test.js')

with the config,

module.exports = {
  output: {
    chunkFormat: "module",
  },
  target: ["web", "es2020"],
};

You get the following output

(()=>{var e,t,r,o,n={},f={};function i(e){var t=f[e];if(void 0!==t)return t.exports;var r=f[e]={exports:{}};return n[e](r,r.exports,i),r.exports}i.m=n,t=Object.getPrototypeOf?e=>Object.getPrototypeOf(e):e=>e.__proto__,i.t=function(r,o){if(1&o&&(r=this(r)),8&o)return r;if("object"==typeof r&&r){if(4&o&&r.__esModule)return r;if(16&o&&"function"==typeof r.then)return r}var n=Object.create(null);i.r(n);var f={};e=e||[null,t({}),t([]),t(t)];for(var u=2&o&&r;"object"==typeof u&&!~e.indexOf(u);u=t(u))Object.getOwnPropertyNames(u).forEach((e=>f[e]=()=>r[e]));return f.default=()=>r,i.d(n,f),n},i.d=(e,t)=>{for(var r in t)i.o(t,r)&&!i.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},i.f={},i.e=e=>Promise.all(Object.keys(i.f).reduce(((t,r)=>(i.f[r](e,t),t)),[])),i.u=e=>e+".js",i.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),i.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r={179:0},o=e=>{var t,o,{ids:n,modules:f,runtime:u}=e,a=0;for(t in f)i.o(f,t)&&(i.m[t]=f[t]);for(u&&u(i);a<n.length;a++)o=n[a],i.o(r,o)&&r[o]&&r[o][0](),r[n[a]]=0},i.f.j=(e,t)=>{var n=i.o(r,e)?r[e]:void 0;if(0!==n)if(n)t.push(n[1]);else{var f=import("./"+i.u(e)).then(o,(t=>{throw 0!==r[e]&&(r[e]=void 0),t}));f=Promise.race([f,new Promise((t=>n=r[e]=[t]))]),t.push(n[1]=f)}},i.e(4).then(i.t.bind(i,4,23))})();

Notably - the import("./"+i.u(e))

Is there a way to force this to output a statically analysable import statement (i.e. import("./4.js"))?

What is motivation or use case for adding/changing the behavior?

If you're using webpack to produce libraries that may be used in other build tools (including webpack), statically analysable imports improve optimisations

How should this be implemented in your opinion?

The current config should produce statically analysable imports

-(i.e = (e) =>
-    Promise.all(Object.keys(i.f).reduce((t, r) => (i.f[r](e, t), t), []))),
+(i.e = (e, importFunction) =>
+    Promise.all(Object.keys(i.f).reduce((t, r) => (i.f[r](e, t, importFunction), t), []))),
-(i.u = (e) => e + ".js"),
 (i.f.j = (e, t, importFunction) => {
     var n = i.o(r, e) ? r[e] : void 0;
     if (0 !== n)
     if (n) t.push(n[1]);
     else {
-        var f = import("./" + i.u(e)).then(o, (t) => {
+        var f = importFunction().then(o, (t) => {
         throw (0 !== r[e] && (r[e] = void 0), t);
         });
         (f = Promise.race([f, new Promise((t) => (n = r[e] = [t]))])),
         t.push((n[1] = f));
     }
 }),
-i.e(4).then(i.t.bind(i, 4, 23));
+i.e(4, () => import("./4.js")).then(i.t.bind(i, 4, 23));

Are you willing to work on this yourself?

No - this is way out of my depth 😅

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions