rsbuild-plugin-dts 0.0.0 → 0.0.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.
@@ -1,4 +1,4 @@
1
- import type { DtsEntry } from './';
1
+ import type { DtsEntry } from 'src';
2
2
  export type BundleOptions = {
3
3
  name: string;
4
4
  cwd: string;
@@ -1,69 +1,72 @@
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";
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
+ import * as __WEBPACK_EXTERNAL_MODULE_picocolors__ from "picocolors";
5
+ import * as __WEBPACK_EXTERNAL_MODULE__utils_js__ from "./utils.js";
6
+
7
+ ;// CONCATENATED MODULE: external "node:path"
8
+
9
+ var external_node_path_namespaceObject = __WEBPACK_EXTERNAL_MODULE_node_path__;
10
+
11
+ ;// CONCATENATED MODULE: external "@microsoft/api-extractor"
12
+
13
+ var api_extractor_namespaceObject = __WEBPACK_EXTERNAL_MODULE__microsoft_api_extractor__;
14
+
15
+ ;// CONCATENATED MODULE: external "@rsbuild/core"
16
+
17
+ var core_namespaceObject = __WEBPACK_EXTERNAL_MODULE__rsbuild_core__;
18
+
19
+ ;// CONCATENATED MODULE: external "picocolors"
20
+
21
+ var external_picocolors_namespaceObject = __WEBPACK_EXTERNAL_MODULE_picocolors__;
22
+
23
+ ;// CONCATENATED MODULE: external "./utils.js"
24
+
25
+ var external_utils_js_namespaceObject = __WEBPACK_EXTERNAL_MODULE__utils_js__;
26
+
27
+ ;// CONCATENATED MODULE: ./src/apiExtractor.ts
28
+
29
+
30
+
31
+
32
+
15
33
  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})`)}`);
34
+ const { name, cwd, outDir, dtsExtension, dtsEntry = {
35
+ name: 'index',
36
+ path: 'index.d.ts'
37
+ }, tsconfigPath = 'tsconfig.json', bundledPackages = [] } = options;
38
+ try {
39
+ const start = Date.now();
40
+ const untrimmedFilePath = (0,external_node_path_namespaceObject.join)(cwd, (0,external_node_path_namespaceObject.relative)(cwd, outDir), `${dtsEntry.name}${dtsExtension}`);
41
+ const mainEntryPointFilePath = dtsEntry.path;
42
+ const internalConfig = {
43
+ mainEntryPointFilePath,
44
+ bundledPackages,
45
+ dtsRollup: {
46
+ enabled: true,
47
+ untrimmedFilePath
48
+ },
49
+ compiler: {
50
+ tsconfigFilePath: tsconfigPath.includes(cwd) ? tsconfigPath : (0,external_node_path_namespaceObject.join)(cwd, tsconfigPath)
51
+ },
52
+ projectFolder: cwd
53
+ };
54
+ const extractorConfig = api_extractor_namespaceObject.ExtractorConfig.prepare({
55
+ configObject: internalConfig,
56
+ configObjectFullPath: undefined,
57
+ packageJsonFullPath: (0,external_node_path_namespaceObject.join)(cwd, 'package.json')
58
+ });
59
+ const extractorResult = api_extractor_namespaceObject.Extractor.invoke(extractorConfig, {
60
+ localBuild: true
61
+ });
62
+ if (!extractorResult.succeeded) {
63
+ throw new Error(`API Extractor error. ${external_picocolors_namespaceObject["default"].gray(`(${name})`)}`);
64
+ }
65
+ core_namespaceObject.logger.info(`API Extractor bundle DTS succeeded: ${external_picocolors_namespaceObject["default"].cyan(untrimmedFilePath)} in ${(0,external_utils_js_namespaceObject.getTimeCost)(start)} ${external_picocolors_namespaceObject["default"].gray(`(${name})`)}`);
66
+ } catch (e) {
67
+ core_namespaceObject.logger.error('API Extractor Error');
68
+ throw new Error(`${e}`);
58
69
  }
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
70
  }
67
- export {
68
- bundleDts
69
- };
71
+
72
+ export { bundleDts };
package/dist/dts.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { DtsGenOptions } from './';
1
+ import type { DtsGenOptions } from 'src';
2
2
  export declare const calcBundledPackages: (options: {
3
3
  autoExternal: DtsGenOptions["autoExternal"];
4
4
  cwd: string;
package/dist/dts.js CHANGED
@@ -1,180 +1,215 @@
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);
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
+ import * as __WEBPACK_EXTERNAL_MODULE_picocolors__ from "picocolors";
5
+ import * as __WEBPACK_EXTERNAL_MODULE_typescript__ from "typescript";
6
+ import * as __WEBPACK_EXTERNAL_MODULE__tsc_js__ from "./tsc.js";
7
+ import * as __WEBPACK_EXTERNAL_MODULE__utils_js__ from "./utils.js";
8
+ var __webpack_modules__ = ({
9
+ "./apiExtractor": (function (module) {
10
+ module.exports = import("./apiExtractor.js");
11
+
12
+ }),
13
+
14
+ });
15
+ /************************************************************************/
16
+ // The module cache
17
+ var __webpack_module_cache__ = {};
18
+
19
+ // The require function
20
+ function __webpack_require__(moduleId) {
21
+
22
+ // Check if module is in cache
23
+ var cachedModule = __webpack_module_cache__[moduleId];
24
+ if (cachedModule !== undefined) {
25
+ return cachedModule.exports;
26
+ }
27
+ // Create a new module (and put it into the cache)
28
+ var module = (__webpack_module_cache__[moduleId] = {
29
+ exports: {}
30
+ });
31
+ // Execute the module function
32
+ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
33
+
34
+ // Return the exports of the module
35
+ return module.exports;
36
+
37
+ }
38
+
39
+ /************************************************************************/
40
+
41
+ ;// CONCATENATED MODULE: external "node:fs"
42
+
43
+ var external_node_fs_namespaceObject = __WEBPACK_EXTERNAL_MODULE_node_fs__;
44
+
45
+ ;// CONCATENATED MODULE: external "node:path"
46
+
47
+ var external_node_path_namespaceObject = __WEBPACK_EXTERNAL_MODULE_node_path__;
48
+
49
+ ;// CONCATENATED MODULE: external "@rsbuild/core"
50
+
51
+ var core_namespaceObject = __WEBPACK_EXTERNAL_MODULE__rsbuild_core__;
52
+
53
+ ;// CONCATENATED MODULE: external "picocolors"
54
+
55
+ var external_picocolors_namespaceObject = __WEBPACK_EXTERNAL_MODULE_picocolors__;
56
+
57
+ ;// CONCATENATED MODULE: external "typescript"
58
+
59
+ var external_typescript_namespaceObject = __WEBPACK_EXTERNAL_MODULE_typescript__;
60
+
61
+ ;// CONCATENATED MODULE: external "./tsc.js"
62
+
63
+ var external_tsc_js_namespaceObject = __WEBPACK_EXTERNAL_MODULE__tsc_js__;
64
+
65
+ ;// CONCATENATED MODULE: external "./utils.js"
66
+
67
+ var external_utils_js_namespaceObject = __WEBPACK_EXTERNAL_MODULE__utils_js__;
68
+
69
+ ;// CONCATENATED MODULE: ./src/dts.ts
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+ const isObject = (obj)=>Object.prototype.toString.call(obj) === '[object Object]';
78
+ // use !externals
79
+ const calcBundledPackages = (options)=>{
80
+ const { autoExternal, cwd, userExternals } = options;
81
+ let pkgJson;
82
+ try {
83
+ const content = external_node_fs_namespaceObject["default"].readFileSync((0,external_node_path_namespaceObject.join)(cwd, 'package.json'), 'utf-8');
84
+ pkgJson = JSON.parse(content);
85
+ } catch (err) {
86
+ core_namespaceObject.logger.warn('The type of third-party packages will not be bundled due to read package.json failed');
87
+ return [];
53
88
  }
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);
89
+ const externalOptions = autoExternal ? {
90
+ dependencies: true,
91
+ peerDependencies: true,
92
+ devDependencies: false,
93
+ ...autoExternal === true ? {} : autoExternal
94
+ } : {
95
+ dependencies: false,
96
+ peerDependencies: false,
97
+ devDependencies: false
98
+ };
99
+ // User externals should not bundled
100
+ // Only handle the case where the externals type is string / (string | RegExp)[] / plain object, function type is too complex.
101
+ const getUserExternalsKeys = (value)=>{
102
+ if (!value) {
103
+ return [];
104
+ }
105
+ if (typeof value === 'string' || value instanceof RegExp) {
106
+ return [
107
+ value
108
+ ];
109
+ }
110
+ if (Array.isArray(value)) {
111
+ return value.flatMap((v)=>getUserExternalsKeys(v));
112
+ }
113
+ if (isObject(userExternals)) {
114
+ return Object.keys(userExternals);
115
+ }
116
+ return [];
117
+ };
118
+ const externals = getUserExternalsKeys(userExternals);
119
+ const allDeps = [];
120
+ for (const type of [
121
+ 'dependencies',
122
+ 'peerDependencies',
123
+ 'devDependencies'
124
+ ]){
125
+ const deps = pkgJson[type] && Object.keys(pkgJson[type]);
126
+ if (deps) {
127
+ if (externalOptions[type]) {
128
+ externals.push(...deps);
129
+ }
130
+ allDeps.push(...deps);
131
+ }
69
132
  }
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));
133
+ const bundledPackages = allDeps.filter((d)=>!externals.some((e)=>typeof e === 'string' ? d === e : e.test(d)));
134
+ return Array.from(new Set(bundledPackages));
75
135
  };
76
136
  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);
137
+ const { bundle, distPath, dtsEntry, tsconfigPath, name, cwd, isWatch, dtsExtension = '.d.ts', autoExternal = true, userExternals } = data;
138
+ core_namespaceObject.logger.start(`Generating DTS... ${external_picocolors_namespaceObject["default"].gray(`(${name})`)}`);
139
+ const configPath = external_typescript_namespaceObject.findConfigFile(cwd, external_typescript_namespaceObject.sys.fileExists, tsconfigPath);
140
+ if (!configPath) {
141
+ core_namespaceObject.logger.error(`tsconfig.json not found in ${cwd}`);
142
+ throw new Error();
101
143
  }
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({
144
+ const { options: rawCompilerOptions, fileNames } = (0,external_utils_js_namespaceObject.loadTsconfig)(configPath);
145
+ const rootDir = rawCompilerOptions.rootDir ?? await (0,external_utils_js_namespaceObject.calcLongestCommonPath)(fileNames) ?? (0,external_node_path_namespaceObject.dirname)(configPath);
146
+ const outDir = distPath ? distPath : rawCompilerOptions.declarationDir || './dist';
147
+ const getDeclarationDir = (bundle, distPath)=>{
148
+ if (bundle) {
149
+ return (0,external_utils_js_namespaceObject.ensureTempDeclarationDir)(cwd);
150
+ }
151
+ return distPath ? distPath : rawCompilerOptions.declarationDir ?? './dist';
152
+ };
153
+ const declarationDir = getDeclarationDir(bundle, distPath);
154
+ const { name: entryName, path: entryPath } = dtsEntry;
155
+ let entry = '';
156
+ if (bundle === true && entryPath) {
157
+ const entrySourcePath = (0,external_node_path_namespaceObject.join)(cwd, entryPath);
158
+ const relativePath = (0,external_node_path_namespaceObject.relative)(rootDir, (0,external_node_path_namespaceObject.dirname)(entrySourcePath));
159
+ entry = (0,external_node_path_namespaceObject.join)(declarationDir, relativePath, (0,external_node_path_namespaceObject.basename)(entrySourcePath)).replace(/\.(js|mjs|jsx|ts|mts|tsx|cjs|cts|cjsx|ctsx|mjsx|mtsx)$/, '.d.ts');
160
+ }
161
+ const bundleDtsIfNeeded = async ()=>{
162
+ if (bundle === true) {
163
+ const { bundleDts } = await Promise.resolve(/* import() */ ).then(__webpack_require__.bind(__webpack_require__, "./apiExtractor"));
164
+ await bundleDts({
165
+ name,
166
+ cwd,
167
+ outDir,
168
+ dtsEntry: {
169
+ name: entryName,
170
+ path: entry
171
+ },
172
+ tsconfigPath,
173
+ dtsExtension,
174
+ bundledPackages: calcBundledPackages({
175
+ autoExternal,
176
+ cwd,
177
+ userExternals
178
+ })
179
+ });
180
+ }
181
+ };
182
+ const onComplete = async (isSuccess)=>{
183
+ if (isSuccess) {
184
+ await bundleDtsIfNeeded();
185
+ }
186
+ };
187
+ await (0,external_tsc_js_namespaceObject.emitDts)({
123
188
  name,
124
189
  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();
190
+ configPath,
191
+ declarationDir,
192
+ dtsExtension
193
+ }, onComplete, bundle, isWatch);
194
+ if (!isWatch) {
195
+ await bundleDtsIfNeeded();
143
196
  }
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
197
  }
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
- }
198
+ process.on('message', async (data)=>{
199
+ if (!data.cwd) {
200
+ return;
201
+ }
202
+ try {
203
+ await generateDts(data);
204
+ } catch (e) {
205
+ core_namespaceObject.logger.error(e);
206
+ process.send('error');
207
+ process.exit(1);
208
+ }
209
+ process.send('success');
210
+ if (!data.isWatch) {
211
+ process.exit();
212
+ }
176
213
  });
177
- export {
178
- calcBundledPackages,
179
- generateDts
180
- };
214
+
215
+ export { calcBundledPackages, generateDts };