javascript - Webpack 5 (Webpack Encore): How to run multiple webpack instances on the same page and avoid any conflicts? - Stack

For a pretty large Web application I want to run multiple instances of Webpack (Encore) on the same pag

For a pretty large Web application I want to run multiple instances of Webpack (Encore) on the same page. When I put all the source code into a single Webpack configuration it all works. However when I have two different configurations with the same source code I always get errors like this:

lib.84f46ab6.js:19 Uncaught TypeError: __webpack_require__.n is not a function
    at Object../assets/js/lib.js (lib.84f46ab6.js:19)
    at __webpack_require__ (bootstrap:19)
    at __webpack_exec__ (codemirror.css?f2af:1)
    at codemirror.css?f2af:1
    at Function.__webpack_require__.O (chunk loaded:25)
    at codemirror.css?f2af:1
    at webpackJsonpCallback (jsonp chunk loading:36)
    at Array.forEach (<anonymous>)
    at jsonp chunk loading:49
    at runtime.7c0b88c9.js:146

After some research I found an article about the issue here: /@cliffers/how-to-run-multiple-webpack-instances-on-the-same-page-and-avoid-any-conflicts-4e2fe0f016d1

It basically says:

Webpack does not run in the global namespace…so whats causing the issue? Its a webpack plugin called CommonsChunkPlugin which is used for ansyc on demand loading of code via JSONP. The plugin registers and uses a global function named window.webpackJsonp and thats where conflict occurs.

Simply change the name of the webpackJsonp function for at least one on the webpack instances running and the conflict is resolved.

But they have removed this function in Webpack 5 and I still can not figure out how to configure this correctly. Does anybody know how to solve this with the most recent version of Webpack Encore (currently 1.1.2)?

Part of my config:

// ...
const libConfig = Encore.getWebpackConfig();

// ...

// build the second configuration
const appConfig = Encore.getWebpackConfig();

appConfig.name = 'appConfig';

// Export the final configuration
module.exports = [libConfig,appConfig]

This is how multiple configurations are basically configured in Encore: .html

For a pretty large Web application I want to run multiple instances of Webpack (Encore) on the same page. When I put all the source code into a single Webpack configuration it all works. However when I have two different configurations with the same source code I always get errors like this:

lib.84f46ab6.js:19 Uncaught TypeError: __webpack_require__.n is not a function
    at Object../assets/js/lib.js (lib.84f46ab6.js:19)
    at __webpack_require__ (bootstrap:19)
    at __webpack_exec__ (codemirror.css?f2af:1)
    at codemirror.css?f2af:1
    at Function.__webpack_require__.O (chunk loaded:25)
    at codemirror.css?f2af:1
    at webpackJsonpCallback (jsonp chunk loading:36)
    at Array.forEach (<anonymous>)
    at jsonp chunk loading:49
    at runtime.7c0b88c9.js:146

After some research I found an article about the issue here: https://medium./@cliffers/how-to-run-multiple-webpack-instances-on-the-same-page-and-avoid-any-conflicts-4e2fe0f016d1

It basically says:

Webpack does not run in the global namespace…so whats causing the issue? Its a webpack plugin called CommonsChunkPlugin which is used for ansyc on demand loading of code via JSONP. The plugin registers and uses a global function named window.webpackJsonp and thats where conflict occurs.

Simply change the name of the webpackJsonp function for at least one on the webpack instances running and the conflict is resolved.

But they have removed this function in Webpack 5 and I still can not figure out how to configure this correctly. Does anybody know how to solve this with the most recent version of Webpack Encore (currently 1.1.2)?

Part of my config:

// ...
const libConfig = Encore.getWebpackConfig();

// ...

// build the second configuration
const appConfig = Encore.getWebpackConfig();

appConfig.name = 'appConfig';

// Export the final configuration
module.exports = [libConfig,appConfig]

This is how multiple configurations are basically configured in Encore: https://symfony./doc/current/frontend/encore/advanced-config.html

Share Improve this question edited Aug 8, 2022 at 13:48 Yves M. 31.1k24 gold badges109 silver badges149 bronze badges asked May 14, 2021 at 22:43 BlackbamBlackbam 19.5k31 gold badges111 silver badges165 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

Finally I solved it by adding the following setting to the configuration:

appConfig.output.chunkLoadingGlobal = 'appConfigChunkLoadingGlobal'

As simple as that. It makes sure that the global Webpack function for loading the chunks does not have the same name for both configs. It is the replacement for the old webpackJsonp function.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744706459a4589082.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信