This repository was archived by the owner on Jan 18, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.js
More file actions
194 lines (182 loc) · 4.53 KB
/
config.js
File metadata and controls
194 lines (182 loc) · 4.53 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { join, resolve, dirname, relative } from 'path';
import fs from 'fs';
import CopyPlugin from "copy-webpack-plugin";
import ExtraWatchWebpackPlugin from 'extra-watch-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import TerserPlugin from 'terser-webpack-plugin';
import { fileURLToPath } from 'url';
import {
getComponents,
getComponentsFromNpmStart
} from './utils.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
const args = getComponentsFromNpmStart();
export default function (config) {
const DIST_DIR = config.dist || 'dist';
const src = config.src || 'src';
const components = getComponents(src);
const inputs = [];
components.forEach(({ input, examples }) => {
inputs.push(resolve('./', input));
examples.forEach(({ exampleInput }) => {
inputs.push(resolve('./', exampleInput));
});
});
const entries = [];
const mode = config.mode || 'development';
if (config.before) {
config.before(components, args, mode);
}
function addEntries(input, name) {
entries.push({
mode: mode,
entry: input,
module: {
rules: [
{
test: /\.css$/i,
use: [{
loader: "css-loader",
options: {
exportType: "css-style-sheet",
},
}],
},
{
test: /\.html$/i,
use: [{
loader: 'html-loader',
options: {
minimize: true,
sources: {
urlFilter: (attribute, value, resourcePath) => {
if (/favicon.svg$/.test(value)) {
return false;
}
return true;
},
},
},
}],
},
{
test: /\.ts$/,
use: [{
loader: 'ts-loader',
options: {
allowTsInNodeModules: true,
}
}],
},
],
},
resolve: {
extensions: ['.ts', '.js'],
},
output: {
filename: `${name}.js`,
path: resolve('./', DIST_DIR),
},
performance: {
hints: false
},
stats: {
preset: 'errors-warnings',
},
infrastructureLogging: {
level: 'warn',
},
optimization: {
minimize: true,
minimizer: [new TerserPlugin({
terserOptions: {
keep_classnames: true
}
})],
},
});
return entries[entries.length - 1];
}
// Individual *.js for each component (very slow)
if (mode === 'production') {
components.forEach(({ input, name }) => {
addEntries(`./${input}`, name);
});
}
// main.js Entry
const mainEntry = addEntries(inputs, 'main');
// Output Basic Runtime
mainEntry.plugins = [];
if (config.copy && config.copy.length > 0) {
mainEntry.plugins.push(
new CopyPlugin({
patterns: config.copy
})
);
};
const favicon = config.favicon || 'favicon.svg';
const favFile = fs.existsSync(join(src, favicon))
? join(src, favicon)
: join(__dirname, 'default', 'favicon.svg');
mainEntry.plugins.push(
new CopyPlugin({
patterns: [
{
from: favFile,
to: `favicon.svg`
}
]
})
);
mainEntry.plugins.push(
{
apply: (compiler) => {
compiler.hooks.afterEmit.tap('AfterEmitPlugin', (compilation) => {
if (config.after) {
config.after(components, args, mode);
}
});
compiler.hooks.watchRun.tap('WatchRunPlugin', (module) => {
if (module.modifiedFiles.size) {
if (config.update) {
config.update(Array.from(module.modifiedFiles).map((toPath) => {
return relative('./', toPath);
}));
}
}
});
}
}
);
mainEntry.plugins.push(
new ExtraWatchWebpackPlugin({
files: config.watch || []
}),
);
if (config.index) {
mainEntry.plugins.push(
new HtmlWebpackPlugin({
templateContent: () => {
return config.index(components, args, mode);
},
})
);
} else {
mainEntry.plugins.push(
new HtmlWebpackPlugin({
template: `./${src}/index.html`,
})
);
}
mainEntry.devServer = {
static: {
directory: join('./', DIST_DIR)
},
compress: true,
port: config.port || 3000,
client: {
logging: 'warn',
},
};
return entries;
};