vite-plugin-lib 1.3.0 → 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 +42 -23
- package/dist/index.mjs +43 -24
- package/package.json +1 -1
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,46 @@ 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
|
+
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";
|
|
42
81
|
function tsconfigPaths({ verbose } = {}) {
|
|
43
82
|
return {
|
|
44
83
|
name: "vite-plugin-lib:alias",
|
|
@@ -179,7 +218,7 @@ function library(options) {
|
|
|
179
218
|
include: `${path__default.resolve(options.entry, "..")}/**`,
|
|
180
219
|
outDir: typesDir,
|
|
181
220
|
staticImport: true,
|
|
182
|
-
afterBuild: includesESFormat(options.formats) ? () =>
|
|
221
|
+
afterBuild: includesESFormat(options.formats) ? () => generateMTSDeclarations(typesDir) : void 0
|
|
183
222
|
})
|
|
184
223
|
];
|
|
185
224
|
}
|
|
@@ -222,26 +261,6 @@ function getErrorMessage(error) {
|
|
|
222
261
|
const isObject = typeof error === "object" && error !== null && "message" in error;
|
|
223
262
|
return isObject ? error.message : String(error);
|
|
224
263
|
}
|
|
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
264
|
|
|
246
265
|
exports.dts = dts__namespace;
|
|
247
266
|
exports.library = library;
|
package/dist/index.mjs
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import { existsSync } from 'node:fs';
|
|
2
|
-
import { readFile,
|
|
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,46 @@ 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
|
+
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";
|
|
22
61
|
function tsconfigPaths({ verbose } = {}) {
|
|
23
62
|
return {
|
|
24
63
|
name: "vite-plugin-lib:alias",
|
|
@@ -159,7 +198,7 @@ function library(options) {
|
|
|
159
198
|
include: `${path.resolve(options.entry, "..")}/**`,
|
|
160
199
|
outDir: typesDir,
|
|
161
200
|
staticImport: true,
|
|
162
|
-
afterBuild: includesESFormat(options.formats) ? () =>
|
|
201
|
+
afterBuild: includesESFormat(options.formats) ? () => generateMTSDeclarations(typesDir) : void 0
|
|
163
202
|
})
|
|
164
203
|
];
|
|
165
204
|
}
|
|
@@ -202,25 +241,5 @@ function getErrorMessage(error) {
|
|
|
202
241
|
const isObject = typeof error === "object" && error !== null && "message" in error;
|
|
203
242
|
return isObject ? error.message : String(error);
|
|
204
243
|
}
|
|
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
244
|
|
|
226
245
|
export { library, tsconfigPaths };
|
package/package.json
CHANGED