Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | // Modules |
| 4 | var webpack = require('webpack'); |
| 5 | var autoprefixer = require('autoprefixer'); |
| 6 | var HtmlWebpackPlugin = require('html-webpack-plugin'); |
| 7 | var ExtractTextPlugin = require('extract-text-webpack-plugin'); |
| 8 | var CopyWebpackPlugin = require('copy-webpack-plugin'); |
| 9 | var CompressionPlugin = require('compression-webpack-plugin'); |
| 10 | var AssetsPlugin = require('assets-webpack-plugin'); |
| 11 | var CopyWebpackPlugin = require('copy-webpack-plugin'); |
| 12 | var path = require('path'); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 13 | var UglifyJsPlugin = require('uglifyjs-webpack-plugin'); |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 14 | |
| 15 | /** |
| 16 | * Env |
| 17 | * Get npm lifecycle event to identify the environment |
| 18 | */ |
| 19 | var ENV = process.env.npm_lifecycle_event; |
| 20 | var isTest = ENV === 'test' || ENV === 'test-watch'; |
| 21 | var isProd = ENV === 'build'; |
| 22 | |
| 23 | module.exports = [ |
| 24 | function makeWebpackConfig() { |
| 25 | /** |
| 26 | * Config |
| 27 | * Reference: http://webpack.github.io/docs/configuration.html |
| 28 | * This is the object where all configuration gets set |
| 29 | */ |
| 30 | var config = {}; |
| 31 | |
| 32 | /** |
| 33 | * Entry |
| 34 | * Reference: http://webpack.github.io/docs/configuration.html#entry |
| 35 | * Should be an empty object if it's generating a test build |
| 36 | * Karma will set this when it's a test build |
| 37 | */ |
| 38 | config.entry = isTest ? void 0 : { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 39 | app: './app/index.js' |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 40 | |
| 41 | }; |
| 42 | |
| 43 | /** |
| 44 | * Output |
| 45 | * Reference: http://webpack.github.io/docs/configuration.html#output |
| 46 | * Should be an empty object if it's generating a test build |
| 47 | * Karma will handle setting it up for you when it's a test build |
| 48 | */ |
| 49 | config.output = isTest ? {} : { |
| 50 | // Absolute output directory |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 51 | path: __dirname + '/dist', |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 52 | |
| 53 | // Output path from the view of the page |
| 54 | // Uses webpack-dev-server in development |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 55 | publicPath: '/', |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 56 | |
| 57 | // Filename for entry points |
| 58 | // Only adds hash in build mode |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 59 | filename: isProd ? '[name].[hash].js' : '[name].bundle.js', |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 60 | |
| 61 | // Filename for non-entry points |
| 62 | // Only adds hash in build mode |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 63 | chunkFilename: isProd ? '[name].[hash].js' : '[name].bundle.js' |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | /** |
| 67 | * Devtool |
| 68 | * Reference: http://webpack.github.io/docs/configuration.html#devtool |
| 69 | * Type of sourcemap to use per build type |
| 70 | */ |
| 71 | if (isTest) { |
| 72 | https: |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 73 | // unix.stackexchange.com/questions/144208/find-files-without-extension |
| 74 | config.devtool = 'inline-source-map'; |
| 75 | } |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 76 | else if (isProd) { |
| 77 | config.devtool = 'source-map'; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 78 | } |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 79 | else { |
| 80 | config.devtool = 'eval-source-map'; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Loaders |
| 85 | * Reference: |
| 86 | * http://webpack.github.io/docs/configuration.html#module-loaders |
| 87 | * List: http://webpack.github.io/docs/list-of-loaders.html |
| 88 | * This handles most of the magic responsible for converting modules |
| 89 | */ |
| 90 | |
| 91 | // Initialize module |
| 92 | config.module = { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 93 | rules: [{ |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 94 | // JS LOADER |
| 95 | // Reference: https://github.com/babel/babel-loader |
| 96 | // Transpile .js files using babel-loader |
| 97 | // Compiles ES6 and ES7 into ES5 code |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 98 | test: /\.js$/, |
| 99 | use: 'babel-loader', |
| 100 | exclude: /node_modules/ |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 101 | }, |
| 102 | { |
| 103 | // CSS LOADER |
| 104 | // Reference: https://github.com/webpack/css-loader |
| 105 | // Allow loading css through js |
| 106 | // |
| 107 | // Reference: https://github.com/postcss/postcss-loader |
| 108 | // Postprocess your css with PostCSS plugins |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 109 | test: /\.css$/, |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 110 | // Reference: https://github.com/webpack/extract-text-webpack-plugin |
| 111 | // Extract css files in production builds |
| 112 | // |
| 113 | // Reference: https://github.com/webpack/style-loader |
| 114 | // Use style-loader in development. |
| 115 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 116 | loader: isTest ? 'null-loader' : ExtractTextPlugin.extract({ |
| 117 | fallback: 'style-loader', |
| 118 | use: [{ |
| 119 | loader: 'css-loader', |
| 120 | query: { |
| 121 | sourceMap: true |
| 122 | } |
| 123 | }, |
| 124 | { |
| 125 | loader: 'postcss-loader' |
| 126 | } |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 127 | ], |
| 128 | }) |
| 129 | }, |
| 130 | { |
| 131 | // ASSET LOADER |
| 132 | // Reference: https://github.com/webpack/file-loader |
| 133 | // Copy png, jpg, jpeg, gif, svg, woff, woff2, ttf, eot files to |
| 134 | // output |
| 135 | // Rename the file using the asset hash |
| 136 | // Pass along the updated reference to your code |
| 137 | // You can add here any file extension you want to get copied to your |
| 138 | // output |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 139 | test: /\.(png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot|ico)$/, |
| 140 | loader: 'file-loader', |
| 141 | options: { |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 142 | name(file) { |
| 143 | if (!isProd) { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 144 | return '[path][name].[ext]'; |
| 145 | } |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 146 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 147 | return '[hash].[ext]'; |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | }, |
| 151 | { |
| 152 | // HTML LOADER |
| 153 | // Reference: https://github.com/webpack/raw-loader |
| 154 | // Allow loading html through js |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 155 | test: /\.html$/, |
| 156 | use: { |
| 157 | loader: 'html-loader' |
| 158 | } |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 159 | }, |
| 160 | // JSON LOADER |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 161 | { |
| 162 | test: /\.json$/, |
| 163 | loader: 'json-loader' |
| 164 | }, { |
| 165 | test: /\.scss$/, |
| 166 | use: [{ |
| 167 | loader: 'style-loader' // creates style nodes from JS strings |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 168 | }, |
| 169 | { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 170 | loader: 'css-loader' // translates CSS into CommonJS |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 171 | }, |
| 172 | { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 173 | loader: 'sass-loader' // compiles Sass to CSS |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 174 | } |
| 175 | ] |
| 176 | } |
| 177 | ] |
| 178 | }; |
| 179 | |
| 180 | // ISTANBUL LOADER |
| 181 | // https://github.com/deepsweet/istanbul-instrumenter-loader |
| 182 | // Instrument JS files with istanbul-lib-instrument for subsequent code |
| 183 | // coverage reporting |
| 184 | // Skips node_modules and files that end with .spec.js |
| 185 | if (isTest) { |
| 186 | config.module.rules.push({ |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 187 | enforce: 'pre', |
| 188 | test: /\.js$/, |
| 189 | exclude: [/node_modules/, /\.spec\.js$/], |
| 190 | loader: 'istanbul-instrumenter-loader', |
| 191 | query: { |
| 192 | esModules: true |
| 193 | } |
| 194 | }); |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /** |
| 198 | * PostCSS |
| 199 | * Reference: https://github.com/postcss/autoprefixer-core |
| 200 | * Add vendor prefixes to your css |
| 201 | */ |
| 202 | // NOTE: This is now handled in the `postcss.config.js` |
| 203 | // webpack2 has some issues, making the config file necessary |
| 204 | |
| 205 | /** |
| 206 | * Plugins |
| 207 | * Reference: http://webpack.github.io/docs/configuration.html#plugins |
| 208 | * List: http://webpack.github.io/docs/list-of-plugins.html |
| 209 | */ |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 210 | config.plugins = [new webpack.LoaderOptionsPlugin({ |
| 211 | test: /\.scss$/i, |
| 212 | options: { |
| 213 | postcss: { |
| 214 | plugins: [autoprefixer] |
| 215 | } |
| 216 | }, |
| 217 | debug: !isProd |
| 218 | })]; |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 219 | |
| 220 | // Skip rendering index.html in test mode |
| 221 | if (!isTest) { |
| 222 | // Reference: https://github.com/ampedandwired/html-webpack-plugin |
| 223 | // Render index.html |
| 224 | config.plugins.push( |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 225 | new HtmlWebpackPlugin({ |
| 226 | template: './app/index.html', |
| 227 | inject: 'body', |
| 228 | favicon: './app/assets/images/favicon.ico' |
| 229 | }), |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 230 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 231 | // Reference: https://github.com/webpack/extract-text-webpack-plugin |
| 232 | // Extract css files |
| 233 | // Disabled when in test mode or not in build mode |
| 234 | new ExtractTextPlugin({ |
| 235 | filename: 'css/[name].css', |
| 236 | disable: !isProd, |
| 237 | allChunks: true |
| 238 | })); |
| 239 | } |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 240 | |
| 241 | // Add build specific plugins |
| 242 | if (isProd) { |
| 243 | config.plugins.push( |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 244 | // Reference: |
| 245 | // http://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin |
| 246 | // Minify all javascript, switch loaders to minimizing mode |
| 247 | // TODO: openbmc/openbmc#2871 Mangling currently breaks the GUI. |
| 248 | new UglifyJsPlugin({ |
| 249 | uglifyOptions: { |
| 250 | mangle: false |
| 251 | } |
| 252 | }), |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 253 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 254 | // Copy assets from the public folder |
| 255 | // Reference: https://github.com/kevlened/copy-webpack-plugin |
| 256 | new CopyWebpackPlugin([{ |
| 257 | from: __dirname + '/app/assets' |
| 258 | }]), |
| 259 | new CompressionPlugin({ |
| 260 | deleteOriginalAssets: true |
| 261 | })); |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Dev server configuration |
| 266 | * Reference: http://webpack.github.io/docs/configuration.html#devserver |
| 267 | * Reference: http://webpack.github.io/docs/webpack-dev-server.html |
| 268 | */ |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 269 | config.devServer = { |
| 270 | contentBase: './src/public', |
| 271 | stats: 'minimal' |
| 272 | }; |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 273 | |
| 274 | return config; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 275 | }() |
| 276 | ]; |