vite-plugin-lib 1.2.3 → 1.4.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
@@ -7,6 +7,7 @@ const path = require('node:path');
7
7
  const c = require('picocolors');
8
8
  const ts = require('typescript');
9
9
  const dts = require('vite-plugin-dts');
10
+ const vite = require('vite');
10
11
 
11
12
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
12
13
 
@@ -37,6 +38,46 @@ function logWarn(text) {
37
38
  function logError(text) {
38
39
  console.error(`${c__default.red("[vite:lib]")} ${text}`);
39
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
+ const isStaticImport = line.includes("import ") && line.includes(`from '.`);
70
+ if (isStaticImport) {
71
+ return `${line.substring(0, line.length - 2)}.d.mts';`;
72
+ }
73
+ const isStaticExport = line.includes("export ") && line.includes(` from '.`);
74
+ if (isStaticExport) {
75
+ return `${line.substring(0, line.length - 2)}.mjs';`;
76
+ }
77
+ return line;
78
+ }
79
+
80
+ const typesDir = "dist/types";
40
81
  function tsconfigPaths({ verbose } = {}) {
41
82
  return {
42
83
  name: "vite-plugin-lib:alias",
@@ -175,8 +216,9 @@ function library(options) {
175
216
  cleanVueFileName: true,
176
217
  copyDtsFiles: true,
177
218
  include: `${path__default.resolve(options.entry, "..")}/**`,
178
- outDir: "dist/types",
179
- staticImport: true
219
+ outDir: typesDir,
220
+ staticImport: true,
221
+ afterBuild: includesESFormat(options.formats) ? () => generateMTSDeclarations(typesDir) : void 0
180
222
  })
181
223
  ];
182
224
  }
@@ -207,11 +249,18 @@ async function readConfig(configPath) {
207
249
  );
208
250
  return options;
209
251
  } catch (error) {
210
- const message = "message" in error ? error.message : error;
252
+ const message = getErrorMessage(error);
211
253
  logError(`Could not read tsconfig.json: ${message}`);
212
254
  throw error;
213
255
  }
214
256
  }
257
+ function includesESFormat(formats) {
258
+ return formats?.includes("es") ?? true;
259
+ }
260
+ function getErrorMessage(error) {
261
+ const isObject = typeof error === "object" && error !== null && "message" in error;
262
+ return isObject ? error.message : String(error);
263
+ }
215
264
 
216
265
  exports.dts = dts__namespace;
217
266
  exports.library = library;
@@ -0,0 +1,15 @@
1
+ import { LibraryFormats, Plugin } from 'vite';
2
+ import * as vitePluginDts from 'vite-plugin-dts';
3
+ export { vitePluginDts as dts };
4
+
5
+ interface Options {
6
+ name: string;
7
+ entry: string;
8
+ formats?: LibraryFormats[];
9
+ externalPackages?: (string | RegExp)[];
10
+ verbose?: boolean;
11
+ }
12
+ declare function tsconfigPaths({ verbose }?: Partial<Options>): Plugin;
13
+ declare function library(options: Options): Plugin[];
14
+
15
+ export { Options, library, tsconfigPaths };
@@ -0,0 +1,15 @@
1
+ import { LibraryFormats, Plugin } from 'vite';
2
+ import * as vitePluginDts from 'vite-plugin-dts';
3
+ export { vitePluginDts as dts };
4
+
5
+ interface Options {
6
+ name: string;
7
+ entry: string;
8
+ formats?: LibraryFormats[];
9
+ externalPackages?: (string | RegExp)[];
10
+ verbose?: boolean;
11
+ }
12
+ declare function tsconfigPaths({ verbose }?: Partial<Options>): Plugin;
13
+ declare function library(options: Options): Plugin[];
14
+
15
+ export { Options, library, tsconfigPaths };
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { existsSync } from 'node:fs';
2
- import { readFile } 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';
@@ -7,6 +7,7 @@ import ts from 'typescript';
7
7
  import dts__default from 'vite-plugin-dts';
8
8
  import * as dts from 'vite-plugin-dts';
9
9
  export { dts };
10
+ import { normalizePath } from 'vite';
10
11
 
11
12
  function log(text) {
12
13
  console.log(`${c.cyan("[vite:lib]")} ${text}`);
@@ -17,6 +18,46 @@ function logWarn(text) {
17
18
  function logError(text) {
18
19
  console.error(`${c.red("[vite:lib]")} ${text}`);
19
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
+ const isStaticImport = line.includes("import ") && line.includes(`from '.`);
50
+ if (isStaticImport) {
51
+ return `${line.substring(0, line.length - 2)}.d.mts';`;
52
+ }
53
+ const isStaticExport = line.includes("export ") && line.includes(` from '.`);
54
+ if (isStaticExport) {
55
+ return `${line.substring(0, line.length - 2)}.mjs';`;
56
+ }
57
+ return line;
58
+ }
59
+
60
+ const typesDir = "dist/types";
20
61
  function tsconfigPaths({ verbose } = {}) {
21
62
  return {
22
63
  name: "vite-plugin-lib:alias",
@@ -155,8 +196,9 @@ function library(options) {
155
196
  cleanVueFileName: true,
156
197
  copyDtsFiles: true,
157
198
  include: `${path.resolve(options.entry, "..")}/**`,
158
- outDir: "dist/types",
159
- staticImport: true
199
+ outDir: typesDir,
200
+ staticImport: true,
201
+ afterBuild: includesESFormat(options.formats) ? () => generateMTSDeclarations(typesDir) : void 0
160
202
  })
161
203
  ];
162
204
  }
@@ -187,10 +229,17 @@ async function readConfig(configPath) {
187
229
  );
188
230
  return options;
189
231
  } catch (error) {
190
- const message = "message" in error ? error.message : error;
232
+ const message = getErrorMessage(error);
191
233
  logError(`Could not read tsconfig.json: ${message}`);
192
234
  throw error;
193
235
  }
194
236
  }
237
+ function includesESFormat(formats) {
238
+ return formats?.includes("es") ?? true;
239
+ }
240
+ function getErrorMessage(error) {
241
+ const isObject = typeof error === "object" && error !== null && "message" in error;
242
+ return isObject ? error.message : String(error);
243
+ }
195
244
 
196
245
  export { library, tsconfigPaths };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-lib",
3
- "version": "1.2.3",
3
+ "version": "1.4.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",
@@ -18,9 +18,14 @@
18
18
  ],
19
19
  "exports": {
20
20
  ".": {
21
- "types": "./dist/index.d.ts",
22
- "require": "./dist/index.cjs",
23
- "import": "./dist/index.mjs"
21
+ "require": {
22
+ "types": "./dist/index.d.ts",
23
+ "default": "./dist/index.cjs"
24
+ },
25
+ "import": {
26
+ "types": "./dist/index.d.mts",
27
+ "default": "./dist/index.mjs"
28
+ }
24
29
  }
25
30
  },
26
31
  "main": "dist/index.cjs",
@@ -36,13 +41,13 @@
36
41
  },
37
42
  "dependencies": {
38
43
  "picocolors": "1.0.0",
39
- "vite-plugin-dts": "3.0.0"
44
+ "vite-plugin-dts": "3.3.1"
40
45
  },
41
46
  "devDependencies": {
42
- "@types/node": "18.16.18",
43
- "typescript": "5.1.3",
44
- "unbuild": "1.2.1",
45
- "vite": "4.3.9",
47
+ "@types/node": "18.17.1",
48
+ "typescript": "5.1.6",
49
+ "unbuild": "v2.0.0-rc.0",
50
+ "vite": "4.4.7",
46
51
  "@yeger/tsconfig": "1.1.2"
47
52
  },
48
53
  "publishConfig": {
@@ -51,7 +56,7 @@
51
56
  "scripts": {
52
57
  "build": "unbuild",
53
58
  "check:publish": "publint run --strict",
54
- "check:tsc": "tsc --noEmit",
59
+ "check:tsc": "tsc",
55
60
  "lint": "yeger-lint"
56
61
  }
57
62
  }