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