vite-plugin-dts 1.3.1 → 1.5.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 CHANGED
@@ -97,6 +97,11 @@ export interface PluginOptions {
97
97
  // Default: 'tsconfig.json'
98
98
  tsConfigFilePath?: string
99
99
 
100
+ // Set which paths should exclude when transform aliases
101
+ // If it's regexp, it will test the original import path directly
102
+ // Default: []
103
+ aliasesExclude?: (string | RegExp)[]
104
+
100
105
  // Whether transform file name '.vue.d.ts' to '.d.ts'
101
106
  // Default: false
102
107
  cleanVueFileName?: boolean
package/README.zh-CN.md CHANGED
@@ -96,6 +96,11 @@ export interface PluginOptions {
96
96
  // 默认值: 'tsconfig.json'
97
97
  tsConfigFilePath?: string
98
98
 
99
+ // 设置在转换别名时哪些路径需要排除
100
+ // 如果为正则,会直接使用 test 和原始路径进行比较
101
+ // 默认值: []
102
+ aliasesExclude?: (string | RegExp)[]
103
+
99
104
  // 是否将 '.vue.d.ts' 文件名转换为 '.d.ts'
100
105
  // 默认值: false
101
106
  cleanVueFileName?: boolean
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Plugin, Alias } from 'vite';
1
+ import { Plugin } from 'vite';
2
2
  import { ts, Diagnostic } from 'ts-morph';
3
3
 
4
4
  interface TransformWriteFile {
@@ -13,7 +13,7 @@ interface PluginOptions {
13
13
  entryRoot?: string;
14
14
  compilerOptions?: ts.CompilerOptions | null;
15
15
  tsConfigFilePath?: string;
16
- aliasesExclude?: Alias['find'][];
16
+ aliasesExclude?: (string | RegExp)[];
17
17
  cleanVueFileName?: boolean;
18
18
  staticImport?: boolean;
19
19
  clearPureImport?: boolean;
package/dist/index.js CHANGED
@@ -219,7 +219,7 @@ var staticImportRE = /(?:import|export)\s?(?:type)?\s?\{?.+\}?\s?from\s?['"](.+)
219
219
  var dynamicImportRE = /import\(['"]([^;\n]+?)['"]\)/;
220
220
  var simpleStaticImportRE = /((?:import|export).+from\s?)['"](.+)['"]/;
221
221
  var simpleDynamicImportRE = /(import\()['"](.+)['"]\)/;
222
- function transformAliasImport(filePath, content, aliases) {
222
+ function transformAliasImport(filePath, content, aliases, exclude = []) {
223
223
  if (!aliases.length)
224
224
  return content;
225
225
  return content.replace(globalImportRE, (str) => {
@@ -232,6 +232,9 @@ function transformAliasImport(filePath, content, aliases) {
232
232
  if (matchResult == null ? void 0 : matchResult[1]) {
233
233
  const matchedAlias = aliases.find((alias) => isAliasMatch(alias, matchResult[1]));
234
234
  if (matchedAlias) {
235
+ if (exclude.some((e) => isRegExp(e) ? e.test(matchResult[1]) : String(e) === matchResult[1])) {
236
+ return str;
237
+ }
235
238
  const truthPath = (0, import_path2.isAbsolute)(matchedAlias.replacement) ? (0, import_vite.normalizePath)((0, import_path2.relative)((0, import_path2.dirname)(filePath), matchedAlias.replacement)) : (0, import_vite.normalizePath)(matchedAlias.replacement);
236
239
  return str.replace(isDynamic ? simpleDynamicImportRE : simpleStaticImportRE, `$1'${matchResult[1].replace(matchedAlias.find, (truthPath.startsWith(".") ? truthPath : `./${truthPath}`) + (typeof matchedAlias.find === "string" && matchedAlias.find.endsWith("/") ? "/" : ""))}'${isDynamic ? ")" : ""}`);
237
240
  }
@@ -256,9 +259,11 @@ function transferSetupPosition(content) {
256
259
  // src/compile.ts
257
260
  var exportDefaultRE = /export\s+default/;
258
261
  var exportDefaultClassRE = /(?:(?:^|\n|;)\s*)export\s+default\s+class\s+([\w$]+)/;
262
+ var noScriptContent = "import { defineComponent } from 'vue'\nexport default defineComponent({})";
259
263
  var index = 1;
260
264
  var compileRoot = null;
261
265
  var compiler;
266
+ var vue;
262
267
  function requireCompiler() {
263
268
  if (!compiler) {
264
269
  if (compileRoot) {
@@ -281,15 +286,43 @@ function requireCompiler() {
281
286
  }
282
287
  return compiler;
283
288
  }
289
+ function isVue3() {
290
+ if (!vue) {
291
+ if (compileRoot) {
292
+ try {
293
+ vue = require(require.resolve("vue", { paths: [compileRoot] }));
294
+ } catch (e) {
295
+ }
296
+ }
297
+ if (!vue) {
298
+ try {
299
+ vue = require("vue");
300
+ } catch (e) {
301
+ throw new Error("vue is not present in the dependency tree.\n");
302
+ }
303
+ }
304
+ }
305
+ return vue.version.startsWith("3");
306
+ }
284
307
  function setCompileRoot(root) {
285
308
  if (root && root !== compileRoot) {
286
309
  compileRoot = root;
287
310
  compiler = null;
288
311
  }
289
312
  }
313
+ function parseCode(code) {
314
+ const { parse } = requireCompiler();
315
+ let descriptor;
316
+ if (isVue3()) {
317
+ descriptor = parse(code).descriptor;
318
+ } else {
319
+ descriptor = parse({ source: code });
320
+ }
321
+ return descriptor;
322
+ }
290
323
  function compileVueCode(code) {
291
- const { parse, compileScript, rewriteDefault } = requireCompiler();
292
- const { descriptor } = parse(code);
324
+ const { compileScript, rewriteDefault } = requireCompiler();
325
+ const descriptor = parseCode(code);
293
326
  const { script, scriptSetup } = descriptor;
294
327
  let content = null;
295
328
  let ext = null;
@@ -317,6 +350,9 @@ const _sfc_main = ${classMatch[1]}`;
317
350
  content += "\nexport default _sfc_main\n";
318
351
  ext = script.lang || "js";
319
352
  }
353
+ } else {
354
+ content = noScriptContent;
355
+ ext = "ts";
320
356
  }
321
357
  return { content, ext };
322
358
  }
@@ -549,7 +585,7 @@ ${import_chalk.default.cyan("[vite:dts]")} Can not resolve declaration directory
549
585
  }
550
586
  },
551
587
  async closeBundle() {
552
- var _a2, _b2, _c, _d, _e, _f;
588
+ var _a2, _b2, _c, _d, _e, _f, _g, _h, _i;
553
589
  if (!outputDirs || !project || isBundle)
554
590
  return;
555
591
  logger.info(import_chalk.default.green(`
@@ -559,15 +595,17 @@ ${logPrefix} Start generate declaration files...`));
559
595
  sourceDtsFiles.clear();
560
596
  const startTime = Date.now();
561
597
  const tsConfig = (_a2 = (0, import_typescript.readConfigFile)(tsConfigPath, project.getFileSystem().readFileSync).config) != null ? _a2 : {};
562
- const include = (_c = (_b2 = options.include) != null ? _b2 : tsConfig.include) != null ? _c : "**/*";
563
- const exclude = (_d = options.exclude) != null ? _d : tsConfig.exclude;
598
+ const parentTsConfigPath = tsConfig.extends && ensureAbsolute(tsConfig.extends, root);
599
+ const parentTsConfig = parentTsConfigPath ? (0, import_typescript.readConfigFile)(parentTsConfigPath, project.getFileSystem().readFileSync).config : {};
600
+ const include = (_d = (_c = (_b2 = options.include) != null ? _b2 : tsConfig.include) != null ? _c : parentTsConfig.include) != null ? _d : "**/*";
601
+ const exclude = (_g = (_f = (_e = options.exclude) != null ? _e : tsConfig.exclude) != null ? _f : parentTsConfig.exclude) != null ? _g : "node_modules/**";
564
602
  bundleDebug("read config");
565
603
  const includedFileSet = /* @__PURE__ */ new Set();
566
604
  if (include && include.length) {
567
605
  const files = await (0, import_fast_glob.default)(ensureArray(include).map(normalizeGlob), {
568
606
  cwd: root,
569
607
  absolute: true,
570
- ignore: ensureArray(exclude != null ? exclude : ["node_modules/**"]).map(normalizeGlob)
608
+ ignore: ensureArray(exclude).map(normalizeGlob)
571
609
  });
572
610
  files.forEach((file) => {
573
611
  if (dtsRE2.test(file)) {
@@ -627,7 +665,7 @@ ${logPrefix} Start generate declaration files...`));
627
665
  }
628
666
  if (!isMapFile && content && content !== noneExport) {
629
667
  content = clearPureImport ? removePureImport(content) : content;
630
- content = transformAliasImport(filePath, content, aliases);
668
+ content = transformAliasImport(filePath, content, aliases, aliasesExclude);
631
669
  content = staticImport || rollupTypes ? transformDynamicImport(content) : content;
632
670
  }
633
671
  filePath = (0, import_path4.resolve)(outputDir, (0, import_path4.relative)(entryRoot, cleanVueFileName ? filePath.replace(".vue.d.ts", ".d.ts") : filePath));
@@ -667,8 +705,8 @@ export default ${libName}
667
705
  if (typeof beforeWriteFile === "function") {
668
706
  const result = beforeWriteFile(typesPath, content);
669
707
  if (result && isNativeObj(result)) {
670
- typesPath = (_e = result.filePath) != null ? _e : typesPath;
671
- content = (_f = result.content) != null ? _f : content;
708
+ typesPath = (_h = result.filePath) != null ? _h : typesPath;
709
+ content = (_i = result.content) != null ? _i : content;
672
710
  }
673
711
  }
674
712
  await import_fs_extra.default.writeFile(typesPath, content, "utf-8");
package/dist/index.mjs CHANGED
@@ -198,7 +198,7 @@ var staticImportRE = /(?:import|export)\s?(?:type)?\s?\{?.+\}?\s?from\s?['"](.+)
198
198
  var dynamicImportRE = /import\(['"]([^;\n]+?)['"]\)/;
199
199
  var simpleStaticImportRE = /((?:import|export).+from\s?)['"](.+)['"]/;
200
200
  var simpleDynamicImportRE = /(import\()['"](.+)['"]\)/;
201
- function transformAliasImport(filePath, content, aliases) {
201
+ function transformAliasImport(filePath, content, aliases, exclude = []) {
202
202
  if (!aliases.length)
203
203
  return content;
204
204
  return content.replace(globalImportRE, (str) => {
@@ -211,6 +211,9 @@ function transformAliasImport(filePath, content, aliases) {
211
211
  if (matchResult == null ? void 0 : matchResult[1]) {
212
212
  const matchedAlias = aliases.find((alias) => isAliasMatch(alias, matchResult[1]));
213
213
  if (matchedAlias) {
214
+ if (exclude.some((e) => isRegExp(e) ? e.test(matchResult[1]) : String(e) === matchResult[1])) {
215
+ return str;
216
+ }
214
217
  const truthPath = isAbsolute2(matchedAlias.replacement) ? normalizePath(relative(dirname2(filePath), matchedAlias.replacement)) : normalizePath(matchedAlias.replacement);
215
218
  return str.replace(isDynamic ? simpleDynamicImportRE : simpleStaticImportRE, `$1'${matchResult[1].replace(matchedAlias.find, (truthPath.startsWith(".") ? truthPath : `./${truthPath}`) + (typeof matchedAlias.find === "string" && matchedAlias.find.endsWith("/") ? "/" : ""))}'${isDynamic ? ")" : ""}`);
216
219
  }
@@ -235,9 +238,11 @@ function transferSetupPosition(content) {
235
238
  // src/compile.ts
236
239
  var exportDefaultRE = /export\s+default/;
237
240
  var exportDefaultClassRE = /(?:(?:^|\n|;)\s*)export\s+default\s+class\s+([\w$]+)/;
241
+ var noScriptContent = "import { defineComponent } from 'vue'\nexport default defineComponent({})";
238
242
  var index = 1;
239
243
  var compileRoot = null;
240
244
  var compiler;
245
+ var vue;
241
246
  function requireCompiler() {
242
247
  if (!compiler) {
243
248
  if (compileRoot) {
@@ -260,15 +265,43 @@ function requireCompiler() {
260
265
  }
261
266
  return compiler;
262
267
  }
268
+ function isVue3() {
269
+ if (!vue) {
270
+ if (compileRoot) {
271
+ try {
272
+ vue = __require(__require.resolve("vue", { paths: [compileRoot] }));
273
+ } catch (e) {
274
+ }
275
+ }
276
+ if (!vue) {
277
+ try {
278
+ vue = __require("vue");
279
+ } catch (e) {
280
+ throw new Error("vue is not present in the dependency tree.\n");
281
+ }
282
+ }
283
+ }
284
+ return vue.version.startsWith("3");
285
+ }
263
286
  function setCompileRoot(root) {
264
287
  if (root && root !== compileRoot) {
265
288
  compileRoot = root;
266
289
  compiler = null;
267
290
  }
268
291
  }
292
+ function parseCode(code) {
293
+ const { parse } = requireCompiler();
294
+ let descriptor;
295
+ if (isVue3()) {
296
+ descriptor = parse(code).descriptor;
297
+ } else {
298
+ descriptor = parse({ source: code });
299
+ }
300
+ return descriptor;
301
+ }
269
302
  function compileVueCode(code) {
270
- const { parse, compileScript, rewriteDefault } = requireCompiler();
271
- const { descriptor } = parse(code);
303
+ const { compileScript, rewriteDefault } = requireCompiler();
304
+ const descriptor = parseCode(code);
272
305
  const { script, scriptSetup } = descriptor;
273
306
  let content = null;
274
307
  let ext = null;
@@ -296,6 +329,9 @@ const _sfc_main = ${classMatch[1]}`;
296
329
  content += "\nexport default _sfc_main\n";
297
330
  ext = script.lang || "js";
298
331
  }
332
+ } else {
333
+ content = noScriptContent;
334
+ ext = "ts";
299
335
  }
300
336
  return { content, ext };
301
337
  }
@@ -531,7 +567,7 @@ ${chalk.cyan("[vite:dts]")} Can not resolve declaration directory, please check
531
567
  }
532
568
  },
533
569
  async closeBundle() {
534
- var _a2, _b2, _c, _d, _e, _f;
570
+ var _a2, _b2, _c, _d, _e, _f, _g, _h, _i;
535
571
  if (!outputDirs || !project || isBundle)
536
572
  return;
537
573
  logger.info(chalk.green(`
@@ -541,15 +577,17 @@ ${logPrefix} Start generate declaration files...`));
541
577
  sourceDtsFiles.clear();
542
578
  const startTime = Date.now();
543
579
  const tsConfig = (_a2 = readConfigFile(tsConfigPath, project.getFileSystem().readFileSync).config) != null ? _a2 : {};
544
- const include = (_c = (_b2 = options.include) != null ? _b2 : tsConfig.include) != null ? _c : "**/*";
545
- const exclude = (_d = options.exclude) != null ? _d : tsConfig.exclude;
580
+ const parentTsConfigPath = tsConfig.extends && ensureAbsolute(tsConfig.extends, root);
581
+ const parentTsConfig = parentTsConfigPath ? readConfigFile(parentTsConfigPath, project.getFileSystem().readFileSync).config : {};
582
+ const include = (_d = (_c = (_b2 = options.include) != null ? _b2 : tsConfig.include) != null ? _c : parentTsConfig.include) != null ? _d : "**/*";
583
+ const exclude = (_g = (_f = (_e = options.exclude) != null ? _e : tsConfig.exclude) != null ? _f : parentTsConfig.exclude) != null ? _g : "node_modules/**";
546
584
  bundleDebug("read config");
547
585
  const includedFileSet = /* @__PURE__ */ new Set();
548
586
  if (include && include.length) {
549
587
  const files = await glob(ensureArray(include).map(normalizeGlob), {
550
588
  cwd: root,
551
589
  absolute: true,
552
- ignore: ensureArray(exclude != null ? exclude : ["node_modules/**"]).map(normalizeGlob)
590
+ ignore: ensureArray(exclude).map(normalizeGlob)
553
591
  });
554
592
  files.forEach((file) => {
555
593
  if (dtsRE2.test(file)) {
@@ -609,7 +647,7 @@ ${logPrefix} Start generate declaration files...`));
609
647
  }
610
648
  if (!isMapFile && content && content !== noneExport) {
611
649
  content = clearPureImport ? removePureImport(content) : content;
612
- content = transformAliasImport(filePath, content, aliases);
650
+ content = transformAliasImport(filePath, content, aliases, aliasesExclude);
613
651
  content = staticImport || rollupTypes ? transformDynamicImport(content) : content;
614
652
  }
615
653
  filePath = resolve3(outputDir, relative2(entryRoot, cleanVueFileName ? filePath.replace(".vue.d.ts", ".d.ts") : filePath));
@@ -649,8 +687,8 @@ export default ${libName}
649
687
  if (typeof beforeWriteFile === "function") {
650
688
  const result = beforeWriteFile(typesPath, content);
651
689
  if (result && isNativeObj(result)) {
652
- typesPath = (_e = result.filePath) != null ? _e : typesPath;
653
- content = (_f = result.content) != null ? _f : content;
690
+ typesPath = (_h = result.filePath) != null ? _h : typesPath;
691
+ content = (_i = result.content) != null ? _i : content;
654
692
  }
655
693
  }
656
694
  await fs.writeFile(typesPath, content, "utf-8");
package/package.json CHANGED
@@ -93,5 +93,5 @@
93
93
  "test:e2e": "cd example && cross-env DEBUG=\"vite-plugin-dts:bundle\" vite build"
94
94
  },
95
95
  "types": "dist/index.d.ts",
96
- "version": "1.3.1"
96
+ "version": "1.5.0"
97
97
  }