-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebpack.config.js
127 lines (121 loc) · 3.38 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
const webpack = require('webpack');
const path = require('path');
const glob = require('glob-all');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const PurgeCssPlugin = require('purgecss-webpack-plugin');
const WebpackAssetsManifest = require('webpack-assets-manifest');
const isProd = process.env.NODE_ENV === 'production';
const ifProd = (val, alt) => {
if (typeof val === 'undefined') {
return isProd;
}
return isProd ? val : alt;
};
const ifDev = (val, alt) => {
if (typeof val === 'undefined') {
return !isProd;
}
return !isProd ? val : alt;
};
const paths = {
CWD: path.resolve(__dirname),
DIST: path.resolve(__dirname, 'static'),
SRC: path.resolve(__dirname, 'src'),
SITE: path.resolve(__dirname, '../..')
};
// Custom PurgeCSS extractor for Tailwind that allows special characters in class names.
class TailwindExtractor {
static extract(content) {
// eslint-disable-next-line no-useless-escape
return content.match(/[A-z0-9-:\/]+/g) || [];
}
}
module.exports = {
// If a string or array of strings is passed, the chunk is named `main`.
entry: [
...glob.sync([
path.join(paths.SRC, 'js', '/**/*.{js,ts}'),
path.join(paths.SITE, 'src', 'js', '/**/*.{js,ts}')
]),
path.join(paths.SRC, 'styles', 'main.css')
// This is imported directly from main.css for now. Revisit when Tailwind can use `@apply` for
// classes that aren't in the same CSS tree.
// path.join(paths.SITE, 'styles', 'main.css')
],
output: {
path: paths.DIST,
filename: 'js/[name].[chunkhash:10].js'
},
devtool: isProd ? 'source-map' : 'cheap-eval-source-map',
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: ['babel-loader']
},
{
test: /\.s?css$/,
// Loader chaining works from right to left
use: [
// Use inline style loader for dev since MiniCss isn't writing things to disk properly
isProd ? MiniCssExtractPlugin.loader : 'style-loader',
{
loader: 'css-loader',
options: {
importLoaders: 1,
minimize: ifProd(),
sourceMap: ifDev()
}
},
{
loader: 'postcss-loader',
options: {
sourceMap: ifDev()
}
}
]
}
]
},
resolve: {
extensions: ['.ts', '.js'],
alias: {
vue$: 'vue/dist/vue.esm.js'
}
},
plugins: [
// https://github.com/webpack-contrib/extract-text-webpack-plugin/issues/701
new MiniCssExtractPlugin({
filename: 'styles/[name].[hash:10].css'
}),
// https://tailwindcss.com/docs/controlling-file-size
// https://www.purgecss.com/with-webpack
ifProd(
new PurgeCssPlugin({
paths: glob.sync([
path.join(paths.CWD, 'layouts/**/*.html'),
path.join(paths.CWD, 'content/**/*.{md,html}'),
path.join(paths.SRC, 'js', '/**/*.{js,ts}'),
path.join(paths.SITE, 'layouts/**/*.html'),
// path.join(paths.SITE, 'content/**/*.{md,html}'), TODO: This causes error! Why!
path.join(paths.SITE, 'src', 'js', '/**/*.{js,ts}')
]),
extractors: [
{
extractor: TailwindExtractor,
extensions: ['html', 'js', 'ts']
}
],
// Target all classes here that are dynamically assembled somewhere in a template,
// e.g. with `grid-columns-{{ .Params.num_cols }}`
// TODO: Revisit this!
whitelistPatterns: () => [/grid-columns/]
})
),
new WebpackAssetsManifest({
output: '../data/assetMap.json'
}),
ifDev(new webpack.NamedModulesPlugin())
].filter(el => el)
};