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/dist/utils.js ADDED
@@ -0,0 +1,134 @@
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 fsP from "node:fs/promises";
9
+ import { platform } from "node:os";
10
+ import path2, { join } from "node:path";
11
+ import { logger } from "@rsbuild/core";
12
+ import fg from "fast-glob";
13
+ import color from "picocolors";
14
+ import * as ts from "typescript";
15
+ const { convertPathToPattern } = fg;
16
+ function loadTsconfig(tsconfigPath) {
17
+ const configFile = ts.readConfigFile(tsconfigPath, ts.sys.readFile);
18
+ const configFileContent = ts.parseJsonConfigFileContent(
19
+ configFile.config,
20
+ ts.sys,
21
+ path2.dirname(tsconfigPath)
22
+ );
23
+ return configFileContent;
24
+ }
25
+ const TEMP_FOLDER = ".rslib";
26
+ const TEMP_DTS_DIR = `${TEMP_FOLDER}/declarations`;
27
+ function ensureTempDeclarationDir(cwd) {
28
+ const dirPath = path2.join(cwd, TEMP_DTS_DIR);
29
+ if (fs.existsSync(dirPath)) {
30
+ return dirPath;
31
+ }
32
+ fs.mkdirSync(dirPath, { recursive: true });
33
+ const gitIgnorePath = path2.join(cwd, TEMP_FOLDER, ".gitignore");
34
+ fs.writeFileSync(gitIgnorePath, "**/*\n");
35
+ return dirPath;
36
+ }
37
+ function getFileLoc(diagnostic) {
38
+ if (diagnostic.file) {
39
+ const { line, character } = ts.getLineAndCharacterOfPosition(
40
+ diagnostic.file,
41
+ diagnostic.start
42
+ );
43
+ return `${color.cyan(diagnostic.file.fileName)}:${color.yellow(line + 1)}:${color.yellow(character + 1)}`;
44
+ }
45
+ return "";
46
+ }
47
+ const prettyTime = (seconds) => {
48
+ const format = (time) => color.bold(time);
49
+ if (seconds < 10) {
50
+ const digits = seconds >= 0.01 ? 2 : 3;
51
+ return `${format(seconds.toFixed(digits))} s`;
52
+ }
53
+ if (seconds < 60) {
54
+ return `${format(seconds.toFixed(1))} s`;
55
+ }
56
+ const minutes = seconds / 60;
57
+ return `${format(minutes.toFixed(2))} m`;
58
+ };
59
+ const convertPath = (path3) => {
60
+ if (platform() === "win32") {
61
+ return convertPathToPattern(path3);
62
+ }
63
+ return path3;
64
+ };
65
+ function getTimeCost(start) {
66
+ const second = (Date.now() - start) / 1e3;
67
+ return prettyTime(second);
68
+ }
69
+ async function processDtsFiles(bundle, dir, dtsExtension) {
70
+ if (bundle) {
71
+ return;
72
+ }
73
+ const dtsFiles = await fg(convertPath(join(dir, "/**/*.d.ts")));
74
+ for (const file of dtsFiles) {
75
+ try {
76
+ const newFile = file.replace(".d.ts", dtsExtension);
77
+ fs.renameSync(file, newFile);
78
+ } catch (error) {
79
+ logger.error(`Error renaming DTS file ${file}: ${error}`);
80
+ }
81
+ }
82
+ }
83
+ function processSourceEntry(bundle, entryConfig) {
84
+ if (!bundle) {
85
+ return {
86
+ name: void 0,
87
+ path: void 0
88
+ };
89
+ }
90
+ if (entryConfig && Object.values(entryConfig).every((val) => typeof val === "string")) {
91
+ return {
92
+ name: Object.keys(entryConfig)[0],
93
+ path: Object.values(entryConfig)[0]
94
+ };
95
+ }
96
+ throw new Error(
97
+ "@microsoft/api-extractor only support single entry of Record<string, string> type to bundle DTS, please check your entry config."
98
+ );
99
+ }
100
+ async function calcLongestCommonPath(absPaths) {
101
+ if (absPaths.length === 0) {
102
+ return null;
103
+ }
104
+ const sep = path2.posix.sep;
105
+ const splitPaths = absPaths.map((p) => p.split(sep));
106
+ let lcaFragments = splitPaths[0];
107
+ for (let i = 1; i < splitPaths.length; i++) {
108
+ const currentPath = splitPaths[i];
109
+ const minLength = Math.min(lcaFragments.length, currentPath.length);
110
+ let j = 0;
111
+ while (j < minLength && lcaFragments[j] === currentPath[j]) {
112
+ j++;
113
+ }
114
+ lcaFragments = lcaFragments.slice(0, j);
115
+ }
116
+ let lca = lcaFragments.length > 0 ? lcaFragments.join(sep) : sep;
117
+ const stats = await fsP.stat(lca);
118
+ if (stats?.isFile()) {
119
+ lca = path2.dirname(lca);
120
+ }
121
+ return lca;
122
+ }
123
+ export {
124
+ TEMP_DTS_DIR,
125
+ TEMP_FOLDER,
126
+ calcLongestCommonPath,
127
+ ensureTempDeclarationDir,
128
+ getFileLoc,
129
+ getTimeCost,
130
+ loadTsconfig,
131
+ prettyTime,
132
+ processDtsFiles,
133
+ processSourceEntry
134
+ };
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "rsbuild-plugin-dts",
3
+ "version": "0.0.0",
4
+ "description": "Dts plugin for Rsbuild",
5
+ "homepage": "https://rslib.dev",
6
+ "bugs": {
7
+ "url": "https://github.com/web-infra-dev/rslib/issues"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/web-infra-dev/rslib",
12
+ "directory": "packages/plugin-dts"
13
+ },
14
+ "license": "MIT",
15
+ "type": "module",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "import": "./dist/index.js",
20
+ "require": "./dist/index.cjs"
21
+ }
22
+ },
23
+ "main": "./dist/index.cjs",
24
+ "types": "./dist/index.d.ts",
25
+ "files": [
26
+ "dist"
27
+ ],
28
+ "dependencies": {
29
+ "fast-glob": "^3.3.2",
30
+ "picocolors": "1.0.1"
31
+ },
32
+ "devDependencies": {
33
+ "@microsoft/api-extractor": "^7.47.5",
34
+ "@rsbuild/core": "1.0.1-beta.11",
35
+ "typescript": "^5.5.4",
36
+ "@rslib/tsconfig": "0.0.1"
37
+ },
38
+ "peerDependencies": {
39
+ "@microsoft/api-extractor": "^7",
40
+ "@rsbuild/core": "1.x",
41
+ "typescript": "^5"
42
+ },
43
+ "peerDependenciesMeta": {
44
+ "@microsoft/api-extractor": {
45
+ "optional": true
46
+ },
47
+ "typescript": {
48
+ "optional": true
49
+ }
50
+ },
51
+ "engines": {
52
+ "node": ">=16.0.0"
53
+ },
54
+ "publishConfig": {
55
+ "access": "public",
56
+ "registry": "https://registry.npmjs.org/"
57
+ },
58
+ "scripts": {
59
+ "build": "modern build",
60
+ "dev": "modern build --watch"
61
+ }
62
+ }