@@ -71,8 +71,87 @@ module.exports = {
71
71
/* ...*/
72
72
};
73
73
```
74
+ When we do have multiple configurations in mind for different environments, the easiest way is to write seperate js files for
75
+ each environment. For example:
76
+ ##config/dev.js
77
+ ``` js
78
+ module .exports = function (env ) {
79
+ debug: true ,
80
+ devtool: ' cheap-module-source-map' ,
81
+ output: {
82
+ path: path .join (__dirname , ' /../dist/assets' ),
83
+ filename: ' [name].bundle.js' ,
84
+ publicPath: publicPath,
85
+ sourceMapFilename: ' [name].map'
86
+ },
87
+ devServer: {
88
+ port: 7777 ,
89
+ host: ' localhost' ,
90
+ historyApiFallback: true ,
91
+ noInfo: false ,
92
+ stats: ' minimal' ,
93
+ publicPath: publicPath
94
+ },
95
+ });
96
+ ```
97
+ ##config/prod.js
98
+ ``` js
99
+ module .exports = function (env ) {
100
+ debug: false ,
101
+ output: {
102
+ path: path .join (__dirname , ' /../dist/assets' ),
103
+ filename: ' [name].bundle.js' ,
104
+ publicPath: publicPath,
105
+ sourceMapFilename: ' [name].map'
106
+ },
107
+ plugins: [
108
+ new DedupePlugin (),
109
+ new UglifyJsPlugin ({
110
+ beautify: false ,
111
+ mangle: { screw_ie8 : true , keep_fnames: true },
112
+ compress: { screw_ie8: true },
113
+ comments: false
114
+ })
115
+ ]
116
+ });
117
+ ```
118
+ Have our webpack.config.js has the following snippet:
119
+ ``` js
120
+ function buildConfig (env ) {
121
+ var config;
122
+ switch (env) {
123
+ case ' prod' :
124
+ config = require (' ./config/prod.js' )({env: ' prod' });
125
+ break ;
126
+ case ' qa' :
127
+ config = require (' ./config/qa.js' )({env: ' qa' });
128
+ break ;
129
+ case ' test' :
130
+ config = require (' ./config/test.js' )({env: ' test' });
131
+ break ;
132
+ case ' dev' :
133
+ config = require (' ./config/dev.js' )({env: ' dev' });
134
+ break ;
135
+ default :
136
+ config = require (' ./config/dev.js' )({env: ' dev' });
137
+ break ;
138
+ }
139
+ return config;
140
+ }
141
+
142
+ module .exports = buildConfig (env);
143
+ ```
144
+ And from our package.json, where we build our application using webpack, the command goes like this:
145
+ ``` js
146
+ " build:dev" : " webpack --env=dev --progress --profile --colors" ,
147
+ " build:dist" : " webpack --env=qa --progress --profile --colors" ,
148
+ ```
149
+
150
+ You could see that we passed the environment variable to our webpack.config.js file. From there we used a simple
151
+ switch-case to build for the environment we passed by simply loading the right js file.
74
152
75
- That's it! You're all set to ship production code.
153
+ An advanced approach would be to have a base configuration file, put in all common functionalities,
154
+ and then have environment specific files and simply use 'webpack-merge' to merge them. This would help to avoid code repetitions.
76
155
77
- ?> TODO: Add reading reference link to "How to manage multiple configurations"
78
- ?> TODO: Add reference link to "Splitting configuration"
156
+ To manage different variables for different environments, the approach is to use 'Define plugin':
157
+ Create global constants. Have their values decided in the environment specific files.
0 commit comments