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'); |
Gunnar Mills | 6bcd6cf | 2018-05-22 15:47:35 -0500 | [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 : { |
| 39 | app : './app/index.js' |
| 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 |
| 51 | path : __dirname + '/dist', |
| 52 | |
| 53 | // Output path from the view of the page |
| 54 | // Uses webpack-dev-server in development |
| 55 | publicPath : '/', |
| 56 | |
| 57 | // Filename for entry points |
| 58 | // Only adds hash in build mode |
| 59 | filename : isProd ? '[name].[hash].js' : '[name].bundle.js', |
| 60 | |
| 61 | // Filename for non-entry points |
| 62 | // Only adds hash in build mode |
| 63 | chunkFilename : isProd ? '[name].[hash].js' : '[name].bundle.js' |
| 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: |
| 73 | // unix.stackexchange.com/questions/144208/find-files-without-extension |
| 74 | config.devtool = 'inline-source-map'; |
| 75 | } |
| 76 | else if (isProd) { |
| 77 | config.devtool = 'source-map'; |
| 78 | } |
| 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 = { |
| 93 | rules : [ |
| 94 | { |
| 95 | // JS LOADER |
| 96 | // Reference: https://github.com/babel/babel-loader |
| 97 | // Transpile .js files using babel-loader |
| 98 | // Compiles ES6 and ES7 into ES5 code |
| 99 | test : /\.js$/, |
Gunnar Mills | 6bcd6cf | 2018-05-22 15:47:35 -0500 | [diff] [blame] | 100 | use : 'babel-loader', |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 101 | exclude : /node_modules/ |
| 102 | }, |
| 103 | { |
| 104 | // CSS LOADER |
| 105 | // Reference: https://github.com/webpack/css-loader |
| 106 | // Allow loading css through js |
| 107 | // |
| 108 | // Reference: https://github.com/postcss/postcss-loader |
| 109 | // Postprocess your css with PostCSS plugins |
| 110 | test : /\.css$/, |
| 111 | // Reference: https://github.com/webpack/extract-text-webpack-plugin |
| 112 | // Extract css files in production builds |
| 113 | // |
| 114 | // Reference: https://github.com/webpack/style-loader |
| 115 | // Use style-loader in development. |
| 116 | |
| 117 | loader : isTest ? 'null-loader' : ExtractTextPlugin.extract({ |
| 118 | fallback : 'style-loader', |
| 119 | use : [ |
| 120 | {loader : 'css-loader', query : {sourceMap : true}}, |
| 121 | {loader : 'postcss-loader'} |
| 122 | ], |
| 123 | }) |
| 124 | }, |
| 125 | { |
| 126 | // ASSET LOADER |
| 127 | // Reference: https://github.com/webpack/file-loader |
| 128 | // Copy png, jpg, jpeg, gif, svg, woff, woff2, ttf, eot files to |
| 129 | // output |
| 130 | // Rename the file using the asset hash |
| 131 | // Pass along the updated reference to your code |
| 132 | // You can add here any file extension you want to get copied to your |
| 133 | // output |
Ed Tanous | 3b3ab8af | 2018-02-02 11:02:46 -0800 | [diff] [blame] | 134 | test : /\.(png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot|ico)$/, |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 135 | loader : 'file-loader', |
| 136 | options : { |
| 137 | name(file) { |
| 138 | if (!isProd) { |
| 139 | return '[path][name].[ext]' |
| 140 | } |
| 141 | |
| 142 | return '[hash].[ext]' |
| 143 | } |
| 144 | } |
| 145 | }, |
| 146 | { |
| 147 | // HTML LOADER |
| 148 | // Reference: https://github.com/webpack/raw-loader |
| 149 | // Allow loading html through js |
| 150 | test : /\.html$/, |
| 151 | use : {loader : 'html-loader'} |
| 152 | }, |
| 153 | // JSON LOADER |
| 154 | {test : /\.json$/, loader : 'json-loader'}, { |
| 155 | test : /\.scss$/, |
| 156 | use : [ |
| 157 | { |
| 158 | loader : 'style-loader' // creates style nodes from JS strings |
| 159 | }, |
| 160 | { |
| 161 | loader : 'css-loader' // translates CSS into CommonJS |
| 162 | }, |
| 163 | { |
| 164 | loader : 'sass-loader' // compiles Sass to CSS |
| 165 | } |
| 166 | ] |
| 167 | } |
| 168 | ] |
| 169 | }; |
| 170 | |
| 171 | // ISTANBUL LOADER |
| 172 | // https://github.com/deepsweet/istanbul-instrumenter-loader |
| 173 | // Instrument JS files with istanbul-lib-instrument for subsequent code |
| 174 | // coverage reporting |
| 175 | // Skips node_modules and files that end with .spec.js |
| 176 | if (isTest) { |
| 177 | config.module.rules.push({ |
| 178 | enforce : 'pre', |
| 179 | test : /\.js$/, |
| 180 | exclude : [ /node_modules/, /\.spec\.js$/], |
| 181 | loader : 'istanbul-instrumenter-loader', |
| 182 | query : {esModules : true} |
| 183 | }) |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * PostCSS |
| 188 | * Reference: https://github.com/postcss/autoprefixer-core |
| 189 | * Add vendor prefixes to your css |
| 190 | */ |
| 191 | // NOTE: This is now handled in the `postcss.config.js` |
| 192 | // webpack2 has some issues, making the config file necessary |
| 193 | |
| 194 | /** |
| 195 | * Plugins |
| 196 | * Reference: http://webpack.github.io/docs/configuration.html#plugins |
| 197 | * List: http://webpack.github.io/docs/list-of-plugins.html |
| 198 | */ |
| 199 | config.plugins = [ new webpack.LoaderOptionsPlugin({ |
| 200 | test : /\.scss$/i, |
| 201 | options : {postcss : {plugins : [ autoprefixer ]}}, |
| 202 | debug : !isProd |
| 203 | }) ]; |
| 204 | |
| 205 | // Skip rendering index.html in test mode |
| 206 | if (!isTest) { |
| 207 | // Reference: https://github.com/ampedandwired/html-webpack-plugin |
| 208 | // Render index.html |
| 209 | config.plugins.push( |
| 210 | new HtmlWebpackPlugin( |
Ed Tanous | 3b3ab8af | 2018-02-02 11:02:46 -0800 | [diff] [blame] | 211 | { |
| 212 | template : './app/index.html', |
| 213 | inject : 'body', |
| 214 | favicon: './app/assets/images/favicon.ico' |
| 215 | }), |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 216 | |
| 217 | // Reference: https://github.com/webpack/extract-text-webpack-plugin |
| 218 | // Extract css files |
| 219 | // Disabled when in test mode or not in build mode |
| 220 | new ExtractTextPlugin({ |
| 221 | filename : 'css/[name].css', |
| 222 | disable : !isProd, |
| 223 | allChunks : true |
| 224 | })) |
| 225 | } |
| 226 | |
| 227 | // Add build specific plugins |
| 228 | if (isProd) { |
| 229 | config.plugins.push( |
| 230 | // Reference: |
| 231 | // http://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin |
| 232 | // Minify all javascript, switch loaders to minimizing mode |
Gunnar Mills | 13251a8 | 2018-05-22 12:45:20 -0500 | [diff] [blame^] | 233 | // TODO: openbmc/openbmc#2871 Mangling currently breaks the GUI. |
Gunnar Mills | 6bcd6cf | 2018-05-22 15:47:35 -0500 | [diff] [blame] | 234 | new UglifyJsPlugin({ |
| 235 | uglifyOptions:{ |
| 236 | mangle: false |
| 237 | } |
Matt Spinler | 8beb651 | 2018-02-02 14:01:16 -0600 | [diff] [blame] | 238 | }), |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 239 | |
| 240 | // Copy assets from the public folder |
| 241 | // Reference: https://github.com/kevlened/copy-webpack-plugin |
| 242 | new CopyWebpackPlugin([ {from : __dirname + '/app/assets'} ]), |
| 243 | new CompressionPlugin({deleteOriginalAssets : true})) |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Dev server configuration |
| 248 | * Reference: http://webpack.github.io/docs/configuration.html#devserver |
| 249 | * Reference: http://webpack.github.io/docs/webpack-dev-server.html |
| 250 | */ |
| 251 | config.devServer = {contentBase : './src/public', stats : 'minimal'}; |
| 252 | |
| 253 | return config; |
| 254 | }()]; |