-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrslib.config.js
More file actions
90 lines (80 loc) · 2.15 KB
/
rslib.config.js
File metadata and controls
90 lines (80 loc) · 2.15 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
import fs from "node:fs";
import path from "node:path";
import { defineConfig } from "@rslib/core";
class RspackDtsCopyPlugin {
/**
*
* @param {import('@rspack/core').Compiler} compiler
*/
apply(compiler) {
const projectDir = compiler.context;
compiler.hooks.emit.tapPromise("RspackDtsCopyPlugin", async (compilation) => {
const srcDir = path.join(projectDir, "src");
const copyDts = (rootDir) => {
const dtsFiles = fs.readdirSync(rootDir, "utf8").filter((v) => v.endsWith(".d.ts"));
const createSource = (filepath, isModule) => {
let content = fs.readFileSync(filepath, "utf8");
// TODO: 更加完善的处理方法应该是使用 AST 进行处理,而不是简单的正则替换
if (isModule) {
// 替换导入的模块路径为对应的模块类型,把 .js 的模块导入,替换成为 .mts
content = content.replace(/(from\s+['"].+?)\.js(['"])/g, "$1.mts$2");
} else {
// 替换导入的模块路径为对应的模块类型,把 .js 的模块导入,替换成为 .cts
content = content.replace(/(from\s+['"].+?)\.js(['"])/g, "$1.cts$2");
}
return new compiler.webpack.sources.RawSource(content);
};
for (const file of dtsFiles) {
const absolutePath = path.join(rootDir, file);
const relativePath = path.relative(srcDir, absolutePath);
compilation.emitAsset("esm/" + relativePath.replace(/\.d\.ts$/, ".d.mts"), createSource(absolutePath, true));
compilation.emitAsset("cjs/" + relativePath.replace(/\.d\.ts$/, ".d.cts"), createSource(absolutePath, false));
}
};
copyDts(path.join(projectDir, "src"));
});
}
}
export default defineConfig({
source: {
entry: {
index: "src/index.js",
},
},
lib: [
{
format: "esm",
syntax: "es5",
output: {
sourceMap: true,
filename: {
js: "[name].mjs",
},
distPath: {
js: "esm",
},
},
},
{
format: "cjs",
syntax: "es5",
output: {
sourceMap: true,
filename: {
js: "[name].cjs",
},
distPath: {
js: "cjs",
},
},
},
],
output: {
target: "node",
},
tools: {
rspack: {
plugins: [new RspackDtsCopyPlugin()],
},
},
});