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'); |
Ed Tanous | fc7a33f | 2018-09-06 16:52:19 -0700 | [diff] [blame] | 6 | var HtmlWebpackInlineSourcePlugin = |
| 7 | require('html-webpack-inline-source-plugin'); |
Ed Tanous | 0f2f981 | 2018-12-19 17:59:28 -0800 | [diff] [blame] | 8 | var CSPWebpackPlugin = require('csp-html-webpack-plugin'); |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 9 | var HtmlWebpackPlugin = require('html-webpack-plugin'); |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 10 | var CopyWebpackPlugin = require('copy-webpack-plugin'); |
| 11 | var CompressionPlugin = require('compression-webpack-plugin'); |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 12 | var path = require('path'); |
Ed Tanous | fc7a33f | 2018-09-06 16:52:19 -0700 | [diff] [blame] | 13 | var FilterChunkWebpackPlugin = require('filter-chunk-webpack-plugin'); |
| 14 | var MiniCssExtractPlugin = require('mini-css-extract-plugin'); |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 15 | |
Ed Tanous | fc7a33f | 2018-09-06 16:52:19 -0700 | [diff] [blame] | 16 | module.exports = (env, options) => { |
| 17 | var isProd = options.mode === 'production'; |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 18 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 19 | /** |
| 20 | * Config |
| 21 | * Reference: http://webpack.github.io/docs/configuration.html |
| 22 | * This is the object where all configuration gets set |
| 23 | */ |
| 24 | var config = {}; |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 25 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 26 | /** |
| 27 | * Entry |
| 28 | * Reference: http://webpack.github.io/docs/configuration.html#entry |
| 29 | * Should be an empty object if it's generating a test build |
| 30 | * Karma will set this when it's a test build |
| 31 | */ |
Ed Tanous | fc7a33f | 2018-09-06 16:52:19 -0700 | [diff] [blame] | 32 | config.entry = {app: './app/index.js'}; |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 33 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Output |
| 36 | * Reference: http://webpack.github.io/docs/configuration.html#output |
| 37 | * Should be an empty object if it's generating a test build |
| 38 | * Karma will handle setting it up for you when it's a test build |
| 39 | */ |
Ed Tanous | fc7a33f | 2018-09-06 16:52:19 -0700 | [diff] [blame] | 40 | config.output = { |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 41 | // Absolute output directory |
| 42 | path: __dirname + '/dist', |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 43 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 44 | // Output path from the view of the page |
| 45 | // Uses webpack-dev-server in development |
| 46 | publicPath: '/', |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 47 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 48 | // Filename for entry points |
| 49 | // Only adds hash in build mode |
Ed Tanous | fc7a33f | 2018-09-06 16:52:19 -0700 | [diff] [blame] | 50 | filename: '[name].bundle.js', |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 51 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 52 | // Filename for non-entry points |
| 53 | // Only adds hash in build mode |
Ed Tanous | fc7a33f | 2018-09-06 16:52:19 -0700 | [diff] [blame] | 54 | chunkFilename: '[name].bundle.js' |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 55 | }; |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 56 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 57 | /** |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 58 | * Loaders |
| 59 | * Reference: |
| 60 | * http://webpack.github.io/docs/configuration.html#module-loaders |
| 61 | * List: http://webpack.github.io/docs/list-of-loaders.html |
| 62 | * This handles most of the magic responsible for converting modules |
| 63 | */ |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 64 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 65 | // Initialize module |
| 66 | config.module = { |
| 67 | rules: [ |
| 68 | { |
| 69 | // JS LOADER |
| 70 | // Reference: https://github.com/babel/babel-loader |
| 71 | // Transpile .js files using babel-loader |
| 72 | // Compiles ES6 and ES7 into ES5 code |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 73 | test: /\.js$/, |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 74 | use: 'babel-loader', |
| 75 | exclude: /node_modules/ |
| 76 | }, |
Ed Tanous | 0f2f981 | 2018-12-19 17:59:28 -0800 | [diff] [blame] | 77 | { |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 78 | // ASSET LOADER |
| 79 | // Reference: https://github.com/webpack/file-loader |
| 80 | // Copy png, jpg, jpeg, gif, svg, woff, woff2, ttf, eot files to |
| 81 | // output |
| 82 | // Rename the file using the asset hash |
| 83 | // Pass along the updated reference to your code |
| 84 | // You can add here any file extension you want to get copied to your |
| 85 | // output |
| 86 | test: /\.(png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot|ico)$/, |
| 87 | loader: 'file-loader', |
Ed Tanous | fc7a33f | 2018-09-06 16:52:19 -0700 | [diff] [blame] | 88 | options: {name: '[path][name].[ext]'} |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 89 | }, |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 90 | { |
| 91 | // HTML LOADER |
| 92 | // Reference: https://github.com/webpack/raw-loader |
| 93 | // Allow loading html through js |
| 94 | test: /\.html$/, |
Ed Tanous | fc7a33f | 2018-09-06 16:52:19 -0700 | [diff] [blame] | 95 | loader: 'html-loader' |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 96 | }, |
Ed Tanous | 0f2f981 | 2018-12-19 17:59:28 -0800 | [diff] [blame] | 97 | {test: /\.css$/, use: [MiniCssExtractPlugin.loader, 'css-loader']}, { |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 98 | test: /\.scss$/, |
Ed Tanous | 0f2f981 | 2018-12-19 17:59:28 -0800 | [diff] [blame] | 99 | use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'] |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 100 | } |
| 101 | ] |
| 102 | }; |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 103 | |
Ed Tanous | fc7a33f | 2018-09-06 16:52:19 -0700 | [diff] [blame] | 104 | config.plugins = [ |
| 105 | new HtmlWebpackPlugin({ |
| 106 | template: './app/index.html', |
| 107 | inject: 'body', |
| 108 | favicon: './app/assets/images/favicon.ico', |
Ed Tanous | fc7a33f | 2018-09-06 16:52:19 -0700 | [diff] [blame] | 109 | minify: {removeComments: true, collapseWhitespace: true}, |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 110 | |
Ed Tanous | fc7a33f | 2018-09-06 16:52:19 -0700 | [diff] [blame] | 111 | }), |
Ed Tanous | 0f2f981 | 2018-12-19 17:59:28 -0800 | [diff] [blame] | 112 | new CSPWebpackPlugin({ |
| 113 | 'base-uri': '\'self\'', |
| 114 | 'object-src': '\'none\'', |
| 115 | 'script-src': ['\'self\''], |
Ed tanous | e9211cb | 2018-04-22 10:53:28 -0700 | [diff] [blame^] | 116 | 'style-src': ['\'self\''], |
| 117 | // KVM requires image buffers from data: payloads, so allow that in |
| 118 | // img-src |
| 119 | // https://stackoverflow.com/questions/18447970/content-security-policy-data-not-working-for-base64-images-in-chrome-28 |
| 120 | 'img-src': ['\'self\'', 'data:'], |
Ed Tanous | 0f2f981 | 2018-12-19 17:59:28 -0800 | [diff] [blame] | 121 | }), |
| 122 | new MiniCssExtractPlugin(), |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 123 | |
Ed Tanous | fc7a33f | 2018-09-06 16:52:19 -0700 | [diff] [blame] | 124 | new FilterChunkWebpackPlugin({ |
Ed Tanous | fc7a33f | 2018-09-06 16:52:19 -0700 | [diff] [blame] | 125 | patterns: [ |
Ed Tanous | fc7a33f | 2018-09-06 16:52:19 -0700 | [diff] [blame] | 126 | '*glyphicons-halflings-regular*.ttf', |
| 127 | '*glyphicons-halflings-regular*.svg', |
| 128 | '*glyphicons-halflings-regular*.eot', |
| 129 | '*glyphicons-halflings-regular*.woff2', |
| 130 | ] |
| 131 | }) |
| 132 | ]; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 133 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 134 | // Add build specific plugins |
| 135 | if (isProd) { |
Ed Tanous | fc7a33f | 2018-09-06 16:52:19 -0700 | [diff] [blame] | 136 | config.plugins.push(new CompressionPlugin({deleteOriginalAssets: true})); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 137 | } |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 138 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 139 | /** |
| 140 | * Dev server configuration |
| 141 | * Reference: http://webpack.github.io/docs/configuration.html#devserver |
| 142 | * Reference: http://webpack.github.io/docs/webpack-dev-server.html |
| 143 | */ |
| 144 | config.devServer = {contentBase: './src/public', stats: 'minimal'}; |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 145 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 146 | return config; |
Ed Tanous | fc7a33f | 2018-09-06 16:52:19 -0700 | [diff] [blame] | 147 | }; |