-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
webpack.config.ts
71 lines (63 loc) · 2.16 KB
/
webpack.config.ts
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
import * as path from "path";
import ForkTsCheckerWebpackPlugin from "fork-ts-checker-webpack-plugin";
import pkg from "./package.json";
let shouldCheckTypeScript = true;
function checkTypeScript(plugins: Array<unknown>) {
plugins.push(new ForkTsCheckerWebpackPlugin({
typescript: {
build: true,
mode: "write-dts", // output a declaration file as well
},
}));
}
function makeConfig(mode: string, filename: string, module: boolean) {
const config = {
mode: mode,
entry: "./src/heapify.ts",
target: "web", // works for Node.js too as long as globalObject is set to `this` (see below)
experiments: {
outputModule: module,
},
output: {
filename: path.basename(filename),
path: path.resolve(__dirname, "dist"),
/*
* Here, `globalObject` must be set to `this` so the same output can work for both Node.js and the browser.
* See:
* - https://stackoverflow.com/a/64639975/778272
* - https://webpack.js.org/configuration/output/#outputglobalobject
*/
globalObject: "this",
library: {
...!module && {name: "Heapify"},
type: module ? "module" : "umd",
},
},
devtool: mode === "development" && "inline-source-map",
module: {
rules: [
{
test: /\.tsx?$/ui,
loader: "ts-loader",
exclude: ["/node_modules/"],
options: {
transpileOnly: true,
},
},
],
},
plugins: [],
resolve: {
extensions: [".tsx", ".ts", ".js"],
},
};
if (shouldCheckTypeScript) { // although we have multiple configurations, we just need to type-check once
checkTypeScript(config.plugins);
shouldCheckTypeScript = false;
}
return config;
}
export default (_env: unknown, argv: {mode: string}) => [
makeConfig(argv.mode, pkg.main, false),
makeConfig(argv.mode, pkg.module, true),
];