-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
159 lines (154 loc) · 6.28 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/////////////////////////////////////////////////////////////////////////////////////////
//
// Webpack 2 and Bootstrap
//
// by Jocca on 29.05.17
//
/////////////////////////////////////////////////////////////////////////////////////////
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const webpack = require('webpack');
const path = require("path");
const bootstrapEntryPoints = require('./webpack.bootstrap.config');
const glob = require('glob');
const PurifyCSSPlugin = require('purifycss-webpack');
const isProd = process.env.NODE_ENV === 'production'; //true or false
// const isProd = process.argv.indexOf('-p') !== -1 // another option to read the production flag
/////////////////////////////////////////////////////////////////////////////////////////
// Style - CSS - SASS
// css-loader is used first to load the CSS, if present
// style-loader is used secondly to inject the CSS as inline in the HTML file
// sass-loader will compile the sass files into css files
// ExtractTextPlugin will extract and transform the CSS content into external CSS files.
/////////////////////////////////////////////////////////////////////////////////////////
const cssDev = [
'style-loader',
'css-loader?sourceMap',
'sass-loader',
{
loader: 'sass-resources-loader',
options: {
// Provide path to the file with resources
resources: [
'./src/resources.scss'
],
},
}];
// here the order matters
const cssProd = ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['css-loader','sass-loader', {
loader: 'sass-resources-loader',
options: {
// Provide path to the file with resources
resources: [
'./src/resources.scss'
],
},
}],
publicPath: '/dist'
})
// based on the environment (NODE_ENV) we use the CSS configuration
// for production or delevelopment
const cssConfig = isProd ? cssProd : cssDev;
const bootstrapConfig = isProd ? bootstrapEntryPoints.prod : bootstrapEntryPoints.dev;
const devtoolConfig = isProd ? 'false' : 'source-map';
module.exports = {
// it controls if and how source maps are generated.
// options:
// eval - pretty fast, but it doesn't display line numbers correctly
// inline-source-map - a SourceMap is added as a DataUrl to the bundle.
// eval-source-map - a SourceMap is added as a DataUrl to the eval(). Initially it is slow, but it provides fast rebuild speed.
// source-map - A full SourceMap is emitted as a separate file. Is is slow.
// see: https://webpack.js.org/configuration/devtool/
devtool: devtoolConfig,
entry: {
app: './src/app.js',
bootstrap: bootstrapConfig
},
output: {
path: path.resolve(__dirname, "dist"),
filename: '[name].bundle.js'
},
/////////////////////////////////////////////////////////////////////////////////////////
// Webpack Loaders
// For each type of file, you need to teach webpack what kind of loader it must use.
/////////////////////////////////////////////////////////////////////////////////////////
module: {
rules: [
{
test: /\.scss$/,
use: cssConfig
},
{
test: /\.js$/,
exclude: /node_modules/,
use: 'babel-loader'
},
{
test: /\.ico$/,
use: 'file-loader?name=images/[name].[ext]'
},
{
test: /\.css$/,
use: 'file-loader?name=css/[name].[ext]'
},
// here the order of the loaders matters
// file-loader: https://github.com/webpack-contrib/file-loader
// image-webpack-loader: https://github.com/tcoopman/image-webpack-loader
{
test: /\.(jpe?g|png|gif|svg)$/i,
use: [
'file-loader?name=images/[name].[ext]',
'image-webpack-loader?bypassOnDebug'
]
},
// apply the url-loader and file-loader to load the bootstrap icon fonts
{ test: /\.(woff2?)$/, use: 'url-loader?limit=10000&name=fonts/[name].[ext]' },
{ test: /\.(ttf|eot)$/, use: 'file-loader?name=fonts/[name].[ext]' },
// Bootstrap 3 uses jquery
// https://github.com/shakacode/bootstrap-loader#installation
{ test:/bootstrap-sass[\/\\]assets[\/\\]javascripts[\/\\]/, use: 'imports-loader?jQuery=jquery' }
]
},
/////////////////////////////////////////////////////////////////////////////////////////
// DEV SERVER
// copmpress: it uses gzip to serve the client / browser requests
// hot: it uses Hot Module Replacement (https://webpack.js.org/concepts/hot-module-replacement/)
// open: open the browser at the first run
// stats: it controls how verbose is the console output
// See https://webpack.js.org/configuration/dev-server/
/////////////////////////////////////////////////////////////////////////////////////////
devServer: {
contentBase: path.join(__dirname, "dist"),
compress: true,
hot: true,
open: true,
stats: 'errors-only'
},
plugins: [
new HtmlWebpackPlugin({
title: 'Project Demo',
hash: true,
template: './src/index.html'
}),
// ExtractTextPlugin does not work together with Hot Module Replacement(HRM)
// therefore we use it only in production
new ExtractTextPlugin({
filename: '/css/[name].css',
disable: !isProd,
allChunks: true
}),
// Hot Module Replacement(HRM) is used in development as a LiveReload replacementto.
// It watches the changes on the source code or the addition of new modules
// and notify the browser without the need to reload the whole application
// docs: https://webpack.js.org/concepts/hot-module-replacement/
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
// Make sure this is after ExtractTextPlugin!
new PurifyCSSPlugin({
// Give paths to parse for rules. These should be absolute!
paths: glob.sync(path.join(__dirname, 'src/*.html'))
})
]
}