Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating AMD modules with webpack and CommonsChunkPlugin

Tags:

webpack

amd

I am developing multiple plugins to a system that loads these plugins as AMD modules (with requirejs). Plugins are written as ES6 modules and packed with webpack (output.libraryTarget = 'amd'). Everything works as expected so far.

But plugins share quite a lot of common code that is bundled into every entry. I tried to use CommonsChunkPlugin webpack plugin but initial chunk contains code that is not AMD module. However other entry points relaying on common chuck are still generated correctly. Also documentation says that it is not good idea to have multiple initial chunks (with JSONp runtime) on one page - there is possibility that another plugin author would use same approach.

Is there a way to optimize this use case in AMD compatible manner? It seems to me as a bug in CommonsChunkPlugin that the initial entry is not AMD (but still its code is not safe to be loaded in plugin env)... Is there a configuration I am missing?

like image 203
ahz Avatar asked Feb 12 '16 10:02

ahz


People also ask

Does webpack build node_modules?

When you set target: 'node' in webpack config, webpack will not bundle the built-ins module of Node. js. path is a built-in module of Node. js, it doesn't come from the node_modules directory.

Does webpack support CommonJS?

Webpack supports the following module types natively: ECMAScript modules. CommonJS modules.

Does webpack bundle node modules?

Webpack allows you to define externals - modules that should not be bundled. When bundling with Webpack for the backend - you usually don't want to bundle its node_modules dependencies. This library creates an externals function that ignores node_modules when bundling in Webpack.

What are 4 core concept of webpack?

There are four basic concepts in webpack: entry , output , modules and plug-ins . These configurations are added in webpack.


1 Answers

I'm not quite sure that understand you correctly. But if you want to build your bundle as AMD module, than you must config it as a library:

...
output: {
    ...
    library: true,
    libraryTarget: 'umd'
},
like image 179
Kreozot Avatar answered Jan 02 '23 11:01

Kreozot