blob: 63ddda2dd45a71a91d68d80331398855775ce008 [file] [log] [blame]
Ed Tanousbbcf6702017-10-06 13:53:06 -07001'use strict';
2
3// Modules
4var webpack = require('webpack');
5var autoprefixer = require('autoprefixer');
6var HtmlWebpackPlugin = require('html-webpack-plugin');
7var ExtractTextPlugin = require('extract-text-webpack-plugin');
8var CopyWebpackPlugin = require('copy-webpack-plugin');
9var CompressionPlugin = require('compression-webpack-plugin');
10var AssetsPlugin = require('assets-webpack-plugin');
11var CopyWebpackPlugin = require('copy-webpack-plugin');
12var path = require('path');
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070013var UglifyJsPlugin = require('uglifyjs-webpack-plugin');
Ed Tanousbbcf6702017-10-06 13:53:06 -070014
15/**
16 * Env
17 * Get npm lifecycle event to identify the environment
18 */
19var ENV = process.env.npm_lifecycle_event;
20var isTest = ENV === 'test' || ENV === 'test-watch';
21var isProd = ENV === 'build';
22
23module.exports = [
24 function makeWebpackConfig() {
25 /**
26 * Config
27 * Reference: http://webpack.github.io/docs/configuration.html
28 * This is the object where all configuration gets set
29 */
30 var config = {};
31
32 /**
33 * Entry
34 * Reference: http://webpack.github.io/docs/configuration.html#entry
35 * Should be an empty object if it's generating a test build
36 * Karma will set this when it's a test build
37 */
38 config.entry = isTest ? void 0 : {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070039 app: './app/index.js'
Ed Tanousbbcf6702017-10-06 13:53:06 -070040
41 };
42
43 /**
44 * Output
45 * Reference: http://webpack.github.io/docs/configuration.html#output
46 * Should be an empty object if it's generating a test build
47 * Karma will handle setting it up for you when it's a test build
48 */
49 config.output = isTest ? {} : {
50 // Absolute output directory
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070051 path: __dirname + '/dist',
Ed Tanousbbcf6702017-10-06 13:53:06 -070052
53 // Output path from the view of the page
54 // Uses webpack-dev-server in development
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070055 publicPath: '/',
Ed Tanousbbcf6702017-10-06 13:53:06 -070056
57 // Filename for entry points
58 // Only adds hash in build mode
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070059 filename: isProd ? '[name].[hash].js' : '[name].bundle.js',
Ed Tanousbbcf6702017-10-06 13:53:06 -070060
61 // Filename for non-entry points
62 // Only adds hash in build mode
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070063 chunkFilename: isProd ? '[name].[hash].js' : '[name].bundle.js'
Ed Tanousbbcf6702017-10-06 13:53:06 -070064 };
65
66 /**
67 * Devtool
68 * Reference: http://webpack.github.io/docs/configuration.html#devtool
69 * Type of sourcemap to use per build type
70 */
71 if (isTest) {
72 https:
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070073 // unix.stackexchange.com/questions/144208/find-files-without-extension
74 config.devtool = 'inline-source-map';
75 }
Ed Tanousbbcf6702017-10-06 13:53:06 -070076 else if (isProd) {
77 config.devtool = 'source-map';
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070078 }
Ed Tanousbbcf6702017-10-06 13:53:06 -070079 else {
80 config.devtool = 'eval-source-map';
81 }
82
83 /**
84 * Loaders
85 * Reference:
86 * http://webpack.github.io/docs/configuration.html#module-loaders
87 * List: http://webpack.github.io/docs/list-of-loaders.html
88 * This handles most of the magic responsible for converting modules
89 */
90
91 // Initialize module
92 config.module = {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070093 rules: [{
Ed Tanousbbcf6702017-10-06 13:53:06 -070094 // JS LOADER
95 // Reference: https://github.com/babel/babel-loader
96 // Transpile .js files using babel-loader
97 // Compiles ES6 and ES7 into ES5 code
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070098 test: /\.js$/,
99 use: 'babel-loader',
100 exclude: /node_modules/
Ed Tanousbbcf6702017-10-06 13:53:06 -0700101 },
102 {
103 // CSS LOADER
104 // Reference: https://github.com/webpack/css-loader
105 // Allow loading css through js
106 //
107 // Reference: https://github.com/postcss/postcss-loader
108 // Postprocess your css with PostCSS plugins
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700109 test: /\.css$/,
Ed Tanousbbcf6702017-10-06 13:53:06 -0700110 // Reference: https://github.com/webpack/extract-text-webpack-plugin
111 // Extract css files in production builds
112 //
113 // Reference: https://github.com/webpack/style-loader
114 // Use style-loader in development.
115
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700116 loader: isTest ? 'null-loader' : ExtractTextPlugin.extract({
117 fallback: 'style-loader',
118 use: [{
119 loader: 'css-loader',
120 query: {
121 sourceMap: true
122 }
123 },
124 {
125 loader: 'postcss-loader'
126 }
Ed Tanousbbcf6702017-10-06 13:53:06 -0700127 ],
128 })
129 },
130 {
131 // ASSET LOADER
132 // Reference: https://github.com/webpack/file-loader
133 // Copy png, jpg, jpeg, gif, svg, woff, woff2, ttf, eot files to
134 // output
135 // Rename the file using the asset hash
136 // Pass along the updated reference to your code
137 // You can add here any file extension you want to get copied to your
138 // output
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700139 test: /\.(png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot|ico)$/,
140 loader: 'file-loader',
141 options: {
Ed Tanousbbcf6702017-10-06 13:53:06 -0700142 name(file) {
143 if (!isProd) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700144 return '[path][name].[ext]';
145 }
Ed Tanousbbcf6702017-10-06 13:53:06 -0700146
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700147 return '[hash].[ext]';
Ed Tanousbbcf6702017-10-06 13:53:06 -0700148 }
149 }
150 },
151 {
152 // HTML LOADER
153 // Reference: https://github.com/webpack/raw-loader
154 // Allow loading html through js
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700155 test: /\.html$/,
156 use: {
157 loader: 'html-loader'
158 }
Ed Tanousbbcf6702017-10-06 13:53:06 -0700159 },
160 // JSON LOADER
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700161 {
162 test: /\.json$/,
163 loader: 'json-loader'
164 }, {
165 test: /\.scss$/,
166 use: [{
167 loader: 'style-loader' // creates style nodes from JS strings
Ed Tanousbbcf6702017-10-06 13:53:06 -0700168 },
169 {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700170 loader: 'css-loader' // translates CSS into CommonJS
Ed Tanousbbcf6702017-10-06 13:53:06 -0700171 },
172 {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700173 loader: 'sass-loader' // compiles Sass to CSS
Ed Tanousbbcf6702017-10-06 13:53:06 -0700174 }
175 ]
176 }
177 ]
178 };
179
180 // ISTANBUL LOADER
181 // https://github.com/deepsweet/istanbul-instrumenter-loader
182 // Instrument JS files with istanbul-lib-instrument for subsequent code
183 // coverage reporting
184 // Skips node_modules and files that end with .spec.js
185 if (isTest) {
186 config.module.rules.push({
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700187 enforce: 'pre',
188 test: /\.js$/,
189 exclude: [/node_modules/, /\.spec\.js$/],
190 loader: 'istanbul-instrumenter-loader',
191 query: {
192 esModules: true
193 }
194 });
Ed Tanousbbcf6702017-10-06 13:53:06 -0700195 }
196
197 /**
198 * PostCSS
199 * Reference: https://github.com/postcss/autoprefixer-core
200 * Add vendor prefixes to your css
201 */
202 // NOTE: This is now handled in the `postcss.config.js`
203 // webpack2 has some issues, making the config file necessary
204
205 /**
206 * Plugins
207 * Reference: http://webpack.github.io/docs/configuration.html#plugins
208 * List: http://webpack.github.io/docs/list-of-plugins.html
209 */
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700210 config.plugins = [new webpack.LoaderOptionsPlugin({
211 test: /\.scss$/i,
212 options: {
213 postcss: {
214 plugins: [autoprefixer]
215 }
216 },
217 debug: !isProd
218 })];
Ed Tanousbbcf6702017-10-06 13:53:06 -0700219
220 // Skip rendering index.html in test mode
221 if (!isTest) {
222 // Reference: https://github.com/ampedandwired/html-webpack-plugin
223 // Render index.html
224 config.plugins.push(
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700225 new HtmlWebpackPlugin({
226 template: './app/index.html',
227 inject: 'body',
228 favicon: './app/assets/images/favicon.ico'
229 }),
Ed Tanousbbcf6702017-10-06 13:53:06 -0700230
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700231 // Reference: https://github.com/webpack/extract-text-webpack-plugin
232 // Extract css files
233 // Disabled when in test mode or not in build mode
234 new ExtractTextPlugin({
235 filename: 'css/[name].css',
236 disable: !isProd,
237 allChunks: true
238 }));
239 }
Ed Tanousbbcf6702017-10-06 13:53:06 -0700240
241 // Add build specific plugins
242 if (isProd) {
243 config.plugins.push(
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700244 // Reference:
245 // http://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin
246 // Minify all javascript, switch loaders to minimizing mode
247 // TODO: openbmc/openbmc#2871 Mangling currently breaks the GUI.
248 new UglifyJsPlugin({
249 uglifyOptions: {
250 mangle: false
251 }
252 }),
Ed Tanousbbcf6702017-10-06 13:53:06 -0700253
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700254 // Copy assets from the public folder
255 // Reference: https://github.com/kevlened/copy-webpack-plugin
256 new CopyWebpackPlugin([{
257 from: __dirname + '/app/assets'
258 }]),
259 new CompressionPlugin({
260 deleteOriginalAssets: true
261 }));
Ed Tanousbbcf6702017-10-06 13:53:06 -0700262 }
263
264 /**
265 * Dev server configuration
266 * Reference: http://webpack.github.io/docs/configuration.html#devserver
267 * Reference: http://webpack.github.io/docs/webpack-dev-server.html
268 */
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700269 config.devServer = {
270 contentBase: './src/public',
271 stats: 'minimal'
272 };
Ed Tanousbbcf6702017-10-06 13:53:06 -0700273
274 return config;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700275 }()
276];