vite-plugin-lib 1.3.0 → 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/dist/index.cjs CHANGED
@@ -6,8 +6,8 @@ const node_module = require('node:module');
6
6
  const path = require('node:path');
7
7
  const c = require('picocolors');
8
8
  const ts = require('typescript');
9
- const vite = require('vite');
10
9
  const dts = require('vite-plugin-dts');
10
+ const vite = require('vite');
11
11
 
12
12
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
13
13
 
@@ -29,7 +29,6 @@ const ts__default = /*#__PURE__*/_interopDefaultCompat(ts);
29
29
  const dts__default = /*#__PURE__*/_interopDefaultCompat(dts);
30
30
  const dts__namespace = /*#__PURE__*/_interopNamespaceCompat(dts);
31
31
 
32
- const typesDir = "dist/types";
33
32
  function log(text) {
34
33
  console.log(`${c__default.cyan("[vite:lib]")} ${text}`);
35
34
  }
@@ -39,6 +38,52 @@ function logWarn(text) {
39
38
  function logError(text) {
40
39
  console.error(`${c__default.red("[vite:lib]")} ${text}`);
41
40
  }
41
+
42
+ async function generateMTSDeclarations(typesDir) {
43
+ const files = await collectFiles(typesDir);
44
+ for (const file of files) {
45
+ await createMTSImports(file);
46
+ }
47
+ log(`Generated ${files.length} MTS declarations.`);
48
+ }
49
+ async function collectFiles(dir) {
50
+ const entries = await promises.readdir(dir, {
51
+ recursive: false,
52
+ // does not provide full path to nested files
53
+ withFileTypes: true
54
+ });
55
+ const files = entries.filter((entry) => entry.isFile());
56
+ const nestedFiles = await Promise.all(
57
+ entries.filter((entry) => entry.isDirectory()).map((entry) => collectFiles(vite.normalizePath(path__default.join(dir, entry.name))))
58
+ );
59
+ return files.map((file) => vite.normalizePath(path__default.join(dir, file.name))).concat(...nestedFiles);
60
+ }
61
+ async function createMTSImports(file) {
62
+ const content = await promises.readFile(file, "utf-8");
63
+ const lines = content.split("\n");
64
+ const modified = lines.map(transformLine);
65
+ const targetFile = file.replace(".d.ts", ".d.mts");
66
+ await promises.writeFile(targetFile, modified.join("\n"));
67
+ }
68
+ function transformLine(line) {
69
+ return transformStaticImport(line, "'") ?? transformStaticImport(line, '"') ?? transformExport(line, "'") ?? transformExport(line, '"') ?? line;
70
+ }
71
+ function transformStaticImport(line, quote) {
72
+ const isStaticImport = line.includes("import ") && line.includes(`from ${quote}.`);
73
+ if (!isStaticImport) {
74
+ return void 0;
75
+ }
76
+ return `${line.substring(0, line.length - 2)}.mjs${quote};`;
77
+ }
78
+ function transformExport(line, quote) {
79
+ const isStaticExport = line.includes("export ") && line.includes(` from ${quote}.`);
80
+ if (!isStaticExport) {
81
+ return void 0;
82
+ }
83
+ return `${line.substring(0, line.length - 2)}.mjs${quote};`;
84
+ }
85
+
86
+ const typesDir = "dist/types";
42
87
  function tsconfigPaths({ verbose } = {}) {
43
88
  return {
44
89
  name: "vite-plugin-lib:alias",
@@ -179,7 +224,7 @@ function library(options) {
179
224
  include: `${path__default.resolve(options.entry, "..")}/**`,
180
225
  outDir: typesDir,
181
226
  staticImport: true,
182
- afterBuild: includesESFormat(options.formats) ? () => copyMTSDeclaration(options) : void 0
227
+ afterBuild: includesESFormat(options.formats) ? () => generateMTSDeclarations(typesDir) : void 0
183
228
  })
184
229
  ];
185
230
  }
@@ -222,26 +267,6 @@ function getErrorMessage(error) {
222
267
  const isObject = typeof error === "object" && error !== null && "message" in error;
223
268
  return isObject ? error.message : String(error);
224
269
  }
225
- async function copyMTSDeclaration(options) {
226
- const declarationFile = options.entry.replace(".ts", ".d.ts").substring(options.entry.lastIndexOf("/") + 1);
227
- const mtsDeclarationFile = declarationFile.replace(".ts", ".mts");
228
- const sourceDeclaration = vite.normalizePath(
229
- path__default.resolve(typesDir, declarationFile)
230
- );
231
- const targetDeclaration = vite.normalizePath(
232
- path__default.resolve(typesDir, mtsDeclarationFile)
233
- );
234
- log(
235
- `Creating mts declaration file ${mtsDeclarationFile} based on ${declarationFile}`
236
- );
237
- try {
238
- await promises.copyFile(sourceDeclaration, targetDeclaration);
239
- } catch (error) {
240
- const message = getErrorMessage(error);
241
- logError(`Could not create mts declaration file: ${message}`);
242
- throw error;
243
- }
244
- }
245
270
 
246
271
  exports.dts = dts__namespace;
247
272
  exports.library = library;
package/dist/index.mjs CHANGED
@@ -1,15 +1,14 @@
1
1
  import { existsSync } from 'node:fs';
2
- import { readFile, copyFile } from 'node:fs/promises';
2
+ import { readdir, readFile, writeFile } from 'node:fs/promises';
3
3
  import { builtinModules } from 'node:module';
4
4
  import path from 'node:path';
5
5
  import c from 'picocolors';
6
6
  import ts from 'typescript';
7
- import { normalizePath } from 'vite';
8
7
  import dts__default from 'vite-plugin-dts';
9
8
  import * as dts from 'vite-plugin-dts';
10
9
  export { dts };
10
+ import { normalizePath } from 'vite';
11
11
 
12
- const typesDir = "dist/types";
13
12
  function log(text) {
14
13
  console.log(`${c.cyan("[vite:lib]")} ${text}`);
15
14
  }
@@ -19,6 +18,52 @@ function logWarn(text) {
19
18
  function logError(text) {
20
19
  console.error(`${c.red("[vite:lib]")} ${text}`);
21
20
  }
21
+
22
+ async function generateMTSDeclarations(typesDir) {
23
+ const files = await collectFiles(typesDir);
24
+ for (const file of files) {
25
+ await createMTSImports(file);
26
+ }
27
+ log(`Generated ${files.length} MTS declarations.`);
28
+ }
29
+ async function collectFiles(dir) {
30
+ const entries = await readdir(dir, {
31
+ recursive: false,
32
+ // does not provide full path to nested files
33
+ withFileTypes: true
34
+ });
35
+ const files = entries.filter((entry) => entry.isFile());
36
+ const nestedFiles = await Promise.all(
37
+ entries.filter((entry) => entry.isDirectory()).map((entry) => collectFiles(normalizePath(path.join(dir, entry.name))))
38
+ );
39
+ return files.map((file) => normalizePath(path.join(dir, file.name))).concat(...nestedFiles);
40
+ }
41
+ async function createMTSImports(file) {
42
+ const content = await readFile(file, "utf-8");
43
+ const lines = content.split("\n");
44
+ const modified = lines.map(transformLine);
45
+ const targetFile = file.replace(".d.ts", ".d.mts");
46
+ await writeFile(targetFile, modified.join("\n"));
47
+ }
48
+ function transformLine(line) {
49
+ return transformStaticImport(line, "'") ?? transformStaticImport(line, '"') ?? transformExport(line, "'") ?? transformExport(line, '"') ?? line;
50
+ }
51
+ function transformStaticImport(line, quote) {
52
+ const isStaticImport = line.includes("import ") && line.includes(`from ${quote}.`);
53
+ if (!isStaticImport) {
54
+ return void 0;
55
+ }
56
+ return `${line.substring(0, line.length - 2)}.mjs${quote};`;
57
+ }
58
+ function transformExport(line, quote) {
59
+ const isStaticExport = line.includes("export ") && line.includes(` from ${quote}.`);
60
+ if (!isStaticExport) {
61
+ return void 0;
62
+ }
63
+ return `${line.substring(0, line.length - 2)}.mjs${quote};`;
64
+ }
65
+
66
+ const typesDir = "dist/types";
22
67
  function tsconfigPaths({ verbose } = {}) {
23
68
  return {
24
69
  name: "vite-plugin-lib:alias",
@@ -159,7 +204,7 @@ function library(options) {
159
204
  include: `${path.resolve(options.entry, "..")}/**`,
160
205
  outDir: typesDir,
161
206
  staticImport: true,
162
- afterBuild: includesESFormat(options.formats) ? () => copyMTSDeclaration(options) : void 0
207
+ afterBuild: includesESFormat(options.formats) ? () => generateMTSDeclarations(typesDir) : void 0
163
208
  })
164
209
  ];
165
210
  }
@@ -202,25 +247,5 @@ function getErrorMessage(error) {
202
247
  const isObject = typeof error === "object" && error !== null && "message" in error;
203
248
  return isObject ? error.message : String(error);
204
249
  }
205
- async function copyMTSDeclaration(options) {
206
- const declarationFile = options.entry.replace(".ts", ".d.ts").substring(options.entry.lastIndexOf("/") + 1);
207
- const mtsDeclarationFile = declarationFile.replace(".ts", ".mts");
208
- const sourceDeclaration = normalizePath(
209
- path.resolve(typesDir, declarationFile)
210
- );
211
- const targetDeclaration = normalizePath(
212
- path.resolve(typesDir, mtsDeclarationFile)
213
- );
214
- log(
215
- `Creating mts declaration file ${mtsDeclarationFile} based on ${declarationFile}`
216
- );
217
- try {
218
- await copyFile(sourceDeclaration, targetDeclaration);
219
- } catch (error) {
220
- const message = getErrorMessage(error);
221
- logError(`Could not create mts declaration file: ${message}`);
222
- throw error;
223
- }
224
- }
225
250
 
226
251
  export { library, tsconfigPaths };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-lib",
3
- "version": "1.3.0",
3
+ "version": "1.5.0",
4
4
  "description": "Vite plugin for build configuration, automatic aliases, and type declarations.",
5
5
  "author": "Jan Müller <janmueller3698@gmail.com>",
6
6
  "license": "MIT",