-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwebpack.config.cjs
110 lines (103 loc) · 2.32 KB
/
webpack.config.cjs
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
'use strict';
const { readFileSync } = require('node:fs');
const { extname, join, posix, relative } = require('node:path');
const ESLintPlugin = require('eslint-webpack-plugin');
const { fdir: Fdir } = require('fdir');
const ignore = require('ignore');
const webpack = require('webpack');
const config = require('./config.cjs');
const isDevelopment = config.get('mode') !== 'production';
const ig = ignore().add(readFileSync('.gitignore', 'utf8'));
const crawler = new Fdir()
.withBasePath()
.filter(
(filePath) =>
!ig.ignores(filePath) &&
['.js', '.mjs', '.cjs'].includes(extname(filePath)),
)
.crawl(posix.join(config.get('srcDir'), 'assets/scripts'));
/**
* @type { import('webpack').Configuration }
*/
module.exports = {
mode: config.get('mode'),
async entry() {
const filePaths = await crawler.withPromise();
return Object.fromEntries(
filePaths.map((filePath) => {
const chunkName = relative(
join(config.get('srcDir'), 'assets/scripts'),
filePath,
).replace(extname(filePath), '');
const entryPoint = `./${filePath}`;
return [chunkName, entryPoint];
}),
);
},
output: {
path: join(
__dirname,
config.get('distDir'),
config.get('publicPath'),
'assets/scripts',
),
},
module: {
rules: [
{
test: /\.(?:js|mjs|cjs)$/,
include: [
join(__dirname, config.get('srcDir'), 'assets/scripts'),
join(__dirname, config.get('srcDir'), 'modules'),
],
use: [
{
loader: 'babel-loader',
options: {
cacheDirectory: true,
},
},
],
},
],
},
resolve: {
alias: {
'~': __dirname,
},
},
devtool: isDevelopment ? 'eval-source-map' : false,
context: __dirname,
plugins: [
new ESLintPlugin({
files: [
join(config.get('srcDir'), 'assets/scripts'),
join(config.get('srcDir'), 'modules'),
],
}),
new webpack.DefinePlugin({
'import.meta.env.MODE': JSON.stringify(config.get('mode')),
'import.meta.env.PUBLIC_PATH': JSON.stringify(config.get('publicPath')),
}),
],
optimization: {
runtimeChunk: 'single',
splitChunks: {
chunks: 'all',
cacheGroups: {
default: false,
defaultVendors: false,
vendors: {
minSize: 0,
name: 'vendors',
test: /[/\\]node_modules[/\\]/i,
},
commons: {
minChunks: 2,
minSize: 0,
name: 'commons',
},
},
},
},
};