-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.cjs
More file actions
129 lines (122 loc) · 4.01 KB
/
webpack.config.cjs
File metadata and controls
129 lines (122 loc) · 4.01 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
const webpack = require('webpack');
const path = require("path");
const TerserPlugin = require("terser-webpack-plugin");
// Remove nodeExternals if not strictly needed, especially for browser builds
// const nodeExternals = require("webpack-node-externals");
const packageJson = require('./package.json'); // Use packageJson consistently
const banner =
`/*!
* ${packageJson.name} - v${packageJson.version} - ${new Date().toISOString().split('T')[0]}
* ${packageJson.repository.url}
* Copyright (c) ${new Date().getFullYear()} ${packageJson.author.name}, Licensed ${packageJson.license}
*/`;
// Common Terser options for minification
const terserOptions = {
extractComments: false, // Do not extract comments to a separate file
terserOptions: {
compress: {
drop_console: true, // Remove console logs in production
},
format: {
comments: /^\/*!/i, // Keep banner comment
},
},
};
// Common Babel Loader options (can be used after ts-loader if needed for older targets)
// const babelLoaderOptions = { ... }
// Base configuration shared across builds
const baseConfig = {
mode: "production",
devtool: 'source-map', // Enable source maps
module: {
rules: [
{
test: /\.ts$/, // Target TypeScript files
exclude: /node_modules/,
use: 'ts-loader', // Use ts-loader
// Optionally chain babel-loader here if needed for older targets:
// use: ['babel-loader', 'ts-loader']
},
// If you still need Babel for JS files (e.g., top-level index.js)
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
// Ensure babel config aligns with targets if used
}
}
]
},
resolve: {
extensions: ['.ts', '.js'], // Resolve both TS and JS files
},
optimization: {
minimize: true,
minimizer: [new TerserPlugin(terserOptions)],
},
plugins: [
new webpack.BannerPlugin({
banner: banner,
raw: true
})
],
};
module.exports = [
// --- CommonJS Build ---
{
...baseConfig,
target: "node",
entry: "./src/index.ts",
output: {
filename: "modulator.cjs",
path: path.resolve(__dirname, "dist"),
libraryTarget: "commonjs2",
clean: true,
},
// externals: [nodeExternals()], // Optional
},
// --- ES Module Build ---
{
...baseConfig,
target: "web",
entry: "./src/index.ts",
experiments: { outputModule: true, },
output: {
filename: "modulator.esm.js",
path: path.resolve(__dirname, "dist"),
library: {
type: 'module', // Output as ES module
},
// No clean: true here, let the first build handle it
},
},
// --- UMD Build ---
{
...baseConfig,
target: "web",
entry: "./src/index.ts",
output: {
filename: "modulator.umd.js",
path: path.resolve(__dirname, "dist"),
library: "Modulator", // Global variable name
libraryTarget: "umd", // UMD format
globalObject: 'this', // Define global context for UMD
umdNamedDefine: true, // Name the AMD module
},
// Adjust Babel targets here if using Babel + TS for older browser support
},
// --- AMD Build (Adding back) ---
{
...baseConfig,
target: "web", // Typically for browser AMD loaders
entry: "./src/index.ts",
output: {
filename: "modulator.amd.js", // Specific AMD filename
path: path.resolve(__dirname, "dist"),
library: "Modulator", // Optional: Define the module name for AMD
libraryTarget: "amd", // Set library target to AMD
},
// Note: No clean: true here
}
];