]>
Commit | Line | Data |
---|---|---|
b20b5fed C |
1 | /** |
2 | * @author: @AngularClass | |
3 | */ | |
4 | ||
5 | const helpers = require('./helpers') | |
6 | const webpackMerge = require('webpack-merge') // used to merge webpack configs | |
7 | const commonConfig = require('./webpack.common.js') // the settings that are common to prod and dev | |
202e7223 | 8 | const videoEmbedConfig = require('./webpack.video-embed.js') |
b20b5fed C |
9 | |
10 | /** | |
11 | * Webpack Plugins | |
12 | */ | |
b20b5fed | 13 | const DefinePlugin = require('webpack/lib/DefinePlugin') |
3523b64a | 14 | const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin |
4d19d2f1 | 15 | const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin') |
c16ce1de | 16 | const NormalModuleReplacementPlugin = require('webpack/lib/NormalModuleReplacementPlugin') |
f7ac0f84 | 17 | const OptimizeJsPlugin = require('optimize-js-plugin') |
8635a2c7 | 18 | const HashedModuleIdsPlugin = require('webpack/lib/HashedModuleIdsPlugin') |
04de542a | 19 | const UglifyJsPlugin = require('uglifyjs-webpack-plugin') |
b20b5fed C |
20 | /** |
21 | * Webpack Constants | |
22 | */ | |
23 | const ENV = process.env.NODE_ENV = process.env.ENV = 'production' | |
24 | const HOST = process.env.HOST || 'localhost' | |
25 | const PORT = process.env.PORT || 8080 | |
04de542a C |
26 | const AOT = process.env.BUILD_AOT || helpers.hasNpmFlag('aot') |
27 | const METADATA = { | |
b20b5fed C |
28 | host: HOST, |
29 | port: PORT, | |
30 | ENV: ENV, | |
1840c2f7 | 31 | HMR: false, |
04de542a | 32 | AOT: AOT, |
1840c2f7 | 33 | API_URL: '' |
04de542a | 34 | } |
b20b5fed | 35 | |
ad22074a | 36 | module.exports = function (env) { |
202e7223 C |
37 | return [ |
38 | videoEmbedConfig({ env: ENV }), | |
4d19d2f1 | 39 | |
202e7223 | 40 | webpackMerge(commonConfig({ env: ENV }), { |
ad22074a | 41 | /** |
202e7223 | 42 | * Developer tool to enhance debugging |
4d19d2f1 | 43 | * |
202e7223 C |
44 | * See: http://webpack.github.io/docs/configuration.html#devtool |
45 | * See: https://github.com/webpack/docs/wiki/build-performance#sourcemaps | |
4d19d2f1 | 46 | */ |
202e7223 | 47 | devtool: 'source-map', |
ad22074a C |
48 | |
49 | /** | |
202e7223 | 50 | * Options affecting the output of the compilation. |
4d19d2f1 | 51 | * |
202e7223 | 52 | * See: http://webpack.github.io/docs/configuration.html#output |
4d19d2f1 | 53 | */ |
202e7223 | 54 | output: { |
ad22074a | 55 | |
202e7223 C |
56 | /** |
57 | * The output directory as absolute path (required). | |
58 | * | |
59 | * See: http://webpack.github.io/docs/configuration.html#output-path | |
60 | */ | |
61 | path: helpers.root('dist'), | |
ad22074a | 62 | |
202e7223 C |
63 | /** |
64 | * Specifies the name of each output file on disk. | |
65 | * IMPORTANT: You must not specify an absolute path here! | |
66 | * | |
67 | * See: http://webpack.github.io/docs/configuration.html#output-filename | |
68 | */ | |
69 | filename: '[name].[chunkhash].bundle.js', | |
70 | ||
71 | /** | |
72 | * The filename of the SourceMaps for the JavaScript files. | |
73 | * They are inside the output.path directory. | |
74 | * | |
75 | * See: http://webpack.github.io/docs/configuration.html#output-sourcemapfilename | |
76 | */ | |
77 | sourceMapFilename: '[file].map', | |
ad22074a | 78 | |
202e7223 C |
79 | /** |
80 | * The filename of non-entry chunks as relative path | |
81 | * inside the output.path directory. | |
82 | * | |
83 | * See: http://webpack.github.io/docs/configuration.html#output-chunkfilename | |
84 | */ | |
85 | chunkFilename: '[name].[chunkhash].chunk.js', | |
ad22074a | 86 | |
202e7223 C |
87 | publicPath: '/client/' |
88 | }, | |
89 | ||
90 | module: { | |
91 | rules: [ | |
92 | { | |
93 | test: /junk\/index\.js$/, | |
94 | // exclude: /(node_modules|bower_components)/, | |
95 | use: { | |
96 | loader: 'babel-loader', | |
97 | options: { | |
98 | presets: [ 'env' ] | |
99 | } | |
f627b712 C |
100 | } |
101 | } | |
202e7223 C |
102 | ] |
103 | }, | |
ad22074a | 104 | |
f7ac0f84 | 105 | /** |
202e7223 | 106 | * Add additional plugins to the compiler. |
f7ac0f84 | 107 | * |
202e7223 | 108 | * See: http://webpack.github.io/docs/configuration.html#plugins |
f7ac0f84 | 109 | */ |
202e7223 | 110 | plugins: [ |
f7ac0f84 | 111 | |
202e7223 C |
112 | /** |
113 | * Webpack plugin to optimize a JavaScript file for faster initial load | |
114 | * by wrapping eagerly-invoked functions. | |
115 | * | |
116 | * See: https://github.com/vigneshshanmugam/optimize-js-plugin | |
117 | */ | |
f7ac0f84 | 118 | |
202e7223 C |
119 | new OptimizeJsPlugin({ |
120 | sourceMap: false | |
121 | }), | |
ad22074a | 122 | |
202e7223 C |
123 | /** |
124 | * Plugin: DedupePlugin | |
125 | * Description: Prevents the inclusion of duplicate code into your bundle | |
126 | * and instead applies a copy of the function at runtime. | |
127 | * | |
128 | * See: https://webpack.github.io/docs/list-of-plugins.html#defineplugin | |
129 | * See: https://github.com/webpack/docs/wiki/optimization#deduplication | |
130 | */ | |
131 | // new DedupePlugin(), | |
132 | ||
133 | /** | |
134 | * Plugin: DefinePlugin | |
135 | * Description: Define free variables. | |
136 | * Useful for having development builds with debug logging or adding global constants. | |
137 | * | |
138 | * Environment helpers | |
139 | * | |
140 | * See: https://webpack.github.io/docs/list-of-plugins.html#defineplugin | |
141 | */ | |
142 | // NOTE: when adding more properties make sure you include them in custom-typings.d.ts | |
143 | new DefinePlugin({ | |
ad22074a | 144 | 'ENV': JSON.stringify(METADATA.ENV), |
202e7223 C |
145 | 'HMR': METADATA.HMR, |
146 | 'API_URL': JSON.stringify(METADATA.API_URL), | |
04de542a | 147 | 'AOT': METADATA.AOT, |
202e7223 | 148 | 'process.version': JSON.stringify(process.version), |
04de542a C |
149 | 'process.env.ENV': JSON.stringify(METADATA.ENV), |
150 | 'process.env.NODE_ENV': JSON.stringify(METADATA.ENV), | |
151 | 'process.env.HMR': METADATA.HMR | |
202e7223 | 152 | }), |
f627b712 | 153 | |
202e7223 C |
154 | /** |
155 | * Plugin: UglifyJsPlugin | |
156 | * Description: Minimize all JavaScript output of chunks. | |
157 | * Loaders are switched into minimizing mode. | |
158 | * | |
159 | * See: https://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin | |
160 | */ | |
161 | // NOTE: To debug prod builds uncomment //debug lines and comment //prod lines | |
162 | new UglifyJsPlugin({ | |
04de542a C |
163 | parallel: true, |
164 | uglifyOptions: { | |
165 | ie8: false, | |
166 | ecma: 6, | |
167 | warnings: true, | |
168 | mangle: true, | |
169 | output: { | |
170 | comments: false, | |
171 | beautify: false | |
172 | } | |
173 | }, | |
174 | warnings: true | |
202e7223 | 175 | }), |
ad22074a | 176 | |
04de542a C |
177 | /** |
178 | * Plugin: NormalModuleReplacementPlugin | |
179 | * Description: Replace resources that matches resourceRegExp with newResource | |
180 | * | |
181 | * See: http://webpack.github.io/docs/list-of-plugins.html#normalmodulereplacementplugin | |
182 | */ | |
202e7223 | 183 | new NormalModuleReplacementPlugin( |
04de542a | 184 | /(angular2|@angularclass)((\\|\/)|-)hmr/, |
202e7223 C |
185 | helpers.root('config/empty.js') |
186 | ), | |
ad22074a | 187 | |
202e7223 C |
188 | new NormalModuleReplacementPlugin( |
189 | /zone\.js(\\|\/)dist(\\|\/)long-stack-trace-zone/, | |
190 | helpers.root('config/empty.js') | |
191 | ), | |
c16ce1de | 192 | |
202e7223 | 193 | new HashedModuleIdsPlugin(), |
c16ce1de | 194 | |
202e7223 | 195 | /** |
04de542a | 196 | * AoT |
202e7223 | 197 | */ |
04de542a C |
198 | (AOT ? ( |
199 | new NormalModuleReplacementPlugin( | |
200 | /@angular(\\|\/)compiler/, | |
201 | helpers.root('config/empty.js') | |
202 | ) | |
203 | ) : (new LoaderOptionsPlugin({}))), | |
b20b5fed | 204 | |
202e7223 C |
205 | /** |
206 | * Plugin LoaderOptionsPlugin (experimental) | |
207 | * | |
208 | * See: https://gist.github.com/sokra/27b24881210b56bbaff7 | |
209 | */ | |
210 | new LoaderOptionsPlugin({ | |
211 | minimize: true, | |
212 | debug: false, | |
213 | options: { | |
b20b5fed | 214 | |
202e7223 C |
215 | /** |
216 | * Static analysis linter for TypeScript advanced options configuration | |
217 | * Description: An extensible linter for the TypeScript language. | |
218 | * | |
219 | * See: https://github.com/wbuchwalter/tslint-loader | |
220 | */ | |
221 | tslint: { | |
222 | emitErrors: true, | |
223 | failOnHint: true, | |
224 | resourcePath: 'src' | |
225 | }, | |
b20b5fed | 226 | |
202e7223 C |
227 | /** |
228 | * Html loader advanced options | |
229 | * | |
230 | * See: https://github.com/webpack/html-loader#advanced-options | |
231 | */ | |
232 | // TODO: Need to workaround Angular 2's html syntax => #id [bind] (event) *ngFor | |
233 | htmlLoader: { | |
234 | minimize: true, | |
235 | removeAttributeQuotes: false, | |
236 | caseSensitive: true, | |
237 | customAttrSurround: [ | |
238 | [/#/, /(?:)/], | |
239 | [/\*/, /(?:)/], | |
240 | [/\[?\(?/, /(?:)/] | |
241 | ], | |
242 | customAttrAssign: [/\)?]?=/] | |
243 | }, | |
4d19d2f1 | 244 | |
202e7223 C |
245 | // FIXME: Remove |
246 | // https://github.com/bholloway/resolve-url-loader/issues/36 | |
247 | // https://github.com/jtangelder/sass-loader/issues/289 | |
248 | context: __dirname, | |
249 | output: { | |
250 | path: helpers.root('dist') | |
251 | } | |
4d19d2f1 | 252 | } |
3523b64a C |
253 | }), |
254 | ||
255 | new BundleAnalyzerPlugin({ | |
256 | // Can be `server`, `static` or `disabled`. | |
257 | // In `server` mode analyzer will start HTTP server to show bundle report. | |
258 | // In `static` mode single HTML file with bundle report will be generated. | |
259 | // In `disabled` mode you can use this plugin to just generate Webpack Stats JSON file by setting `generateStatsFile` to `true`. | |
260 | analyzerMode: 'static', | |
261 | // Path to bundle report file that will be generated in `static` mode. | |
262 | // Relative to bundles output directory. | |
263 | reportFilename: 'report.html', | |
264 | // Automatically open report in default browser | |
265 | openAnalyzer: false, | |
266 | // If `true`, Webpack Stats JSON file will be generated in bundles output directory | |
267 | generateStatsFile: true, | |
268 | // Name of Webpack Stats JSON file that will be generated if `generateStatsFile` is `true`. | |
269 | // Relative to bundles output directory. | |
270 | statsFilename: 'stats.json', | |
271 | // Options for `stats.toJson()` method. | |
272 | // For example you can exclude sources of your modules from stats file with `source: false` option. | |
273 | // See more options here: https://github.com/webpack/webpack/blob/webpack-1/lib/Stats.js#L21 | |
274 | statsOptions: null, | |
275 | // Log level. Can be 'info', 'warn', 'error' or 'silent'. | |
276 | logLevel: 'info' | |
202e7223 C |
277 | }) |
278 | ], | |
ad22074a | 279 | |
202e7223 C |
280 | /* |
281 | * Include polyfills or mocks for various node stuff | |
282 | * Description: Node configuration | |
283 | * | |
284 | * See: https://webpack.github.io/docs/configuration.html#node | |
285 | */ | |
286 | node: { | |
287 | global: true, | |
288 | crypto: 'empty', | |
289 | fs: 'empty', | |
290 | process: true, | |
291 | module: false, | |
292 | clearImmediate: false, | |
293 | setImmediate: false | |
294 | } | |
ad22074a | 295 | |
202e7223 C |
296 | }) |
297 | ] | |
ad22074a | 298 | } |