Any way to use webpack to load a resource at runtime?

前端 未结 3 1117
深忆病人
深忆病人 2020-12-28 15:07

I have a single .json file that contains configuration stuff that I would like to reference from another script file using the typical import/require syntax. Currently I\'m

3条回答
  •  [愿得一人]
    2020-12-28 16:01

    I think what you want is require.ensure, webpack's code splitting. The modules that you 'ensure' are put into a separate bundle, and when your 'ensure' executes at runtime, the webpack runtime automatically fetches the bundle via ajax. Note the callback syntax for ensure -- your callback runs when the bundle has finished loading. You still need to require the desired modules at that point; .ensure just makes sure they're available.

    Code splitting is one of webpack's major features, it lets you load only what you need at any given time. There's plugins etc. to optimize the multiple bundles as well.

提交回复
热议问题