|
| 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 |
| 8 | +const ngtools = require('@ngtools/webpack'); |
| 9 | + |
| 10 | +/** |
| 11 | + * Webpack Plugins |
| 12 | + */ |
| 13 | +const DedupePlugin = require('webpack/lib/optimize/DedupePlugin'); |
| 14 | +const DefinePlugin = require('webpack/lib/DefinePlugin'); |
| 15 | +const IgnorePlugin = require('webpack/lib/IgnorePlugin'); |
| 16 | +const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin'); |
| 17 | +const NormalModuleReplacementPlugin = require('webpack/lib/NormalModuleReplacementPlugin'); |
| 18 | +const ProvidePlugin = require('webpack/lib/ProvidePlugin'); |
| 19 | +const UglifyJsPlugin = require('webpack/lib/optimize/UglifyJsPlugin'); |
| 20 | +const WebpackMd5Hash = require('webpack-md5-hash'); |
| 21 | + |
| 22 | +/** |
| 23 | + * Webpack Constants |
| 24 | + */ |
| 25 | +const ENV = process.env.NODE_ENV = process.env.ENV = 'production'; |
| 26 | +const HOST = process.env.HOST || 'localhost'; |
| 27 | +const PORT = process.env.PORT || 8080; |
| 28 | +const METADATA = webpackMerge(commonConfig({ |
| 29 | + env: ENV |
| 30 | +}).metadata, { |
| 31 | + host: HOST, |
| 32 | + port: PORT, |
| 33 | + ENV: ENV, |
| 34 | + HMR: false |
| 35 | +}); |
| 36 | + |
| 37 | +module.exports = function (env) { |
| 38 | + return webpackMerge(commonConfig({ |
| 39 | + env: ENV |
| 40 | + }), { |
| 41 | + |
| 42 | + /** |
| 43 | + * Developer tool to enhance debugging |
| 44 | + * |
| 45 | + * See: http://webpack.github.io/docs/configuration.html#devtool |
| 46 | + * See: https://github.com/webpack/docs/wiki/build-performance#sourcemaps |
| 47 | + */ |
| 48 | + devtool: 'source-map', |
| 49 | + |
| 50 | + /* |
| 51 | + * The entry point for the bundle |
| 52 | + * Our Angular.js app |
| 53 | + * |
| 54 | + * See: http://webpack.github.io/docs/configuration.html#entry |
| 55 | + */ |
| 56 | + entry: { |
| 57 | + |
| 58 | + // 'polyfills': './src/polyfills.browser', |
| 59 | + // 'vendor': './src/vendor.browser', |
| 60 | + 'main': './src/main.browser.aot' |
| 61 | + |
| 62 | + }, |
| 63 | + |
| 64 | + /** |
| 65 | + * Options affecting the output of the compilation. |
| 66 | + * |
| 67 | + * See: http://webpack.github.io/docs/configuration.html#output |
| 68 | + */ |
| 69 | + output: { |
| 70 | + |
| 71 | + /** |
| 72 | + * The output directory as absolute path (required). |
| 73 | + * |
| 74 | + * See: http://webpack.github.io/docs/configuration.html#output-path |
| 75 | + */ |
| 76 | + path: helpers.root('dist'), |
| 77 | + |
| 78 | + /** |
| 79 | + * Specifies the name of each output file on disk. |
| 80 | + * IMPORTANT: You must not specify an absolute path here! |
| 81 | + * |
| 82 | + * See: http://webpack.github.io/docs/configuration.html#output-filename |
| 83 | + */ |
| 84 | + filename: '[name].[chunkhash].bundle.js', |
| 85 | + |
| 86 | + /** |
| 87 | + * The filename of the SourceMaps for the JavaScript files. |
| 88 | + * They are inside the output.path directory. |
| 89 | + * |
| 90 | + * See: http://webpack.github.io/docs/configuration.html#output-sourcemapfilename |
| 91 | + */ |
| 92 | + sourceMapFilename: '[name].[chunkhash].bundle.map', |
| 93 | + |
| 94 | + /** |
| 95 | + * The filename of non-entry chunks as relative path |
| 96 | + * inside the output.path directory. |
| 97 | + * |
| 98 | + * See: http://webpack.github.io/docs/configuration.html#output-chunkfilename |
| 99 | + */ |
| 100 | + chunkFilename: '[id].[chunkhash].chunk.js' |
| 101 | + |
| 102 | + }, |
| 103 | + |
| 104 | + /** |
| 105 | + * Add additional plugins to the compiler. |
| 106 | + * |
| 107 | + * See: http://webpack.github.io/docs/configuration.html#plugins |
| 108 | + */ |
| 109 | + plugins: [ |
| 110 | + |
| 111 | + /** |
| 112 | + * Plugin: WebpackMd5Hash |
| 113 | + * Description: Plugin to replace a standard webpack chunkhash with md5. |
| 114 | + * |
| 115 | + * See: https://www.npmjs.com/package/webpack-md5-hash |
| 116 | + */ |
| 117 | + new WebpackMd5Hash(), |
| 118 | + |
| 119 | + new ngtools.AotPlugin({ |
| 120 | + tsConfigPath: './tsconfig.aot.json', |
| 121 | + baseDir: helpers.root('src'), |
| 122 | + entryModule: helpers.root('src/app/app.module') + '#AppModule' |
| 123 | + }), |
| 124 | + |
| 125 | + /** |
| 126 | + * Plugin: DedupePlugin |
| 127 | + * Description: Prevents the inclusion of duplicate code into your bundle |
| 128 | + * and instead applies a copy of the function at runtime. |
| 129 | + * |
| 130 | + * See: https://webpack.github.io/docs/list-of-plugins.html#defineplugin |
| 131 | + * See: https://github.com/webpack/docs/wiki/optimization#deduplication |
| 132 | + */ |
| 133 | + // new DedupePlugin(), // see: https://github.com/angular/angular-cli/issues/1587 |
| 134 | + |
| 135 | + /** |
| 136 | + * Plugin: DefinePlugin |
| 137 | + * Description: Define free variables. |
| 138 | + * Useful for having development builds with debug logging or adding global constants. |
| 139 | + * |
| 140 | + * Environment helpers |
| 141 | + * |
| 142 | + * See: https://webpack.github.io/docs/list-of-plugins.html#defineplugin |
| 143 | + */ |
| 144 | + // NOTE: when adding more properties make sure you include them in custom-typings.d.ts |
| 145 | + new DefinePlugin({ |
| 146 | + 'ENV': JSON.stringify(METADATA.ENV), |
| 147 | + 'HMR': METADATA.HMR, |
| 148 | + 'process.env': { |
| 149 | + 'ENV': JSON.stringify(METADATA.ENV), |
| 150 | + 'NODE_ENV': JSON.stringify(METADATA.ENV), |
| 151 | + 'HMR': METADATA.HMR, |
| 152 | + } |
| 153 | + }), |
| 154 | + |
| 155 | + /** |
| 156 | + * Plugin: UglifyJsPlugin |
| 157 | + * Description: Minimize all JavaScript output of chunks. |
| 158 | + * Loaders are switched into minimizing mode. |
| 159 | + * |
| 160 | + * See: https://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin |
| 161 | + */ |
| 162 | + // NOTE: To debug prod builds uncomment //debug lines and comment //prod lines |
| 163 | + new UglifyJsPlugin({ |
| 164 | + // beautify: true, //debug |
| 165 | + // mangle: false, //debug |
| 166 | + // dead_code: false, //debug |
| 167 | + // unused: false, //debug |
| 168 | + // deadCode: false, //debug |
| 169 | + // compress: { |
| 170 | + // screw_ie8: true, |
| 171 | + // keep_fnames: true, |
| 172 | + // drop_debugger: false, |
| 173 | + // dead_code: false, |
| 174 | + // unused: false |
| 175 | + // }, // debug |
| 176 | + // comments: true, //debug |
| 177 | + |
| 178 | + |
| 179 | + beautify: false, //prod |
| 180 | + mangle: { |
| 181 | + screw_ie8: true, |
| 182 | + keep_fnames: true |
| 183 | + }, //prod |
| 184 | + compress: { |
| 185 | + screw_ie8: true |
| 186 | + }, //prod |
| 187 | + comments: false //prod |
| 188 | + }), |
| 189 | + |
| 190 | + /** |
| 191 | + * Plugin: NormalModuleReplacementPlugin |
| 192 | + * Description: Replace resources that matches resourceRegExp with newResource |
| 193 | + * |
| 194 | + * See: http://webpack.github.io/docs/list-of-plugins.html#normalmodulereplacementplugin |
| 195 | + */ |
| 196 | + |
| 197 | + new NormalModuleReplacementPlugin( |
| 198 | + /angular2-hmr/, |
| 199 | + helpers.root('config/modules/angular2-hmr-prod.js') |
| 200 | + ), |
| 201 | + |
| 202 | + /** |
| 203 | + * Plugin: IgnorePlugin |
| 204 | + * Description: Don’t generate modules for requests matching the provided RegExp. |
| 205 | + * |
| 206 | + * See: http://webpack.github.io/docs/list-of-plugins.html#ignoreplugin |
| 207 | + */ |
| 208 | + |
| 209 | + // new IgnorePlugin(/angular2-hmr/), |
| 210 | + |
| 211 | + /** |
| 212 | + * Plugin: CompressionPlugin |
| 213 | + * Description: Prepares compressed versions of assets to serve |
| 214 | + * them with Content-Encoding |
| 215 | + * |
| 216 | + * See: https://github.com/webpack/compression-webpack-plugin |
| 217 | + */ |
| 218 | + // install compression-webpack-plugin |
| 219 | + // new CompressionPlugin({ |
| 220 | + // regExp: /\.css$|\.html$|\.js$|\.map$/, |
| 221 | + // threshold: 2 * 1024 |
| 222 | + // }) |
| 223 | + |
| 224 | + /** |
| 225 | + * Plugin LoaderOptionsPlugin (experimental) |
| 226 | + * |
| 227 | + * See: https://gist.github.com/sokra/27b24881210b56bbaff7 |
| 228 | + */ |
| 229 | + new LoaderOptionsPlugin({ |
| 230 | + debug: false, |
| 231 | + options: { |
| 232 | + |
| 233 | + /** |
| 234 | + * Static analysis linter for TypeScript advanced options configuration |
| 235 | + * Description: An extensible linter for the TypeScript language. |
| 236 | + * |
| 237 | + * See: https://github.com/wbuchwalter/tslint-loader |
| 238 | + */ |
| 239 | + tslint: { |
| 240 | + emitErrors: true, |
| 241 | + failOnHint: true, |
| 242 | + resourcePath: 'src' |
| 243 | + }, |
| 244 | + |
| 245 | + |
| 246 | + /** |
| 247 | + * Html loader advanced options |
| 248 | + * |
| 249 | + * See: https://github.com/webpack/html-loader#advanced-options |
| 250 | + */ |
| 251 | + // TODO: Need to workaround Angular 2's html syntax => #id [bind] (event) *ngFor |
| 252 | + htmlLoader: { |
| 253 | + minimize: true, |
| 254 | + removeAttributeQuotes: false, |
| 255 | + caseSensitive: true, |
| 256 | + customAttrSurround: [ |
| 257 | + [/#/, /(?:)/], |
| 258 | + [/\*/, /(?:)/], |
| 259 | + [/\[?\(?/, /(?:)/] |
| 260 | + ], |
| 261 | + customAttrAssign: [/\)?\]?=/] |
| 262 | + }, |
| 263 | + |
| 264 | + } |
| 265 | + }), |
| 266 | + |
| 267 | + ], |
| 268 | + |
| 269 | + /* |
| 270 | + * Include polyfills or mocks for various node stuff |
| 271 | + * Description: Node configuration |
| 272 | + * |
| 273 | + * See: https://webpack.github.io/docs/configuration.html#node |
| 274 | + */ |
| 275 | + node: { |
| 276 | + global: true, |
| 277 | + crypto: 'empty', |
| 278 | + process: false, |
| 279 | + module: false, |
| 280 | + clearImmediate: false, |
| 281 | + setImmediate: false |
| 282 | + } |
| 283 | + |
| 284 | + }); |
| 285 | +} |
0 commit comments