-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.js
85 lines (78 loc) · 1.88 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
/* eslint-disable no-empty-function */
const path = require("path");
const fs = require("fs");
const TerserPlugin = require("terser-webpack-plugin");
const DtsBundler = require("dts-bundle-generator");
const targetIsNode = process.env.WEBPACK_TARGET === 'node';
// -----------------------------------------------------------------------------
module.exports = {
mode: "production",
target: targetIsNode ? "node" : "web",
entry: "./src/index.ts",
output: {
filename: targetIsNode ? "index.js" : "json-bigint.min.js",
path: path.resolve(__dirname, targetIsNode ? "build" : "dist"),
library: (targetIsNode ? {
type: "commonjs2",
export: 'default'
} : {
name: "JSONBigInt",
type: "umd",
export: 'default'
}),
globalObject: "this"
},
module: {
rules: [
{
test: /\.ts$/u,
include: path.resolve(__dirname, "src"),
use: [
{
loader: 'ts-loader'
}
]
}
]
},
resolve: {
extensions: [ '.ts', '.js' ]
},
devtool: 'source-map',
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
extractComments: true,
terserOptions: {
ecma: 2020
}
})
]
},
plugins: [
new DtsBundlerPlugin()
]
};
// -----------------------------------------------------------------------------
function DtsBundlerPlugin() {}
DtsBundlerPlugin.prototype.apply = function(compiler) {
compiler.hooks.afterEmit.tap(
"DtsBundlerPlugin",
(compilation) => {
if (compilation.options.entry.main.import.length > 0) {
const output = DtsBundler.generateDtsBundle([
{
filePath: compilation.options.entry.main.import[0],
output: {
umdModuleName: "JSONBigInt",
noBanner: true
}
}
], {});
const filename = "." + path.sep + (targetIsNode ? "index.d.ts" : "json-bigint.min.d.ts");
fs.writeFileSync(path.resolve(compilation.options.output.path + path.sep, filename), output[0]);
}
}
);
};