-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
167 lines (156 loc) · 4.12 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
166
167
const path = require("path");
const fs = require("fs-extra");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const autoprefixer = require("autoprefixer");
const TerserWebpackPlugin = require("terser-webpack-plugin");
module.exports = (env, args) => {
const isDev = args.mode === "development";
// Generate Wideo config
const wideoConfigPath = "./.wideo.json";
if (!fs.existsSync(wideoConfigPath)) {
const wideoConfig = {
ignoreFiles: [
".wideo.json",
"webpack.config.js",
"README.md",
"package.json",
"package-lock.json",
"yarn.lock",
".gitignore",
"src",
"build",
"node_modules",
"bower_components",
".git",
"docs",
".editorconfig",
"docker-compose.yml",
".dockerignore",
"docker",
"cache",
],
};
fs.writeFileSync(
wideoConfigPath,
JSON.stringify(wideoConfig, null, 2),
"utf8"
);
}
// Load config, variables and entries
const wideoConfig = require(wideoConfigPath);
const entry = {};
const plugins = [];
fs.readdirSync("./src/packs").forEach((fileName) => {
const key = path.parse(fileName).name;
entry[key] = entry[key] || [];
entry[key].push(`./src/packs/${fileName}`);
});
// Define custom build plugin
plugins.push({
apply: (compiler) => {
compiler.hooks.beforeCompile.tap("WideoCleanTheme", () => {
fs.removeSync("./build");
});
compiler.hooks.afterEmit.tap("WideoBuildTheme", (compilation) => {
if (!isDev) {
fs.mkdirSync("./build", { recursive: true });
// Copy all files except those in the ignore list
fs.readdirSync("./").forEach((fileName) => {
if (!wideoConfig.ignoreFiles.includes(fileName)) {
fs.copySync(`./${fileName}`, `./build/${fileName}`);
}
});
// Copy assets (minified files)
fs.copySync("./assets", "./build/assets");
}
});
},
});
// Copy webpack plugin for fonts and images
plugins.push(
new CopyWebpackPlugin({
patterns: [
{
from: "src/images",
to: "images",
globOptions: { ignore: ["*.DS_Store"] },
},
{
from: "src/fonts",
to: "fonts",
globOptions: { ignore: ["*.DS_Store"] },
},
],
})
);
// Mini CSS Extract Plugin
plugins.push(new MiniCssExtractPlugin({ filename: "[name].css" }));
// Rules definitions
const rules = [
{
test: /\.s?css$/,
use: [
MiniCssExtractPlugin.loader,
{ loader: "css-loader", options: { sourceMap: isDev, url: false } },
{
loader: "postcss-loader",
options: {
postcssOptions: { plugins: [autoprefixer()] },
sourceMap: isDev,
},
},
{ loader: "sass-loader", options: { sourceMap: isDev } },
],
},
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: "babel-loader",
options: {
presets: [["@babel/preset-env", { modules: false }]],
plugins: [["@babel/plugin-proposal-class-properties"]],
},
},
},
{
test: /\.(woff(2)?|ttf|eot|svg)$/,
type: "asset/resource",
generator: {
filename: "fonts/[name][ext][query]",
},
exclude: /src\/images/,
},
{
test: /\.(svg)$/,
type: "asset/resource",
generator: {
filename: "images/[name][ext][query]",
},
exclude: /src\/fonts/,
},
];
return {
target: "web",
devtool: isDev ? "source-map" : false,
mode: args.mode,
entry: entry,
output: {
filename: "[name].js",
path: path.resolve(__dirname, "assets"),
clean: true,
},
module: { rules: rules },
plugins: plugins,
watch: isDev,
optimization: {
minimize: !isDev,
minimizer: [
new TerserWebpackPlugin({
terserOptions: { compress: { drop_console: !isDev } },
}),
],
},
};
};