|
1 | | -/** |
2 | | - * Webpack config for development |
3 | | - */ |
4 | | -module.exports = require('./webpack.make')({ |
5 | | - BUILD: false, |
6 | | - TEST: false |
7 | | -}); |
| 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 | + |
| 9 | +module.exports = function makeWebpackConfig (options) { |
| 10 | + /** |
| 11 | + * Environment type |
| 12 | + * BUILD is for generating minified builds |
| 13 | + * TEST is for generating test builds |
| 14 | + */ |
| 15 | + var BUILD = !!options.BUILD; |
| 16 | + var TEST = !!options.TEST; |
| 17 | + |
| 18 | + /** |
| 19 | + * Config |
| 20 | + * Reference: http://webpack.github.io/docs/configuration.html |
| 21 | + * This is the object where all configuration gets set |
| 22 | + */ |
| 23 | + var config = {}; |
| 24 | + |
| 25 | + /** |
| 26 | + * Entry |
| 27 | + * Reference: http://webpack.github.io/docs/configuration.html#entry |
| 28 | + * Should be an empty object if it's generating a test build |
| 29 | + * Karma will set this when it's a test build |
| 30 | + */ |
| 31 | + if (TEST) { |
| 32 | + config.entry = {} |
| 33 | + } else { |
| 34 | + config.entry = { |
| 35 | + app: './src/app.js' |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Output |
| 41 | + * Reference: http://webpack.github.io/docs/configuration.html#output |
| 42 | + * Should be an empty object if it's generating a test build |
| 43 | + * Karma will handle setting it up for you when it's a test build |
| 44 | + */ |
| 45 | + if (TEST) { |
| 46 | + config.output = {} |
| 47 | + } else { |
| 48 | + config.output = { |
| 49 | + // Absolute output directory |
| 50 | + path: __dirname + '/public', |
| 51 | + |
| 52 | + // Output path from the view of the page |
| 53 | + // Uses webpack-dev-server in development |
| 54 | + publicPath: BUILD ? '/' : 'http://localhost:8080/', |
| 55 | + |
| 56 | + // Filename for entry points |
| 57 | + // Only adds hash in build mode |
| 58 | + filename: BUILD ? '[name].[hash].js' : '[name].bundle.js', |
| 59 | + |
| 60 | + // Filename for non-entry points |
| 61 | + // Only adds hash in build mode |
| 62 | + chunkFilename: BUILD ? '[name].[hash].js' : '[name].bundle.js' |
| 63 | + } |
| 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 (TEST) { |
| 72 | + config.devtool = 'inline-source-map'; |
| 73 | + } else if (BUILD) { |
| 74 | + config.devtool = 'source-map'; |
| 75 | + } else { |
| 76 | + config.devtool = 'eval'; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Loaders |
| 81 | + * Reference: http://webpack.github.io/docs/configuration.html#module-loaders |
| 82 | + * List: http://webpack.github.io/docs/list-of-loaders.html |
| 83 | + * This handles most of the magic responsible for converting modules |
| 84 | + */ |
| 85 | + |
| 86 | + // Initialize module |
| 87 | + config.module = { |
| 88 | + preLoaders: [], |
| 89 | + loaders: [{ |
| 90 | + // JS LOADER |
| 91 | + // Reference: https://github.com/babel/babel-loader |
| 92 | + // Transpile .js files using babel-loader |
| 93 | + // Compiles ES6 and ES7 into ES5 code |
| 94 | + test: /\.js$/, |
| 95 | + loader: 'babel', |
| 96 | + exclude: /node_modules/ |
| 97 | + }, { |
| 98 | + // ASSET LOADER |
| 99 | + // Reference: https://github.com/webpack/file-loader |
| 100 | + // Copy png, jpg, jpeg, gif, svg, woff, woff2, ttf, eot files to output |
| 101 | + // Rename the file using the asset hash |
| 102 | + // Pass along the updated reference to your code |
| 103 | + // You can add here any file extension you want to get copied to your output |
| 104 | + test: /\.(png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot)$/, |
| 105 | + loader: 'file' |
| 106 | + }, { |
| 107 | + // HTML LOADER |
| 108 | + // Reference: https://github.com/webpack/raw-loader |
| 109 | + // Allow loading html through js |
| 110 | + test: /\.html$/, |
| 111 | + loader: 'raw' |
| 112 | + }] |
| 113 | + }; |
| 114 | + |
| 115 | + // ISPARTA LOADER |
| 116 | + // Reference: https://github.com/ColCh/isparta-instrumenter-loader |
| 117 | + // Instrument JS files with Isparta for subsequent code coverage reporting |
| 118 | + // Skips node_modules and files that end with .test.js |
| 119 | + if (TEST) { |
| 120 | + config.module.preLoaders.push({ |
| 121 | + test: /\.js$/, |
| 122 | + exclude: [ |
| 123 | + /node_modules/, |
| 124 | + /\.test\.js$/ |
| 125 | + ], |
| 126 | + loader: 'isparta-instrumenter' |
| 127 | + }) |
| 128 | + } |
| 129 | + |
| 130 | + // CSS LOADER |
| 131 | + // Reference: https://github.com/webpack/css-loader |
| 132 | + // Allow loading css through js |
| 133 | + // |
| 134 | + // Reference: https://github.com/postcss/postcss-loader |
| 135 | + // Postprocess your css with PostCSS plugins |
| 136 | + var cssLoader = { |
| 137 | + test: /\.css$/, |
| 138 | + // Reference: https://github.com/webpack/extract-text-webpack-plugin |
| 139 | + // Extract css files in production builds |
| 140 | + // |
| 141 | + // Reference: https://github.com/webpack/style-loader |
| 142 | + // Use style-loader in development. |
| 143 | + loader: ExtractTextPlugin.extract('style', 'css?sourceMap!postcss') |
| 144 | + }; |
| 145 | + |
| 146 | + // Skip loading css in test mode |
| 147 | + if (TEST) { |
| 148 | + // Reference: https://github.com/webpack/null-loader |
| 149 | + // Return an empty module |
| 150 | + cssLoader.loader = 'null' |
| 151 | + } |
| 152 | + |
| 153 | + // Add cssLoader to the loader list |
| 154 | + config.module.loaders.push(cssLoader); |
| 155 | + |
| 156 | + /** |
| 157 | + * PostCSS |
| 158 | + * Reference: https://github.com/postcss/autoprefixer-core |
| 159 | + * Add vendor prefixes to your css |
| 160 | + */ |
| 161 | + config.postcss = [ |
| 162 | + autoprefixer({ |
| 163 | + browsers: ['last 2 version'] |
| 164 | + }) |
| 165 | + ]; |
| 166 | + |
| 167 | + /** |
| 168 | + * Plugins |
| 169 | + * Reference: http://webpack.github.io/docs/configuration.html#plugins |
| 170 | + * List: http://webpack.github.io/docs/list-of-plugins.html |
| 171 | + */ |
| 172 | + config.plugins = [ |
| 173 | + // Reference: https://github.com/webpack/extract-text-webpack-plugin |
| 174 | + // Extract css files |
| 175 | + // Disabled when in test mode or not in build mode |
| 176 | + new ExtractTextPlugin('[name].[hash].css', { |
| 177 | + disable: !BUILD || TEST |
| 178 | + }) |
| 179 | + ]; |
| 180 | + |
| 181 | + // Skip rendering index.html in test mode |
| 182 | + if (!TEST) { |
| 183 | + // Reference: https://github.com/ampedandwired/html-webpack-plugin |
| 184 | + // Render index.html |
| 185 | + config.plugins.push( |
| 186 | + new HtmlWebpackPlugin({ |
| 187 | + template: './src/index.html', |
| 188 | + inject: 'body' |
| 189 | + }) |
| 190 | + ) |
| 191 | + } |
| 192 | + |
| 193 | + // Add build specific plugins |
| 194 | + if (BUILD) { |
| 195 | + config.plugins.push( |
| 196 | + // Reference: http://webpack.github.io/docs/list-of-plugins.html#noerrorsplugin |
| 197 | + // Only emit files when there are no errors |
| 198 | + new webpack.NoErrorsPlugin(), |
| 199 | + |
| 200 | + // Reference: http://webpack.github.io/docs/list-of-plugins.html#dedupeplugin |
| 201 | + // Dedupe modules in the output |
| 202 | + new webpack.optimize.DedupePlugin(), |
| 203 | + |
| 204 | + // Reference: http://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin |
| 205 | + // Minify all javascript, switch loaders to minimizing mode |
| 206 | + new webpack.optimize.UglifyJsPlugin() |
| 207 | + ) |
| 208 | + } |
| 209 | + |
| 210 | + /** |
| 211 | + * Dev server configuration |
| 212 | + * Reference: http://webpack.github.io/docs/configuration.html#devserver |
| 213 | + * Reference: http://webpack.github.io/docs/webpack-dev-server.html |
| 214 | + */ |
| 215 | + config.devServer = { |
| 216 | + contentBase: './public', |
| 217 | + stats: { |
| 218 | + modules: false, |
| 219 | + cached: false, |
| 220 | + colors: true, |
| 221 | + chunk: false |
| 222 | + } |
| 223 | + }; |
| 224 | + |
| 225 | + return config; |
| 226 | +}; |
0 commit comments