blob: 7630954d076a2028e1e046cc5913c06115402713 [file] [log] [blame]
Ed Tanousbbcf6702017-10-06 13:53:06 -07001'use strict';
2
3// Modules
4var webpack = require('webpack');
5var autoprefixer = require('autoprefixer');
Ed Tanousfc7a33f2018-09-06 16:52:19 -07006var HtmlWebpackInlineSourcePlugin =
7 require('html-webpack-inline-source-plugin');
Ed Tanousbbcf6702017-10-06 13:53:06 -07008var HtmlWebpackPlugin = require('html-webpack-plugin');
Ed Tanousbbcf6702017-10-06 13:53:06 -07009var CopyWebpackPlugin = require('copy-webpack-plugin');
10var CompressionPlugin = require('compression-webpack-plugin');
Ed Tanousbbcf6702017-10-06 13:53:06 -070011var path = require('path');
Ed Tanousfc7a33f2018-09-06 16:52:19 -070012var FilterChunkWebpackPlugin = require('filter-chunk-webpack-plugin');
13var MiniCssExtractPlugin = require('mini-css-extract-plugin');
Ed Tanousbbcf6702017-10-06 13:53:06 -070014
Ed Tanousdc25db02019-07-31 16:42:11 -070015const isPathInside = require('is-path-inside')
16const pkgDir = require('pkg-dir')
17const coreJsDir = pkgDir.sync(require.resolve('core-js'))
18
Ed Tanousfc7a33f2018-09-06 16:52:19 -070019module.exports = (env, options) => {
20 var isProd = options.mode === 'production';
Ed Tanousbbcf6702017-10-06 13:53:06 -070021
Andrew Geisslerd27bb132018-05-24 11:07:27 -070022 /**
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 Tanousbbcf6702017-10-06 13:53:06 -070028
Andrew Geisslerd27bb132018-05-24 11:07:27 -070029 /**
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 Tanousfc7a33f2018-09-06 16:52:19 -070035 config.entry = {app: './app/index.js'};
Ed Tanousbbcf6702017-10-06 13:53:06 -070036
Andrew Geisslerd27bb132018-05-24 11:07:27 -070037 /**
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 Tanousfc7a33f2018-09-06 16:52:19 -070043 config.output = {
Andrew Geisslerd27bb132018-05-24 11:07:27 -070044 // Absolute output directory
45 path: __dirname + '/dist',
Ed Tanousbbcf6702017-10-06 13:53:06 -070046
Andrew Geisslerd27bb132018-05-24 11:07:27 -070047 // Output path from the view of the page
48 // Uses webpack-dev-server in development
49 publicPath: '/',
Ed Tanousbbcf6702017-10-06 13:53:06 -070050
Andrew Geisslerd27bb132018-05-24 11:07:27 -070051 // Filename for entry points
52 // Only adds hash in build mode
Ed Tanousfc7a33f2018-09-06 16:52:19 -070053 filename: '[name].bundle.js',
Ed Tanousbbcf6702017-10-06 13:53:06 -070054
Andrew Geisslerd27bb132018-05-24 11:07:27 -070055 // Filename for non-entry points
56 // Only adds hash in build mode
Ed Tanousfc7a33f2018-09-06 16:52:19 -070057 chunkFilename: '[name].bundle.js'
Andrew Geisslerd27bb132018-05-24 11:07:27 -070058 };
Ed Tanousbbcf6702017-10-06 13:53:06 -070059
Andrew Geisslerd27bb132018-05-24 11:07:27 -070060 /**
Andrew Geisslerd27bb132018-05-24 11:07:27 -070061 * 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 Tanousbbcf6702017-10-06 13:53:06 -070067
Andrew Geisslerd27bb132018-05-24 11:07:27 -070068 // 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 Geisslerba5e3f32018-05-24 10:58:00 -070076 test: /\.js$/,
Ed Tanousdc25db02019-07-31 16:42:11 -070077 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 Geisslerd27bb132018-05-24 11:07:27 -070084 },
Ed Tanous0f2f9812018-12-19 17:59:28 -080085 {
Andrew Geisslerd27bb132018-05-24 11:07:27 -070086 // 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 Muranakac86ce3c2019-06-05 12:30:30 -050092 // You can add here any file extension you want to get copied
93 // to your output
94 // Excludes .svg files in icons directory
Andrew Geisslerd27bb132018-05-24 11:07:27 -070095 test: /\.(png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot|ico)$/,
Yoshie Muranakac86ce3c2019-06-05 12:30:30 -050096 exclude: /icons\/.*\.svg$/,
Andrew Geisslerd27bb132018-05-24 11:07:27 -070097 loader: 'file-loader',
Ed Tanousfc7a33f2018-09-06 16:52:19 -070098 options: {name: '[path][name].[ext]'}
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070099 },
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700100 {
Yoshie Muranakac86ce3c2019-06-05 12:30:30 -0500101 // 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 Geisslerd27bb132018-05-24 11:07:27 -0700108 // HTML LOADER
109 // Reference: https://github.com/webpack/raw-loader
110 // Allow loading html through js
111 test: /\.html$/,
Ed Tanousfc7a33f2018-09-06 16:52:19 -0700112 loader: 'html-loader'
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700113 },
Yoshie Muranakac86ce3c2019-06-05 12:30:30 -0500114 {
115 test: /\.css$/,
116 use: [MiniCssExtractPlugin.loader, 'css-loader'],
117 },
118 {
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700119 test: /\.scss$/,
Ed Tanous0f2f9812018-12-19 17:59:28 -0800120 use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700121 }
122 ]
123 };
Ed Tanousbbcf6702017-10-06 13:53:06 -0700124
Ed Tanousfc7a33f2018-09-06 16:52:19 -0700125 config.plugins = [
126 new HtmlWebpackPlugin({
127 template: './app/index.html',
128 inject: 'body',
129 favicon: './app/assets/images/favicon.ico',
Ed Tanousfc7a33f2018-09-06 16:52:19 -0700130 minify: {removeComments: true, collapseWhitespace: true},
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700131
Ed Tanousfc7a33f2018-09-06 16:52:19 -0700132 }),
Ed Tanous0f2f9812018-12-19 17:59:28 -0800133 new MiniCssExtractPlugin(),
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700134
Ed Tanousfc7a33f2018-09-06 16:52:19 -0700135 new FilterChunkWebpackPlugin({
Ed Tanousfc7a33f2018-09-06 16:52:19 -0700136 patterns: [
Ed Tanousfc7a33f2018-09-06 16:52:19 -0700137 '*glyphicons-halflings-regular*.ttf',
138 '*glyphicons-halflings-regular*.svg',
139 '*glyphicons-halflings-regular*.eot',
140 '*glyphicons-halflings-regular*.woff2',
141 ]
142 })
143 ];
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700144
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700145 // Add build specific plugins
146 if (isProd) {
Ed Tanousfc7a33f2018-09-06 16:52:19 -0700147 config.plugins.push(new CompressionPlugin({deleteOriginalAssets: true}));
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700148 }
Ed Tanousbbcf6702017-10-06 13:53:06 -0700149
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700150 /**
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 Tanousbbcf6702017-10-06 13:53:06 -0700156
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700157 return config;
Ed Tanousfc7a33f2018-09-06 16:52:19 -0700158};