rsbuild-plugin-dts 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +19 -0
- package/dist/apiExtractor.cjs +94 -0
- package/dist/apiExtractor.d.ts +11 -0
- package/dist/apiExtractor.js +69 -0
- package/dist/dts.cjs +205 -0
- package/dist/dts.d.ts +7 -0
- package/dist/dts.js +180 -0
- package/dist/index.cjs +111 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +92 -0
- package/dist/tsc.cjs +134 -0
- package/dist/tsc.d.ts +8 -0
- package/dist/tsc.js +111 -0
- package/dist/utils.cjs +171 -0
- package/dist/utils.d.ts +13 -0
- package/dist/utils.js +134 -0
- package/package.json +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-present Bytedance, Inc. and its affiliates.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
<picture>
|
|
2
|
+
<img alt="Rslib Banner" src="https://assets.rspack.dev/rslib/rslib-banner.png">
|
|
3
|
+
</picture>
|
|
4
|
+
|
|
5
|
+
# Rslib
|
|
6
|
+
|
|
7
|
+
Rslib is a library build tool powered by [Rsbuild](https://rsbuild.dev). It allows library developers to leverage the knowledge and ecosystem of webpack and Rspack.
|
|
8
|
+
|
|
9
|
+
<!-- ## Documentation
|
|
10
|
+
|
|
11
|
+
https://rslib.dev/ -->
|
|
12
|
+
|
|
13
|
+
## Contributing
|
|
14
|
+
|
|
15
|
+
Please read the [Contributing Guide](https://github.com/web-infra-dev/rslib/blob/main/CONTRIBUTING.md).
|
|
16
|
+
|
|
17
|
+
## License
|
|
18
|
+
|
|
19
|
+
Rslib is [MIT licensed](https://github.com/web-infra-dev/rslib/blob/main/LICENSE).
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var apiExtractor_exports = {};
|
|
30
|
+
__export(apiExtractor_exports, {
|
|
31
|
+
bundleDts: () => bundleDts
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(apiExtractor_exports);
|
|
34
|
+
var import_node_path = require("node:path");
|
|
35
|
+
var import_api_extractor = require("@microsoft/api-extractor");
|
|
36
|
+
var import_core = require("@rsbuild/core");
|
|
37
|
+
var import_picocolors = __toESM(require("picocolors"));
|
|
38
|
+
var import_utils = require("./utils.cjs");
|
|
39
|
+
async function bundleDts(options) {
|
|
40
|
+
const {
|
|
41
|
+
name,
|
|
42
|
+
cwd,
|
|
43
|
+
outDir,
|
|
44
|
+
dtsExtension,
|
|
45
|
+
dtsEntry = {
|
|
46
|
+
name: "index",
|
|
47
|
+
path: "index.d.ts"
|
|
48
|
+
},
|
|
49
|
+
tsconfigPath = "tsconfig.json",
|
|
50
|
+
bundledPackages = []
|
|
51
|
+
} = options;
|
|
52
|
+
try {
|
|
53
|
+
const start = Date.now();
|
|
54
|
+
const untrimmedFilePath = (0, import_node_path.join)(
|
|
55
|
+
cwd,
|
|
56
|
+
(0, import_node_path.relative)(cwd, outDir),
|
|
57
|
+
`${dtsEntry.name}${dtsExtension}`
|
|
58
|
+
);
|
|
59
|
+
const mainEntryPointFilePath = dtsEntry.path;
|
|
60
|
+
const internalConfig = {
|
|
61
|
+
mainEntryPointFilePath,
|
|
62
|
+
bundledPackages,
|
|
63
|
+
dtsRollup: {
|
|
64
|
+
enabled: true,
|
|
65
|
+
untrimmedFilePath
|
|
66
|
+
},
|
|
67
|
+
compiler: {
|
|
68
|
+
tsconfigFilePath: tsconfigPath.includes(cwd) ? tsconfigPath : (0, import_node_path.join)(cwd, tsconfigPath)
|
|
69
|
+
},
|
|
70
|
+
projectFolder: cwd
|
|
71
|
+
};
|
|
72
|
+
const extractorConfig = import_api_extractor.ExtractorConfig.prepare({
|
|
73
|
+
configObject: internalConfig,
|
|
74
|
+
configObjectFullPath: void 0,
|
|
75
|
+
packageJsonFullPath: (0, import_node_path.join)(cwd, "package.json")
|
|
76
|
+
});
|
|
77
|
+
const extractorResult = import_api_extractor.Extractor.invoke(extractorConfig, {
|
|
78
|
+
localBuild: true
|
|
79
|
+
});
|
|
80
|
+
if (!extractorResult.succeeded) {
|
|
81
|
+
throw new Error(`API Extractor error. ${import_picocolors.default.gray(`(${name})`)}`);
|
|
82
|
+
}
|
|
83
|
+
import_core.logger.info(
|
|
84
|
+
`API Extractor bundle DTS succeeded: ${import_picocolors.default.cyan(untrimmedFilePath)} in ${(0, import_utils.getTimeCost)(start)} ${import_picocolors.default.gray(`(${name})`)}`
|
|
85
|
+
);
|
|
86
|
+
} catch (e) {
|
|
87
|
+
import_core.logger.error("API Extractor Error");
|
|
88
|
+
throw new Error(`${e}`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
92
|
+
0 && (module.exports = {
|
|
93
|
+
bundleDts
|
|
94
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { DtsEntry } from './';
|
|
2
|
+
export type BundleOptions = {
|
|
3
|
+
name: string;
|
|
4
|
+
cwd: string;
|
|
5
|
+
outDir: string;
|
|
6
|
+
dtsExtension: string;
|
|
7
|
+
dtsEntry: DtsEntry;
|
|
8
|
+
tsconfigPath?: string;
|
|
9
|
+
bundledPackages?: string[];
|
|
10
|
+
};
|
|
11
|
+
export declare function bundleDts(options: BundleOptions): Promise<void>;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { fileURLToPath } from "url";
|
|
2
|
+
import path from "path";
|
|
3
|
+
var getFilename = () => fileURLToPath(import.meta.url);
|
|
4
|
+
var getDirname = () => path.dirname(getFilename());
|
|
5
|
+
var __dirname = /* @__PURE__ */ getDirname();
|
|
6
|
+
var __filename = /* @__PURE__ */ getFilename();
|
|
7
|
+
import { join, relative } from "node:path";
|
|
8
|
+
import {
|
|
9
|
+
Extractor,
|
|
10
|
+
ExtractorConfig
|
|
11
|
+
} from "@microsoft/api-extractor";
|
|
12
|
+
import { logger } from "@rsbuild/core";
|
|
13
|
+
import color from "picocolors";
|
|
14
|
+
import { getTimeCost } from "./utils.js";
|
|
15
|
+
async function bundleDts(options) {
|
|
16
|
+
const {
|
|
17
|
+
name,
|
|
18
|
+
cwd,
|
|
19
|
+
outDir,
|
|
20
|
+
dtsExtension,
|
|
21
|
+
dtsEntry = {
|
|
22
|
+
name: "index",
|
|
23
|
+
path: "index.d.ts"
|
|
24
|
+
},
|
|
25
|
+
tsconfigPath = "tsconfig.json",
|
|
26
|
+
bundledPackages = []
|
|
27
|
+
} = options;
|
|
28
|
+
try {
|
|
29
|
+
const start = Date.now();
|
|
30
|
+
const untrimmedFilePath = join(
|
|
31
|
+
cwd,
|
|
32
|
+
relative(cwd, outDir),
|
|
33
|
+
`${dtsEntry.name}${dtsExtension}`
|
|
34
|
+
);
|
|
35
|
+
const mainEntryPointFilePath = dtsEntry.path;
|
|
36
|
+
const internalConfig = {
|
|
37
|
+
mainEntryPointFilePath,
|
|
38
|
+
bundledPackages,
|
|
39
|
+
dtsRollup: {
|
|
40
|
+
enabled: true,
|
|
41
|
+
untrimmedFilePath
|
|
42
|
+
},
|
|
43
|
+
compiler: {
|
|
44
|
+
tsconfigFilePath: tsconfigPath.includes(cwd) ? tsconfigPath : join(cwd, tsconfigPath)
|
|
45
|
+
},
|
|
46
|
+
projectFolder: cwd
|
|
47
|
+
};
|
|
48
|
+
const extractorConfig = ExtractorConfig.prepare({
|
|
49
|
+
configObject: internalConfig,
|
|
50
|
+
configObjectFullPath: void 0,
|
|
51
|
+
packageJsonFullPath: join(cwd, "package.json")
|
|
52
|
+
});
|
|
53
|
+
const extractorResult = Extractor.invoke(extractorConfig, {
|
|
54
|
+
localBuild: true
|
|
55
|
+
});
|
|
56
|
+
if (!extractorResult.succeeded) {
|
|
57
|
+
throw new Error(`API Extractor error. ${color.gray(`(${name})`)}`);
|
|
58
|
+
}
|
|
59
|
+
logger.info(
|
|
60
|
+
`API Extractor bundle DTS succeeded: ${color.cyan(untrimmedFilePath)} in ${getTimeCost(start)} ${color.gray(`(${name})`)}`
|
|
61
|
+
);
|
|
62
|
+
} catch (e) {
|
|
63
|
+
logger.error("API Extractor Error");
|
|
64
|
+
throw new Error(`${e}`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
export {
|
|
68
|
+
bundleDts
|
|
69
|
+
};
|
package/dist/dts.cjs
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var dts_exports = {};
|
|
30
|
+
__export(dts_exports, {
|
|
31
|
+
calcBundledPackages: () => calcBundledPackages,
|
|
32
|
+
generateDts: () => generateDts
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(dts_exports);
|
|
35
|
+
var import_node_fs = __toESM(require("node:fs"));
|
|
36
|
+
var import_node_path = require("node:path");
|
|
37
|
+
var import_core = require("@rsbuild/core");
|
|
38
|
+
var import_picocolors = __toESM(require("picocolors"));
|
|
39
|
+
var ts = __toESM(require("typescript"));
|
|
40
|
+
var import_tsc = require("./tsc.cjs");
|
|
41
|
+
var import_utils = require("./utils.cjs");
|
|
42
|
+
const isObject = (obj) => Object.prototype.toString.call(obj) === "[object Object]";
|
|
43
|
+
const calcBundledPackages = (options) => {
|
|
44
|
+
const { autoExternal, cwd, userExternals } = options;
|
|
45
|
+
let pkgJson;
|
|
46
|
+
try {
|
|
47
|
+
const content = import_node_fs.default.readFileSync((0, import_node_path.join)(cwd, "package.json"), "utf-8");
|
|
48
|
+
pkgJson = JSON.parse(content);
|
|
49
|
+
} catch (err) {
|
|
50
|
+
import_core.logger.warn(
|
|
51
|
+
"The type of third-party packages will not be bundled due to read package.json failed"
|
|
52
|
+
);
|
|
53
|
+
return [];
|
|
54
|
+
}
|
|
55
|
+
const externalOptions = autoExternal ? {
|
|
56
|
+
dependencies: true,
|
|
57
|
+
peerDependencies: true,
|
|
58
|
+
devDependencies: false,
|
|
59
|
+
...autoExternal === true ? {} : autoExternal
|
|
60
|
+
} : {
|
|
61
|
+
dependencies: false,
|
|
62
|
+
peerDependencies: false,
|
|
63
|
+
devDependencies: false
|
|
64
|
+
};
|
|
65
|
+
const getUserExternalsKeys = (value) => {
|
|
66
|
+
if (!value) {
|
|
67
|
+
return [];
|
|
68
|
+
}
|
|
69
|
+
if (typeof value === "string" || value instanceof RegExp) {
|
|
70
|
+
return [value];
|
|
71
|
+
}
|
|
72
|
+
if (Array.isArray(value)) {
|
|
73
|
+
return value.flatMap((v) => getUserExternalsKeys(v));
|
|
74
|
+
}
|
|
75
|
+
if (isObject(userExternals)) {
|
|
76
|
+
return Object.keys(userExternals);
|
|
77
|
+
}
|
|
78
|
+
return [];
|
|
79
|
+
};
|
|
80
|
+
const externals = getUserExternalsKeys(userExternals);
|
|
81
|
+
const allDeps = [];
|
|
82
|
+
for (const type of [
|
|
83
|
+
"dependencies",
|
|
84
|
+
"peerDependencies",
|
|
85
|
+
"devDependencies"
|
|
86
|
+
]) {
|
|
87
|
+
const deps = pkgJson[type] && Object.keys(pkgJson[type]);
|
|
88
|
+
if (deps) {
|
|
89
|
+
if (externalOptions[type]) {
|
|
90
|
+
externals.push(...deps);
|
|
91
|
+
}
|
|
92
|
+
allDeps.push(...deps);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const bundledPackages = allDeps.filter(
|
|
96
|
+
(d) => !externals.some((e) => typeof e === "string" ? d === e : e.test(d))
|
|
97
|
+
);
|
|
98
|
+
return Array.from(new Set(bundledPackages));
|
|
99
|
+
};
|
|
100
|
+
async function generateDts(data) {
|
|
101
|
+
const {
|
|
102
|
+
bundle,
|
|
103
|
+
distPath,
|
|
104
|
+
dtsEntry,
|
|
105
|
+
tsconfigPath,
|
|
106
|
+
name,
|
|
107
|
+
cwd,
|
|
108
|
+
isWatch,
|
|
109
|
+
dtsExtension = ".d.ts",
|
|
110
|
+
autoExternal = true,
|
|
111
|
+
userExternals
|
|
112
|
+
} = data;
|
|
113
|
+
import_core.logger.start(`Generating DTS... ${import_picocolors.default.gray(`(${name})`)}`);
|
|
114
|
+
const configPath = ts.findConfigFile(cwd, ts.sys.fileExists, tsconfigPath);
|
|
115
|
+
if (!configPath) {
|
|
116
|
+
import_core.logger.error(`tsconfig.json not found in ${cwd}`);
|
|
117
|
+
throw new Error();
|
|
118
|
+
}
|
|
119
|
+
const { options: rawCompilerOptions, fileNames } = (0, import_utils.loadTsconfig)(configPath);
|
|
120
|
+
const rootDir = rawCompilerOptions.rootDir ?? await (0, import_utils.calcLongestCommonPath)(fileNames) ?? (0, import_node_path.dirname)(configPath);
|
|
121
|
+
const outDir = distPath ? distPath : rawCompilerOptions.declarationDir || "./dist";
|
|
122
|
+
const getDeclarationDir = (bundle2, distPath2) => {
|
|
123
|
+
if (bundle2) {
|
|
124
|
+
return (0, import_utils.ensureTempDeclarationDir)(cwd);
|
|
125
|
+
}
|
|
126
|
+
return distPath2 ? distPath2 : rawCompilerOptions.declarationDir ?? "./dist";
|
|
127
|
+
};
|
|
128
|
+
const declarationDir = getDeclarationDir(bundle, distPath);
|
|
129
|
+
const { name: entryName, path: entryPath } = dtsEntry;
|
|
130
|
+
let entry = "";
|
|
131
|
+
if (bundle === true && entryPath) {
|
|
132
|
+
const entrySourcePath = (0, import_node_path.join)(cwd, entryPath);
|
|
133
|
+
const relativePath = (0, import_node_path.relative)(rootDir, (0, import_node_path.dirname)(entrySourcePath));
|
|
134
|
+
entry = (0, import_node_path.join)(
|
|
135
|
+
declarationDir,
|
|
136
|
+
relativePath,
|
|
137
|
+
(0, import_node_path.basename)(entrySourcePath)
|
|
138
|
+
).replace(
|
|
139
|
+
/\.(js|mjs|jsx|ts|mts|tsx|cjs|cts|cjsx|ctsx|mjsx|mtsx)$/,
|
|
140
|
+
".d.ts"
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
const bundleDtsIfNeeded = async () => {
|
|
144
|
+
if (bundle === true) {
|
|
145
|
+
const { bundleDts } = await Promise.resolve().then(() => __toESM(require("./apiExtractor.cjs")));
|
|
146
|
+
await bundleDts({
|
|
147
|
+
name,
|
|
148
|
+
cwd,
|
|
149
|
+
outDir,
|
|
150
|
+
dtsEntry: {
|
|
151
|
+
name: entryName,
|
|
152
|
+
path: entry
|
|
153
|
+
},
|
|
154
|
+
tsconfigPath,
|
|
155
|
+
dtsExtension,
|
|
156
|
+
bundledPackages: calcBundledPackages({
|
|
157
|
+
autoExternal,
|
|
158
|
+
cwd,
|
|
159
|
+
userExternals
|
|
160
|
+
})
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
const onComplete = async (isSuccess) => {
|
|
165
|
+
if (isSuccess) {
|
|
166
|
+
await bundleDtsIfNeeded();
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
await (0, import_tsc.emitDts)(
|
|
170
|
+
{
|
|
171
|
+
name,
|
|
172
|
+
cwd,
|
|
173
|
+
configPath,
|
|
174
|
+
declarationDir,
|
|
175
|
+
dtsExtension
|
|
176
|
+
},
|
|
177
|
+
onComplete,
|
|
178
|
+
bundle,
|
|
179
|
+
isWatch
|
|
180
|
+
);
|
|
181
|
+
if (!isWatch) {
|
|
182
|
+
await bundleDtsIfNeeded();
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
process.on("message", async (data) => {
|
|
186
|
+
if (!data.cwd) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
try {
|
|
190
|
+
await generateDts(data);
|
|
191
|
+
} catch (e) {
|
|
192
|
+
import_core.logger.error(e);
|
|
193
|
+
process.send("error");
|
|
194
|
+
process.exit(1);
|
|
195
|
+
}
|
|
196
|
+
process.send("success");
|
|
197
|
+
if (!data.isWatch) {
|
|
198
|
+
process.exit();
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
202
|
+
0 && (module.exports = {
|
|
203
|
+
calcBundledPackages,
|
|
204
|
+
generateDts
|
|
205
|
+
});
|
package/dist/dts.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { DtsGenOptions } from './';
|
|
2
|
+
export declare const calcBundledPackages: (options: {
|
|
3
|
+
autoExternal: DtsGenOptions["autoExternal"];
|
|
4
|
+
cwd: string;
|
|
5
|
+
userExternals?: DtsGenOptions["userExternals"];
|
|
6
|
+
}) => string[];
|
|
7
|
+
export declare function generateDts(data: DtsGenOptions): Promise<void>;
|
package/dist/dts.js
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { fileURLToPath } from "url";
|
|
2
|
+
import path from "path";
|
|
3
|
+
var getFilename = () => fileURLToPath(import.meta.url);
|
|
4
|
+
var getDirname = () => path.dirname(getFilename());
|
|
5
|
+
var __dirname = /* @__PURE__ */ getDirname();
|
|
6
|
+
var __filename = /* @__PURE__ */ getFilename();
|
|
7
|
+
import fs from "node:fs";
|
|
8
|
+
import { basename, dirname, join, relative } from "node:path";
|
|
9
|
+
import { logger } from "@rsbuild/core";
|
|
10
|
+
import color from "picocolors";
|
|
11
|
+
import * as ts from "typescript";
|
|
12
|
+
import { emitDts } from "./tsc.js";
|
|
13
|
+
import {
|
|
14
|
+
calcLongestCommonPath,
|
|
15
|
+
ensureTempDeclarationDir,
|
|
16
|
+
loadTsconfig
|
|
17
|
+
} from "./utils.js";
|
|
18
|
+
const isObject = (obj) => Object.prototype.toString.call(obj) === "[object Object]";
|
|
19
|
+
const calcBundledPackages = (options) => {
|
|
20
|
+
const { autoExternal, cwd, userExternals } = options;
|
|
21
|
+
let pkgJson;
|
|
22
|
+
try {
|
|
23
|
+
const content = fs.readFileSync(join(cwd, "package.json"), "utf-8");
|
|
24
|
+
pkgJson = JSON.parse(content);
|
|
25
|
+
} catch (err) {
|
|
26
|
+
logger.warn(
|
|
27
|
+
"The type of third-party packages will not be bundled due to read package.json failed"
|
|
28
|
+
);
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
31
|
+
const externalOptions = autoExternal ? {
|
|
32
|
+
dependencies: true,
|
|
33
|
+
peerDependencies: true,
|
|
34
|
+
devDependencies: false,
|
|
35
|
+
...autoExternal === true ? {} : autoExternal
|
|
36
|
+
} : {
|
|
37
|
+
dependencies: false,
|
|
38
|
+
peerDependencies: false,
|
|
39
|
+
devDependencies: false
|
|
40
|
+
};
|
|
41
|
+
const getUserExternalsKeys = (value) => {
|
|
42
|
+
if (!value) {
|
|
43
|
+
return [];
|
|
44
|
+
}
|
|
45
|
+
if (typeof value === "string" || value instanceof RegExp) {
|
|
46
|
+
return [value];
|
|
47
|
+
}
|
|
48
|
+
if (Array.isArray(value)) {
|
|
49
|
+
return value.flatMap((v) => getUserExternalsKeys(v));
|
|
50
|
+
}
|
|
51
|
+
if (isObject(userExternals)) {
|
|
52
|
+
return Object.keys(userExternals);
|
|
53
|
+
}
|
|
54
|
+
return [];
|
|
55
|
+
};
|
|
56
|
+
const externals = getUserExternalsKeys(userExternals);
|
|
57
|
+
const allDeps = [];
|
|
58
|
+
for (const type of [
|
|
59
|
+
"dependencies",
|
|
60
|
+
"peerDependencies",
|
|
61
|
+
"devDependencies"
|
|
62
|
+
]) {
|
|
63
|
+
const deps = pkgJson[type] && Object.keys(pkgJson[type]);
|
|
64
|
+
if (deps) {
|
|
65
|
+
if (externalOptions[type]) {
|
|
66
|
+
externals.push(...deps);
|
|
67
|
+
}
|
|
68
|
+
allDeps.push(...deps);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const bundledPackages = allDeps.filter(
|
|
72
|
+
(d) => !externals.some((e) => typeof e === "string" ? d === e : e.test(d))
|
|
73
|
+
);
|
|
74
|
+
return Array.from(new Set(bundledPackages));
|
|
75
|
+
};
|
|
76
|
+
async function generateDts(data) {
|
|
77
|
+
const {
|
|
78
|
+
bundle,
|
|
79
|
+
distPath,
|
|
80
|
+
dtsEntry,
|
|
81
|
+
tsconfigPath,
|
|
82
|
+
name,
|
|
83
|
+
cwd,
|
|
84
|
+
isWatch,
|
|
85
|
+
dtsExtension = ".d.ts",
|
|
86
|
+
autoExternal = true,
|
|
87
|
+
userExternals
|
|
88
|
+
} = data;
|
|
89
|
+
logger.start(`Generating DTS... ${color.gray(`(${name})`)}`);
|
|
90
|
+
const configPath = ts.findConfigFile(cwd, ts.sys.fileExists, tsconfigPath);
|
|
91
|
+
if (!configPath) {
|
|
92
|
+
logger.error(`tsconfig.json not found in ${cwd}`);
|
|
93
|
+
throw new Error();
|
|
94
|
+
}
|
|
95
|
+
const { options: rawCompilerOptions, fileNames } = loadTsconfig(configPath);
|
|
96
|
+
const rootDir = rawCompilerOptions.rootDir ?? await calcLongestCommonPath(fileNames) ?? dirname(configPath);
|
|
97
|
+
const outDir = distPath ? distPath : rawCompilerOptions.declarationDir || "./dist";
|
|
98
|
+
const getDeclarationDir = (bundle2, distPath2) => {
|
|
99
|
+
if (bundle2) {
|
|
100
|
+
return ensureTempDeclarationDir(cwd);
|
|
101
|
+
}
|
|
102
|
+
return distPath2 ? distPath2 : rawCompilerOptions.declarationDir ?? "./dist";
|
|
103
|
+
};
|
|
104
|
+
const declarationDir = getDeclarationDir(bundle, distPath);
|
|
105
|
+
const { name: entryName, path: entryPath } = dtsEntry;
|
|
106
|
+
let entry = "";
|
|
107
|
+
if (bundle === true && entryPath) {
|
|
108
|
+
const entrySourcePath = join(cwd, entryPath);
|
|
109
|
+
const relativePath = relative(rootDir, dirname(entrySourcePath));
|
|
110
|
+
entry = join(
|
|
111
|
+
declarationDir,
|
|
112
|
+
relativePath,
|
|
113
|
+
basename(entrySourcePath)
|
|
114
|
+
).replace(
|
|
115
|
+
/\.(js|mjs|jsx|ts|mts|tsx|cjs|cts|cjsx|ctsx|mjsx|mtsx)$/,
|
|
116
|
+
".d.ts"
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
const bundleDtsIfNeeded = async () => {
|
|
120
|
+
if (bundle === true) {
|
|
121
|
+
const { bundleDts } = await import("./apiExtractor.js");
|
|
122
|
+
await bundleDts({
|
|
123
|
+
name,
|
|
124
|
+
cwd,
|
|
125
|
+
outDir,
|
|
126
|
+
dtsEntry: {
|
|
127
|
+
name: entryName,
|
|
128
|
+
path: entry
|
|
129
|
+
},
|
|
130
|
+
tsconfigPath,
|
|
131
|
+
dtsExtension,
|
|
132
|
+
bundledPackages: calcBundledPackages({
|
|
133
|
+
autoExternal,
|
|
134
|
+
cwd,
|
|
135
|
+
userExternals
|
|
136
|
+
})
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
const onComplete = async (isSuccess) => {
|
|
141
|
+
if (isSuccess) {
|
|
142
|
+
await bundleDtsIfNeeded();
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
await emitDts(
|
|
146
|
+
{
|
|
147
|
+
name,
|
|
148
|
+
cwd,
|
|
149
|
+
configPath,
|
|
150
|
+
declarationDir,
|
|
151
|
+
dtsExtension
|
|
152
|
+
},
|
|
153
|
+
onComplete,
|
|
154
|
+
bundle,
|
|
155
|
+
isWatch
|
|
156
|
+
);
|
|
157
|
+
if (!isWatch) {
|
|
158
|
+
await bundleDtsIfNeeded();
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
process.on("message", async (data) => {
|
|
162
|
+
if (!data.cwd) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
try {
|
|
166
|
+
await generateDts(data);
|
|
167
|
+
} catch (e) {
|
|
168
|
+
logger.error(e);
|
|
169
|
+
process.send("error");
|
|
170
|
+
process.exit(1);
|
|
171
|
+
}
|
|
172
|
+
process.send("success");
|
|
173
|
+
if (!data.isWatch) {
|
|
174
|
+
process.exit();
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
export {
|
|
178
|
+
calcBundledPackages,
|
|
179
|
+
generateDts
|
|
180
|
+
};
|