vite-plugin-dts 2.0.0-beta.1 → 2.0.0-beta.3
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 +15 -17
- package/README.zh-CN.md +12 -14
- package/dist/index.cjs +163 -136
- package/dist/index.d.ts +12 -14
- package/dist/index.mjs +163 -136
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -112,6 +112,7 @@ This is a exist issue when TypeScript inferring types from packages which under
|
|
|
112
112
|
|
|
113
113
|
```ts
|
|
114
114
|
import type { ts, Diagnostic } from 'ts-morph'
|
|
115
|
+
import type { LogLevel } from 'vite'
|
|
115
116
|
|
|
116
117
|
interface TransformWriteFile {
|
|
117
118
|
filePath?: string
|
|
@@ -122,7 +123,7 @@ export interface PluginOptions {
|
|
|
122
123
|
/**
|
|
123
124
|
* Depends on the root directory
|
|
124
125
|
*
|
|
125
|
-
*
|
|
126
|
+
* By Default it base on 'root' option of your vite config
|
|
126
127
|
*/
|
|
127
128
|
root?: string
|
|
128
129
|
|
|
@@ -131,16 +132,16 @@ export interface PluginOptions {
|
|
|
131
132
|
*
|
|
132
133
|
* Can be specified a array to output to multiple directories
|
|
133
134
|
*
|
|
134
|
-
*
|
|
135
|
+
* By Default it base on 'build.outDir' option of your vite config
|
|
135
136
|
*/
|
|
136
137
|
outputDir?: string | string[]
|
|
137
138
|
|
|
138
139
|
/**
|
|
139
140
|
* Manually set the root path of the entry files
|
|
140
141
|
*
|
|
141
|
-
* The output path of each file will be
|
|
142
|
+
* The output path of each file will be calculated base on it
|
|
142
143
|
*
|
|
143
|
-
*
|
|
144
|
+
* By Default it is the smallest public path for all files
|
|
144
145
|
*/
|
|
145
146
|
entryRoot?: string
|
|
146
147
|
|
|
@@ -154,7 +155,7 @@ export interface PluginOptions {
|
|
|
154
155
|
/**
|
|
155
156
|
* Project init tsconfig.json file path by ts-morph
|
|
156
157
|
*
|
|
157
|
-
* Plugin also resolve
|
|
158
|
+
* Plugin also resolve include and exclude files from tsconfig.json
|
|
158
159
|
*
|
|
159
160
|
* @default 'tsconfig.json'
|
|
160
161
|
*/
|
|
@@ -190,14 +191,14 @@ export interface PluginOptions {
|
|
|
190
191
|
/**
|
|
191
192
|
* Manual set include glob
|
|
192
193
|
*
|
|
193
|
-
*
|
|
194
|
+
* By Default it base on 'include' option of the tsconfig.json
|
|
194
195
|
*/
|
|
195
196
|
include?: string | string[]
|
|
196
197
|
|
|
197
198
|
/**
|
|
198
199
|
* Manual set exclude glob
|
|
199
200
|
*
|
|
200
|
-
*
|
|
201
|
+
* By Default it base on 'exclude' option of the tsconfig.json, be 'node_module/**' when empty
|
|
201
202
|
*/
|
|
202
203
|
exclude?: string | string[]
|
|
203
204
|
|
|
@@ -253,16 +254,6 @@ export interface PluginOptions {
|
|
|
253
254
|
*/
|
|
254
255
|
skipDiagnostics?: boolean
|
|
255
256
|
|
|
256
|
-
/**
|
|
257
|
-
* Whether log diagnostic informations
|
|
258
|
-
*
|
|
259
|
-
* Not effective when `skipDiagnostics` is true
|
|
260
|
-
*
|
|
261
|
-
* @deprecated
|
|
262
|
-
* @default false
|
|
263
|
-
*/
|
|
264
|
-
logDiagnostics?: boolean
|
|
265
|
-
|
|
266
257
|
/**
|
|
267
258
|
* Customize typescript lib folder path
|
|
268
259
|
*
|
|
@@ -272,6 +263,13 @@ export interface PluginOptions {
|
|
|
272
263
|
*/
|
|
273
264
|
libFolderPath?: string
|
|
274
265
|
|
|
266
|
+
/**
|
|
267
|
+
* Specify the log level of plugin
|
|
268
|
+
*
|
|
269
|
+
* By Default it base on 'logLevel' option of your vite config
|
|
270
|
+
*/
|
|
271
|
+
logLevel?: LogLevel
|
|
272
|
+
|
|
275
273
|
/**
|
|
276
274
|
* After emit diagnostic hook
|
|
277
275
|
*
|
package/README.zh-CN.md
CHANGED
|
@@ -111,6 +111,7 @@ const props = defineProps<{
|
|
|
111
111
|
|
|
112
112
|
```ts
|
|
113
113
|
import type { ts, Diagnostic } from 'ts-morph'
|
|
114
|
+
import type { LogLevel } from 'vite'
|
|
114
115
|
|
|
115
116
|
interface TransformWriteFile {
|
|
116
117
|
filePath?: string
|
|
@@ -121,7 +122,7 @@ export interface PluginOptions {
|
|
|
121
122
|
/**
|
|
122
123
|
* 执行的根目录
|
|
123
124
|
*
|
|
124
|
-
* 默认基于 vite 配置的 root 选项
|
|
125
|
+
* 默认基于 vite 配置的 'root' 选项
|
|
125
126
|
*/
|
|
126
127
|
root?: string
|
|
127
128
|
|
|
@@ -130,7 +131,7 @@ export interface PluginOptions {
|
|
|
130
131
|
*
|
|
131
132
|
* 可以指定一个数组来输出到多个目录中
|
|
132
133
|
*
|
|
133
|
-
* 默认基于 vite
|
|
134
|
+
* 默认基于 vite 配置的 'build.outDir' 选项
|
|
134
135
|
*/
|
|
135
136
|
outputDir?: string | string[]
|
|
136
137
|
|
|
@@ -189,14 +190,14 @@ export interface PluginOptions {
|
|
|
189
190
|
/**
|
|
190
191
|
* 手动设置包含路径的 glob
|
|
191
192
|
*
|
|
192
|
-
* 默认基于 tsconfig.json 的 include 选项
|
|
193
|
+
* 默认基于 tsconfig.json 的 'include' 选项
|
|
193
194
|
*/
|
|
194
195
|
include?: string | string[]
|
|
195
196
|
|
|
196
197
|
/**
|
|
197
198
|
* 手动设置排除路径的 glob
|
|
198
199
|
*
|
|
199
|
-
* 默认基于 tsconfig.json 的 exclude 选线,未设置时为 'node_module/**'
|
|
200
|
+
* 默认基于 tsconfig.json 的 'exclude' 选线,未设置时为 'node_module/**'
|
|
200
201
|
*/
|
|
201
202
|
exclude?: string | string[]
|
|
202
203
|
|
|
@@ -252,16 +253,6 @@ export interface PluginOptions {
|
|
|
252
253
|
*/
|
|
253
254
|
skipDiagnostics?: boolean
|
|
254
255
|
|
|
255
|
-
/**
|
|
256
|
-
* 是否打印类型诊断信息
|
|
257
|
-
*
|
|
258
|
-
* 当跳过类型诊断时该属性将不会生效
|
|
259
|
-
*
|
|
260
|
-
* @deprecated
|
|
261
|
-
* @default false
|
|
262
|
-
*/
|
|
263
|
-
logDiagnostics?: boolean
|
|
264
|
-
|
|
265
256
|
/**
|
|
266
257
|
* 定制 typescript 的 lib 文件夹路径
|
|
267
258
|
*
|
|
@@ -271,6 +262,13 @@ export interface PluginOptions {
|
|
|
271
262
|
*/
|
|
272
263
|
libFolderPath?: string
|
|
273
264
|
|
|
265
|
+
/**
|
|
266
|
+
* 指定插件的输出等级
|
|
267
|
+
*
|
|
268
|
+
* 默认基于 vite 配置的 'logLevel' 选项
|
|
269
|
+
*/
|
|
270
|
+
logLevel?: LogLevel
|
|
271
|
+
|
|
274
272
|
/**
|
|
275
273
|
* 获取诊断信息后的钩子
|
|
276
274
|
*
|
package/dist/index.cjs
CHANGED
|
@@ -360,62 +360,90 @@ function preprocessVueCode(code, setupScript) {
|
|
|
360
360
|
}
|
|
361
361
|
}
|
|
362
362
|
}
|
|
363
|
+
const declRecord = /* @__PURE__ */ new Map();
|
|
364
|
+
let defaultExport;
|
|
365
|
+
let options;
|
|
363
366
|
for (const node of scriptAst) {
|
|
367
|
+
if (node.type === "VariableDeclaration") {
|
|
368
|
+
for (const decl of node.declarations) {
|
|
369
|
+
if (decl.id.type === "Identifier" && decl.init) {
|
|
370
|
+
let properties;
|
|
371
|
+
if (decl.init.type === "ObjectExpression") {
|
|
372
|
+
properties = decl.init.properties;
|
|
373
|
+
} else if (decl.init.type === "CallExpression" && decl.init.arguments[0].type === "ObjectExpression") {
|
|
374
|
+
properties = decl.init.arguments[0].properties;
|
|
375
|
+
}
|
|
376
|
+
if (!properties)
|
|
377
|
+
continue;
|
|
378
|
+
if (defaultExport && decl.id.name === defaultExport) {
|
|
379
|
+
options = properties;
|
|
380
|
+
break;
|
|
381
|
+
} else {
|
|
382
|
+
declRecord.set(decl.id.name, properties);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
364
387
|
if (node.type === "ExportDefaultDeclaration") {
|
|
365
|
-
let options;
|
|
366
388
|
if (node.declaration.type === "ObjectExpression") {
|
|
367
389
|
options = node.declaration.properties;
|
|
368
390
|
} else if (node.declaration.type === "CallExpression" && node.declaration.arguments[0].type === "ObjectExpression") {
|
|
369
391
|
options = node.declaration.arguments[0].properties;
|
|
392
|
+
} else if (node.declaration.type === "Identifier") {
|
|
393
|
+
if (declRecord.has(node.declaration.name)) {
|
|
394
|
+
options = declRecord.get(node.declaration.name);
|
|
395
|
+
} else {
|
|
396
|
+
defaultExport = node.declaration.name;
|
|
397
|
+
}
|
|
370
398
|
}
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
399
|
+
}
|
|
400
|
+
if (options) {
|
|
401
|
+
for (const option of options) {
|
|
402
|
+
if (propsTypeName && option.type === "ObjectProperty" && option.key.type === "Identifier" && option.key.name === "props" && option.value.type === "ObjectExpression") {
|
|
403
|
+
for (const prop of option.value.properties) {
|
|
404
|
+
if (prop.type === "ObjectProperty" && prop.key.type === "Identifier") {
|
|
405
|
+
if (prop.value.type === "ObjectExpression") {
|
|
406
|
+
for (const propDef of prop.value.properties) {
|
|
407
|
+
if (propDef.type === "ObjectProperty" && propDef.key.type === "Identifier" && propDef.key.name === "type") {
|
|
408
|
+
source.prependLeft(
|
|
409
|
+
propDef.end,
|
|
410
|
+
` as unknown as __PropType<${propsTypeName}['${prop.key.name}']>`
|
|
411
|
+
);
|
|
384
412
|
}
|
|
385
|
-
} else {
|
|
386
|
-
source.prependLeft(
|
|
387
|
-
prop.end,
|
|
388
|
-
` as unknown as __PropType<${propsTypeName}['${prop.key.name}']>`
|
|
389
|
-
);
|
|
390
413
|
}
|
|
414
|
+
} else {
|
|
415
|
+
source.prependLeft(
|
|
416
|
+
prop.end,
|
|
417
|
+
` as unknown as __PropType<${propsTypeName}['${prop.key.name}']>`
|
|
418
|
+
);
|
|
391
419
|
}
|
|
392
420
|
}
|
|
393
421
|
}
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
422
|
+
}
|
|
423
|
+
if (option.type === "ObjectProperty" && option.key.type === "Identifier" && option.key.name === "components") {
|
|
424
|
+
source.remove(option.start, option.end);
|
|
425
|
+
}
|
|
426
|
+
if (option.type === "ObjectMethod" && option.key.type === "Identifier" && option.key.name === "setup") {
|
|
427
|
+
let exposed;
|
|
428
|
+
let returned;
|
|
429
|
+
for (const node2 of option.body.body) {
|
|
430
|
+
if (!exposed && node2.type === "ExpressionStatement" && node2.expression.type === "CallExpression" && node2.expression.callee.type === "Identifier" && node2.expression.callee.name === "expose") {
|
|
431
|
+
exposed = node2.expression.arguments[0];
|
|
432
|
+
continue;
|
|
433
|
+
}
|
|
434
|
+
if (node2.type === "ReturnStatement") {
|
|
435
|
+
returned = node2;
|
|
436
|
+
break;
|
|
409
437
|
}
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
438
|
+
}
|
|
439
|
+
const newReturned = exposed && exposed.type === "ObjectExpression" ? `return ${code.substring(exposed.start, exposed.end)}` : setupScript ? "return {}" : "";
|
|
440
|
+
if (newReturned) {
|
|
441
|
+
if (returned) {
|
|
442
|
+
source.overwrite(returned.start, returned.end, newReturned);
|
|
443
|
+
} else if (option.body.body.length) {
|
|
444
|
+
source.appendRight(option.body.body.at(-1).end, `
|
|
416
445
|
${newReturned}
|
|
417
446
|
`);
|
|
418
|
-
}
|
|
419
447
|
}
|
|
420
448
|
}
|
|
421
449
|
}
|
|
@@ -475,10 +503,11 @@ function compileVueCode(code) {
|
|
|
475
503
|
const dtsRE$1 = /\.d\.tsx?$/;
|
|
476
504
|
function rollupDeclarationFiles({
|
|
477
505
|
root,
|
|
506
|
+
compilerOptions,
|
|
478
507
|
outputDir,
|
|
479
508
|
entryPath,
|
|
480
509
|
fileName,
|
|
481
|
-
|
|
510
|
+
libFolder
|
|
482
511
|
}) {
|
|
483
512
|
const configObjectFullPath = node_path.resolve(root, "api-extractor.json");
|
|
484
513
|
const packageJsonLookup = new nodeCoreLibrary.PackageJsonLookup();
|
|
@@ -527,8 +556,9 @@ function rollupDeclarationFiles({
|
|
|
527
556
|
packageJsonFullPath
|
|
528
557
|
});
|
|
529
558
|
const compilerState = apiExtractor.CompilerState.create(extractorConfig, {
|
|
530
|
-
localBuild:
|
|
531
|
-
showVerboseMessages: false
|
|
559
|
+
localBuild: false,
|
|
560
|
+
showVerboseMessages: false,
|
|
561
|
+
typescriptCompilerFolder: libFolder ? node_path.resolve(libFolder, "..") : void 0
|
|
532
562
|
});
|
|
533
563
|
const sourceMapper = new SourceMapper_js.SourceMapper();
|
|
534
564
|
const messageRouter = new MessageRouter_js.MessageRouter({
|
|
@@ -569,6 +599,7 @@ const defaultIndex = "index.d.ts";
|
|
|
569
599
|
const noop = () => {
|
|
570
600
|
};
|
|
571
601
|
const extPrefix = (file) => mtjsRE.test(file) ? "m" : ctjsRE.test(file) ? "c" : "";
|
|
602
|
+
const resolve = (...paths) => vite.normalizePath(node_path.resolve(...paths));
|
|
572
603
|
const logPrefix = kolorist.cyan("[vite:dts]");
|
|
573
604
|
const bundleDebug = debug("vite-plugin-dts:bundle");
|
|
574
605
|
function dtsPlugin(options = {}) {
|
|
@@ -582,9 +613,8 @@ function dtsPlugin(options = {}) {
|
|
|
582
613
|
rollupTypes = false,
|
|
583
614
|
noEmitOnError = false,
|
|
584
615
|
skipDiagnostics = false,
|
|
585
|
-
logDiagnostics = void 0,
|
|
586
616
|
copyDtsFiles = true,
|
|
587
|
-
|
|
617
|
+
logLevel = void 0,
|
|
588
618
|
afterDiagnostic = noop,
|
|
589
619
|
beforeWriteFile = noop,
|
|
590
620
|
afterBuild = noop
|
|
@@ -604,10 +634,39 @@ function dtsPlugin(options = {}) {
|
|
|
604
634
|
let include;
|
|
605
635
|
let exclude;
|
|
606
636
|
let filter;
|
|
637
|
+
let libFolderPath = options.libFolderPath;
|
|
607
638
|
const sourceDtsFiles = /* @__PURE__ */ new Set();
|
|
639
|
+
const emittedFiles = /* @__PURE__ */ new Map();
|
|
608
640
|
let hasJsVue = false;
|
|
609
641
|
let allowJs = false;
|
|
610
642
|
let transformError = false;
|
|
643
|
+
function internalTransform(code, id) {
|
|
644
|
+
if (!project || !filter(id)) {
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
if (vueRE.test(id)) {
|
|
648
|
+
const { error, content, ext } = compileVueCode(code);
|
|
649
|
+
if (!transformError && error) {
|
|
650
|
+
logger.error(
|
|
651
|
+
kolorist.red(
|
|
652
|
+
`
|
|
653
|
+
${kolorist.cyan(
|
|
654
|
+
"[vite:dts]"
|
|
655
|
+
)} A error occurred when transform code, maybe there are some inertnal bugs.
|
|
656
|
+
`
|
|
657
|
+
)
|
|
658
|
+
);
|
|
659
|
+
transformError = true;
|
|
660
|
+
}
|
|
661
|
+
if (content) {
|
|
662
|
+
if (ext === "js" || ext === "jsx")
|
|
663
|
+
hasJsVue = true;
|
|
664
|
+
project.createSourceFile(`${id}.${ext || "js"}`, content, { overwrite: true });
|
|
665
|
+
}
|
|
666
|
+
} else if (!id.includes(".vue?vue") && (tsRE.test(id) || allowJs && jsRE.test(id))) {
|
|
667
|
+
project.createSourceFile(id, code, { overwrite: true });
|
|
668
|
+
}
|
|
669
|
+
}
|
|
611
670
|
return {
|
|
612
671
|
name: "vite:dts",
|
|
613
672
|
apply: "build",
|
|
@@ -634,18 +693,7 @@ function dtsPlugin(options = {}) {
|
|
|
634
693
|
configResolved(config) {
|
|
635
694
|
if (isBundle)
|
|
636
695
|
return;
|
|
637
|
-
logger = config.logger;
|
|
638
|
-
if (logDiagnostics != null) {
|
|
639
|
-
logger.warn(
|
|
640
|
-
kolorist.yellow(
|
|
641
|
-
`
|
|
642
|
-
${kolorist.cyan(
|
|
643
|
-
"[vite:dts]"
|
|
644
|
-
)} 'logDiagnostics' has been deprecated, the original feature now following 'skipDiagnostics'.
|
|
645
|
-
`
|
|
646
|
-
)
|
|
647
|
-
);
|
|
648
|
-
}
|
|
696
|
+
logger = logLevel ? vite.createLogger(logLevel, { allowClearScreen: config.clearScreen }) : config.logger;
|
|
649
697
|
if (!config.build.lib) {
|
|
650
698
|
logger.warn(
|
|
651
699
|
kolorist.yellow(
|
|
@@ -669,6 +717,7 @@ ${kolorist.cyan(
|
|
|
669
717
|
}
|
|
670
718
|
root = ensureAbsolute(options.root ?? "", config.root);
|
|
671
719
|
tsConfigPath = ensureAbsolute(tsConfigFilePath, root);
|
|
720
|
+
libFolderPath = libFolderPath && ensureAbsolute(libFolderPath, root);
|
|
672
721
|
outputDirs = options.outputDir ? ensureArray(options.outputDir).map((d) => ensureAbsolute(d, root)) : [ensureAbsolute(config.build.outDir, root)];
|
|
673
722
|
if (!outputDirs[0]) {
|
|
674
723
|
logger.error(
|
|
@@ -687,7 +736,7 @@ ${kolorist.cyan(
|
|
|
687
736
|
compilerOptions: mergeObjects(compilerOptions, {
|
|
688
737
|
rootDir: compilerOptions.rootDir || root,
|
|
689
738
|
noEmitOnError,
|
|
690
|
-
outDir:
|
|
739
|
+
outDir: outputDirs[0],
|
|
691
740
|
declarationDir: void 0,
|
|
692
741
|
noUnusedParameters: false,
|
|
693
742
|
declaration: true,
|
|
@@ -697,7 +746,7 @@ ${kolorist.cyan(
|
|
|
697
746
|
}),
|
|
698
747
|
tsConfigFilePath: tsConfigPath,
|
|
699
748
|
skipAddingFilesFromTsConfig: true,
|
|
700
|
-
libFolderPath
|
|
749
|
+
libFolderPath
|
|
701
750
|
});
|
|
702
751
|
allowJs = project.getCompilerOptions().allowJs ?? false;
|
|
703
752
|
const tsConfig = { compilerOptions: {} };
|
|
@@ -732,39 +781,16 @@ ${kolorist.cyan(
|
|
|
732
781
|
}
|
|
733
782
|
},
|
|
734
783
|
transform(code, id) {
|
|
735
|
-
|
|
736
|
-
return null;
|
|
737
|
-
}
|
|
738
|
-
if (vueRE.test(id)) {
|
|
739
|
-
const { error, content, ext } = compileVueCode(code);
|
|
740
|
-
if (!transformError && error) {
|
|
741
|
-
logger.error(
|
|
742
|
-
kolorist.red(
|
|
743
|
-
`
|
|
744
|
-
${kolorist.cyan(
|
|
745
|
-
"[vite:dts]"
|
|
746
|
-
)} A error occurred when transform code, maybe there are some inertnal bugs.
|
|
747
|
-
`
|
|
748
|
-
)
|
|
749
|
-
);
|
|
750
|
-
transformError = true;
|
|
751
|
-
}
|
|
752
|
-
if (content) {
|
|
753
|
-
if (ext === "js" || ext === "jsx")
|
|
754
|
-
hasJsVue = true;
|
|
755
|
-
project.createSourceFile(`${id}.${ext || "js"}`, content, { overwrite: true });
|
|
756
|
-
}
|
|
757
|
-
} else if (!id.includes(".vue?vue") && (tsRE.test(id) || allowJs && jsRE.test(id))) {
|
|
758
|
-
project.createSourceFile(id, code, { overwrite: true });
|
|
759
|
-
}
|
|
784
|
+
internalTransform(code, id);
|
|
760
785
|
return null;
|
|
761
786
|
},
|
|
762
|
-
watchChange(id) {
|
|
787
|
+
async watchChange(id) {
|
|
763
788
|
if (watchExtensionRE.test(id)) {
|
|
764
789
|
isBundle = false;
|
|
765
790
|
if (project) {
|
|
766
791
|
const sourceFile = project.getSourceFile(vite.normalizePath(id));
|
|
767
792
|
sourceFile && project.removeSourceFile(sourceFile);
|
|
793
|
+
internalTransform(await fs.readFile(id, "utf-8"), id);
|
|
768
794
|
}
|
|
769
795
|
}
|
|
770
796
|
},
|
|
@@ -776,6 +802,7 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
776
802
|
bundleDebug("start");
|
|
777
803
|
isBundle = true;
|
|
778
804
|
sourceDtsFiles.clear();
|
|
805
|
+
emittedFiles.clear();
|
|
779
806
|
const startTime = Date.now();
|
|
780
807
|
const includedFileSet = /* @__PURE__ */ new Set();
|
|
781
808
|
if (include && include.length) {
|
|
@@ -822,6 +849,7 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
822
849
|
}
|
|
823
850
|
bundleDebug("diagnostics");
|
|
824
851
|
}
|
|
852
|
+
const outputDir = outputDirs[0];
|
|
825
853
|
const dtsOutputFiles = Array.from(sourceDtsFiles).map((sourceFile) => ({
|
|
826
854
|
path: sourceFile.getFilePath(),
|
|
827
855
|
content: sourceFile.getFullText()
|
|
@@ -829,17 +857,13 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
829
857
|
const service = project.getLanguageService();
|
|
830
858
|
const outputFiles = project.getSourceFiles().map(
|
|
831
859
|
(sourceFile) => service.getEmitOutput(sourceFile, true).getOutputFiles().map((outputFile) => ({
|
|
832
|
-
path:
|
|
860
|
+
path: resolve(root, node_path.relative(outputDir, outputFile.compilerObject.name)),
|
|
833
861
|
content: outputFile.getText()
|
|
834
862
|
}))
|
|
835
863
|
).flat().concat(dtsOutputFiles);
|
|
836
864
|
bundleDebug("emit");
|
|
837
|
-
|
|
838
|
-
entryRoot = queryPublicPath(outputFiles.map((file) => file.path));
|
|
839
|
-
}
|
|
865
|
+
entryRoot = entryRoot || queryPublicPath(outputFiles.map((file) => file.path));
|
|
840
866
|
entryRoot = ensureAbsolute(entryRoot, root);
|
|
841
|
-
const wroteFiles = /* @__PURE__ */ new Set();
|
|
842
|
-
const outputDir = outputDirs[0];
|
|
843
867
|
await runParallel(os.cpus().length, outputFiles, async (outputFile) => {
|
|
844
868
|
let filePath = outputFile.path;
|
|
845
869
|
let content = outputFile.content;
|
|
@@ -852,40 +876,46 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
852
876
|
content = transformAliasImport(filePath, content, aliases, aliasesExclude);
|
|
853
877
|
content = staticImport || rollupTypes ? transformDynamicImport(content) : content;
|
|
854
878
|
}
|
|
855
|
-
filePath =
|
|
879
|
+
filePath = resolve(
|
|
856
880
|
outputDir,
|
|
857
881
|
node_path.relative(entryRoot, cleanVueFileName ? filePath.replace(".vue.d.ts", ".d.ts") : filePath)
|
|
858
882
|
);
|
|
883
|
+
content = cleanVueFileName ? content.replace(/['"](.+)\.vue['"]/g, '"$1"') : content;
|
|
859
884
|
if (typeof beforeWriteFile === "function") {
|
|
860
885
|
const result = beforeWriteFile(filePath, content);
|
|
861
886
|
if (result === false)
|
|
862
887
|
return;
|
|
863
888
|
if (result && isNativeObj(result)) {
|
|
864
|
-
filePath = result.filePath
|
|
889
|
+
filePath = result.filePath || filePath;
|
|
865
890
|
content = result.content ?? content;
|
|
866
891
|
}
|
|
867
892
|
}
|
|
893
|
+
filePath = vite.normalizePath(filePath);
|
|
868
894
|
await fs.mkdir(node_path.dirname(filePath), { recursive: true });
|
|
869
|
-
await fs.writeFile(
|
|
870
|
-
|
|
871
|
-
cleanVueFileName ? content.replace(/['"](.+)\.vue['"]/g, '"$1"') : content,
|
|
872
|
-
"utf-8"
|
|
873
|
-
);
|
|
874
|
-
wroteFiles.add(vite.normalizePath(filePath));
|
|
895
|
+
await fs.writeFile(filePath, content, "utf-8");
|
|
896
|
+
emittedFiles.set(filePath, content);
|
|
875
897
|
});
|
|
876
898
|
bundleDebug("output");
|
|
899
|
+
if (copyDtsFiles) {
|
|
900
|
+
await runParallel(os.cpus().length, dtsOutputFiles, async ({ path, content }) => {
|
|
901
|
+
const filePath = resolve(outputDir, node_path.basename(path));
|
|
902
|
+
await fs.writeFile(filePath, content, "utf-8");
|
|
903
|
+
emittedFiles.set(filePath, content);
|
|
904
|
+
});
|
|
905
|
+
}
|
|
906
|
+
bundleDebug("copy dts");
|
|
877
907
|
if (insertTypesEntry || rollupTypes) {
|
|
878
|
-
const pkgPath =
|
|
908
|
+
const pkgPath = resolve(root, "package.json");
|
|
879
909
|
const pkg = fs.existsSync(pkgPath) ? JSON.parse(await fs.readFile(pkgPath, "utf-8")) : {};
|
|
880
910
|
const entryNames = Object.keys(entries);
|
|
881
|
-
const types = pkg.types || pkg.typings || pkg.publishConfig?.types || pkg.publishConfig?.typings;
|
|
911
|
+
const types = pkg.types || pkg.typings || pkg.publishConfig?.types || pkg.publishConfig?.typings || (pkg.exports?.["."] || pkg.exports?.["./"])?.types;
|
|
882
912
|
const multiple = entryNames.length > 1;
|
|
883
|
-
const typesPath = types ?
|
|
913
|
+
const typesPath = types ? resolve(root, types) : resolve(outputDir, indexName);
|
|
884
914
|
for (const name of entryNames) {
|
|
885
|
-
let filePath = multiple ?
|
|
915
|
+
let filePath = multiple ? resolve(outputDir, `${name.replace(tsRE, "")}.d.ts`) : typesPath;
|
|
886
916
|
if (fs.existsSync(filePath))
|
|
887
917
|
continue;
|
|
888
|
-
const index =
|
|
918
|
+
const index = resolve(
|
|
889
919
|
outputDir,
|
|
890
920
|
node_path.relative(entryRoot, `${entries[name].replace(tsRE, "")}.d.ts`)
|
|
891
921
|
);
|
|
@@ -910,9 +940,10 @@ export default ${libName}
|
|
|
910
940
|
content = result.content ?? content;
|
|
911
941
|
}
|
|
912
942
|
}
|
|
943
|
+
filePath = vite.normalizePath(filePath);
|
|
913
944
|
if (result !== false) {
|
|
914
945
|
await fs.writeFile(filePath, content, "utf-8");
|
|
915
|
-
|
|
946
|
+
emittedFiles.set(filePath, content);
|
|
916
947
|
}
|
|
917
948
|
}
|
|
918
949
|
bundleDebug("insert index");
|
|
@@ -921,17 +952,17 @@ export default ${libName}
|
|
|
921
952
|
const rollupFiles = /* @__PURE__ */ new Set();
|
|
922
953
|
if (multiple) {
|
|
923
954
|
for (const name of entryNames) {
|
|
924
|
-
const path =
|
|
955
|
+
const path = resolve(outputDir, `${name.replace(tsRE, "")}.d.ts`);
|
|
925
956
|
rollupDeclarationFiles({
|
|
926
957
|
root,
|
|
927
958
|
compilerOptions,
|
|
928
959
|
outputDir,
|
|
929
960
|
entryPath: path,
|
|
930
|
-
fileName: node_path.basename(path)
|
|
961
|
+
fileName: node_path.basename(path),
|
|
962
|
+
libFolder: libFolderPath
|
|
931
963
|
});
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
rollupFiles.add(wroteFile);
|
|
964
|
+
emittedFiles.delete(path);
|
|
965
|
+
rollupFiles.add(path);
|
|
935
966
|
}
|
|
936
967
|
} else {
|
|
937
968
|
rollupDeclarationFiles({
|
|
@@ -939,41 +970,37 @@ export default ${libName}
|
|
|
939
970
|
compilerOptions,
|
|
940
971
|
outputDir,
|
|
941
972
|
entryPath: typesPath,
|
|
942
|
-
fileName: node_path.basename(typesPath)
|
|
973
|
+
fileName: node_path.basename(typesPath),
|
|
974
|
+
libFolder: libFolderPath
|
|
943
975
|
});
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
rollupFiles.add(wroteFile);
|
|
976
|
+
emittedFiles.delete(typesPath);
|
|
977
|
+
rollupFiles.add(typesPath);
|
|
947
978
|
}
|
|
948
|
-
await runParallel(os.cpus().length, Array.from(
|
|
979
|
+
await runParallel(os.cpus().length, Array.from(emittedFiles.keys()), (f) => fs.unlink(f));
|
|
949
980
|
removeDirIfEmpty(outputDir);
|
|
950
|
-
|
|
981
|
+
emittedFiles.clear();
|
|
951
982
|
for (const file of rollupFiles) {
|
|
952
|
-
|
|
953
|
-
}
|
|
954
|
-
if (copyDtsFiles) {
|
|
955
|
-
await runParallel(os.cpus().length, dtsOutputFiles, async ({ path, content }) => {
|
|
956
|
-
const filePath = node_path.resolve(outputDir, node_path.basename(path));
|
|
957
|
-
await fs.writeFile(filePath, content, "utf-8");
|
|
958
|
-
wroteFiles.add(vite.normalizePath(filePath));
|
|
959
|
-
});
|
|
983
|
+
emittedFiles.set(file, await fs.readFile(file, "utf-8"));
|
|
960
984
|
}
|
|
961
985
|
bundleDebug("rollup");
|
|
962
986
|
}
|
|
963
987
|
}
|
|
964
988
|
if (outputDirs.length > 1) {
|
|
965
989
|
const dirs = outputDirs.slice(1);
|
|
966
|
-
await runParallel(
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
990
|
+
await runParallel(
|
|
991
|
+
os.cpus().length,
|
|
992
|
+
Array.from(emittedFiles),
|
|
993
|
+
async ([wroteFile, content]) => {
|
|
994
|
+
const relativePath = node_path.relative(outputDir, wroteFile);
|
|
995
|
+
await Promise.all(
|
|
996
|
+
dirs.map(async (dir) => {
|
|
997
|
+
const filePath = resolve(dir, relativePath);
|
|
998
|
+
await fs.mkdir(node_path.dirname(filePath), { recursive: true });
|
|
999
|
+
await fs.writeFile(filePath, content, "utf-8");
|
|
1000
|
+
})
|
|
1001
|
+
);
|
|
1002
|
+
}
|
|
1003
|
+
);
|
|
977
1004
|
}
|
|
978
1005
|
if (typeof afterBuild === "function") {
|
|
979
1006
|
const result = afterBuild();
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as vite from 'vite';
|
|
2
|
+
import { LogLevel } from 'vite';
|
|
2
3
|
import { ts, Diagnostic } from 'ts-morph';
|
|
3
4
|
|
|
4
5
|
interface TransformWriteFile {
|
|
@@ -9,7 +10,7 @@ interface PluginOptions {
|
|
|
9
10
|
/**
|
|
10
11
|
* Depends on the root directory
|
|
11
12
|
*
|
|
12
|
-
*
|
|
13
|
+
* By Default it base on 'root' option of your vite config
|
|
13
14
|
*/
|
|
14
15
|
root?: string;
|
|
15
16
|
/**
|
|
@@ -17,7 +18,7 @@ interface PluginOptions {
|
|
|
17
18
|
*
|
|
18
19
|
* Can be specified a array to output to multiple directories
|
|
19
20
|
*
|
|
20
|
-
*
|
|
21
|
+
* By Default it base on 'build.outDir' option of your vite config
|
|
21
22
|
*/
|
|
22
23
|
outputDir?: string | string[];
|
|
23
24
|
/**
|
|
@@ -25,7 +26,7 @@ interface PluginOptions {
|
|
|
25
26
|
*
|
|
26
27
|
* The output path of each file will be calculated base on it
|
|
27
28
|
*
|
|
28
|
-
*
|
|
29
|
+
* By Default it is the smallest public path for all files
|
|
29
30
|
*/
|
|
30
31
|
entryRoot?: string;
|
|
31
32
|
/**
|
|
@@ -69,13 +70,13 @@ interface PluginOptions {
|
|
|
69
70
|
/**
|
|
70
71
|
* Manual set include glob
|
|
71
72
|
*
|
|
72
|
-
*
|
|
73
|
+
* By Default it base on 'include' option of the tsconfig.json
|
|
73
74
|
*/
|
|
74
75
|
include?: string | string[];
|
|
75
76
|
/**
|
|
76
77
|
* Manual set exclude glob
|
|
77
78
|
*
|
|
78
|
-
*
|
|
79
|
+
* By Default it base on 'exclude' option of the tsconfig.json, be 'node_module/**' when empty
|
|
79
80
|
*/
|
|
80
81
|
exclude?: string | string[];
|
|
81
82
|
/**
|
|
@@ -124,15 +125,6 @@ interface PluginOptions {
|
|
|
124
125
|
* @default false
|
|
125
126
|
*/
|
|
126
127
|
skipDiagnostics?: boolean;
|
|
127
|
-
/**
|
|
128
|
-
* Whether log diagnostic informations
|
|
129
|
-
*
|
|
130
|
-
* Not effective when `skipDiagnostics` is true
|
|
131
|
-
*
|
|
132
|
-
* @deprecated
|
|
133
|
-
* @default false
|
|
134
|
-
*/
|
|
135
|
-
logDiagnostics?: boolean;
|
|
136
128
|
/**
|
|
137
129
|
* Customize typescript lib folder path
|
|
138
130
|
*
|
|
@@ -141,6 +133,12 @@ interface PluginOptions {
|
|
|
141
133
|
* @default undefined
|
|
142
134
|
*/
|
|
143
135
|
libFolderPath?: string;
|
|
136
|
+
/**
|
|
137
|
+
* Specify the log level of plugin
|
|
138
|
+
*
|
|
139
|
+
* By Default it base on 'logLevel' option of your vite config
|
|
140
|
+
*/
|
|
141
|
+
logLevel?: LogLevel;
|
|
144
142
|
/**
|
|
145
143
|
* After emit diagnostic hook
|
|
146
144
|
*
|
package/dist/index.mjs
CHANGED
|
@@ -5,14 +5,14 @@ import __cjs_mod__ from 'module';
|
|
|
5
5
|
const __filename = __cjs_url__.fileURLToPath(import.meta.url);
|
|
6
6
|
const __dirname = __cjs_path__.dirname(__filename);
|
|
7
7
|
const require = __cjs_mod__.createRequire(import.meta.url);
|
|
8
|
-
import { isAbsolute, resolve, dirname, normalize, sep, relative, basename } from 'node:path';
|
|
8
|
+
import { isAbsolute, resolve as resolve$1, dirname, normalize, sep, relative, basename } from 'node:path';
|
|
9
9
|
import fs from 'fs-extra';
|
|
10
10
|
import os from 'os';
|
|
11
11
|
import { cyan, yellow, red, green } from 'kolorist';
|
|
12
12
|
import glob from 'fast-glob';
|
|
13
13
|
import debug from 'debug';
|
|
14
14
|
import { Project } from 'ts-morph';
|
|
15
|
-
import { normalizePath } from 'vite';
|
|
15
|
+
import { normalizePath, createLogger } from 'vite';
|
|
16
16
|
import typescript from 'typescript';
|
|
17
17
|
import { createFilter } from '@rollup/pluginutils';
|
|
18
18
|
import { existsSync, readdirSync, lstatSync, rmdirSync } from 'node:fs';
|
|
@@ -69,7 +69,7 @@ function mergeObjects(sourceObj, targetObj) {
|
|
|
69
69
|
return sourceObj;
|
|
70
70
|
}
|
|
71
71
|
function ensureAbsolute(path, root) {
|
|
72
|
-
return path ? isAbsolute(path) ? path : resolve(root, path) : root;
|
|
72
|
+
return path ? isAbsolute(path) ? path : resolve$1(root, path) : root;
|
|
73
73
|
}
|
|
74
74
|
function ensureArray(value) {
|
|
75
75
|
return Array.isArray(value) ? value : value ? [value] : [];
|
|
@@ -134,7 +134,7 @@ function removeDirIfEmpty(dir) {
|
|
|
134
134
|
}
|
|
135
135
|
let onlyHasDir = true;
|
|
136
136
|
for (const file of readdirSync(dir)) {
|
|
137
|
-
const abs = resolve(dir, file);
|
|
137
|
+
const abs = resolve$1(dir, file);
|
|
138
138
|
if (lstatSync(abs).isDirectory()) {
|
|
139
139
|
if (!removeDirIfEmpty(abs)) {
|
|
140
140
|
onlyHasDir = false;
|
|
@@ -365,62 +365,90 @@ function preprocessVueCode(code, setupScript) {
|
|
|
365
365
|
}
|
|
366
366
|
}
|
|
367
367
|
}
|
|
368
|
+
const declRecord = /* @__PURE__ */ new Map();
|
|
369
|
+
let defaultExport;
|
|
370
|
+
let options;
|
|
368
371
|
for (const node of scriptAst) {
|
|
372
|
+
if (node.type === "VariableDeclaration") {
|
|
373
|
+
for (const decl of node.declarations) {
|
|
374
|
+
if (decl.id.type === "Identifier" && decl.init) {
|
|
375
|
+
let properties;
|
|
376
|
+
if (decl.init.type === "ObjectExpression") {
|
|
377
|
+
properties = decl.init.properties;
|
|
378
|
+
} else if (decl.init.type === "CallExpression" && decl.init.arguments[0].type === "ObjectExpression") {
|
|
379
|
+
properties = decl.init.arguments[0].properties;
|
|
380
|
+
}
|
|
381
|
+
if (!properties)
|
|
382
|
+
continue;
|
|
383
|
+
if (defaultExport && decl.id.name === defaultExport) {
|
|
384
|
+
options = properties;
|
|
385
|
+
break;
|
|
386
|
+
} else {
|
|
387
|
+
declRecord.set(decl.id.name, properties);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
369
392
|
if (node.type === "ExportDefaultDeclaration") {
|
|
370
|
-
let options;
|
|
371
393
|
if (node.declaration.type === "ObjectExpression") {
|
|
372
394
|
options = node.declaration.properties;
|
|
373
395
|
} else if (node.declaration.type === "CallExpression" && node.declaration.arguments[0].type === "ObjectExpression") {
|
|
374
396
|
options = node.declaration.arguments[0].properties;
|
|
397
|
+
} else if (node.declaration.type === "Identifier") {
|
|
398
|
+
if (declRecord.has(node.declaration.name)) {
|
|
399
|
+
options = declRecord.get(node.declaration.name);
|
|
400
|
+
} else {
|
|
401
|
+
defaultExport = node.declaration.name;
|
|
402
|
+
}
|
|
375
403
|
}
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
404
|
+
}
|
|
405
|
+
if (options) {
|
|
406
|
+
for (const option of options) {
|
|
407
|
+
if (propsTypeName && option.type === "ObjectProperty" && option.key.type === "Identifier" && option.key.name === "props" && option.value.type === "ObjectExpression") {
|
|
408
|
+
for (const prop of option.value.properties) {
|
|
409
|
+
if (prop.type === "ObjectProperty" && prop.key.type === "Identifier") {
|
|
410
|
+
if (prop.value.type === "ObjectExpression") {
|
|
411
|
+
for (const propDef of prop.value.properties) {
|
|
412
|
+
if (propDef.type === "ObjectProperty" && propDef.key.type === "Identifier" && propDef.key.name === "type") {
|
|
413
|
+
source.prependLeft(
|
|
414
|
+
propDef.end,
|
|
415
|
+
` as unknown as __PropType<${propsTypeName}['${prop.key.name}']>`
|
|
416
|
+
);
|
|
389
417
|
}
|
|
390
|
-
} else {
|
|
391
|
-
source.prependLeft(
|
|
392
|
-
prop.end,
|
|
393
|
-
` as unknown as __PropType<${propsTypeName}['${prop.key.name}']>`
|
|
394
|
-
);
|
|
395
418
|
}
|
|
419
|
+
} else {
|
|
420
|
+
source.prependLeft(
|
|
421
|
+
prop.end,
|
|
422
|
+
` as unknown as __PropType<${propsTypeName}['${prop.key.name}']>`
|
|
423
|
+
);
|
|
396
424
|
}
|
|
397
425
|
}
|
|
398
426
|
}
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
427
|
+
}
|
|
428
|
+
if (option.type === "ObjectProperty" && option.key.type === "Identifier" && option.key.name === "components") {
|
|
429
|
+
source.remove(option.start, option.end);
|
|
430
|
+
}
|
|
431
|
+
if (option.type === "ObjectMethod" && option.key.type === "Identifier" && option.key.name === "setup") {
|
|
432
|
+
let exposed;
|
|
433
|
+
let returned;
|
|
434
|
+
for (const node2 of option.body.body) {
|
|
435
|
+
if (!exposed && node2.type === "ExpressionStatement" && node2.expression.type === "CallExpression" && node2.expression.callee.type === "Identifier" && node2.expression.callee.name === "expose") {
|
|
436
|
+
exposed = node2.expression.arguments[0];
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
if (node2.type === "ReturnStatement") {
|
|
440
|
+
returned = node2;
|
|
441
|
+
break;
|
|
414
442
|
}
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
443
|
+
}
|
|
444
|
+
const newReturned = exposed && exposed.type === "ObjectExpression" ? `return ${code.substring(exposed.start, exposed.end)}` : setupScript ? "return {}" : "";
|
|
445
|
+
if (newReturned) {
|
|
446
|
+
if (returned) {
|
|
447
|
+
source.overwrite(returned.start, returned.end, newReturned);
|
|
448
|
+
} else if (option.body.body.length) {
|
|
449
|
+
source.appendRight(option.body.body.at(-1).end, `
|
|
421
450
|
${newReturned}
|
|
422
451
|
`);
|
|
423
|
-
}
|
|
424
452
|
}
|
|
425
453
|
}
|
|
426
454
|
}
|
|
@@ -480,12 +508,13 @@ function compileVueCode(code) {
|
|
|
480
508
|
const dtsRE$1 = /\.d\.tsx?$/;
|
|
481
509
|
function rollupDeclarationFiles({
|
|
482
510
|
root,
|
|
511
|
+
compilerOptions,
|
|
483
512
|
outputDir,
|
|
484
513
|
entryPath,
|
|
485
514
|
fileName,
|
|
486
|
-
|
|
515
|
+
libFolder
|
|
487
516
|
}) {
|
|
488
|
-
const configObjectFullPath = resolve(root, "api-extractor.json");
|
|
517
|
+
const configObjectFullPath = resolve$1(root, "api-extractor.json");
|
|
489
518
|
const packageJsonLookup = new PackageJsonLookup();
|
|
490
519
|
const packageJsonFullPath = packageJsonLookup.tryGetPackageJsonFilePathFor(configObjectFullPath);
|
|
491
520
|
if (!dtsRE$1.test(fileName)) {
|
|
@@ -510,7 +539,7 @@ function rollupDeclarationFiles({
|
|
|
510
539
|
},
|
|
511
540
|
dtsRollup: {
|
|
512
541
|
enabled: true,
|
|
513
|
-
publicTrimmedFilePath: resolve(outputDir, fileName)
|
|
542
|
+
publicTrimmedFilePath: resolve$1(outputDir, fileName)
|
|
514
543
|
},
|
|
515
544
|
tsdocMetadata: {
|
|
516
545
|
enabled: false
|
|
@@ -532,8 +561,9 @@ function rollupDeclarationFiles({
|
|
|
532
561
|
packageJsonFullPath
|
|
533
562
|
});
|
|
534
563
|
const compilerState = CompilerState.create(extractorConfig, {
|
|
535
|
-
localBuild:
|
|
536
|
-
showVerboseMessages: false
|
|
564
|
+
localBuild: false,
|
|
565
|
+
showVerboseMessages: false,
|
|
566
|
+
typescriptCompilerFolder: libFolder ? resolve$1(libFolder, "..") : void 0
|
|
537
567
|
});
|
|
538
568
|
const sourceMapper = new SourceMapper();
|
|
539
569
|
const messageRouter = new MessageRouter({
|
|
@@ -574,6 +604,7 @@ const defaultIndex = "index.d.ts";
|
|
|
574
604
|
const noop = () => {
|
|
575
605
|
};
|
|
576
606
|
const extPrefix = (file) => mtjsRE.test(file) ? "m" : ctjsRE.test(file) ? "c" : "";
|
|
607
|
+
const resolve = (...paths) => normalizePath(resolve$1(...paths));
|
|
577
608
|
const logPrefix = cyan("[vite:dts]");
|
|
578
609
|
const bundleDebug = debug("vite-plugin-dts:bundle");
|
|
579
610
|
function dtsPlugin(options = {}) {
|
|
@@ -587,9 +618,8 @@ function dtsPlugin(options = {}) {
|
|
|
587
618
|
rollupTypes = false,
|
|
588
619
|
noEmitOnError = false,
|
|
589
620
|
skipDiagnostics = false,
|
|
590
|
-
logDiagnostics = void 0,
|
|
591
621
|
copyDtsFiles = true,
|
|
592
|
-
|
|
622
|
+
logLevel = void 0,
|
|
593
623
|
afterDiagnostic = noop,
|
|
594
624
|
beforeWriteFile = noop,
|
|
595
625
|
afterBuild = noop
|
|
@@ -609,10 +639,39 @@ function dtsPlugin(options = {}) {
|
|
|
609
639
|
let include;
|
|
610
640
|
let exclude;
|
|
611
641
|
let filter;
|
|
642
|
+
let libFolderPath = options.libFolderPath;
|
|
612
643
|
const sourceDtsFiles = /* @__PURE__ */ new Set();
|
|
644
|
+
const emittedFiles = /* @__PURE__ */ new Map();
|
|
613
645
|
let hasJsVue = false;
|
|
614
646
|
let allowJs = false;
|
|
615
647
|
let transformError = false;
|
|
648
|
+
function internalTransform(code, id) {
|
|
649
|
+
if (!project || !filter(id)) {
|
|
650
|
+
return;
|
|
651
|
+
}
|
|
652
|
+
if (vueRE.test(id)) {
|
|
653
|
+
const { error, content, ext } = compileVueCode(code);
|
|
654
|
+
if (!transformError && error) {
|
|
655
|
+
logger.error(
|
|
656
|
+
red(
|
|
657
|
+
`
|
|
658
|
+
${cyan(
|
|
659
|
+
"[vite:dts]"
|
|
660
|
+
)} A error occurred when transform code, maybe there are some inertnal bugs.
|
|
661
|
+
`
|
|
662
|
+
)
|
|
663
|
+
);
|
|
664
|
+
transformError = true;
|
|
665
|
+
}
|
|
666
|
+
if (content) {
|
|
667
|
+
if (ext === "js" || ext === "jsx")
|
|
668
|
+
hasJsVue = true;
|
|
669
|
+
project.createSourceFile(`${id}.${ext || "js"}`, content, { overwrite: true });
|
|
670
|
+
}
|
|
671
|
+
} else if (!id.includes(".vue?vue") && (tsRE.test(id) || allowJs && jsRE.test(id))) {
|
|
672
|
+
project.createSourceFile(id, code, { overwrite: true });
|
|
673
|
+
}
|
|
674
|
+
}
|
|
616
675
|
return {
|
|
617
676
|
name: "vite:dts",
|
|
618
677
|
apply: "build",
|
|
@@ -639,18 +698,7 @@ function dtsPlugin(options = {}) {
|
|
|
639
698
|
configResolved(config) {
|
|
640
699
|
if (isBundle)
|
|
641
700
|
return;
|
|
642
|
-
logger = config.logger;
|
|
643
|
-
if (logDiagnostics != null) {
|
|
644
|
-
logger.warn(
|
|
645
|
-
yellow(
|
|
646
|
-
`
|
|
647
|
-
${cyan(
|
|
648
|
-
"[vite:dts]"
|
|
649
|
-
)} 'logDiagnostics' has been deprecated, the original feature now following 'skipDiagnostics'.
|
|
650
|
-
`
|
|
651
|
-
)
|
|
652
|
-
);
|
|
653
|
-
}
|
|
701
|
+
logger = logLevel ? createLogger(logLevel, { allowClearScreen: config.clearScreen }) : config.logger;
|
|
654
702
|
if (!config.build.lib) {
|
|
655
703
|
logger.warn(
|
|
656
704
|
yellow(
|
|
@@ -674,6 +722,7 @@ ${cyan(
|
|
|
674
722
|
}
|
|
675
723
|
root = ensureAbsolute(options.root ?? "", config.root);
|
|
676
724
|
tsConfigPath = ensureAbsolute(tsConfigFilePath, root);
|
|
725
|
+
libFolderPath = libFolderPath && ensureAbsolute(libFolderPath, root);
|
|
677
726
|
outputDirs = options.outputDir ? ensureArray(options.outputDir).map((d) => ensureAbsolute(d, root)) : [ensureAbsolute(config.build.outDir, root)];
|
|
678
727
|
if (!outputDirs[0]) {
|
|
679
728
|
logger.error(
|
|
@@ -692,7 +741,7 @@ ${cyan(
|
|
|
692
741
|
compilerOptions: mergeObjects(compilerOptions, {
|
|
693
742
|
rootDir: compilerOptions.rootDir || root,
|
|
694
743
|
noEmitOnError,
|
|
695
|
-
outDir:
|
|
744
|
+
outDir: outputDirs[0],
|
|
696
745
|
declarationDir: void 0,
|
|
697
746
|
noUnusedParameters: false,
|
|
698
747
|
declaration: true,
|
|
@@ -702,7 +751,7 @@ ${cyan(
|
|
|
702
751
|
}),
|
|
703
752
|
tsConfigFilePath: tsConfigPath,
|
|
704
753
|
skipAddingFilesFromTsConfig: true,
|
|
705
|
-
libFolderPath
|
|
754
|
+
libFolderPath
|
|
706
755
|
});
|
|
707
756
|
allowJs = project.getCompilerOptions().allowJs ?? false;
|
|
708
757
|
const tsConfig = { compilerOptions: {} };
|
|
@@ -737,39 +786,16 @@ ${cyan(
|
|
|
737
786
|
}
|
|
738
787
|
},
|
|
739
788
|
transform(code, id) {
|
|
740
|
-
|
|
741
|
-
return null;
|
|
742
|
-
}
|
|
743
|
-
if (vueRE.test(id)) {
|
|
744
|
-
const { error, content, ext } = compileVueCode(code);
|
|
745
|
-
if (!transformError && error) {
|
|
746
|
-
logger.error(
|
|
747
|
-
red(
|
|
748
|
-
`
|
|
749
|
-
${cyan(
|
|
750
|
-
"[vite:dts]"
|
|
751
|
-
)} A error occurred when transform code, maybe there are some inertnal bugs.
|
|
752
|
-
`
|
|
753
|
-
)
|
|
754
|
-
);
|
|
755
|
-
transformError = true;
|
|
756
|
-
}
|
|
757
|
-
if (content) {
|
|
758
|
-
if (ext === "js" || ext === "jsx")
|
|
759
|
-
hasJsVue = true;
|
|
760
|
-
project.createSourceFile(`${id}.${ext || "js"}`, content, { overwrite: true });
|
|
761
|
-
}
|
|
762
|
-
} else if (!id.includes(".vue?vue") && (tsRE.test(id) || allowJs && jsRE.test(id))) {
|
|
763
|
-
project.createSourceFile(id, code, { overwrite: true });
|
|
764
|
-
}
|
|
789
|
+
internalTransform(code, id);
|
|
765
790
|
return null;
|
|
766
791
|
},
|
|
767
|
-
watchChange(id) {
|
|
792
|
+
async watchChange(id) {
|
|
768
793
|
if (watchExtensionRE.test(id)) {
|
|
769
794
|
isBundle = false;
|
|
770
795
|
if (project) {
|
|
771
796
|
const sourceFile = project.getSourceFile(normalizePath(id));
|
|
772
797
|
sourceFile && project.removeSourceFile(sourceFile);
|
|
798
|
+
internalTransform(await fs.readFile(id, "utf-8"), id);
|
|
773
799
|
}
|
|
774
800
|
}
|
|
775
801
|
},
|
|
@@ -781,6 +807,7 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
781
807
|
bundleDebug("start");
|
|
782
808
|
isBundle = true;
|
|
783
809
|
sourceDtsFiles.clear();
|
|
810
|
+
emittedFiles.clear();
|
|
784
811
|
const startTime = Date.now();
|
|
785
812
|
const includedFileSet = /* @__PURE__ */ new Set();
|
|
786
813
|
if (include && include.length) {
|
|
@@ -827,6 +854,7 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
827
854
|
}
|
|
828
855
|
bundleDebug("diagnostics");
|
|
829
856
|
}
|
|
857
|
+
const outputDir = outputDirs[0];
|
|
830
858
|
const dtsOutputFiles = Array.from(sourceDtsFiles).map((sourceFile) => ({
|
|
831
859
|
path: sourceFile.getFilePath(),
|
|
832
860
|
content: sourceFile.getFullText()
|
|
@@ -834,17 +862,13 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
834
862
|
const service = project.getLanguageService();
|
|
835
863
|
const outputFiles = project.getSourceFiles().map(
|
|
836
864
|
(sourceFile) => service.getEmitOutput(sourceFile, true).getOutputFiles().map((outputFile) => ({
|
|
837
|
-
path:
|
|
865
|
+
path: resolve(root, relative(outputDir, outputFile.compilerObject.name)),
|
|
838
866
|
content: outputFile.getText()
|
|
839
867
|
}))
|
|
840
868
|
).flat().concat(dtsOutputFiles);
|
|
841
869
|
bundleDebug("emit");
|
|
842
|
-
|
|
843
|
-
entryRoot = queryPublicPath(outputFiles.map((file) => file.path));
|
|
844
|
-
}
|
|
870
|
+
entryRoot = entryRoot || queryPublicPath(outputFiles.map((file) => file.path));
|
|
845
871
|
entryRoot = ensureAbsolute(entryRoot, root);
|
|
846
|
-
const wroteFiles = /* @__PURE__ */ new Set();
|
|
847
|
-
const outputDir = outputDirs[0];
|
|
848
872
|
await runParallel(os.cpus().length, outputFiles, async (outputFile) => {
|
|
849
873
|
let filePath = outputFile.path;
|
|
850
874
|
let content = outputFile.content;
|
|
@@ -861,29 +885,35 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
861
885
|
outputDir,
|
|
862
886
|
relative(entryRoot, cleanVueFileName ? filePath.replace(".vue.d.ts", ".d.ts") : filePath)
|
|
863
887
|
);
|
|
888
|
+
content = cleanVueFileName ? content.replace(/['"](.+)\.vue['"]/g, '"$1"') : content;
|
|
864
889
|
if (typeof beforeWriteFile === "function") {
|
|
865
890
|
const result = beforeWriteFile(filePath, content);
|
|
866
891
|
if (result === false)
|
|
867
892
|
return;
|
|
868
893
|
if (result && isNativeObj(result)) {
|
|
869
|
-
filePath = result.filePath
|
|
894
|
+
filePath = result.filePath || filePath;
|
|
870
895
|
content = result.content ?? content;
|
|
871
896
|
}
|
|
872
897
|
}
|
|
898
|
+
filePath = normalizePath(filePath);
|
|
873
899
|
await fs.mkdir(dirname(filePath), { recursive: true });
|
|
874
|
-
await fs.writeFile(
|
|
875
|
-
|
|
876
|
-
cleanVueFileName ? content.replace(/['"](.+)\.vue['"]/g, '"$1"') : content,
|
|
877
|
-
"utf-8"
|
|
878
|
-
);
|
|
879
|
-
wroteFiles.add(normalizePath(filePath));
|
|
900
|
+
await fs.writeFile(filePath, content, "utf-8");
|
|
901
|
+
emittedFiles.set(filePath, content);
|
|
880
902
|
});
|
|
881
903
|
bundleDebug("output");
|
|
904
|
+
if (copyDtsFiles) {
|
|
905
|
+
await runParallel(os.cpus().length, dtsOutputFiles, async ({ path, content }) => {
|
|
906
|
+
const filePath = resolve(outputDir, basename(path));
|
|
907
|
+
await fs.writeFile(filePath, content, "utf-8");
|
|
908
|
+
emittedFiles.set(filePath, content);
|
|
909
|
+
});
|
|
910
|
+
}
|
|
911
|
+
bundleDebug("copy dts");
|
|
882
912
|
if (insertTypesEntry || rollupTypes) {
|
|
883
913
|
const pkgPath = resolve(root, "package.json");
|
|
884
914
|
const pkg = fs.existsSync(pkgPath) ? JSON.parse(await fs.readFile(pkgPath, "utf-8")) : {};
|
|
885
915
|
const entryNames = Object.keys(entries);
|
|
886
|
-
const types = pkg.types || pkg.typings || pkg.publishConfig?.types || pkg.publishConfig?.typings;
|
|
916
|
+
const types = pkg.types || pkg.typings || pkg.publishConfig?.types || pkg.publishConfig?.typings || (pkg.exports?.["."] || pkg.exports?.["./"])?.types;
|
|
887
917
|
const multiple = entryNames.length > 1;
|
|
888
918
|
const typesPath = types ? resolve(root, types) : resolve(outputDir, indexName);
|
|
889
919
|
for (const name of entryNames) {
|
|
@@ -915,9 +945,10 @@ export default ${libName}
|
|
|
915
945
|
content = result.content ?? content;
|
|
916
946
|
}
|
|
917
947
|
}
|
|
948
|
+
filePath = normalizePath(filePath);
|
|
918
949
|
if (result !== false) {
|
|
919
950
|
await fs.writeFile(filePath, content, "utf-8");
|
|
920
|
-
|
|
951
|
+
emittedFiles.set(filePath, content);
|
|
921
952
|
}
|
|
922
953
|
}
|
|
923
954
|
bundleDebug("insert index");
|
|
@@ -932,11 +963,11 @@ export default ${libName}
|
|
|
932
963
|
compilerOptions,
|
|
933
964
|
outputDir,
|
|
934
965
|
entryPath: path,
|
|
935
|
-
fileName: basename(path)
|
|
966
|
+
fileName: basename(path),
|
|
967
|
+
libFolder: libFolderPath
|
|
936
968
|
});
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
rollupFiles.add(wroteFile);
|
|
969
|
+
emittedFiles.delete(path);
|
|
970
|
+
rollupFiles.add(path);
|
|
940
971
|
}
|
|
941
972
|
} else {
|
|
942
973
|
rollupDeclarationFiles({
|
|
@@ -944,41 +975,37 @@ export default ${libName}
|
|
|
944
975
|
compilerOptions,
|
|
945
976
|
outputDir,
|
|
946
977
|
entryPath: typesPath,
|
|
947
|
-
fileName: basename(typesPath)
|
|
978
|
+
fileName: basename(typesPath),
|
|
979
|
+
libFolder: libFolderPath
|
|
948
980
|
});
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
rollupFiles.add(wroteFile);
|
|
981
|
+
emittedFiles.delete(typesPath);
|
|
982
|
+
rollupFiles.add(typesPath);
|
|
952
983
|
}
|
|
953
|
-
await runParallel(os.cpus().length, Array.from(
|
|
984
|
+
await runParallel(os.cpus().length, Array.from(emittedFiles.keys()), (f) => fs.unlink(f));
|
|
954
985
|
removeDirIfEmpty(outputDir);
|
|
955
|
-
|
|
986
|
+
emittedFiles.clear();
|
|
956
987
|
for (const file of rollupFiles) {
|
|
957
|
-
|
|
958
|
-
}
|
|
959
|
-
if (copyDtsFiles) {
|
|
960
|
-
await runParallel(os.cpus().length, dtsOutputFiles, async ({ path, content }) => {
|
|
961
|
-
const filePath = resolve(outputDir, basename(path));
|
|
962
|
-
await fs.writeFile(filePath, content, "utf-8");
|
|
963
|
-
wroteFiles.add(normalizePath(filePath));
|
|
964
|
-
});
|
|
988
|
+
emittedFiles.set(file, await fs.readFile(file, "utf-8"));
|
|
965
989
|
}
|
|
966
990
|
bundleDebug("rollup");
|
|
967
991
|
}
|
|
968
992
|
}
|
|
969
993
|
if (outputDirs.length > 1) {
|
|
970
994
|
const dirs = outputDirs.slice(1);
|
|
971
|
-
await runParallel(
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
995
|
+
await runParallel(
|
|
996
|
+
os.cpus().length,
|
|
997
|
+
Array.from(emittedFiles),
|
|
998
|
+
async ([wroteFile, content]) => {
|
|
999
|
+
const relativePath = relative(outputDir, wroteFile);
|
|
1000
|
+
await Promise.all(
|
|
1001
|
+
dirs.map(async (dir) => {
|
|
1002
|
+
const filePath = resolve(dir, relativePath);
|
|
1003
|
+
await fs.mkdir(dirname(filePath), { recursive: true });
|
|
1004
|
+
await fs.writeFile(filePath, content, "utf-8");
|
|
1005
|
+
})
|
|
1006
|
+
);
|
|
1007
|
+
}
|
|
1008
|
+
);
|
|
982
1009
|
}
|
|
983
1010
|
if (typeof afterBuild === "function") {
|
|
984
1011
|
const result = afterBuild();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-dts",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "qmhc",
|
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
"prettier": "pretty-quick --staged",
|
|
30
30
|
"release": "tsx scripts/release.ts",
|
|
31
31
|
"test": "vitest run",
|
|
32
|
+
"test:react": "pnpm -C examples/react build",
|
|
32
33
|
"test:ts": "pnpm -C examples/ts build",
|
|
33
34
|
"test:vue": "pnpm -C examples/vue build"
|
|
34
35
|
},
|