rsbuild-plugin-dts 0.2.1 → 0.2.2
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/apiExtractor.js +11 -11
- package/dist/dts.js +20 -20
- package/dist/index.js +14 -14
- package/dist/tsc.js +17 -17
- package/dist/utils.js +33 -33
- package/package.json +1 -1
package/dist/apiExtractor.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import * as
|
|
2
|
-
import * as
|
|
3
|
-
import * as
|
|
1
|
+
import * as __WEBPACK_EXTERNAL_MODULE_node_path__ from "node:path";
|
|
2
|
+
import * as __WEBPACK_EXTERNAL_MODULE__microsoft_api_extractor__ from "@microsoft/api-extractor";
|
|
3
|
+
import * as __WEBPACK_EXTERNAL_MODULE__rsbuild_core__ from "@rsbuild/core";
|
|
4
4
|
import * as __WEBPACK_EXTERNAL_MODULE_picocolors__ from "picocolors";
|
|
5
|
-
import * as
|
|
5
|
+
import * as __WEBPACK_EXTERNAL_MODULE__utils_js__ from "./utils.js";
|
|
6
6
|
async function bundleDts(options) {
|
|
7
7
|
const { name, cwd, distPath, dtsExtension, banner, footer, dtsEntry = {
|
|
8
8
|
name: 'index',
|
|
@@ -10,7 +10,7 @@ async function bundleDts(options) {
|
|
|
10
10
|
}, tsconfigPath = 'tsconfig.json', bundledPackages = [] } = options;
|
|
11
11
|
try {
|
|
12
12
|
const start = Date.now();
|
|
13
|
-
const untrimmedFilePath = (0,
|
|
13
|
+
const untrimmedFilePath = (0, __WEBPACK_EXTERNAL_MODULE_node_path__.join)(cwd, (0, __WEBPACK_EXTERNAL_MODULE_node_path__.relative)(cwd, distPath), `${dtsEntry.name}${dtsExtension}`);
|
|
14
14
|
const mainEntryPointFilePath = dtsEntry.path.replace(/\?.*$/, '');
|
|
15
15
|
const internalConfig = {
|
|
16
16
|
mainEntryPointFilePath,
|
|
@@ -24,19 +24,19 @@ async function bundleDts(options) {
|
|
|
24
24
|
},
|
|
25
25
|
projectFolder: cwd
|
|
26
26
|
};
|
|
27
|
-
const extractorConfig =
|
|
27
|
+
const extractorConfig = __WEBPACK_EXTERNAL_MODULE__microsoft_api_extractor__.ExtractorConfig.prepare({
|
|
28
28
|
configObject: internalConfig,
|
|
29
29
|
configObjectFullPath: void 0,
|
|
30
|
-
packageJsonFullPath: (0,
|
|
30
|
+
packageJsonFullPath: (0, __WEBPACK_EXTERNAL_MODULE_node_path__.join)(cwd, 'package.json')
|
|
31
31
|
});
|
|
32
|
-
const extractorResult =
|
|
32
|
+
const extractorResult = __WEBPACK_EXTERNAL_MODULE__microsoft_api_extractor__.Extractor.invoke(extractorConfig, {
|
|
33
33
|
localBuild: true
|
|
34
34
|
});
|
|
35
35
|
if (!extractorResult.succeeded) throw new Error(`API Extractor error. ${__WEBPACK_EXTERNAL_MODULE_picocolors__["default"].gray(`(${name})`)}`);
|
|
36
|
-
await (0,
|
|
37
|
-
|
|
36
|
+
await (0, __WEBPACK_EXTERNAL_MODULE__utils_js__.addBannerAndFooter)(untrimmedFilePath, banner, footer);
|
|
37
|
+
__WEBPACK_EXTERNAL_MODULE__rsbuild_core__.logger.info(`API Extractor bundle DTS succeeded: ${__WEBPACK_EXTERNAL_MODULE_picocolors__["default"].cyan(untrimmedFilePath)} in ${(0, __WEBPACK_EXTERNAL_MODULE__utils_js__.getTimeCost)(start)} ${__WEBPACK_EXTERNAL_MODULE_picocolors__["default"].gray(`(${name})`)}`);
|
|
38
38
|
} catch (e) {
|
|
39
|
-
|
|
39
|
+
__WEBPACK_EXTERNAL_MODULE__rsbuild_core__.logger.error('API Extractor Error');
|
|
40
40
|
throw new Error(`${e}`);
|
|
41
41
|
}
|
|
42
42
|
}
|
package/dist/dts.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import * as
|
|
2
|
-
import * as
|
|
3
|
-
import * as
|
|
1
|
+
import * as __WEBPACK_EXTERNAL_MODULE_node_fs__ from "node:fs";
|
|
2
|
+
import * as __WEBPACK_EXTERNAL_MODULE_node_path__ from "node:path";
|
|
3
|
+
import * as __WEBPACK_EXTERNAL_MODULE__rsbuild_core__ from "@rsbuild/core";
|
|
4
4
|
import * as __WEBPACK_EXTERNAL_MODULE_picocolors__ from "picocolors";
|
|
5
|
-
import * as
|
|
6
|
-
import * as
|
|
5
|
+
import * as __WEBPACK_EXTERNAL_MODULE__tsc_js__ from "./tsc.js";
|
|
6
|
+
import * as __WEBPACK_EXTERNAL_MODULE__utils_js__ from "./utils.js";
|
|
7
7
|
const isObject = (obj)=>'[object Object]' === Object.prototype.toString.call(obj);
|
|
8
8
|
const calcBundledPackages = (options)=>{
|
|
9
9
|
const { autoExternal, cwd, userExternals } = options;
|
|
10
10
|
let pkgJson;
|
|
11
11
|
try {
|
|
12
|
-
const content =
|
|
12
|
+
const content = __WEBPACK_EXTERNAL_MODULE_node_fs__["default"].readFileSync((0, __WEBPACK_EXTERNAL_MODULE_node_path__.join)(cwd, 'package.json'), 'utf-8');
|
|
13
13
|
pkgJson = JSON.parse(content);
|
|
14
14
|
} catch (err) {
|
|
15
|
-
|
|
15
|
+
__WEBPACK_EXTERNAL_MODULE__rsbuild_core__.logger.warn('The type of third-party packages will not be bundled due to read package.json failed');
|
|
16
16
|
return [];
|
|
17
17
|
}
|
|
18
18
|
const externalOptions = autoExternal ? {
|
|
@@ -53,28 +53,28 @@ const calcBundledPackages = (options)=>{
|
|
|
53
53
|
};
|
|
54
54
|
async function generateDts(data) {
|
|
55
55
|
const { bundle, dtsEntry, tsconfigPath, tsConfigResult, distPath, rootDistPath, cleanDistPath, name, cwd, build, isWatch, dtsExtension = '.d.ts', autoExternal = true, userExternals, banner, footer } = data;
|
|
56
|
-
|
|
56
|
+
__WEBPACK_EXTERNAL_MODULE__rsbuild_core__.logger.start(`Generating DTS... ${__WEBPACK_EXTERNAL_MODULE_picocolors__["default"].gray(`(${name})`)}`);
|
|
57
57
|
const { options: rawCompilerOptions, fileNames } = tsConfigResult;
|
|
58
58
|
const dtsEmitPath = distPath ?? rawCompilerOptions.declarationDir ?? rootDistPath;
|
|
59
|
-
if (false !== cleanDistPath) await (0,
|
|
60
|
-
if (bundle) await (0,
|
|
61
|
-
if (rawCompilerOptions.composite || rawCompilerOptions.incremental || build) await (0,
|
|
62
|
-
const rootDir = rawCompilerOptions.rootDir ?? (rawCompilerOptions.composite ? (0,
|
|
63
|
-
const resolvedDtsEmitPath = (0,
|
|
59
|
+
if (false !== cleanDistPath) await (0, __WEBPACK_EXTERNAL_MODULE__utils_js__.cleanDtsFiles)(dtsEmitPath);
|
|
60
|
+
if (bundle) await (0, __WEBPACK_EXTERNAL_MODULE__utils_js__.clearTempDeclarationDir)(cwd);
|
|
61
|
+
if (rawCompilerOptions.composite || rawCompilerOptions.incremental || build) await (0, __WEBPACK_EXTERNAL_MODULE__utils_js__.cleanTsBuildInfoFile)(tsconfigPath, rawCompilerOptions);
|
|
62
|
+
const rootDir = rawCompilerOptions.rootDir ?? (rawCompilerOptions.composite ? (0, __WEBPACK_EXTERNAL_MODULE_node_path__.dirname)(tsconfigPath) : await (0, __WEBPACK_EXTERNAL_MODULE__utils_js__.calcLongestCommonPath)(fileNames.filter((fileName)=>!/\.d\.(ts|mts|cts)$/.test(fileName)))) ?? (0, __WEBPACK_EXTERNAL_MODULE_node_path__.dirname)(tsconfigPath);
|
|
63
|
+
const resolvedDtsEmitPath = (0, __WEBPACK_EXTERNAL_MODULE_node_path__.normalize)((0, __WEBPACK_EXTERNAL_MODULE_node_path__.resolve)((0, __WEBPACK_EXTERNAL_MODULE_node_path__.dirname)(tsconfigPath), dtsEmitPath));
|
|
64
64
|
if (build) {
|
|
65
65
|
if (bundle) throw Error('Can not set "dts.bundle: true" when "dts.build: true"');
|
|
66
|
-
if ((!rawCompilerOptions.outDir || (0,
|
|
66
|
+
if ((!rawCompilerOptions.outDir || (0, __WEBPACK_EXTERNAL_MODULE_node_path__.normalize)(rawCompilerOptions.outDir) !== resolvedDtsEmitPath) && (!rawCompilerOptions.declarationDir || (0, __WEBPACK_EXTERNAL_MODULE_node_path__.normalize)(rawCompilerOptions.declarationDir) !== resolvedDtsEmitPath)) {
|
|
67
67
|
const info = rawCompilerOptions.outDir && !rawCompilerOptions.declarationDir ? 'outDir' : 'declarationDir';
|
|
68
68
|
throw Error(`Please set ${info}: "${dtsEmitPath}" in ${__WEBPACK_EXTERNAL_MODULE_picocolors__["default"].underline(tsconfigPath)} to keep it same as "dts.distPath" or "output.distPath.root" field in lib config.`);
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
|
-
const declarationDir = bundle ? (0,
|
|
71
|
+
const declarationDir = bundle ? (0, __WEBPACK_EXTERNAL_MODULE__utils_js__.ensureTempDeclarationDir)(cwd, name) : dtsEmitPath;
|
|
72
72
|
const { name: entryName, path: entryPath } = dtsEntry;
|
|
73
73
|
let entry = '';
|
|
74
74
|
if (true === bundle && entryPath) {
|
|
75
|
-
const entrySourcePath = (0,
|
|
76
|
-
const relativePath = (0,
|
|
77
|
-
entry = (0,
|
|
75
|
+
const entrySourcePath = (0, __WEBPACK_EXTERNAL_MODULE_node_path__.isAbsolute)(entryPath) ? entryPath : (0, __WEBPACK_EXTERNAL_MODULE_node_path__.join)(cwd, entryPath);
|
|
76
|
+
const relativePath = (0, __WEBPACK_EXTERNAL_MODULE_node_path__.relative)(rootDir, (0, __WEBPACK_EXTERNAL_MODULE_node_path__.dirname)(entrySourcePath));
|
|
77
|
+
entry = (0, __WEBPACK_EXTERNAL_MODULE_node_path__.join)(declarationDir, relativePath, (0, __WEBPACK_EXTERNAL_MODULE_node_path__.basename)(entrySourcePath)).replace(/\?.*$/, '').replace(/\.(js|mjs|jsx|ts|mts|tsx|cjs|cts|cjsx|ctsx|mjsx|mtsx)$/, '.d.ts');
|
|
78
78
|
}
|
|
79
79
|
const bundleDtsIfNeeded = async ()=>{
|
|
80
80
|
if (true === bundle) {
|
|
@@ -102,7 +102,7 @@ async function generateDts(data) {
|
|
|
102
102
|
const onComplete = async (isSuccess)=>{
|
|
103
103
|
if (isSuccess) await bundleDtsIfNeeded();
|
|
104
104
|
};
|
|
105
|
-
await (0,
|
|
105
|
+
await (0, __WEBPACK_EXTERNAL_MODULE__tsc_js__.emitDts)({
|
|
106
106
|
name,
|
|
107
107
|
cwd,
|
|
108
108
|
configPath: tsconfigPath,
|
|
@@ -119,7 +119,7 @@ process.on('message', async (data)=>{
|
|
|
119
119
|
try {
|
|
120
120
|
await generateDts(data);
|
|
121
121
|
} catch (e) {
|
|
122
|
-
|
|
122
|
+
__WEBPACK_EXTERNAL_MODULE__rsbuild_core__.logger.error(e);
|
|
123
123
|
process.send('error');
|
|
124
124
|
process.exit(1);
|
|
125
125
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import * as
|
|
2
|
-
import * as
|
|
3
|
-
import * as
|
|
4
|
-
import * as
|
|
1
|
+
import * as __WEBPACK_EXTERNAL_MODULE_node_child_process__ from "node:child_process";
|
|
2
|
+
import * as __WEBPACK_EXTERNAL_MODULE_node_path__ from "node:path";
|
|
3
|
+
import * as __WEBPACK_EXTERNAL_MODULE_node_url__ from "node:url";
|
|
4
|
+
import * as __WEBPACK_EXTERNAL_MODULE__rsbuild_core__ from "@rsbuild/core";
|
|
5
5
|
import * as __WEBPACK_EXTERNAL_MODULE_typescript__ from "typescript";
|
|
6
|
-
import * as
|
|
7
|
-
const src_rslib_entry_filename = (0,
|
|
8
|
-
const src_rslib_entry_dirname = (0,
|
|
6
|
+
import * as __WEBPACK_EXTERNAL_MODULE__utils_js__ from "./utils.js";
|
|
7
|
+
const src_rslib_entry_filename = (0, __WEBPACK_EXTERNAL_MODULE_node_url__.fileURLToPath)(import.meta.url);
|
|
8
|
+
const src_rslib_entry_dirname = (0, __WEBPACK_EXTERNAL_MODULE_node_path__.dirname)(src_rslib_entry_filename);
|
|
9
9
|
const PLUGIN_DTS_NAME = 'rsbuild:dts';
|
|
10
10
|
const pluginDts = (options = {})=>({
|
|
11
11
|
name: PLUGIN_DTS_NAME,
|
|
@@ -19,16 +19,16 @@ const pluginDts = (options = {})=>({
|
|
|
19
19
|
api.onBeforeEnvironmentCompile(async ({ isWatch, isFirstCompile, environment })=>{
|
|
20
20
|
if (!isFirstCompile) return;
|
|
21
21
|
const { config } = environment;
|
|
22
|
-
const dtsEntry = (0,
|
|
22
|
+
const dtsEntry = (0, __WEBPACK_EXTERNAL_MODULE__utils_js__.processSourceEntry)(options.bundle, config.source?.entry);
|
|
23
23
|
const cwd = api.context.rootPath;
|
|
24
24
|
const tsconfigPath = __WEBPACK_EXTERNAL_MODULE_typescript__["default"].findConfigFile(cwd, __WEBPACK_EXTERNAL_MODULE_typescript__["default"].sys.fileExists, config.source.tsconfigPath);
|
|
25
25
|
if (!tsconfigPath) {
|
|
26
|
-
|
|
26
|
+
__WEBPACK_EXTERNAL_MODULE__rsbuild_core__.logger.error(`tsconfig.json not found in ${cwd}`);
|
|
27
27
|
throw new Error();
|
|
28
28
|
}
|
|
29
|
-
const tsConfigResult = (0,
|
|
30
|
-
const jsExtension = (0,
|
|
31
|
-
const childProcess = (0,
|
|
29
|
+
const tsConfigResult = (0, __WEBPACK_EXTERNAL_MODULE__utils_js__.loadTsconfig)(tsconfigPath);
|
|
30
|
+
const jsExtension = (0, __WEBPACK_EXTERNAL_MODULE_node_path__.extname)(src_rslib_entry_filename);
|
|
31
|
+
const childProcess = (0, __WEBPACK_EXTERNAL_MODULE_node_child_process__.fork)((0, __WEBPACK_EXTERNAL_MODULE_node_path__.join)(src_rslib_entry_dirname, `./dts${jsExtension}`), [], {
|
|
32
32
|
stdio: 'inherit'
|
|
33
33
|
});
|
|
34
34
|
childProcesses.push(childProcess);
|
|
@@ -66,8 +66,8 @@ const pluginDts = (options = {})=>({
|
|
|
66
66
|
if (!isFirstCompile) return;
|
|
67
67
|
for (const result of promisesResult)if ('error' === result.status) {
|
|
68
68
|
if (options.abortOnError) throw new Error(result.errorMessage);
|
|
69
|
-
result.errorMessage &&
|
|
70
|
-
|
|
69
|
+
result.errorMessage && __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.logger.error(result.errorMessage);
|
|
70
|
+
__WEBPACK_EXTERNAL_MODULE__rsbuild_core__.logger.warn('With the `abortOnError` configuration currently turned off, type errors do not cause build failures, but they do not guarantee proper type file output.');
|
|
71
71
|
}
|
|
72
72
|
},
|
|
73
73
|
order: 'post'
|
package/dist/tsc.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as __WEBPACK_EXTERNAL_MODULE__rsbuild_core__ from "@rsbuild/core";
|
|
2
2
|
import * as __WEBPACK_EXTERNAL_MODULE_picocolors__ from "picocolors";
|
|
3
3
|
import * as __WEBPACK_EXTERNAL_MODULE_typescript__ from "typescript";
|
|
4
|
-
import * as
|
|
4
|
+
import * as __WEBPACK_EXTERNAL_MODULE__utils_js__ from "./utils.js";
|
|
5
5
|
async function handleDiagnosticsAndProcessFiles(diagnostics, configPath, host, bundle, declarationDir, dtsExtension, banner, footer, name) {
|
|
6
6
|
const diagnosticMessages = [];
|
|
7
7
|
for (const diagnostic of diagnostics){
|
|
8
|
-
const fileLoc = (0,
|
|
8
|
+
const fileLoc = (0, __WEBPACK_EXTERNAL_MODULE__utils_js__.getFileLoc)(diagnostic, configPath);
|
|
9
9
|
const message = `${fileLoc} - ${__WEBPACK_EXTERNAL_MODULE_picocolors__["default"].red('error')} ${__WEBPACK_EXTERNAL_MODULE_picocolors__["default"].gray(`TS${diagnostic.code}:`)} ${__WEBPACK_EXTERNAL_MODULE_typescript__["default"].flattenDiagnosticMessageText(diagnostic.messageText, host.getNewLine())}`;
|
|
10
10
|
diagnosticMessages.push(message);
|
|
11
11
|
}
|
|
12
|
-
await (0,
|
|
12
|
+
await (0, __WEBPACK_EXTERNAL_MODULE__utils_js__.processDtsFiles)(bundle, declarationDir, dtsExtension, banner, footer);
|
|
13
13
|
if (diagnosticMessages.length) {
|
|
14
|
-
|
|
15
|
-
for (const message of diagnosticMessages)
|
|
14
|
+
__WEBPACK_EXTERNAL_MODULE__rsbuild_core__.logger.error(`Failed to emit declaration files. ${__WEBPACK_EXTERNAL_MODULE_picocolors__["default"].gray(`(${name})`)}`);
|
|
15
|
+
for (const message of diagnosticMessages)__WEBPACK_EXTERNAL_MODULE__rsbuild_core__.logger.error(message);
|
|
16
16
|
throw new Error('DTS generation failed');
|
|
17
17
|
}
|
|
18
18
|
}
|
|
@@ -35,23 +35,23 @@ async function emitDts(options, onComplete, bundle = false, isWatch = false, bui
|
|
|
35
35
|
getNewLine: ()=>__WEBPACK_EXTERNAL_MODULE_typescript__["default"].sys.newLine
|
|
36
36
|
};
|
|
37
37
|
const reportDiagnostic = (diagnostic)=>{
|
|
38
|
-
const fileLoc = (0,
|
|
39
|
-
|
|
38
|
+
const fileLoc = (0, __WEBPACK_EXTERNAL_MODULE__utils_js__.getFileLoc)(diagnostic, configPath);
|
|
39
|
+
__WEBPACK_EXTERNAL_MODULE__rsbuild_core__.logger.error(`${fileLoc} - ${__WEBPACK_EXTERNAL_MODULE_picocolors__["default"].red('error')} ${__WEBPACK_EXTERNAL_MODULE_picocolors__["default"].gray(`TS${diagnostic.code}:`)}`, __WEBPACK_EXTERNAL_MODULE_typescript__["default"].flattenDiagnosticMessageText(diagnostic.messageText, formatHost.getNewLine()));
|
|
40
40
|
};
|
|
41
41
|
const reportWatchStatusChanged = async (diagnostic, _newLine, _options, errorCount)=>{
|
|
42
42
|
const message = `${__WEBPACK_EXTERNAL_MODULE_typescript__["default"].flattenDiagnosticMessageText(diagnostic.messageText, formatHost.getNewLine())} ${__WEBPACK_EXTERNAL_MODULE_picocolors__["default"].gray(`(${name})`)}`;
|
|
43
|
-
if (6031 === diagnostic.code || 6032 === diagnostic.code)
|
|
43
|
+
if (6031 === diagnostic.code || 6032 === diagnostic.code) __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.logger.info(message);
|
|
44
44
|
if (6194 === diagnostic.code) {
|
|
45
|
-
if (0 !== errorCount && errorCount)
|
|
45
|
+
if (0 !== errorCount && errorCount) __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.logger.error(message);
|
|
46
46
|
else {
|
|
47
|
-
|
|
47
|
+
__WEBPACK_EXTERNAL_MODULE__rsbuild_core__.logger.info(message);
|
|
48
48
|
onComplete(true);
|
|
49
49
|
}
|
|
50
|
-
await (0,
|
|
50
|
+
await (0, __WEBPACK_EXTERNAL_MODULE__utils_js__.processDtsFiles)(bundle, declarationDir, dtsExtension, banner, footer);
|
|
51
51
|
}
|
|
52
52
|
if (6193 === diagnostic.code) {
|
|
53
|
-
|
|
54
|
-
await (0,
|
|
53
|
+
__WEBPACK_EXTERNAL_MODULE__rsbuild_core__.logger.error(message);
|
|
54
|
+
await (0, __WEBPACK_EXTERNAL_MODULE__utils_js__.processDtsFiles)(bundle, declarationDir, dtsExtension, banner, footer);
|
|
55
55
|
}
|
|
56
56
|
};
|
|
57
57
|
const system = {
|
|
@@ -95,9 +95,9 @@ async function emitDts(options, onComplete, bundle = false, isWatch = false, bui
|
|
|
95
95
|
configPath
|
|
96
96
|
], compilerOptions);
|
|
97
97
|
solutionBuilder.build();
|
|
98
|
-
await (0,
|
|
98
|
+
await (0, __WEBPACK_EXTERNAL_MODULE__utils_js__.processDtsFiles)(bundle, declarationDir, dtsExtension, banner, footer);
|
|
99
99
|
if (errorNumber > 0) {
|
|
100
|
-
|
|
100
|
+
__WEBPACK_EXTERNAL_MODULE__rsbuild_core__.logger.error(`Failed to emit declaration files. ${__WEBPACK_EXTERNAL_MODULE_picocolors__["default"].gray(`(${name})`)}`);
|
|
101
101
|
throw new Error('DTS generation failed');
|
|
102
102
|
}
|
|
103
103
|
}
|
|
@@ -114,7 +114,7 @@ async function emitDts(options, onComplete, bundle = false, isWatch = false, bui
|
|
|
114
114
|
const allDiagnostics = __WEBPACK_EXTERNAL_MODULE_typescript__["default"].getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
115
115
|
await handleDiagnosticsAndProcessFiles(allDiagnostics, configPath, host, bundle, declarationDir, dtsExtension, banner, footer, name);
|
|
116
116
|
}
|
|
117
|
-
|
|
117
|
+
__WEBPACK_EXTERNAL_MODULE__rsbuild_core__.logger.ready(`DTS generated in ${(0, __WEBPACK_EXTERNAL_MODULE__utils_js__.getTimeCost)(start)} ${__WEBPACK_EXTERNAL_MODULE_picocolors__["default"].gray(`(${name})`)}`);
|
|
118
118
|
}
|
|
119
119
|
}
|
|
120
120
|
export { emitDts };
|
package/dist/utils.js
CHANGED
|
@@ -1,46 +1,46 @@
|
|
|
1
|
-
import * as
|
|
2
|
-
import * as
|
|
3
|
-
import * as
|
|
4
|
-
import * as
|
|
5
|
-
import * as
|
|
6
|
-
import * as
|
|
1
|
+
import * as __WEBPACK_EXTERNAL_MODULE_node_fs__ from "node:fs";
|
|
2
|
+
import * as __WEBPACK_EXTERNAL_MODULE_node_fs_promises__ from "node:fs/promises";
|
|
3
|
+
import * as __WEBPACK_EXTERNAL_MODULE_node_os__ from "node:os";
|
|
4
|
+
import * as __WEBPACK_EXTERNAL_MODULE_node_path__ from "node:path";
|
|
5
|
+
import * as __WEBPACK_EXTERNAL_MODULE__rsbuild_core__ from "@rsbuild/core";
|
|
6
|
+
import * as __WEBPACK_EXTERNAL_MODULE_magic_string__ from "magic-string";
|
|
7
7
|
import * as __WEBPACK_EXTERNAL_MODULE_picocolors__ from "picocolors";
|
|
8
8
|
import * as __WEBPACK_EXTERNAL_MODULE_tinyglobby__ from "tinyglobby";
|
|
9
9
|
import * as __WEBPACK_EXTERNAL_MODULE_typescript__ from "typescript";
|
|
10
10
|
function loadTsconfig(tsconfigPath) {
|
|
11
11
|
const configFile = __WEBPACK_EXTERNAL_MODULE_typescript__["default"].readConfigFile(tsconfigPath, __WEBPACK_EXTERNAL_MODULE_typescript__["default"].sys.readFile);
|
|
12
|
-
const configFileContent = __WEBPACK_EXTERNAL_MODULE_typescript__["default"].parseJsonConfigFileContent(configFile.config, __WEBPACK_EXTERNAL_MODULE_typescript__["default"].sys,
|
|
12
|
+
const configFileContent = __WEBPACK_EXTERNAL_MODULE_typescript__["default"].parseJsonConfigFileContent(configFile.config, __WEBPACK_EXTERNAL_MODULE_typescript__["default"].sys, __WEBPACK_EXTERNAL_MODULE_node_path__["default"].dirname(tsconfigPath));
|
|
13
13
|
return configFileContent;
|
|
14
14
|
}
|
|
15
15
|
const TEMP_FOLDER = '.rslib';
|
|
16
16
|
const TEMP_DTS_DIR = `${TEMP_FOLDER}/declarations`;
|
|
17
17
|
function ensureTempDeclarationDir(cwd, name) {
|
|
18
|
-
const dirPath =
|
|
19
|
-
if (
|
|
20
|
-
|
|
18
|
+
const dirPath = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].join(cwd, TEMP_DTS_DIR, name);
|
|
19
|
+
if (__WEBPACK_EXTERNAL_MODULE_node_fs__["default"].existsSync(dirPath)) return dirPath;
|
|
20
|
+
__WEBPACK_EXTERNAL_MODULE_node_fs__["default"].mkdirSync(dirPath, {
|
|
21
21
|
recursive: true
|
|
22
22
|
});
|
|
23
|
-
const gitIgnorePath =
|
|
24
|
-
|
|
23
|
+
const gitIgnorePath = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].join(cwd, TEMP_FOLDER, '.gitignore');
|
|
24
|
+
__WEBPACK_EXTERNAL_MODULE_node_fs__["default"].writeFileSync(gitIgnorePath, '**/*\n');
|
|
25
25
|
return dirPath;
|
|
26
26
|
}
|
|
27
27
|
async function pathExists(path) {
|
|
28
|
-
return
|
|
28
|
+
return __WEBPACK_EXTERNAL_MODULE_node_fs__["default"].promises.access(path).then(()=>true).catch(()=>false);
|
|
29
29
|
}
|
|
30
30
|
async function emptyDir(dir) {
|
|
31
31
|
if (!await pathExists(dir)) return;
|
|
32
32
|
try {
|
|
33
|
-
for (const file of (await
|
|
33
|
+
for (const file of (await __WEBPACK_EXTERNAL_MODULE_node_fs__["default"].promises.readdir(dir)))await __WEBPACK_EXTERNAL_MODULE_node_fs__["default"].promises.rm(__WEBPACK_EXTERNAL_MODULE_node_path__["default"].resolve(dir, file), {
|
|
34
34
|
recursive: true,
|
|
35
35
|
force: true
|
|
36
36
|
});
|
|
37
37
|
} catch (err) {
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
__WEBPACK_EXTERNAL_MODULE__rsbuild_core__.logger.debug(`Failed to empty dir: ${dir}`);
|
|
39
|
+
__WEBPACK_EXTERNAL_MODULE__rsbuild_core__.logger.debug(err);
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
async function clearTempDeclarationDir(cwd) {
|
|
43
|
-
const dirPath =
|
|
43
|
+
const dirPath = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].join(cwd, TEMP_DTS_DIR);
|
|
44
44
|
await emptyDir(dirPath);
|
|
45
45
|
}
|
|
46
46
|
function getFileLoc(diagnostic, configPath) {
|
|
@@ -61,7 +61,7 @@ const prettyTime = (seconds)=>{
|
|
|
61
61
|
return `${format(minutes.toFixed(2))} m`;
|
|
62
62
|
};
|
|
63
63
|
const convertPath = (path)=>{
|
|
64
|
-
if ('win32' === (0,
|
|
64
|
+
if ('win32' === (0, __WEBPACK_EXTERNAL_MODULE_node_os__.platform)()) return (0, __WEBPACK_EXTERNAL_MODULE_tinyglobby__.convertPathToPattern)(path);
|
|
65
65
|
return path;
|
|
66
66
|
};
|
|
67
67
|
function getTimeCost(start) {
|
|
@@ -70,23 +70,23 @@ function getTimeCost(start) {
|
|
|
70
70
|
}
|
|
71
71
|
async function addBannerAndFooter(file, banner, footer) {
|
|
72
72
|
if (!banner && !footer) return;
|
|
73
|
-
const content = await
|
|
74
|
-
const code = new
|
|
73
|
+
const content = await __WEBPACK_EXTERNAL_MODULE_node_fs_promises__["default"].readFile(file, 'utf-8');
|
|
74
|
+
const code = new __WEBPACK_EXTERNAL_MODULE_magic_string__["default"](content);
|
|
75
75
|
if (banner && !content.trimStart().startsWith(banner.trim())) code.prepend(`${banner}\n`);
|
|
76
76
|
if (footer && !content.trimEnd().endsWith(footer.trim())) code.append(`\n${footer}\n`);
|
|
77
|
-
if (code.hasChanged()) await
|
|
77
|
+
if (code.hasChanged()) await __WEBPACK_EXTERNAL_MODULE_node_fs_promises__["default"].writeFile(file, code.toString());
|
|
78
78
|
}
|
|
79
79
|
async function processDtsFiles(bundle, dir, dtsExtension, banner, footer) {
|
|
80
80
|
if (bundle) return;
|
|
81
|
-
const dtsFiles = await (0, __WEBPACK_EXTERNAL_MODULE_tinyglobby__.glob)(convertPath((0,
|
|
81
|
+
const dtsFiles = await (0, __WEBPACK_EXTERNAL_MODULE_tinyglobby__.glob)(convertPath((0, __WEBPACK_EXTERNAL_MODULE_node_path__.join)(dir, '/**/*.d.ts')), {
|
|
82
82
|
absolute: true
|
|
83
83
|
});
|
|
84
84
|
for (const file of dtsFiles)try {
|
|
85
85
|
await addBannerAndFooter(file, banner, footer);
|
|
86
86
|
const newFile = file.replace('.d.ts', dtsExtension);
|
|
87
|
-
|
|
87
|
+
__WEBPACK_EXTERNAL_MODULE_node_fs__["default"].renameSync(file, newFile);
|
|
88
88
|
} catch (error) {
|
|
89
|
-
|
|
89
|
+
__WEBPACK_EXTERNAL_MODULE__rsbuild_core__.logger.error(`Error renaming DTS file ${file}: ${error}`);
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
92
|
function processSourceEntry(bundle, entryConfig) {
|
|
@@ -102,7 +102,7 @@ function processSourceEntry(bundle, entryConfig) {
|
|
|
102
102
|
}
|
|
103
103
|
async function calcLongestCommonPath(absPaths) {
|
|
104
104
|
if (0 === absPaths.length) return null;
|
|
105
|
-
const sep =
|
|
105
|
+
const sep = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].posix.sep;
|
|
106
106
|
const splitPaths = absPaths.map((p)=>p.split(sep));
|
|
107
107
|
let lcaFragments = splitPaths[0];
|
|
108
108
|
for(let i = 1; i < splitPaths.length; i++){
|
|
@@ -113,8 +113,8 @@ async function calcLongestCommonPath(absPaths) {
|
|
|
113
113
|
lcaFragments = lcaFragments.slice(0, j);
|
|
114
114
|
}
|
|
115
115
|
let lca = lcaFragments.length > 0 ? lcaFragments.join(sep) : sep;
|
|
116
|
-
const stats = await
|
|
117
|
-
if (stats?.isFile()) lca =
|
|
116
|
+
const stats = await __WEBPACK_EXTERNAL_MODULE_node_fs_promises__["default"].stat(lca);
|
|
117
|
+
if (stats?.isFile()) lca = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].dirname(lca);
|
|
118
118
|
return lca;
|
|
119
119
|
}
|
|
120
120
|
async function cleanDtsFiles(dir) {
|
|
@@ -123,20 +123,20 @@ async function cleanDtsFiles(dir) {
|
|
|
123
123
|
'/**/*.d.cts',
|
|
124
124
|
'/**/*.d.mts'
|
|
125
125
|
];
|
|
126
|
-
const files = await Promise.all(patterns.map((pattern)=>(0, __WEBPACK_EXTERNAL_MODULE_tinyglobby__.glob)(convertPath((0,
|
|
126
|
+
const files = await Promise.all(patterns.map((pattern)=>(0, __WEBPACK_EXTERNAL_MODULE_tinyglobby__.glob)(convertPath((0, __WEBPACK_EXTERNAL_MODULE_node_path__.join)(dir, pattern)), {
|
|
127
127
|
absolute: true
|
|
128
128
|
})));
|
|
129
129
|
const allFiles = files.flat();
|
|
130
|
-
await Promise.all(allFiles.map((file)=>
|
|
130
|
+
await Promise.all(allFiles.map((file)=>__WEBPACK_EXTERNAL_MODULE_node_fs_promises__["default"].rm(file, {
|
|
131
131
|
force: true
|
|
132
132
|
})));
|
|
133
133
|
}
|
|
134
134
|
async function cleanTsBuildInfoFile(tsconfigPath, compilerOptions) {
|
|
135
|
-
const tsconfigDir = (0,
|
|
135
|
+
const tsconfigDir = (0, __WEBPACK_EXTERNAL_MODULE_node_path__.dirname)(tsconfigPath);
|
|
136
136
|
const { outDir, rootDir, tsBuildInfoFile } = compilerOptions;
|
|
137
|
-
let tsbuildInfoFilePath = `${(0,
|
|
138
|
-
if (outDir) tsbuildInfoFilePath = rootDir ? (0,
|
|
139
|
-
if (await pathExists(tsbuildInfoFilePath)) await
|
|
137
|
+
let tsbuildInfoFilePath = `${(0, __WEBPACK_EXTERNAL_MODULE_node_path__.basename)(tsconfigPath, '.json')}${tsBuildInfoFile ?? '.tsbuildinfo'}`;
|
|
138
|
+
if (outDir) tsbuildInfoFilePath = rootDir ? (0, __WEBPACK_EXTERNAL_MODULE_node_path__.join)(outDir, (0, __WEBPACK_EXTERNAL_MODULE_node_path__.relative)((0, __WEBPACK_EXTERNAL_MODULE_node_path__.resolve)(tsconfigDir, rootDir), tsconfigDir), tsbuildInfoFilePath) : (0, __WEBPACK_EXTERNAL_MODULE_node_path__.join)(outDir, tsbuildInfoFilePath);
|
|
139
|
+
if (await pathExists(tsbuildInfoFilePath)) await __WEBPACK_EXTERNAL_MODULE_node_fs_promises__["default"].rm(tsbuildInfoFilePath, {
|
|
140
140
|
force: true
|
|
141
141
|
});
|
|
142
142
|
}
|