generated from andymantell/noodling-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
130 lines (124 loc) · 3.33 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
const glob = require('glob')
const path = require('path')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const fs = require('fs')
const yaml = require('js-yaml')
// Workaround for https://github.com/webpack/webpack/issues/7300 whereby
// webpack outputs empty chunks when used with the css extract plugin
// See exact solution at https://github.com/webpack/webpack/issues/7300#issuecomment-413959996
// Without this workaround, the css extract solution leaves behind empty JavaScript files
// Once this ticket has been resolved this workaround can be removed
class MiniCssExtractPluginCleanUp {
constructor (deleteWhere = /css.*\.js(\.map)?$/) {
this.shouldDelete = new RegExp(deleteWhere)
}
apply (compiler) {
compiler.hooks.emit.tapAsync('MiniCssExtractPluginCleanup', (compilation, callback) => {
Object.keys(compilation.assets).forEach((asset) => {
if (this.shouldDelete.test(asset)) {
delete compilation.assets[asset]
}
})
callback()
})
}
}
// Collect top level js and scss entrypoints
const files = glob.sync(path.join('src/{js,scss}/*.{js,scss}'))
const entrypoints = files.reduce((accumulator, value) => {
const assetExtension = path.extname(value)
const extMap = {
'.scss': 'css',
'.js': 'js'
}
accumulator[path.join(extMap[assetExtension], path.basename(value, assetExtension))] = path.resolve(value)
return accumulator
}, {})
// Construct the webpack config
const webpackConfig = {
mode: 'production',
bail: false,
devtool: 'source-map',
output: {
path: path.resolve('public')
},
resolve: {
modules: [
'node_modules'
]
},
resolveLoader: {
modules: [
'node_modules'
]
},
devServer: {
contentBase: './public',
hot: true,
watchContentBase: true
},
stats: {
assets: true,
colors: true,
entrypoints: false,
hash: false,
modules: false,
version: false
},
entry: entrypoints,
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules\/(?!(govuk-react-components|hmlr-design-system)\/).*/,
use: {
loader: 'babel-loader'
}
},
{
test: /\.(png|jpg|gif|svg)$/,
use: [
{
loader: 'file-loader',
options: {
name (file) {
const componentName = path.dirname(file).split(path.sep).pop()
return `images/hmlr-design-system/${componentName}/[name].[ext]`
}
}
}
]
},
{
test: /\.scss$/,
resolve: {
extensions: ['.scss']
},
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader?-url'
},
{
loader: 'postcss-loader'
},
{
loader: 'sass-loader'
}
]
}
]
},
plugins: [
new MiniCssExtractPlugin(),
new MiniCssExtractPluginCleanUp()
]
}
// If we're running inside the docker container, node_modules is placed
// at a different location defined by the NODE_PATH environment variable
// so we need to tell webpack to check there too
if ('NODE_PATH' in process.env) {
webpackConfig.resolve.modules.push(process.env.NODE_PATH)
webpackConfig.resolveLoader.modules.push(process.env.NODE_PATH)
}
module.exports = webpackConfig