This repository has been archived by the owner on Feb 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
webpack.base.config.js
160 lines (144 loc) · 3.89 KB
/
webpack.base.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
160
/*
* Base
* Two base configs are exported here - one for the site and the other
* for services (lambdas).
*/
const path = require('path');
const webpack = require('webpack');
const webpackMerge = require('webpack-merge').smart;
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const sharpImageLoader = require('responsive-loader/sharp');
const ImageminPlugin = require('imagemin-webpack-plugin').default;
const publicPath = `/${process.env.URL_BASENAME || ''}`;
const robots = process.env.ALLOW_ROBOTS ? 'robots-allow.txt' : 'robots-disallow.txt';
const devMode = process.env.NODE_ENV !== 'production';
const pathToFolder = folder => path.resolve(__dirname, folder);
function mediaFilesLoaders(options = {}) {
const { emitFile = true } = options;
const name = 'assets-honestly/[name]-[hash:base64:5].[ext]';
const exclude = ['dist', 'node_modules'].map(pathToFolder);
return [
{
test: /\.(jpe?g|png)$/i,
use: [
{
loader: 'responsive-loader',
options: {
name,
publicPath,
adapter: sharpImageLoader,
},
},
],
},
{
test: /\.(gif|eot|ttf|woff|woff2|mp4|webm)$/,
exclude,
use: [
{
loader: 'file-loader',
options: {
name,
publicPath,
emitFile,
},
},
],
},
];
}
const baseConfig = {
stats: 'minimal',
mode: 'development',
output: {
path: pathToFolder('dist'),
filename: '[name].js',
chunkFilename: '[name].js',
publicPath,
},
module: {
rules: [
{
test: /\.css$/,
use: [
devMode
? { loader: 'style-loader', options: { singleton: true } }
: MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
modules: true,
sourceMap: true,
importLoaders: 1,
localIdentName: '[name]__[local]___[hash:base64:5]',
},
},
'postcss-loader',
],
},
{
test: /\.js$/,
exclude: pathToFolder('node_modules'),
use: [
{
loader: 'babel-loader',
},
],
},
...mediaFilesLoaders(),
{
test: /\.ejs/,
use: [
{
loader: 'underscore-template-loader',
},
],
},
{
test: /.pug$/,
use: 'pug-loader',
},
],
},
devtool: 'source-map',
plugins: [
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
new webpack.EnvironmentPlugin(Object.keys(process.env)),
new MiniCssExtractPlugin({
filename: 'assets-honestly/styles-[contenthash].css',
}),
new OptimizeCSSAssetsPlugin(),
],
};
exports.baseWebConfig = webpackMerge(baseConfig, {
plugins: [
new CopyWebpackPlugin([
{ from: 'assets/favicons', to: 'assets-honestly/favicons' },
{ from: `assets/${robots}`, to: 'robots.txt' },
{ from: 'assets/manifest.json', to: 'manifest.json' },
{ from: 'assets/sitemap.xml', to: 'sitemap.xml' },
{ from: 'assets/google16329df1f462dd5e.html', to: 'google16329df1f462dd5e.html' },
{ from: 'assets/txt', to: 'txt/' },
{ from: 'assets/fonts', to: 'assets-honestly/' },
]),
// responsive-loader currently optimises jpegs but not other image types:
// https://github.com/herrstucki/responsive-loader/issues/63
new ImageminPlugin({
test: /\.(png|gif)$/,
disable: devMode,
optipng: {
optimizationLevel: 5,
},
gifsicle: {
optimizationLevel: 3,
},
}),
],
});
exports.baseServiceConfig = webpackMerge(baseConfig, {
module: {
rules: mediaFilesLoaders({ emitFile: false }),
},
});