forked from spiral/shopify-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
165 lines (158 loc) · 4.43 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
160
161
162
163
164
165
const path = require('path');
const CopyPlugin = require('copy-webpack-plugin');
const ImageminPlugin = require('imagemin-webpack-plugin').default;
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const RemoveEmptyScriptsPlugin = require('webpack-remove-empty-scripts');
const RemoveFilesPlugin = require('remove-files-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const webpack = require('webpack');
const SafePostCssParser = require('postcss-safe-parser');
const Autoprefixer = require('autoprefixer');
const {
makeTemplatesEntryPoints,
makeSnippetCopyPluginPattern,
makeJsEntryPoints,
makeTemplateCopyPluginPattern,
makeSectionCopyPluginPattern,
getDirNames,
} = require('./webpack-helpers');
const TEXT_FILES_PATTERN = /\.(md|txt)$/m;
const IMAGE_FILES_PATTERN = /\.(jpg|jpeg|png|gif|svg)$/i;
const SRC_TEMPLATES_LIST = [
...getDirNames('src/templates').filter((dieName) => dieName !== 'common'),
...getDirNames('src/customers'),
];
const config = {
mode: 'production',
entry: {
...makeTemplatesEntryPoints('src/templates'),
...makeTemplatesEntryPoints('src/customers'),
...makeJsEntryPoints('src/scripts'),
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'assets/[name].js',
},
optimization: {
emitOnErrors: true,
minimize: false,
minimizer: [
new OptimizeCSSAssetsPlugin({
cssProcessorOptions: {
parser: SafePostCssParser,
map: {
// `inline: false` forces the sourcemap to be output into a
// separate file
inline: false,
// `annotation: true` appends the sourceMappingURL to the end of
// the css file, helping the browser find the sourcemap
annotation: true,
},
},
}),
new TerserPlugin({
terserOptions: {
compress: false,
},
}),
],
},
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
},
},
},
{
test: /\.(sass|scss|css)$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
'sass-loader',
],
},
{
test: IMAGE_FILES_PATTERN,
type: 'asset/resource',
},
],
},
plugins: [
new webpack.ProgressPlugin(),
new RemoveEmptyScriptsPlugin(),
new CopyPlugin({
patterns: [
{
from: path.resolve(__dirname, `theme`),
to: path.resolve(__dirname, `dist`),
noErrorOnMissing: true,
},
{
from: path.resolve(__dirname, `src/layout`),
to: path.resolve(__dirname, `dist/layout`),
noErrorOnMissing: true,
},
{
from: path.resolve(__dirname, `src/assets`),
to: path.resolve(__dirname, `dist/assets`),
noErrorOnMissing: true,
},
makeTemplateCopyPluginPattern('src/templates'),
makeTemplateCopyPluginPattern('src/customers', '/customers/'),
makeSectionCopyPluginPattern('src/templates'),
makeSnippetCopyPluginPattern('src/snippets'),
],
}),
new MiniCssExtractPlugin({
// Creating style snippet for each template and
// using snippet as inline styles.
// Implements a scoped styles.
filename: ({ chunk: { name } }) =>
SRC_TEMPLATES_LIST.includes(name)
? `snippets/${name}.css.liquid`
: `assets/${name}.css`,
}),
new RemoveFilesPlugin({
before: {
include: ['./dist'],
log: false,
logWarning: true,
logError: true,
logDebug: false,
},
after: {
test: [
{
folder: 'dist',
method: (absoluteItemPath) =>
TEXT_FILES_PATTERN.test(absoluteItemPath),
recursive: true,
},
],
log: false,
logWarning: false,
logError: false,
logDebug: false,
},
}),
Autoprefixer,
],
};
module.exports = (env, argv) => {
if (argv.mode === 'production') {
config.plugins = [
...config.plugins,
new ImageminPlugin({ test: IMAGE_FILES_PATTERN }),
];
config.optimization.minimize = true;
}
return config;
};