blob: 1d66f12c34f4d2d759db8523dd837a79e3e8ff3e [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');
11var AssetsPlugin = require('assets-webpack-plugin');
Ed Tanousbbcf6702017-10-06 13:53:06 -070012var path = require('path');
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070013var UglifyJsPlugin = require('uglifyjs-webpack-plugin');
Ed Tanousfc7a33f2018-09-06 16:52:19 -070014var FilterChunkWebpackPlugin = require('filter-chunk-webpack-plugin');
15var MiniCssExtractPlugin = require('mini-css-extract-plugin');
Ed Tanousbbcf6702017-10-06 13:53:06 -070016
Ed Tanousfc7a33f2018-09-06 16:52:19 -070017module.exports = (env, options) => {
18 var isProd = options.mode === 'production';
Ed Tanousbbcf6702017-10-06 13:53:06 -070019
Andrew Geisslerd27bb132018-05-24 11:07:27 -070020 /**
21 * Config
22 * Reference: http://webpack.github.io/docs/configuration.html
23 * This is the object where all configuration gets set
24 */
25 var config = {};
Ed Tanousbbcf6702017-10-06 13:53:06 -070026
Andrew Geisslerd27bb132018-05-24 11:07:27 -070027 /**
28 * Entry
29 * Reference: http://webpack.github.io/docs/configuration.html#entry
30 * Should be an empty object if it's generating a test build
31 * Karma will set this when it's a test build
32 */
Ed Tanousfc7a33f2018-09-06 16:52:19 -070033 config.entry = {app: './app/index.js'};
Ed Tanousbbcf6702017-10-06 13:53:06 -070034
Andrew Geisslerd27bb132018-05-24 11:07:27 -070035 /**
36 * Output
37 * Reference: http://webpack.github.io/docs/configuration.html#output
38 * Should be an empty object if it's generating a test build
39 * Karma will handle setting it up for you when it's a test build
40 */
Ed Tanousfc7a33f2018-09-06 16:52:19 -070041 config.output = {
Andrew Geisslerd27bb132018-05-24 11:07:27 -070042 // Absolute output directory
43 path: __dirname + '/dist',
Ed Tanousbbcf6702017-10-06 13:53:06 -070044
Andrew Geisslerd27bb132018-05-24 11:07:27 -070045 // Output path from the view of the page
46 // Uses webpack-dev-server in development
47 publicPath: '/',
Ed Tanousbbcf6702017-10-06 13:53:06 -070048
Andrew Geisslerd27bb132018-05-24 11:07:27 -070049 // Filename for entry points
50 // Only adds hash in build mode
Ed Tanousfc7a33f2018-09-06 16:52:19 -070051 filename: '[name].bundle.js',
Ed Tanousbbcf6702017-10-06 13:53:06 -070052
Andrew Geisslerd27bb132018-05-24 11:07:27 -070053 // Filename for non-entry points
54 // Only adds hash in build mode
Ed Tanousfc7a33f2018-09-06 16:52:19 -070055 chunkFilename: '[name].bundle.js'
Andrew Geisslerd27bb132018-05-24 11:07:27 -070056 };
Ed Tanousbbcf6702017-10-06 13:53:06 -070057
Andrew Geisslerd27bb132018-05-24 11:07:27 -070058 /**
Andrew Geisslerd27bb132018-05-24 11:07:27 -070059 * Loaders
60 * Reference:
61 * http://webpack.github.io/docs/configuration.html#module-loaders
62 * List: http://webpack.github.io/docs/list-of-loaders.html
63 * This handles most of the magic responsible for converting modules
64 */
Ed Tanousbbcf6702017-10-06 13:53:06 -070065
Andrew Geisslerd27bb132018-05-24 11:07:27 -070066 // Initialize module
67 config.module = {
68 rules: [
69 {
70 // JS LOADER
71 // Reference: https://github.com/babel/babel-loader
72 // Transpile .js files using babel-loader
73 // Compiles ES6 and ES7 into ES5 code
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070074 test: /\.js$/,
Andrew Geisslerd27bb132018-05-24 11:07:27 -070075 use: 'babel-loader',
76 exclude: /node_modules/
77 },
Ed Tanousfc7a33f2018-09-06 16:52:19 -070078 {test: /\.css$/, use: [MiniCssExtractPlugin.loader, 'css-loader']}, {
Andrew Geisslerd27bb132018-05-24 11:07:27 -070079 // ASSET LOADER
80 // Reference: https://github.com/webpack/file-loader
81 // Copy png, jpg, jpeg, gif, svg, woff, woff2, ttf, eot files to
82 // output
83 // Rename the file using the asset hash
84 // Pass along the updated reference to your code
85 // You can add here any file extension you want to get copied to your
86 // output
87 test: /\.(png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot|ico)$/,
88 loader: 'file-loader',
Ed Tanousfc7a33f2018-09-06 16:52:19 -070089 options: {name: '[path][name].[ext]'}
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070090 },
Andrew Geisslerd27bb132018-05-24 11:07:27 -070091 {
92 // HTML LOADER
93 // Reference: https://github.com/webpack/raw-loader
94 // Allow loading html through js
95 test: /\.html$/,
Ed Tanousfc7a33f2018-09-06 16:52:19 -070096 loader: 'html-loader'
Andrew Geisslerd27bb132018-05-24 11:07:27 -070097 },
Ed Tanousfc7a33f2018-09-06 16:52:19 -070098 {
Andrew Geisslerd27bb132018-05-24 11:07:27 -070099 test: /\.scss$/,
100 use: [
101 {
102 loader: 'style-loader' // creates style nodes from JS strings
103 },
104 {
105 loader: 'css-loader' // translates CSS into CommonJS
106 },
107 {
108 loader: 'sass-loader' // compiles Sass to CSS
109 }
110 ]
111 }
112 ]
113 };
Ed Tanousbbcf6702017-10-06 13:53:06 -0700114
Ed Tanousfc7a33f2018-09-06 16:52:19 -0700115 config.plugins = [
116 new HtmlWebpackPlugin({
117 template: './app/index.html',
118 inject: 'body',
119 favicon: './app/assets/images/favicon.ico',
120 inlineSource: '.(js|css)$', // embed all javascript and css inline
121 minify: {removeComments: true, collapseWhitespace: true},
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700122
Ed Tanousfc7a33f2018-09-06 16:52:19 -0700123 }),
124 new MiniCssExtractPlugin(), new HtmlWebpackInlineSourcePlugin(),
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700125
Ed Tanousfc7a33f2018-09-06 16:52:19 -0700126 new FilterChunkWebpackPlugin({
127 // The webpack inline source plugin will embed the css and javascript
128 // into our html, so we need to strip it out here so it doesn't take
129 // up space
130 patterns: [
131 '*.css',
132 '*.js',
133 '*glyphicons-halflings-regular*.ttf',
134 '*glyphicons-halflings-regular*.svg',
135 '*glyphicons-halflings-regular*.eot',
136 '*glyphicons-halflings-regular*.woff2',
137 ]
138 })
139 ];
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700140
Ed Tanousbbcf6702017-10-06 13:53:06 -0700141
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700142 // Add build specific plugins
143 if (isProd) {
Ed Tanousfc7a33f2018-09-06 16:52:19 -0700144 config.plugins.push(new CompressionPlugin({deleteOriginalAssets: true}));
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700145 }
Ed Tanousbbcf6702017-10-06 13:53:06 -0700146
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700147 /**
148 * Dev server configuration
149 * Reference: http://webpack.github.io/docs/configuration.html#devserver
150 * Reference: http://webpack.github.io/docs/webpack-dev-server.html
151 */
152 config.devServer = {contentBase: './src/public', stats: 'minimal'};
Ed Tanousbbcf6702017-10-06 13:53:06 -0700153
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700154 return config;
Ed Tanousfc7a33f2018-09-06 16:52:19 -0700155};