@@ -22,12 +22,8 @@ Install this grunt plugin next to your project's [Gruntfile.js](http://gruntjs.c
2222
2323``` bash
2424yarn add webpack grunt-webpack --dev
25- ```
26-
27- You can still use npm
28-
29- ``` bash
30- npm i webpack grunt-webpack --save-dev
25+ // or
26+ // npm i webpack grunt-webpack --save-dev
3127```
3228
3329If you also want to use the webpack-dev-server task you also need to install ` webpack-dev-server `
@@ -39,15 +35,15 @@ yarn add webpack-dev-server --dev
3935Then add this line to your project's ` Gruntfile.js ` gruntfile:
4036
4137``` javascript
38+ const webpackConfig = require (' ./webpack.config.js' );
39+
4240module .exports = function (grunt ) {
4341
4442 // Project configuration.
45- grunt .initConfig ({
43+ grunt .initConfig ({
4644 ... ,
4745 webpack: {
48- myConfig: {
49- // your webpack config
50- },
46+ myConfig: webpackConfig,
5147 },
5248 ...
5349 });
@@ -121,11 +117,13 @@ The webpack-dev-server options [`host`][5], [`hotOnly`][6], [`inline`][1], [`por
121117
122118### Webpack
123119
120+ #### imported config
121+
124122This is a simple example that requires the webpack config from the config file.
125123It also disables stats in non 'development' environments and enables watch in development.
126124
127125``` javascript
128- const webpackConfig = require (' ./webpack.config' );
126+ const webpackConfig = require (' ./webpack.config.js ' );
129127
130128module .exports = function (grunt ) {
131129 grunt .initConfig ({
@@ -159,25 +157,107 @@ On the command line you can then do the following.
159157
160158> For more examples and information have a look at the [ webpack documentation] [ 9 ] which mostly also applies here besides the noted differences above.
161159
160+ #### Lazy config loading
161+
162+ In some cases you might want to load the webpack config lazy. This can be done by specifying a function as the config value. The first paramter to this function will be the complete grunt config, which can be used in cases where grunt templates do not work (see below).
163+
164+ ``` js
165+ const webpackConfig = require (' ./webpack.config.js' );
166+
167+ module .exports = function (grunt ) {
168+ grunt .initConfig ({
169+ webpack: {
170+ myconfig : () => ({
171+ entry: path .join (__dirname , " entry" ),
172+ output: {
173+ path: __dirname ,
174+ filename: " output.js" ,
175+ },
176+ }),
177+ },
178+ });
179+
180+ grunt .loadNpmTasks (' grunt-webpack' );
181+ };
182+ ```
183+
184+ #### Grunt templates
185+
186+ grunt-webpack supports grunt templates in all string values in it's configuration.
187+
188+ In the next example we use a template for ` output.filename ` .
189+
190+ ``` js
191+ const webpackConfig = require (' ./webpack.config.js' );
192+
193+ module .exports = function (grunt ) {
194+ grunt .initConfig ({
195+ outputFileName: " output.js" ,
196+ webpack: {
197+ myconfig: {
198+ entry: path .join (__dirname , " entry" ),
199+ output: {
200+ path: __dirname ,
201+ filename: " <%= outputFileName %>" ,
202+ },
203+ },
204+ },
205+ });
206+
207+ grunt .loadNpmTasks (' grunt-webpack' );
208+ };
209+ ```
210+
211+ For plugins we cannot support grunt template interpolation, as plugins are class instances which we cannot modify during runtime without breaking them. If you need to use template in a string option to a plugin, you can use lazy config loading and use the supplied config. You can also use grunt inside directly to access utility methods:
212+
213+ ``` js
214+ const webpackConfig = require (' ./webpack.config.js' );
215+
216+ module .exports = function (grunt ) {
217+ grunt .initConfig ({
218+ name: " Webpack" ,
219+ pkg: {
220+ copyright: ' <%= name %>' ,
221+ version: ' 6.55.345' ,
222+ },
223+ webpack: {
224+ myconfig : (config ) => ({
225+ entry: path .join (__dirname , " entry" ),
226+ output: {
227+ path: __dirname ,
228+ filename: " output.js" ,
229+ },
230+ plugins: [
231+ new webpack.BannerPlugin ({
232+ banner: ` /*! ${ config .pkg .copyright } - Version ${ config .pkg .version } dated ${ grunt .template .today ()} */` ,
233+ raw: true ,
234+ }),
235+ ],
236+ }),
237+ },
238+ });
239+
240+ grunt .loadNpmTasks (' grunt-webpack' );
241+ };
242+ ```
243+
162244<h2 align =" center " >Troubleshooting</h2 >
163245
164246### Circular reference detected (.plugins)
165247
166248If you encounter this message it means that one of the plugins which are present in your config have circular references.
167- This is totally fine for webpack, but sadly grunt cannot handle these.
249+ This is not a bug in the plugin and is totally fine for webpack, but sadly grunt cannot handle these.
168250
169251To workaround this problem use lazy config loading, by wrapping your config inside a function.
170252
171253``` js
254+ const webpackConfig = require (' ./webpack.config.js' );
255+
172256module .exports = function (grunt ) {
173257 grunt .initConfig ({
174258 webpack: {
175- myconfig : function () {
176- return {
177- plugins: [... ]
178- };
179- }
180- }
259+ myconfig : () => webpackConfig,
260+ },
181261 });
182262
183263 grunt .loadNpmTasks (' grunt-webpack' );
0 commit comments