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