-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.js
118 lines (104 loc) · 3.19 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
// Require modules and plugins
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const WebpackShellPluginNext = require("webpack-shell-plugin-next");
const fs = require("fs");
const glob = require("glob");
const path = require("path");
// Define a constant that expects a directoryPattern and a fileExtension
// Find all matching files in a folder and return it in an array
const mapFilenamesToEntries = (directoryPattern, fileExtension) =>
glob.sync(directoryPattern).reduce((entries, filename) => {
const regex = new RegExp("([^/]+)." + fileExtension + "$");
const [, name] = filename.match(regex);
return { ...entries, [name]: "./" + filename };
}, {});
// Define the JS configuration
const jsConfig = {
// Define the entry points for the JS configuration
entry: {
...mapFilenamesToEntries("./assets/src/js/**/*.js", "js"),
},
// Define the output for the JS configuration
output: {
// Export the output JS files to the ./assets/dist/js folder
path: path.resolve(__dirname, "./assets/dist/js"),
// Keep the same output name from the input file
filename: "[name].js",
},
// Define the rules for the JS configuration
module: {
rules: [
{
// Regex to match JS files
test: /\.js$/,
// Only include files in the src directory and its subdirectories
include: path.resolve(__dirname, "./assets/src/js"),
// Use these loaders to compile JS files
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env"],
},
},
},
],
},
};
// Define the CSS configuration
const cssConfig = {
// Define the entry points for your CSS configuration
entry: {
...mapFilenamesToEntries("./assets/src/scss/**/[^_]*.scss", "scss"),
},
// Export the output files (.js) to the /assets/tmp folder
output: {
// Keep the same output name from the input file
path: path.resolve(__dirname, "./assets/dist/tmp"),
},
// Define the rules for the CSS configuration
module: {
rules: [
{
// Regex to match SASS, SCSS and CSS files
test: /\.(sa|sc|c)ss$/,
// Only include files in the src directory and its subdirectories
include: path.resolve(__dirname, "./assets/src/scss"),
// Use these loaders to compile SASS, SCSS and CSS files
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
// Avoid parsing url or image-set in CSS
url: false,
},
},
{
loader: "sass-loader",
options: {
// Prefer `dart-sass`
implementation: require.resolve("sass"),
},
},
],
},
],
},
// Define the plugins used for the CSS configuration
plugins: [
new MiniCssExtractPlugin({
filename: "../css/[name].css",
}),
new WebpackShellPluginNext({
onBuildEnd: {
scripts: [
() => {
fs.rmSync("./assets/dist/tmp", { recursive: true });
},
],
},
}),
],
};
// Export both configurations as an array
module.exports = [jsConfig, cssConfig];