-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.cjs
147 lines (137 loc) · 5.01 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
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
const path = require('path');
const workingDir = process.cwd();
const TerserPlugin = require('terser-webpack-plugin');
const { targets, webpackFormats } = require(`${workingDir}/source-files.cjs`);
const { getFlagValue } = require('./NodeHelpers.cjs');
const mode = getFlagValue('mode') ?? 'production';
const formats = webpackFormats ?? [
{ type: 'umd', dir: 'umd', ext: 'js' },
{ type: 'commonjs2', dir: 'cjs', ext: 'cjs' },
{ type: 'window', dir: 'browser', ext: 'js' },
];
/**
* Generates a Webpack configuration for a given library name and target.
*
* @param {string} fileSrc - The file source.
* @param {string} target - The target name.
* @param {string} dir - The directory name.
* @param {string} libraryTarget - The library target format.
* @param {string} ext - The file extension.
* @returns {Object} - The Webpack configuration.
*/
const getWebpackConfig = (config) => ({
mode: mode,
entry: `${workingDir}/src/${config.fileSrc}`,
resolve: {
modules: [
'node_modules',
path.resolve(__dirname, 'src'),
path.resolve(__dirname, `${workingDir}/src`),
path.resolve(__dirname, `${workingDir}/node_modules`),
],
extensions: ['.mjs', '.js', '.json', '.cjs'],
},
output: {
path: path.resolve(__dirname, `${workingDir}/dist/${config.dir}`),
filename: `${config.exportName}.${config.ext}`,
// library: config.exportName,
// libraryTarget: config.libraryTarget,
umdNamedDefine: true,
// libraryExport: config.windowExport,
globalObject: 'this',
library: {
name: config.exportName,
type: config.libraryTarget,
export: config.windowExport,
umdNamedDefine: true,
},
},
optimization: {
minimize: mode === 'production' ? true : false,
minimizer: [
new TerserPlugin({
parallel: true,
// exclude: [/cb\.js$/, /extra\.js$/],
terserOptions: {
// https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
ecma: 6,
// keep_classnames: true, // Preserve class names
// keep_fnames: true, // Preserve function names
// safari10: true, // Workaround for Safari 10/11 loop scoping and await bugs
toplevel: true,
output: {
ascii_only: true, // Escape Unicode characters
braces: true, // Always insert braces in if, for, do, while or with statements
semicolons: true, // Separate statements with semicolons
comments: false,
},
compress: {
arrows: true, // Enable arrow function compression (transform to function expressions)
},
parse: {
html5_comments: false, // Ignore HTML5 comments
shebang: true, // Preserve shebang
},
mangle: true,
},
extractComments: false, // Do not extract comments to separate file
}),
],
},
stats: mode === 'production' ? 'normal' : 'minimal',
module: {
rules: [
{
test: /\.js$/,
use: {
loader: 'babel-loader',
options: {
presets: [
[
'@babel/preset-env',
{
modules: false,
},
],
],
},
},
exclude: /node_modules/,
},
],
},
});
/**
* Extracts the file name without extension from a path if it ends with '.js'.
* @param {string} path - The file path.
* @returns {string|null} The file name without extension or null if not a '.js' file.
*/
function extractFileName(path) {
const match = /([^\/]+)\.js$/.exec(path);
return match ? match[1] : path;
}
// Generate multiple configurations
function getAllConfigs() {
const configs = [];
targets.forEach((target) => {
const fileName = extractFileName(target.file);
// Generate multiple configurations
for (const format of formats) {
const exportName = target.exportName ?? fileName;
configs.push(
getWebpackConfig({
fileSrc: target.file,
fileName,
libraryTarget: format.type,
dir: format.dir,
ext: format.ext,
exportName: exportName,
windowExport: target.windowExport ?? undefined,
})
);
}
});
return configs;
}
const configs = getAllConfigs();
module.exports = configs;