This repository has been archived by the owner on Jun 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
93 lines (85 loc) · 2.37 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
86
87
88
89
90
91
92
93
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require("fs");
const path = require("path");
const pkg = require("./package.json");
const dts = require("dts-bundle");
const removeEmpty = require("remove-empty-directories");
const paths = {
src: "./src",
dist: "./dist",
entry: "./src/index.ts",
};
Object.keys(paths).forEach((key) => (paths[key] = path.join(__dirname, paths[key])));
module.exports = {
entry: paths.entry,
mode: "development",
devtool: "inline-source-map",
target: "node",
node: { fs: "empty" },
stats: {
warningsFilter: /^(?!CriticalDependenciesWarning$)/,
},
output: {
filename: "index.js",
library: {
root: pkg.name,
amd: pkg.name,
commonjs: pkg.name,
},
// libraryExport: ["default"],
libraryTarget: "umd",
globalObject: "this",
},
externals: ["axios"],
devServer: {
contentBase: paths.dist,
compress: true,
port: 9000,
hot: true,
},
resolve: {
enforceExtension: false,
extensions: [".ts", ".tsx", ".js", ".jsx", ".json"],
},
module: {
rules: [
{
test: /\.tsx?$/,
use: "ts-loader",
exclude: /node_modules/,
},
],
},
plugins: [new DtsBundlePlugin(), new RemoveEmptyDirsPlugin(), new DtsRemoveDynamicImportsPlugin()],
};
function DtsBundlePlugin() {}
DtsBundlePlugin.prototype.apply = function (compiler) {
compiler.plugin("done", function () {
dts.bundle({
name: pkg.name,
main: pkg.types,
out: "../" + pkg.types,
indent: " ",
newline: "\n",
removeSource: true,
outputAsModuleFolder: true, // to use npm in-package typings
});
});
};
function RemoveEmptyDirsPlugin() {}
RemoveEmptyDirsPlugin.prototype.apply = function (compiler) {
compiler.plugin("done", function () {
removeEmpty(paths.dist);
});
};
function DtsRemoveDynamicImportsPlugin() {}
DtsRemoveDynamicImportsPlugin.prototype.apply = function (compiler) {
compiler.plugin("done", function () {
const regexDynamicImport = /import\("[^"]+"\)\./g;
const regexUnexportedConst = /([\n\r]+)(const [^\s:]+:)/g;
const dtsSource = fs.readFileSync(pkg.types).toString("utf8");
const stripped = dtsSource.replace(regexDynamicImport, "");
const replaced = stripped.replace(regexUnexportedConst, (all, lb, val) => `${lb}export ${val}`);
fs.writeFileSync(pkg.types, replaced);
});
};