vite-plugin-dts 2.0.0-beta.3 → 2.0.1

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 CHANGED
@@ -232,7 +232,8 @@ export interface PluginOptions {
232
232
  /**
233
233
  * Whether copy .d.ts source files into outputDir
234
234
  *
235
- * @default true
235
+ * @default false
236
+ * @remarks Before 2.0 it defaults to true
236
237
  */
237
238
  copyDtsFiles?: boolean
238
239
 
@@ -251,6 +252,7 @@ export interface PluginOptions {
251
252
  * But for the source files with type errors will not be emitted
252
253
  *
253
254
  * @default false
255
+ * @remarks Before 1.7 it defaults to true
254
256
  */
255
257
  skipDiagnostics?: boolean
256
258
 
package/README.zh-CN.md CHANGED
@@ -231,7 +231,8 @@ export interface PluginOptions {
231
231
  /**
232
232
  * 是否将源码里的 .d.ts 文件复制到 outputDir
233
233
  *
234
- * @default true
234
+ * @default false
235
+ * @remarks 在 2.0 之前它默认为 true
235
236
  */
236
237
  copyDtsFiles?: boolean
237
238
 
@@ -250,6 +251,7 @@ export interface PluginOptions {
250
251
  * 但对于出现错误的源文件,将无法生成相应的类型文件
251
252
  *
252
253
  * @default false
254
+ * @remarks 在 1.7 之前它默认为 true
253
255
  */
254
256
  skipDiagnostics?: boolean
255
257
 
package/dist/index.cjs CHANGED
@@ -370,7 +370,7 @@ function preprocessVueCode(code, setupScript) {
370
370
  let properties;
371
371
  if (decl.init.type === "ObjectExpression") {
372
372
  properties = decl.init.properties;
373
- } else if (decl.init.type === "CallExpression" && decl.init.arguments[0].type === "ObjectExpression") {
373
+ } else if (decl.init.type === "CallExpression" && decl.init.arguments[0]?.type === "ObjectExpression") {
374
374
  properties = decl.init.arguments[0].properties;
375
375
  }
376
376
  if (!properties)
@@ -387,7 +387,7 @@ function preprocessVueCode(code, setupScript) {
387
387
  if (node.type === "ExportDefaultDeclaration") {
388
388
  if (node.declaration.type === "ObjectExpression") {
389
389
  options = node.declaration.properties;
390
- } else if (node.declaration.type === "CallExpression" && node.declaration.arguments[0].type === "ObjectExpression") {
390
+ } else if (node.declaration.type === "CallExpression" && node.declaration.arguments[0]?.type === "ObjectExpression") {
391
391
  options = node.declaration.arguments[0].properties;
392
392
  } else if (node.declaration.type === "Identifier") {
393
393
  if (declRecord.has(node.declaration.name)) {
@@ -421,7 +421,7 @@ function preprocessVueCode(code, setupScript) {
421
421
  }
422
422
  }
423
423
  if (option.type === "ObjectProperty" && option.key.type === "Identifier" && option.key.name === "components") {
424
- source.remove(option.start, option.end);
424
+ source.overwrite(option.value.start, option.value.end, "undefined");
425
425
  }
426
426
  if (option.type === "ObjectMethod" && option.key.type === "Identifier" && option.key.name === "setup") {
427
427
  let exposed;
@@ -613,7 +613,7 @@ function dtsPlugin(options = {}) {
613
613
  rollupTypes = false,
614
614
  noEmitOnError = false,
615
615
  skipDiagnostics = false,
616
- copyDtsFiles = true,
616
+ copyDtsFiles = false,
617
617
  logLevel = void 0,
618
618
  afterDiagnostic = noop,
619
619
  beforeWriteFile = noop,
@@ -636,16 +636,17 @@ function dtsPlugin(options = {}) {
636
636
  let filter;
637
637
  let libFolderPath = options.libFolderPath;
638
638
  const sourceDtsFiles = /* @__PURE__ */ new Set();
639
+ const includedFiles = /* @__PURE__ */ new Set();
639
640
  const emittedFiles = /* @__PURE__ */ new Map();
640
641
  let hasJsVue = false;
641
642
  let allowJs = false;
642
643
  let transformError = false;
643
- function internalTransform(code, id) {
644
+ async function internalTransform(id) {
644
645
  if (!project || !filter(id)) {
645
646
  return;
646
647
  }
647
648
  if (vueRE.test(id)) {
648
- const { error, content, ext } = compileVueCode(code);
649
+ const { error, content, ext } = compileVueCode(await fs.readFile(id, "utf-8"));
649
650
  if (!transformError && error) {
650
651
  logger.error(
651
652
  kolorist.red(
@@ -664,7 +665,7 @@ ${kolorist.cyan(
664
665
  project.createSourceFile(`${id}.${ext || "js"}`, content, { overwrite: true });
665
666
  }
666
667
  } else if (!id.includes(".vue?vue") && (tsRE.test(id) || allowJs && jsRE.test(id))) {
667
- project.createSourceFile(id, code, { overwrite: true });
668
+ project.createSourceFile(id, await fs.readFile(id, "utf-8"), { overwrite: true });
668
669
  }
669
670
  }
670
671
  return {
@@ -770,7 +771,7 @@ ${kolorist.cyan(
770
771
  filter = pluginutils.createFilter(include, exclude, { resolve: root });
771
772
  compilerOptions = tsConfig.compilerOptions;
772
773
  },
773
- buildStart(inputOptions) {
774
+ async buildStart(inputOptions) {
774
775
  if (Array.isArray(inputOptions.input)) {
775
776
  entries = inputOptions.input.reduce((prev, current) => {
776
777
  prev[node_path.basename(current)] = current;
@@ -779,49 +780,27 @@ ${kolorist.cyan(
779
780
  } else {
780
781
  entries = { ...inputOptions.input };
781
782
  }
782
- },
783
- transform(code, id) {
784
- internalTransform(code, id);
785
- return null;
786
- },
787
- async watchChange(id) {
788
- if (watchExtensionRE.test(id)) {
789
- isBundle = false;
790
- if (project) {
791
- const sourceFile = project.getSourceFile(vite.normalizePath(id));
792
- sourceFile && project.removeSourceFile(sourceFile);
793
- internalTransform(await fs.readFile(id, "utf-8"), id);
794
- }
795
- }
796
- },
797
- async closeBundle() {
798
- if (!outputDirs || !project || isBundle)
799
- return;
800
- logger.info(kolorist.green(`
801
- ${logPrefix} Start generate declaration files...`));
802
- bundleDebug("start");
803
- isBundle = true;
783
+ bundleDebug("parse entries");
804
784
  sourceDtsFiles.clear();
805
- emittedFiles.clear();
806
- const startTime = Date.now();
807
- const includedFileSet = /* @__PURE__ */ new Set();
808
- if (include && include.length) {
785
+ includedFiles.clear();
786
+ if (project && include && include.length) {
809
787
  const files = await glob(include, {
810
788
  cwd: root,
811
789
  absolute: true,
812
790
  ignore: exclude
813
791
  });
814
- files.forEach((file) => {
792
+ for (const file of files) {
793
+ this.addWatchFile(file);
815
794
  if (dtsRE.test(file)) {
816
795
  sourceDtsFiles.add(project.addSourceFileAtPath(file));
817
796
  if (!copyDtsFiles) {
818
- return;
797
+ continue;
819
798
  }
820
- includedFileSet.add(file);
821
- return;
799
+ includedFiles.add(file);
800
+ continue;
822
801
  }
823
- includedFileSet.add(`${file.replace(tjsRE, "")}.d.${extPrefix(file)}ts`);
824
- });
802
+ includedFiles.add(`${file.replace(tjsRE, "")}.d.${extPrefix(file)}ts`);
803
+ }
825
804
  if (hasJsVue) {
826
805
  if (!allowJs) {
827
806
  logger.warn(
@@ -836,6 +815,30 @@ ${logPrefix} Start generate declaration files...`));
836
815
  }
837
816
  bundleDebug("collect files");
838
817
  }
818
+ },
819
+ async transform(_, id) {
820
+ await internalTransform(id);
821
+ return null;
822
+ },
823
+ async watchChange(id) {
824
+ if (watchExtensionRE.test(id)) {
825
+ isBundle = false;
826
+ if (project) {
827
+ const sourceFile = project.getSourceFile(vite.normalizePath(id));
828
+ sourceFile && project.removeSourceFile(sourceFile);
829
+ await internalTransform(id);
830
+ }
831
+ }
832
+ },
833
+ async closeBundle() {
834
+ if (!outputDirs || !project || isBundle)
835
+ return;
836
+ logger.info(kolorist.green(`
837
+ ${logPrefix} Start generate declaration files...`));
838
+ bundleDebug("start");
839
+ isBundle = true;
840
+ emittedFiles.clear();
841
+ const startTime = Date.now();
839
842
  project.resolveSourceFileDependencies();
840
843
  bundleDebug("resolve");
841
844
  if (!skipDiagnostics) {
@@ -868,7 +871,7 @@ ${logPrefix} Start generate declaration files...`));
868
871
  let filePath = outputFile.path;
869
872
  let content = outputFile.content;
870
873
  const isMapFile = filePath.endsWith(".map");
871
- if (!includedFileSet.has(isMapFile ? filePath.slice(0, -4) : filePath) || clearPureImport && content === noneExport) {
874
+ if (!includedFiles.has(isMapFile ? filePath.slice(0, -4) : filePath) || clearPureImport && content === noneExport) {
872
875
  return;
873
876
  }
874
877
  if (!isMapFile && content && content !== noneExport) {
@@ -896,14 +899,6 @@ ${logPrefix} Start generate declaration files...`));
896
899
  emittedFiles.set(filePath, content);
897
900
  });
898
901
  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");
907
902
  if (insertTypesEntry || rollupTypes) {
908
903
  const pkgPath = resolve(root, "package.json");
909
904
  const pkg = fs.existsSync(pkgPath) ? JSON.parse(await fs.readFile(pkgPath, "utf-8")) : {};
package/dist/index.d.ts CHANGED
@@ -106,7 +106,8 @@ interface PluginOptions {
106
106
  /**
107
107
  * Whether copy .d.ts source files into outputDir
108
108
  *
109
- * @default true
109
+ * @default false
110
+ * @remarks Before 2.0 it defaults to true
110
111
  */
111
112
  copyDtsFiles?: boolean;
112
113
  /**
@@ -123,6 +124,7 @@ interface PluginOptions {
123
124
  * But for the source files with type errors will not be emitted
124
125
  *
125
126
  * @default false
127
+ * @remarks Before 1.7 it defaults to true
126
128
  */
127
129
  skipDiagnostics?: boolean;
128
130
  /**
package/dist/index.mjs CHANGED
@@ -375,7 +375,7 @@ function preprocessVueCode(code, setupScript) {
375
375
  let properties;
376
376
  if (decl.init.type === "ObjectExpression") {
377
377
  properties = decl.init.properties;
378
- } else if (decl.init.type === "CallExpression" && decl.init.arguments[0].type === "ObjectExpression") {
378
+ } else if (decl.init.type === "CallExpression" && decl.init.arguments[0]?.type === "ObjectExpression") {
379
379
  properties = decl.init.arguments[0].properties;
380
380
  }
381
381
  if (!properties)
@@ -392,7 +392,7 @@ function preprocessVueCode(code, setupScript) {
392
392
  if (node.type === "ExportDefaultDeclaration") {
393
393
  if (node.declaration.type === "ObjectExpression") {
394
394
  options = node.declaration.properties;
395
- } else if (node.declaration.type === "CallExpression" && node.declaration.arguments[0].type === "ObjectExpression") {
395
+ } else if (node.declaration.type === "CallExpression" && node.declaration.arguments[0]?.type === "ObjectExpression") {
396
396
  options = node.declaration.arguments[0].properties;
397
397
  } else if (node.declaration.type === "Identifier") {
398
398
  if (declRecord.has(node.declaration.name)) {
@@ -426,7 +426,7 @@ function preprocessVueCode(code, setupScript) {
426
426
  }
427
427
  }
428
428
  if (option.type === "ObjectProperty" && option.key.type === "Identifier" && option.key.name === "components") {
429
- source.remove(option.start, option.end);
429
+ source.overwrite(option.value.start, option.value.end, "undefined");
430
430
  }
431
431
  if (option.type === "ObjectMethod" && option.key.type === "Identifier" && option.key.name === "setup") {
432
432
  let exposed;
@@ -618,7 +618,7 @@ function dtsPlugin(options = {}) {
618
618
  rollupTypes = false,
619
619
  noEmitOnError = false,
620
620
  skipDiagnostics = false,
621
- copyDtsFiles = true,
621
+ copyDtsFiles = false,
622
622
  logLevel = void 0,
623
623
  afterDiagnostic = noop,
624
624
  beforeWriteFile = noop,
@@ -641,16 +641,17 @@ function dtsPlugin(options = {}) {
641
641
  let filter;
642
642
  let libFolderPath = options.libFolderPath;
643
643
  const sourceDtsFiles = /* @__PURE__ */ new Set();
644
+ const includedFiles = /* @__PURE__ */ new Set();
644
645
  const emittedFiles = /* @__PURE__ */ new Map();
645
646
  let hasJsVue = false;
646
647
  let allowJs = false;
647
648
  let transformError = false;
648
- function internalTransform(code, id) {
649
+ async function internalTransform(id) {
649
650
  if (!project || !filter(id)) {
650
651
  return;
651
652
  }
652
653
  if (vueRE.test(id)) {
653
- const { error, content, ext } = compileVueCode(code);
654
+ const { error, content, ext } = compileVueCode(await fs.readFile(id, "utf-8"));
654
655
  if (!transformError && error) {
655
656
  logger.error(
656
657
  red(
@@ -669,7 +670,7 @@ ${cyan(
669
670
  project.createSourceFile(`${id}.${ext || "js"}`, content, { overwrite: true });
670
671
  }
671
672
  } else if (!id.includes(".vue?vue") && (tsRE.test(id) || allowJs && jsRE.test(id))) {
672
- project.createSourceFile(id, code, { overwrite: true });
673
+ project.createSourceFile(id, await fs.readFile(id, "utf-8"), { overwrite: true });
673
674
  }
674
675
  }
675
676
  return {
@@ -775,7 +776,7 @@ ${cyan(
775
776
  filter = createFilter(include, exclude, { resolve: root });
776
777
  compilerOptions = tsConfig.compilerOptions;
777
778
  },
778
- buildStart(inputOptions) {
779
+ async buildStart(inputOptions) {
779
780
  if (Array.isArray(inputOptions.input)) {
780
781
  entries = inputOptions.input.reduce((prev, current) => {
781
782
  prev[basename(current)] = current;
@@ -784,49 +785,27 @@ ${cyan(
784
785
  } else {
785
786
  entries = { ...inputOptions.input };
786
787
  }
787
- },
788
- transform(code, id) {
789
- internalTransform(code, id);
790
- return null;
791
- },
792
- async watchChange(id) {
793
- if (watchExtensionRE.test(id)) {
794
- isBundle = false;
795
- if (project) {
796
- const sourceFile = project.getSourceFile(normalizePath(id));
797
- sourceFile && project.removeSourceFile(sourceFile);
798
- internalTransform(await fs.readFile(id, "utf-8"), id);
799
- }
800
- }
801
- },
802
- async closeBundle() {
803
- if (!outputDirs || !project || isBundle)
804
- return;
805
- logger.info(green(`
806
- ${logPrefix} Start generate declaration files...`));
807
- bundleDebug("start");
808
- isBundle = true;
788
+ bundleDebug("parse entries");
809
789
  sourceDtsFiles.clear();
810
- emittedFiles.clear();
811
- const startTime = Date.now();
812
- const includedFileSet = /* @__PURE__ */ new Set();
813
- if (include && include.length) {
790
+ includedFiles.clear();
791
+ if (project && include && include.length) {
814
792
  const files = await glob(include, {
815
793
  cwd: root,
816
794
  absolute: true,
817
795
  ignore: exclude
818
796
  });
819
- files.forEach((file) => {
797
+ for (const file of files) {
798
+ this.addWatchFile(file);
820
799
  if (dtsRE.test(file)) {
821
800
  sourceDtsFiles.add(project.addSourceFileAtPath(file));
822
801
  if (!copyDtsFiles) {
823
- return;
802
+ continue;
824
803
  }
825
- includedFileSet.add(file);
826
- return;
804
+ includedFiles.add(file);
805
+ continue;
827
806
  }
828
- includedFileSet.add(`${file.replace(tjsRE, "")}.d.${extPrefix(file)}ts`);
829
- });
807
+ includedFiles.add(`${file.replace(tjsRE, "")}.d.${extPrefix(file)}ts`);
808
+ }
830
809
  if (hasJsVue) {
831
810
  if (!allowJs) {
832
811
  logger.warn(
@@ -841,6 +820,30 @@ ${logPrefix} Start generate declaration files...`));
841
820
  }
842
821
  bundleDebug("collect files");
843
822
  }
823
+ },
824
+ async transform(_, id) {
825
+ await internalTransform(id);
826
+ return null;
827
+ },
828
+ async watchChange(id) {
829
+ if (watchExtensionRE.test(id)) {
830
+ isBundle = false;
831
+ if (project) {
832
+ const sourceFile = project.getSourceFile(normalizePath(id));
833
+ sourceFile && project.removeSourceFile(sourceFile);
834
+ await internalTransform(id);
835
+ }
836
+ }
837
+ },
838
+ async closeBundle() {
839
+ if (!outputDirs || !project || isBundle)
840
+ return;
841
+ logger.info(green(`
842
+ ${logPrefix} Start generate declaration files...`));
843
+ bundleDebug("start");
844
+ isBundle = true;
845
+ emittedFiles.clear();
846
+ const startTime = Date.now();
844
847
  project.resolveSourceFileDependencies();
845
848
  bundleDebug("resolve");
846
849
  if (!skipDiagnostics) {
@@ -873,7 +876,7 @@ ${logPrefix} Start generate declaration files...`));
873
876
  let filePath = outputFile.path;
874
877
  let content = outputFile.content;
875
878
  const isMapFile = filePath.endsWith(".map");
876
- if (!includedFileSet.has(isMapFile ? filePath.slice(0, -4) : filePath) || clearPureImport && content === noneExport) {
879
+ if (!includedFiles.has(isMapFile ? filePath.slice(0, -4) : filePath) || clearPureImport && content === noneExport) {
877
880
  return;
878
881
  }
879
882
  if (!isMapFile && content && content !== noneExport) {
@@ -901,14 +904,6 @@ ${logPrefix} Start generate declaration files...`));
901
904
  emittedFiles.set(filePath, content);
902
905
  });
903
906
  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");
912
907
  if (insertTypesEntry || rollupTypes) {
913
908
  const pkgPath = resolve(root, "package.json");
914
909
  const pkg = fs.existsSync(pkgPath) ? JSON.parse(await fs.readFile(pkgPath, "utf-8")) : {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-dts",
3
- "version": "2.0.0-beta.3",
3
+ "version": "2.0.1",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "author": "qmhc",