webpack / webpack.js.org

Repository for webpack documentation and more!
https://webpack.js.org
Creative Commons Attribution 4.0 International
2.21k stars 3.31k forks source link

Weird behaviour with dynamic imports #3946

Open yoosif0 opened 4 years ago

yoosif0 commented 4 years ago

Bug report

What is the current behavior? Replacing a static string to a variable string in the dynamic import path change build time and change the size of the resulting build files.

The code snippet below results in fast and smaller builds

const x = 'defaultHome.json'
const dir = 'secondaryApp'
import(`../../secondaryApp/fixtures/${x}`).then(y => {
  console.log(y)
})

image

The code snippet below results in slow and larger builds

const x = 'defaultHome.json'
const dir = 'secondaryApp'
import(`../../${dir}/fixtures/${x}`).then(y => {
  console.log(y)
})

image

Here is a repo to reproduce the bug. You only need to npm install then npm start

What is the expected behavior? There should be no difference in the resulting build files and time to build

Other relevant information: webpack version: 4.44.1 Node.js version: 12.18.3 Operating System: Windows Additional tools:

alexander-akait commented 4 years ago

It is expected, because we don't know value of variable, you can use const dir = myComplexFunction() ? 'secondaryApp' : 'firstApp', we are static analyzer, not dynamic, here docs https://webpack.js.org/api/module-methods/#dynamic-expressions-in-import, so we include all files from ../../ in the second example

alexander-akait commented 4 years ago

I think we should improve docs for this example