-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.theme.js
173 lines (162 loc) · 4.43 KB
/
webpack.theme.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
166
167
168
169
170
171
172
173
import path from 'path';
import { glob } from 'glob';
import MiniCssExtractPlugin from "mini-css-extract-plugin";
import RemoveEmptyScriptsPlugin from 'webpack-remove-empty-scripts';
import PostCSSAssetsPlugin from 'postcss-assets-webpack-plugin';
import sortMediaQueries from 'postcss-sort-media-queries';
import CssMinimizerPlugin from "css-minimizer-webpack-plugin";
import TerserPlugin from "terser-webpack-plugin";
const rootDir = path.resolve(import.meta.dirname);
const buildDir = 'dist';
// Define all entries:
const globals = ['./css/global.scss'];
const components = glob.sync('./templates/**/*.{scss,js}', {
dotRelative: true,
ignore: [
"./templates/**/_*", // Exclude from the bundle process files starting with '_'.
"./templates/**/_*/**" // Exclude from the bundle process entire directories starting with '_'.
]
});
const allMatches = [...globals, ...components];
const localOutputPath = (filePath) => {
const fileDir = path.dirname(filePath);
const fileBasename = path.basename(filePath);
const fileExtension = path.extname(fileBasename);
const fileName = path.basename(fileBasename, fileExtension);
let fileExtensionDir = 'ERROR/';
switch (fileExtension) {
case '.js':
fileExtensionDir = 'js/';
break;
case '.scss':
fileExtensionDir = 'css/';
break;
}
return fileName + '/' + fileExtensionDir + fileName + '.processed';
};
const entry = allMatches.reduce((acc, match) => {
acc[localOutputPath(match)] = match;
return acc;
}, {});
export default (env, argv) => {
const isProduction = argv.mode === 'production';
const config = {
entry: entry,
output: {
path: rootDir + "/" + buildDir,
filename: "[name].js",
clean: true,
},
module: {
rules: [
{
test: /\.scss$/,
exclude: [/node_modules/],
use: [
{
loader: MiniCssExtractPlugin.loader
},
{
loader: "css-loader",
options: {
sourceMap: !isProduction
}
},
{
loader: 'postcss-loader',
options: {
sourceMap: !isProduction,
postcssOptions: {
plugins: [
[
"autoprefixer",
{},
],
],
},
},
},
{
loader: "sass-loader",
options: {
sourceMap: !isProduction,
additionalData: `@import "${rootDir}/abstractions/index";`
}
}
]
},
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: { loader: 'babel-loader' }
},
{
test: /\.(png|jp(e*)g|svg|gif)$/,
exclude: /node_modules/,
type: 'asset',
generator: {
filename: (pathData) => {
const pathParts = pathData.filename.split("/");
const imagesIndex = pathParts.findIndex(k => k === 'images');
return pathParts.slice(imagesIndex - 1).join('/');
}
}
}
]
},
resolve: {
extensions: [".js"],
alias: {
Components: path.resolve(import.meta.dirname, "templates/"),
},
},
plugins: [
new RemoveEmptyScriptsPlugin(),
new MiniCssExtractPlugin({
filename: '[name].css'
}),
new PostCSSAssetsPlugin({
test: /\.css$/,
log: false,
plugins: [
sortMediaQueries
]
}),
],
stats: {
preset: 'minimal',
},
mode: argv.mode,
devtool: isProduction ? false : 'eval-source-map',
watch: !isProduction,
watchOptions: {
poll: !isProduction ? 500 : undefined,
ignored: ["node_modules/**", "assets/**"],
},
optimization: isProduction ? {
minimize: true,
minimizer: [
new TerserPlugin({
parallel: true,
terserOptions: {
format: {
comments: false,
},
},
extractComments: false,
}),
new CssMinimizerPlugin({
minimizerOptions: {
preset: [
"default",
{
discardComments: { removeAllButFirst: true },
},
],
},
}),
],
} : undefined,
};
return config;
};