-
Notifications
You must be signed in to change notification settings - Fork 10
/
webpack.config.js
106 lines (100 loc) · 2.83 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
/** @format */
import path from 'path';
import { glob } from 'glob';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import CssMinimizerPlugin from 'css-minimizer-webpack-plugin';
import WebpackObfuscatorPlugin from 'webpack-obfuscator';
import RemoveEmptyScriptsPlugin from 'webpack-remove-empty-scripts';
const outputDir = path.resolve(process.cwd(), 'dist');
const extensionsFilenames = {
js: 'scripts',
scss: 'styles',
less: 'styles',
css: 'styles',
html: 'markup',
};
const getEntries = (extension, isProduction) => {
const entries = {};
const folders = ['footer', 'header', 'index'];
folders.forEach(folder => {
const files = glob.sync(`./src/${folder}/**/*.${extension}`);
if (files.length > 0) {
const foundExtension = files[0].split('.').pop();
const filename = extensionsFilenames[foundExtension];
const minExtension = isProduction ? '.min' : '';
entries[`${filename}.${folder}${minExtension}`] = files.map(str => './' + str);
}
});
return entries;
};
const getGlobalAssetsEntry = () => {
const entries = {};
const files = glob.sync('./assets/**/*');
if (files.length > 0) {
entries['assets'] = files.map(str => './' + str);
}
return entries;
};
export default env => {
const isProduction = env.production === true;
return {
mode: isProduction ? 'production' : 'development',
devtool: isProduction ? false : 'eval',
entry: {
...getEntries('js', isProduction),
...getEntries('{scss,less}', isProduction),
...getEntries('css', isProduction),
// TODO: add html entries
// TODO: add copy assets entries
// TODO: add TS entries
...getGlobalAssetsEntry(),
},
output: {
path: outputDir,
clean: true,
},
plugins: [new MiniCssExtractPlugin(), new RemoveEmptyScriptsPlugin()],
...(isProduction && {
optimization: {
minimize: true,
minimizer: [
new WebpackObfuscatorPlugin({
rotateStringArray: true,
}),
new CssMinimizerPlugin(),
],
},
}),
module: {
rules: [
{
test: /\.js$/,
use: ['babel-loader'],
},
{
test: /\.less$/i,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'less-loader'],
},
{
test: /\.scss$/i,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
{ loader: 'sass-loader', options: { sassOptions: { outputStyle: 'expanded' } } },
],
},
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader'],
},
{
test: /\.(png|jpe?g|gif|svg|woff2?|ttf|eot)$/,
type: 'asset/resource',
generator: {
filename: 'assets/[name][ext]',
},
},
],
},
};
};