blob: 91cbea8f2952a6ea1e4b2aa61b0b832fc08e82f6 [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 Tanous0f2f9812018-12-19 17:59:28 -08008var CSPWebpackPlugin = require('csp-html-webpack-plugin');
Ed Tanousbbcf6702017-10-06 13:53:06 -07009var HtmlWebpackPlugin = require('html-webpack-plugin');
Ed Tanousbbcf6702017-10-06 13:53:06 -070010var CopyWebpackPlugin = require('copy-webpack-plugin');
11var CompressionPlugin = require('compression-webpack-plugin');
Ed Tanousbbcf6702017-10-06 13:53:06 -070012var path = require('path');
Ed Tanousfc7a33f2018-09-06 16:52:19 -070013var FilterChunkWebpackPlugin = require('filter-chunk-webpack-plugin');
14var MiniCssExtractPlugin = require('mini-css-extract-plugin');
Ed Tanousbbcf6702017-10-06 13:53:06 -070015
Ed Tanousfc7a33f2018-09-06 16:52:19 -070016module.exports = (env, options) => {
17 var isProd = options.mode === 'production';
Ed Tanousbbcf6702017-10-06 13:53:06 -070018
Andrew Geisslerd27bb132018-05-24 11:07:27 -070019 /**
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 Tanousbbcf6702017-10-06 13:53:06 -070025
Andrew Geisslerd27bb132018-05-24 11:07:27 -070026 /**
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 Tanousfc7a33f2018-09-06 16:52:19 -070032 config.entry = {app: './app/index.js'};
Ed Tanousbbcf6702017-10-06 13:53:06 -070033
Andrew Geisslerd27bb132018-05-24 11:07:27 -070034 /**
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 Tanousfc7a33f2018-09-06 16:52:19 -070040 config.output = {
Andrew Geisslerd27bb132018-05-24 11:07:27 -070041 // Absolute output directory
42 path: __dirname + '/dist',
Ed Tanousbbcf6702017-10-06 13:53:06 -070043
Andrew Geisslerd27bb132018-05-24 11:07:27 -070044 // Output path from the view of the page
45 // Uses webpack-dev-server in development
46 publicPath: '/',
Ed Tanousbbcf6702017-10-06 13:53:06 -070047
Andrew Geisslerd27bb132018-05-24 11:07:27 -070048 // Filename for entry points
49 // Only adds hash in build mode
Ed Tanousfc7a33f2018-09-06 16:52:19 -070050 filename: '[name].bundle.js',
Ed Tanousbbcf6702017-10-06 13:53:06 -070051
Andrew Geisslerd27bb132018-05-24 11:07:27 -070052 // Filename for non-entry points
53 // Only adds hash in build mode
Ed Tanousfc7a33f2018-09-06 16:52:19 -070054 chunkFilename: '[name].bundle.js'
Andrew Geisslerd27bb132018-05-24 11:07:27 -070055 };
Ed Tanousbbcf6702017-10-06 13:53:06 -070056
Andrew Geisslerd27bb132018-05-24 11:07:27 -070057 /**
Andrew Geisslerd27bb132018-05-24 11:07:27 -070058 * 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 Tanousbbcf6702017-10-06 13:53:06 -070064
Andrew Geisslerd27bb132018-05-24 11:07:27 -070065 // 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 Geisslerba5e3f32018-05-24 10:58:00 -070073 test: /\.js$/,
Andrew Geisslerd27bb132018-05-24 11:07:27 -070074 use: 'babel-loader',
75 exclude: /node_modules/
76 },
Ed Tanous0f2f9812018-12-19 17:59:28 -080077 {
Andrew Geisslerd27bb132018-05-24 11:07:27 -070078 // 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 Tanousfc7a33f2018-09-06 16:52:19 -070088 options: {name: '[path][name].[ext]'}
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070089 },
Andrew Geisslerd27bb132018-05-24 11:07:27 -070090 {
91 // HTML LOADER
92 // Reference: https://github.com/webpack/raw-loader
93 // Allow loading html through js
94 test: /\.html$/,
Ed Tanousfc7a33f2018-09-06 16:52:19 -070095 loader: 'html-loader'
Andrew Geisslerd27bb132018-05-24 11:07:27 -070096 },
Ed Tanous0f2f9812018-12-19 17:59:28 -080097 {test: /\.css$/, use: [MiniCssExtractPlugin.loader, 'css-loader']}, {
Andrew Geisslerd27bb132018-05-24 11:07:27 -070098 test: /\.scss$/,
Ed Tanous0f2f9812018-12-19 17:59:28 -080099 use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700100 }
101 ]
102 };
Ed Tanousbbcf6702017-10-06 13:53:06 -0700103
Ed Tanousfc7a33f2018-09-06 16:52:19 -0700104 config.plugins = [
105 new HtmlWebpackPlugin({
106 template: './app/index.html',
107 inject: 'body',
108 favicon: './app/assets/images/favicon.ico',
Ed Tanousfc7a33f2018-09-06 16:52:19 -0700109 minify: {removeComments: true, collapseWhitespace: true},
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700110
Ed Tanousfc7a33f2018-09-06 16:52:19 -0700111 }),
Ed Tanous0f2f9812018-12-19 17:59:28 -0800112 new CSPWebpackPlugin({
113 'base-uri': '\'self\'',
114 'object-src': '\'none\'',
115 'script-src': ['\'self\''],
116 'style-src': ['\'self\'']
117 }),
118 new MiniCssExtractPlugin(),
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700119
Ed Tanousfc7a33f2018-09-06 16:52:19 -0700120 new FilterChunkWebpackPlugin({
Ed Tanousfc7a33f2018-09-06 16:52:19 -0700121 patterns: [
Ed Tanousfc7a33f2018-09-06 16:52:19 -0700122 '*glyphicons-halflings-regular*.ttf',
123 '*glyphicons-halflings-regular*.svg',
124 '*glyphicons-halflings-regular*.eot',
125 '*glyphicons-halflings-regular*.woff2',
126 ]
127 })
128 ];
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700129
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700130 // Add build specific plugins
131 if (isProd) {
Ed Tanousfc7a33f2018-09-06 16:52:19 -0700132 config.plugins.push(new CompressionPlugin({deleteOriginalAssets: true}));
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700133 }
Ed Tanousbbcf6702017-10-06 13:53:06 -0700134
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700135 /**
136 * Dev server configuration
137 * Reference: http://webpack.github.io/docs/configuration.html#devserver
138 * Reference: http://webpack.github.io/docs/webpack-dev-server.html
139 */
140 config.devServer = {contentBase: './src/public', stats: 'minimal'};
Ed Tanousbbcf6702017-10-06 13:53:06 -0700141
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700142 return config;
Ed Tanousfc7a33f2018-09-06 16:52:19 -0700143};