1

I have this code

import('./myfile.js')
.then(m=>console.log(m.default))
.catch(err=>console.error(err))

webpack bundles "myfile.js", but I want to copy this file to 'dist' and import it dynamically, i.e. I want to avoid compiling this import() statement.

note that this code is a part of a function and the file 'myfile.js' is provided by a function argument, so I cannot add the path to the webpack's externals array.

I tried the following approaches:

  • using webpac's magic comments /* webpackIgnore: true, webpackMode: "lazy" */
  • provide a variable path
let file = 'myfile.js'
import('./' + file)
  • provide the path as a function call
let getPath = path=>path
import(getPath('./myfile.js'))
  • put the import statement inside a function
function load(path){
  return import('./' + path)
}

load('myfile.js')
Sh eldeeb
  • 1,589
  • 3
  • 18
  • 41
  • 1
    Does this answer your question? [Dynamic loading of external modules in webpack fails](https://stackoverflow.com/questions/56691391/dynamic-loading-of-external-modules-in-webpack-fails) – tevemadar Jan 15 '23 at 15:04
  • thanks for your suggestion @tevemadar. the answer you mentioned explains how webpack works with dynamic imports, but unfortunately, doesn't provide a solution to my use case. – Sh eldeeb Jan 20 '23 at 14:05

0 Answers0