rsbuild-plugin-dts 0.0.18 → 0.1.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/README.md +158 -5
- package/dist/apiExtractor.d.ts +1 -1
- package/dist/apiExtractor.js +3 -3
- package/dist/dts.js +9 -11
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -2
- package/dist/tsc.js +52 -30
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -2,13 +2,166 @@
|
|
|
2
2
|
<img alt="Rslib Banner" src="https://assets.rspack.dev/rslib/rslib-banner.png">
|
|
3
3
|
</picture>
|
|
4
4
|
|
|
5
|
-
#
|
|
5
|
+
# rsbuild-plugin-dts
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
An [Rsbuild plugin](https://www.npmjs.com/package/rsbuild-plugin-dts) to emit declaration files for TypeScript which is built-in in Rslib.
|
|
8
8
|
|
|
9
|
-
##
|
|
9
|
+
## Using in Rslib
|
|
10
10
|
|
|
11
|
-
https://lib.rsbuild.dev/
|
|
11
|
+
Read [DTS](https://lib.rsbuild.dev/guide/advanced/dts) and [lib.dts](https://lib.rsbuild.dev/config/lib/dts) for more details.
|
|
12
|
+
|
|
13
|
+
## Using in Rsbuild
|
|
14
|
+
|
|
15
|
+
Install:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm add rsbuild-plugin-dts -D
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Add plugin to `rsbuild.config.ts`:
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
// rsbuild.config.ts
|
|
25
|
+
import { pluginDts } from 'rsbuild-plugin-dts';
|
|
26
|
+
|
|
27
|
+
export default {
|
|
28
|
+
plugins: [pluginDts()],
|
|
29
|
+
};
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Options
|
|
33
|
+
|
|
34
|
+
### bundle
|
|
35
|
+
|
|
36
|
+
- **Type:** `boolean`
|
|
37
|
+
- **Default:** `false`
|
|
38
|
+
|
|
39
|
+
Whether to bundle the DTS files.
|
|
40
|
+
|
|
41
|
+
If you want to [bundle DTS](https://lib.rsbuild.dev/guide/advanced/dts#bundle-dts) files, you should:
|
|
42
|
+
|
|
43
|
+
1. Install `@microsoft/api-extractor` as a development dependency, which is the underlying tool used for bundling DTS files.
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
npm add @microsoft/api-extractor -D
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
2. Set `bundle` to `true`.
|
|
50
|
+
|
|
51
|
+
```js
|
|
52
|
+
pluginDts({
|
|
53
|
+
bundle: true,
|
|
54
|
+
});
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### distPath
|
|
58
|
+
|
|
59
|
+
- **Type:** `string`
|
|
60
|
+
|
|
61
|
+
The output directory of DTS files. The default value follows the priority below:
|
|
62
|
+
|
|
63
|
+
1. The `distPath` value of the plugin options.
|
|
64
|
+
2. The `declarationDir` value in the `tsconfig.json` file.
|
|
65
|
+
3. The [output.distPath.root](https://rsbuild.dev/config/output/dist-path) value of Rsbuild configuration.
|
|
66
|
+
|
|
67
|
+
```js
|
|
68
|
+
pluginDts({
|
|
69
|
+
distPath: './dist-types',
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### build
|
|
74
|
+
|
|
75
|
+
- **Type:** `boolean`
|
|
76
|
+
- **Default:** `false`
|
|
77
|
+
|
|
78
|
+
Determines whether to generate DTS files while building the project references. This is equivalent to using the `--build` flag with the `tsc` command. See [Project References](https://www.typescriptlang.org/docs/handbook/project-references.html) for more details.
|
|
79
|
+
|
|
80
|
+
When this option is enabled, you must explicitly set `declarationDir` or `outDir` in `tsconfig.json` in order to meet the build requirements.
|
|
81
|
+
|
|
82
|
+
### abortOnError
|
|
83
|
+
|
|
84
|
+
- **Type:** `boolean`
|
|
85
|
+
- **Default:** `true`
|
|
86
|
+
|
|
87
|
+
Whether to abort the build process when an error occurs during DTS generation.
|
|
88
|
+
|
|
89
|
+
By default, type errors will cause the build to fail.
|
|
90
|
+
|
|
91
|
+
When `abortOnError` is set to `false`, the build will still succeed even if there are type issues in the code.
|
|
92
|
+
|
|
93
|
+
```js
|
|
94
|
+
pluginDts({
|
|
95
|
+
abortOnError: false,
|
|
96
|
+
});
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### dtsExtension
|
|
100
|
+
|
|
101
|
+
- **Type:** `string`
|
|
102
|
+
- **Default:** `'.d.ts'`
|
|
103
|
+
|
|
104
|
+
The extension of the DTS file.
|
|
105
|
+
|
|
106
|
+
```js
|
|
107
|
+
pluginDts({
|
|
108
|
+
dtsExtension: '.d.mts',
|
|
109
|
+
});
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### autoExternal
|
|
113
|
+
|
|
114
|
+
- **Type:** `boolean`
|
|
115
|
+
- **Default:** `true`
|
|
116
|
+
|
|
117
|
+
Whether to automatically externalize dependencies of different dependency types and do not bundle them into the DTS file.
|
|
118
|
+
|
|
119
|
+
The default value of `autoExternal` is `true`, which means the following dependency types will not be bundled:
|
|
120
|
+
|
|
121
|
+
- `dependencies`
|
|
122
|
+
- `optionalDependencies`
|
|
123
|
+
- `peerDependencies`
|
|
124
|
+
|
|
125
|
+
And the following dependency types will be bundled:
|
|
126
|
+
|
|
127
|
+
- `devDependencies`
|
|
128
|
+
|
|
129
|
+
```js
|
|
130
|
+
pluginDts({
|
|
131
|
+
autoExternal: {
|
|
132
|
+
dependencies: true,
|
|
133
|
+
optionalDependencies: true,
|
|
134
|
+
peerDependencies: true,
|
|
135
|
+
devDependencies: false,
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### banner
|
|
141
|
+
|
|
142
|
+
- **Type:** `string`
|
|
143
|
+
- **Default:** `undefined`
|
|
144
|
+
|
|
145
|
+
Inject content into the top of each DTS file.
|
|
146
|
+
|
|
147
|
+
```js
|
|
148
|
+
pluginDts({
|
|
149
|
+
banner: '/** @banner */',
|
|
150
|
+
});
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### footer
|
|
154
|
+
|
|
155
|
+
- **Type:** `string`
|
|
156
|
+
- **Default:** `undefined`
|
|
157
|
+
|
|
158
|
+
Inject content into the bottom of each DTS file.
|
|
159
|
+
|
|
160
|
+
```js
|
|
161
|
+
pluginDts({
|
|
162
|
+
footer: '/** @footer */',
|
|
163
|
+
});
|
|
164
|
+
```
|
|
12
165
|
|
|
13
166
|
## Contributing
|
|
14
167
|
|
|
@@ -16,4 +169,4 @@ Please read the [Contributing Guide](https://github.com/web-infra-dev/rslib/blob
|
|
|
16
169
|
|
|
17
170
|
## License
|
|
18
171
|
|
|
19
|
-
|
|
172
|
+
[MIT licensed](https://github.com/web-infra-dev/rslib/blob/main/LICENSE).
|
package/dist/apiExtractor.d.ts
CHANGED
package/dist/apiExtractor.js
CHANGED
|
@@ -4,14 +4,14 @@ import * as __WEBPACK_EXTERNAL_MODULE__rsbuild_core__ from "@rsbuild/core";
|
|
|
4
4
|
import * as __WEBPACK_EXTERNAL_MODULE_picocolors__ from "picocolors";
|
|
5
5
|
import * as __WEBPACK_EXTERNAL_MODULE__utils_js__ from "./utils.js";
|
|
6
6
|
async function bundleDts(options) {
|
|
7
|
-
const { name, cwd,
|
|
7
|
+
const { name, cwd, distPath, dtsExtension, banner, footer, dtsEntry = {
|
|
8
8
|
name: 'index',
|
|
9
9
|
path: 'index.d.ts'
|
|
10
10
|
}, tsconfigPath = 'tsconfig.json', bundledPackages = [] } = options;
|
|
11
11
|
try {
|
|
12
12
|
const start = Date.now();
|
|
13
|
-
const untrimmedFilePath = (0, __WEBPACK_EXTERNAL_MODULE_node_path__.join)(cwd, (0, __WEBPACK_EXTERNAL_MODULE_node_path__.relative)(cwd,
|
|
14
|
-
const mainEntryPointFilePath = dtsEntry.path;
|
|
13
|
+
const untrimmedFilePath = (0, __WEBPACK_EXTERNAL_MODULE_node_path__.join)(cwd, (0, __WEBPACK_EXTERNAL_MODULE_node_path__.relative)(cwd, distPath), `${dtsEntry.name}${dtsExtension}`);
|
|
14
|
+
const mainEntryPointFilePath = dtsEntry.path.replace(/\?.*$/, '');
|
|
15
15
|
const internalConfig = {
|
|
16
16
|
mainEntryPointFilePath,
|
|
17
17
|
bundledPackages,
|
package/dist/dts.js
CHANGED
|
@@ -56,7 +56,7 @@ const calcBundledPackages = (options)=>{
|
|
|
56
56
|
return Array.from(new Set(bundledPackages));
|
|
57
57
|
};
|
|
58
58
|
async function generateDts(data) {
|
|
59
|
-
const { bundle, distPath, dtsEntry, tsconfigPath, name, cwd, build, isWatch, dtsExtension = '.d.ts', autoExternal = true, userExternals, banner, footer } = data;
|
|
59
|
+
const { bundle, distPath, dtsEntry, tsconfigPath, name, cwd, build, isWatch, dtsExtension = '.d.ts', autoExternal = true, userExternals, banner, footer, rootDistPath } = data;
|
|
60
60
|
__WEBPACK_EXTERNAL_MODULE__rsbuild_core__.logger.start(`Generating DTS... ${__WEBPACK_EXTERNAL_MODULE_picocolors__["default"].gray(`(${name})`)}`);
|
|
61
61
|
const configPath = __WEBPACK_EXTERNAL_MODULE_typescript__["default"].findConfigFile(cwd, __WEBPACK_EXTERNAL_MODULE_typescript__["default"].sys.fileExists, tsconfigPath);
|
|
62
62
|
if (!configPath) {
|
|
@@ -68,27 +68,25 @@ async function generateDts(data) {
|
|
|
68
68
|
// If composite is set, the default is instead the directory containing the tsconfig.json file.
|
|
69
69
|
// see https://www.typescriptlang.org/tsconfig/#rootDir
|
|
70
70
|
const rootDir = rawCompilerOptions.rootDir ?? (rawCompilerOptions.composite ? (0, __WEBPACK_EXTERNAL_MODULE_node_path__.dirname)(configPath) : await (0, __WEBPACK_EXTERNAL_MODULE__utils_js__.calcLongestCommonPath)(fileNames.filter((fileName)=>!/\.d\.(ts|mts|cts)$/.test(fileName)))) ?? (0, __WEBPACK_EXTERNAL_MODULE_node_path__.dirname)(configPath);
|
|
71
|
-
const
|
|
71
|
+
const dtsEmitPath = distPath ?? rawCompilerOptions.declarationDir ?? rootDistPath;
|
|
72
|
+
const resolvedDtsEmitPath = (0, __WEBPACK_EXTERNAL_MODULE_node_path__.normalize)((0, __WEBPACK_EXTERNAL_MODULE_node_path__.resolve)((0, __WEBPACK_EXTERNAL_MODULE_node_path__.dirname)(configPath), dtsEmitPath));
|
|
72
73
|
if (build) {
|
|
73
74
|
// do not allow to use bundle DTS when 'build: true' since temp declarationDir should be set by user in tsconfig
|
|
74
75
|
if (bundle) throw Error('Can not set "dts.bundle: true" when "dts.build: true"');
|
|
75
76
|
// can not set '--declarationDir' or '--outDir' when 'build: true'.
|
|
76
|
-
if ((!rawCompilerOptions.outDir || (0, __WEBPACK_EXTERNAL_MODULE_node_path__.normalize)(rawCompilerOptions.outDir) !==
|
|
77
|
+
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)) {
|
|
77
78
|
const info = rawCompilerOptions.outDir && !rawCompilerOptions.declarationDir ? 'outDir' : 'declarationDir';
|
|
78
|
-
throw Error(`Please set ${info}: "${
|
|
79
|
+
throw Error(`Please set ${info}: "${dtsEmitPath}" in ${__WEBPACK_EXTERNAL_MODULE_picocolors__["default"].underline(configPath)} to keep it same as "dts.distPath" or "output.distPath.root" field in lib config.`);
|
|
79
80
|
}
|
|
80
81
|
}
|
|
81
|
-
const
|
|
82
|
-
if (bundle) return (0, __WEBPACK_EXTERNAL_MODULE__utils_js__.ensureTempDeclarationDir)(cwd);
|
|
83
|
-
return distPath ? distPath : rawCompilerOptions.declarationDir ?? './dist';
|
|
84
|
-
};
|
|
85
|
-
const declarationDir = getDeclarationDir(bundle, distPath);
|
|
82
|
+
const declarationDir = bundle ? (0, __WEBPACK_EXTERNAL_MODULE__utils_js__.ensureTempDeclarationDir)(cwd) : dtsEmitPath;
|
|
86
83
|
const { name: entryName, path: entryPath } = dtsEntry;
|
|
87
84
|
let entry = '';
|
|
88
85
|
if (true === bundle && entryPath) {
|
|
89
86
|
const entrySourcePath = (0, __WEBPACK_EXTERNAL_MODULE_node_path__.isAbsolute)(entryPath) ? entryPath : (0, __WEBPACK_EXTERNAL_MODULE_node_path__.join)(cwd, entryPath);
|
|
90
87
|
const relativePath = (0, __WEBPACK_EXTERNAL_MODULE_node_path__.relative)(rootDir, (0, __WEBPACK_EXTERNAL_MODULE_node_path__.dirname)(entrySourcePath));
|
|
91
|
-
entry = (0, __WEBPACK_EXTERNAL_MODULE_node_path__.join)(declarationDir, relativePath, (0, __WEBPACK_EXTERNAL_MODULE_node_path__.basename)(entrySourcePath))
|
|
88
|
+
entry = (0, __WEBPACK_EXTERNAL_MODULE_node_path__.join)(declarationDir, relativePath, (0, __WEBPACK_EXTERNAL_MODULE_node_path__.basename)(entrySourcePath)) // Remove query in file path, such as RSLIB_ENTRY_QUERY.
|
|
89
|
+
.replace(/\?.*$/, '').replace(/\.(js|mjs|jsx|ts|mts|tsx|cjs|cts|cjsx|ctsx|mjsx|mtsx)$/, '.d.ts');
|
|
92
90
|
}
|
|
93
91
|
const bundleDtsIfNeeded = async ()=>{
|
|
94
92
|
if (true === bundle) {
|
|
@@ -96,7 +94,7 @@ async function generateDts(data) {
|
|
|
96
94
|
await bundleDts({
|
|
97
95
|
name,
|
|
98
96
|
cwd,
|
|
99
|
-
|
|
97
|
+
distPath: dtsEmitPath,
|
|
100
98
|
dtsEntry: {
|
|
101
99
|
name: entryName,
|
|
102
100
|
path: entry
|
package/dist/index.d.ts
CHANGED
|
@@ -22,9 +22,10 @@ export type DtsGenOptions = PluginDtsOptions & {
|
|
|
22
22
|
cwd: string;
|
|
23
23
|
isWatch: boolean;
|
|
24
24
|
dtsEntry: DtsEntry;
|
|
25
|
+
rootDistPath: string;
|
|
25
26
|
build?: boolean;
|
|
26
27
|
tsconfigPath?: string;
|
|
27
28
|
userExternals?: NonNullable<RsbuildConfig['output']>['externals'];
|
|
28
29
|
};
|
|
29
30
|
export declare const PLUGIN_DTS_NAME = "rsbuild:dts";
|
|
30
|
-
export declare const pluginDts: (options
|
|
31
|
+
export declare const pluginDts: (options?: PluginDtsOptions) => RsbuildPlugin;
|
package/dist/index.js
CHANGED
|
@@ -9,7 +9,7 @@ const PLUGIN_DTS_NAME = 'rsbuild:dts';
|
|
|
9
9
|
// use ts compiler API to generate bundleless dts
|
|
10
10
|
// use ts compiler API and api-extractor to generate dts bundle
|
|
11
11
|
// TODO: deal alias in dts
|
|
12
|
-
const pluginDts = (options)=>({
|
|
12
|
+
const pluginDts = (options = {})=>({
|
|
13
13
|
name: PLUGIN_DTS_NAME,
|
|
14
14
|
setup (api) {
|
|
15
15
|
options.bundle = options.bundle ?? false;
|
|
@@ -20,7 +20,6 @@ const pluginDts = (options)=>({
|
|
|
20
20
|
api.onBeforeEnvironmentCompile(({ isWatch, isFirstCompile, environment })=>{
|
|
21
21
|
if (!isFirstCompile) return;
|
|
22
22
|
const { config } = environment;
|
|
23
|
-
options.distPath = options.distPath ?? config.output?.distPath?.root;
|
|
24
23
|
const jsExtension = (0, __WEBPACK_EXTERNAL_MODULE_node_path__.extname)(src_filename);
|
|
25
24
|
const childProcess = (0, __WEBPACK_EXTERNAL_MODULE_node_child_process__.fork)((0, __WEBPACK_EXTERNAL_MODULE_node_path__.join)(src_dirname, `./dts${jsExtension}`), [], {
|
|
26
25
|
stdio: 'inherit'
|
|
@@ -32,6 +31,7 @@ const pluginDts = (options)=>({
|
|
|
32
31
|
...options,
|
|
33
32
|
dtsEntry,
|
|
34
33
|
userExternals: config.output.externals,
|
|
34
|
+
rootDistPath: config.output?.distPath?.root,
|
|
35
35
|
tsconfigPath: config.source.tsconfigPath,
|
|
36
36
|
name: environment.name,
|
|
37
37
|
cwd: api.context.rootPath,
|
package/dist/tsc.js
CHANGED
|
@@ -2,6 +2,20 @@ 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
4
|
import * as __WEBPACK_EXTERNAL_MODULE__utils_js__ from "./utils.js";
|
|
5
|
+
async function handleDiagnosticsAndProcessFiles(diagnostics, configPath, host, bundle, declarationDir, dtsExtension, banner, footer, name) {
|
|
6
|
+
const diagnosticMessages = [];
|
|
7
|
+
for (const diagnostic of diagnostics){
|
|
8
|
+
const fileLoc = (0, __WEBPACK_EXTERNAL_MODULE__utils_js__.getFileLoc)(diagnostic, configPath);
|
|
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
|
+
diagnosticMessages.push(message);
|
|
11
|
+
}
|
|
12
|
+
await (0, __WEBPACK_EXTERNAL_MODULE__utils_js__.processDtsFiles)(bundle, declarationDir, dtsExtension, banner, footer);
|
|
13
|
+
if (diagnosticMessages.length) {
|
|
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
|
+
throw new Error('DTS generation failed');
|
|
17
|
+
}
|
|
18
|
+
}
|
|
5
19
|
async function emitDts(options, onComplete, bundle = false, isWatch = false, build = false) {
|
|
6
20
|
const start = Date.now();
|
|
7
21
|
const { configPath, declarationDir, name, dtsExtension, banner, footer } = options;
|
|
@@ -9,6 +23,7 @@ async function emitDts(options, onComplete, bundle = false, isWatch = false, bui
|
|
|
9
23
|
const { options: rawCompilerOptions, fileNames, projectReferences } = configFileParseResult;
|
|
10
24
|
const compilerOptions = {
|
|
11
25
|
...rawCompilerOptions,
|
|
26
|
+
configFilePath: configPath,
|
|
12
27
|
noEmit: false,
|
|
13
28
|
declaration: true,
|
|
14
29
|
declarationDir,
|
|
@@ -47,10 +62,11 @@ async function emitDts(options, onComplete, bundle = false, isWatch = false, bui
|
|
|
47
62
|
const system = {
|
|
48
63
|
...__WEBPACK_EXTERNAL_MODULE_typescript__["default"].sys
|
|
49
64
|
};
|
|
65
|
+
// build mode
|
|
50
66
|
if (isWatch) {
|
|
51
|
-
// watch mode
|
|
67
|
+
// watch mode, can also deal with incremental build
|
|
52
68
|
if (build) {
|
|
53
|
-
// incremental build with project references
|
|
69
|
+
// incremental build watcher with build project references
|
|
54
70
|
const host = __WEBPACK_EXTERNAL_MODULE_typescript__["default"].createSolutionBuilderWithWatchHost(system, createProgram, reportDiagnostic, void 0, reportWatchStatusChanged);
|
|
55
71
|
const solutionBuilder = __WEBPACK_EXTERNAL_MODULE_typescript__["default"].createSolutionBuilderWithWatch(host, [
|
|
56
72
|
configPath
|
|
@@ -63,22 +79,39 @@ async function emitDts(options, onComplete, bundle = false, isWatch = false, bui
|
|
|
63
79
|
__WEBPACK_EXTERNAL_MODULE_typescript__["default"].createWatchProgram(host);
|
|
64
80
|
}
|
|
65
81
|
} else {
|
|
66
|
-
// build
|
|
67
|
-
if (build) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
+
// normal build - npx tsc
|
|
83
|
+
if (build || compilerOptions.composite) {
|
|
84
|
+
if (!build && compilerOptions.composite) {
|
|
85
|
+
// incremental build with composite true - npx tsc
|
|
86
|
+
const host = __WEBPACK_EXTERNAL_MODULE_typescript__["default"].createIncrementalCompilerHost(compilerOptions);
|
|
87
|
+
const program = __WEBPACK_EXTERNAL_MODULE_typescript__["default"].createIncrementalProgram({
|
|
88
|
+
rootNames: fileNames,
|
|
89
|
+
options: compilerOptions,
|
|
90
|
+
configFileParsingDiagnostics: __WEBPACK_EXTERNAL_MODULE_typescript__["default"].getConfigFileParsingDiagnostics(configFileParseResult),
|
|
91
|
+
projectReferences,
|
|
92
|
+
host,
|
|
93
|
+
createProgram
|
|
94
|
+
});
|
|
95
|
+
program.emit();
|
|
96
|
+
const allDiagnostics = program.getSemanticDiagnostics().concat(program.getConfigFileParsingDiagnostics());
|
|
97
|
+
await handleDiagnosticsAndProcessFiles(allDiagnostics, configPath, host, bundle, declarationDir, dtsExtension, banner, footer, name);
|
|
98
|
+
} else {
|
|
99
|
+
// incremental build with build project references
|
|
100
|
+
// The equivalent of the `--build` flag for the tsc command.
|
|
101
|
+
let errorNumber = 0;
|
|
102
|
+
const reportErrorSummary = (errorCount)=>{
|
|
103
|
+
errorNumber = errorCount;
|
|
104
|
+
};
|
|
105
|
+
const host = __WEBPACK_EXTERNAL_MODULE_typescript__["default"].createSolutionBuilderHost(system, createProgram, reportDiagnostic, void 0, reportErrorSummary);
|
|
106
|
+
const solutionBuilder = __WEBPACK_EXTERNAL_MODULE_typescript__["default"].createSolutionBuilder(host, [
|
|
107
|
+
configPath
|
|
108
|
+
], compilerOptions);
|
|
109
|
+
solutionBuilder.build();
|
|
110
|
+
await (0, __WEBPACK_EXTERNAL_MODULE__utils_js__.processDtsFiles)(bundle, declarationDir, dtsExtension, banner, footer);
|
|
111
|
+
if (errorNumber > 0) {
|
|
112
|
+
__WEBPACK_EXTERNAL_MODULE__rsbuild_core__.logger.error(`Failed to emit declaration files. ${__WEBPACK_EXTERNAL_MODULE_picocolors__["default"].gray(`(${name})`)}`);
|
|
113
|
+
throw new Error('DTS generation failed');
|
|
114
|
+
}
|
|
82
115
|
}
|
|
83
116
|
} else {
|
|
84
117
|
const host = __WEBPACK_EXTERNAL_MODULE_typescript__["default"].createCompilerHost(compilerOptions);
|
|
@@ -91,18 +124,7 @@ async function emitDts(options, onComplete, bundle = false, isWatch = false, bui
|
|
|
91
124
|
});
|
|
92
125
|
const emitResult = program.emit();
|
|
93
126
|
const allDiagnostics = __WEBPACK_EXTERNAL_MODULE_typescript__["default"].getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
94
|
-
|
|
95
|
-
for (const diagnostic of allDiagnostics){
|
|
96
|
-
const fileLoc = (0, __WEBPACK_EXTERNAL_MODULE__utils_js__.getFileLoc)(diagnostic, configPath);
|
|
97
|
-
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())}`;
|
|
98
|
-
diagnosticMessages.push(message);
|
|
99
|
-
}
|
|
100
|
-
await (0, __WEBPACK_EXTERNAL_MODULE__utils_js__.processDtsFiles)(bundle, declarationDir, dtsExtension, banner, footer);
|
|
101
|
-
if (diagnosticMessages.length) {
|
|
102
|
-
__WEBPACK_EXTERNAL_MODULE__rsbuild_core__.logger.error(`Failed to emit declaration files. ${__WEBPACK_EXTERNAL_MODULE_picocolors__["default"].gray(`(${name})`)}`);
|
|
103
|
-
for (const message of diagnosticMessages)__WEBPACK_EXTERNAL_MODULE__rsbuild_core__.logger.error(message);
|
|
104
|
-
throw new Error('DTS generation failed');
|
|
105
|
-
}
|
|
127
|
+
await handleDiagnosticsAndProcessFiles(allDiagnostics, configPath, host, bundle, declarationDir, dtsExtension, banner, footer, name);
|
|
106
128
|
}
|
|
107
129
|
__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})`)}`);
|
|
108
130
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rsbuild-plugin-dts",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "Rsbuild plugin that supports emitting declaration files for TypeScript.",
|
|
5
5
|
"homepage": "https://lib.rsbuild.dev",
|
|
6
6
|
"bugs": {
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@microsoft/api-extractor": "^7.47.11",
|
|
34
|
-
"@rsbuild/core": "~1.1.
|
|
35
|
-
"rslib": "npm:@rslib/core@0.0.
|
|
34
|
+
"@rsbuild/core": "~1.1.4",
|
|
35
|
+
"rslib": "npm:@rslib/core@0.0.18",
|
|
36
36
|
"typescript": "^5.6.3",
|
|
37
37
|
"@rslib/tsconfig": "0.0.1"
|
|
38
38
|
},
|