vite-plugin-lib 2.2.1 → 3.0.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 +10 -1
- package/dist/index.d.mts +32 -11
- package/dist/index.d.ts +32 -11
- package/dist/index.mjs +84 -22
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -45,7 +45,16 @@ export default defineConfig({
|
|
|
45
45
|
entry: 'src/index.ts', // file name determines output file names, default is 'src/index.ts'
|
|
46
46
|
formats: ['es'], // optional, default is ['es']
|
|
47
47
|
name: 'YourGlobalUMDName', // optional if format does not include 'umd' or 'iife'
|
|
48
|
-
|
|
48
|
+
// optional, default externalizes all builtin modules, node_modules, dependencies, and peerDependencies
|
|
49
|
+
bundle: {
|
|
50
|
+
builtin: false,
|
|
51
|
+
dependencies: false,
|
|
52
|
+
devDependencies: true,
|
|
53
|
+
peerDependencies: false,
|
|
54
|
+
exclude: [], // individual packages or modules to externalize
|
|
55
|
+
include: [], // override the default externalization for individual packages or modules
|
|
56
|
+
nodeModules: false,
|
|
57
|
+
},
|
|
49
58
|
manifest: 'package.json', // relative path to package.json, default is package.json,
|
|
50
59
|
tsconfig: 'tsconfig.json', // relative path to tsconfig.json, default is tsconfig.json
|
|
51
60
|
}),
|
package/dist/index.d.mts
CHANGED
|
@@ -8,26 +8,47 @@ declare const coverage: {
|
|
|
8
8
|
include: string[];
|
|
9
9
|
provider: "v8";
|
|
10
10
|
};
|
|
11
|
-
interface
|
|
11
|
+
interface CommonOptions {
|
|
12
|
+
verbose: boolean;
|
|
13
|
+
}
|
|
14
|
+
interface TSConfigPathsOptions extends CommonOptions {
|
|
15
|
+
/** Path to the tsconfig file (relative to the project root). Defaults to `./tsconfig.json` */
|
|
16
|
+
tsconfig: string;
|
|
17
|
+
}
|
|
18
|
+
interface BundleOptions {
|
|
19
|
+
/** If `false`, all builtin modules will be externalized. Defaults to `false`. */
|
|
20
|
+
builtin: boolean;
|
|
21
|
+
/** If `false`, all dependencies will be externalized. Defaults to `false`. */
|
|
22
|
+
dependencies: boolean;
|
|
23
|
+
/** If `false`, all devDependencies will be externalized. Defaults to `true`. */
|
|
24
|
+
devDependencies: boolean;
|
|
25
|
+
/** If `false`, all dependencies will be externalized. Defaults to `false`. */
|
|
26
|
+
peerDependencies: boolean;
|
|
27
|
+
/** List of packages or modules to externalize. Defaults to `[]`. */
|
|
28
|
+
exclude: (string | RegExp)[];
|
|
29
|
+
/** List of packages or modules to bundle. Acts as an override and defaults to `[]`. */
|
|
30
|
+
include: (string | RegExp)[];
|
|
31
|
+
/** If `false`, all direct imports from `node_modules` will be externalized. Defaults to `false`. */
|
|
32
|
+
nodeModules: boolean;
|
|
33
|
+
}
|
|
34
|
+
interface LibraryOptions extends TSConfigPathsOptions {
|
|
12
35
|
/** Defaults to `src/index.ts`. */
|
|
13
36
|
entry: string;
|
|
14
|
-
|
|
37
|
+
/** Bundle configuration for packages and modules. See {@link BundleOptions} for defaults. */
|
|
38
|
+
bundle: Partial<BundleOptions>;
|
|
15
39
|
/** Defaults to `['es']`. */
|
|
16
|
-
formats
|
|
40
|
+
formats: LibraryFormats[];
|
|
17
41
|
/** Defaults to `package.json`. */
|
|
18
42
|
manifest: string;
|
|
19
43
|
name?: string;
|
|
20
|
-
verbose?: boolean;
|
|
21
44
|
/** Remove any temporary build files. Defaults to `true`. */
|
|
22
|
-
cleanup
|
|
23
|
-
/** Path to the tsconfig file (relative to the project root). Defaults to `./tsconfig.json` */
|
|
24
|
-
tsconfig: string;
|
|
45
|
+
cleanup: boolean;
|
|
25
46
|
}
|
|
26
|
-
declare function tsconfigPaths(options?: Partial<
|
|
27
|
-
declare function library(options?: Partial<
|
|
47
|
+
declare function tsconfigPaths(options?: Partial<TSConfigPathsOptions>): Plugin;
|
|
48
|
+
declare function library(options?: Partial<LibraryOptions>): Plugin[];
|
|
28
49
|
/**
|
|
29
50
|
* Remove any temporary `vite.config.ts.timestamp-*` files.
|
|
30
51
|
*/
|
|
31
|
-
declare function cleanup(): Plugin;
|
|
52
|
+
declare function cleanup(options?: Partial<CommonOptions>): Plugin;
|
|
32
53
|
|
|
33
|
-
export { type
|
|
54
|
+
export { type BundleOptions, type CommonOptions, type LibraryOptions, type TSConfigPathsOptions, cleanup, coverage, library, tsconfigPaths };
|
package/dist/index.d.ts
CHANGED
|
@@ -8,26 +8,47 @@ declare const coverage: {
|
|
|
8
8
|
include: string[];
|
|
9
9
|
provider: "v8";
|
|
10
10
|
};
|
|
11
|
-
interface
|
|
11
|
+
interface CommonOptions {
|
|
12
|
+
verbose: boolean;
|
|
13
|
+
}
|
|
14
|
+
interface TSConfigPathsOptions extends CommonOptions {
|
|
15
|
+
/** Path to the tsconfig file (relative to the project root). Defaults to `./tsconfig.json` */
|
|
16
|
+
tsconfig: string;
|
|
17
|
+
}
|
|
18
|
+
interface BundleOptions {
|
|
19
|
+
/** If `false`, all builtin modules will be externalized. Defaults to `false`. */
|
|
20
|
+
builtin: boolean;
|
|
21
|
+
/** If `false`, all dependencies will be externalized. Defaults to `false`. */
|
|
22
|
+
dependencies: boolean;
|
|
23
|
+
/** If `false`, all devDependencies will be externalized. Defaults to `true`. */
|
|
24
|
+
devDependencies: boolean;
|
|
25
|
+
/** If `false`, all dependencies will be externalized. Defaults to `false`. */
|
|
26
|
+
peerDependencies: boolean;
|
|
27
|
+
/** List of packages or modules to externalize. Defaults to `[]`. */
|
|
28
|
+
exclude: (string | RegExp)[];
|
|
29
|
+
/** List of packages or modules to bundle. Acts as an override and defaults to `[]`. */
|
|
30
|
+
include: (string | RegExp)[];
|
|
31
|
+
/** If `false`, all direct imports from `node_modules` will be externalized. Defaults to `false`. */
|
|
32
|
+
nodeModules: boolean;
|
|
33
|
+
}
|
|
34
|
+
interface LibraryOptions extends TSConfigPathsOptions {
|
|
12
35
|
/** Defaults to `src/index.ts`. */
|
|
13
36
|
entry: string;
|
|
14
|
-
|
|
37
|
+
/** Bundle configuration for packages and modules. See {@link BundleOptions} for defaults. */
|
|
38
|
+
bundle: Partial<BundleOptions>;
|
|
15
39
|
/** Defaults to `['es']`. */
|
|
16
|
-
formats
|
|
40
|
+
formats: LibraryFormats[];
|
|
17
41
|
/** Defaults to `package.json`. */
|
|
18
42
|
manifest: string;
|
|
19
43
|
name?: string;
|
|
20
|
-
verbose?: boolean;
|
|
21
44
|
/** Remove any temporary build files. Defaults to `true`. */
|
|
22
|
-
cleanup
|
|
23
|
-
/** Path to the tsconfig file (relative to the project root). Defaults to `./tsconfig.json` */
|
|
24
|
-
tsconfig: string;
|
|
45
|
+
cleanup: boolean;
|
|
25
46
|
}
|
|
26
|
-
declare function tsconfigPaths(options?: Partial<
|
|
27
|
-
declare function library(options?: Partial<
|
|
47
|
+
declare function tsconfigPaths(options?: Partial<TSConfigPathsOptions>): Plugin;
|
|
48
|
+
declare function library(options?: Partial<LibraryOptions>): Plugin[];
|
|
28
49
|
/**
|
|
29
50
|
* Remove any temporary `vite.config.ts.timestamp-*` files.
|
|
30
51
|
*/
|
|
31
|
-
declare function cleanup(): Plugin;
|
|
52
|
+
declare function cleanup(options?: Partial<CommonOptions>): Plugin;
|
|
32
53
|
|
|
33
|
-
export { type
|
|
54
|
+
export { type BundleOptions, type CommonOptions, type LibraryOptions, type TSConfigPathsOptions, cleanup, coverage, library, tsconfigPaths };
|
package/dist/index.mjs
CHANGED
|
@@ -104,21 +104,39 @@ const coverage = {
|
|
|
104
104
|
include: ["src/**/*.*"],
|
|
105
105
|
provider: "v8"
|
|
106
106
|
};
|
|
107
|
-
const
|
|
107
|
+
const COMMON_DEFAULTS = {
|
|
108
|
+
verbose: false
|
|
109
|
+
};
|
|
110
|
+
const TS_CONFIG_PATHS_OPTIONS = {
|
|
111
|
+
...COMMON_DEFAULTS,
|
|
112
|
+
tsconfig: "./tsconfig.json"
|
|
113
|
+
};
|
|
114
|
+
const BUNDLE_DEFAULTS = {
|
|
115
|
+
builtin: false,
|
|
116
|
+
dependencies: false,
|
|
117
|
+
devDependencies: true,
|
|
118
|
+
peerDependencies: false,
|
|
119
|
+
exclude: [],
|
|
120
|
+
include: [],
|
|
121
|
+
nodeModules: false
|
|
122
|
+
};
|
|
123
|
+
const LIBRARY_DEFAULTS = {
|
|
124
|
+
...TS_CONFIG_PATHS_OPTIONS,
|
|
125
|
+
cleanup: true,
|
|
108
126
|
entry: "src/index.ts",
|
|
127
|
+
bundle: {},
|
|
109
128
|
formats: ["es"],
|
|
110
|
-
manifest: "package.json"
|
|
111
|
-
cleanup: true,
|
|
112
|
-
tsconfig: "./tsconfig.json"
|
|
129
|
+
manifest: "package.json"
|
|
113
130
|
};
|
|
114
131
|
function mergeWithDefaults(options) {
|
|
115
132
|
return {
|
|
116
|
-
...
|
|
133
|
+
...LIBRARY_DEFAULTS,
|
|
117
134
|
...options
|
|
118
135
|
};
|
|
119
136
|
}
|
|
120
137
|
function tsconfigPaths(options = {}) {
|
|
121
|
-
const
|
|
138
|
+
const tsconfig = options.tsconfig ?? TS_CONFIG_PATHS_OPTIONS.tsconfig;
|
|
139
|
+
const verbose = options.verbose ?? TS_CONFIG_PATHS_OPTIONS.verbose;
|
|
122
140
|
return {
|
|
123
141
|
name: "vite-plugin-lib:alias",
|
|
124
142
|
enforce: "pre",
|
|
@@ -150,10 +168,20 @@ function buildConfig({
|
|
|
150
168
|
formats,
|
|
151
169
|
manifest,
|
|
152
170
|
name,
|
|
153
|
-
|
|
171
|
+
bundle,
|
|
172
|
+
verbose
|
|
154
173
|
}) {
|
|
155
|
-
|
|
156
|
-
|
|
174
|
+
const bundleWithDefaults = { ...BUNDLE_DEFAULTS, ...bundle };
|
|
175
|
+
const packagesToExternalize = [
|
|
176
|
+
...getBuiltinModules(bundleWithDefaults),
|
|
177
|
+
...getDependencies(manifest, bundleWithDefaults, verbose),
|
|
178
|
+
...bundleWithDefaults.exclude
|
|
179
|
+
];
|
|
180
|
+
if (!bundleWithDefaults.nodeModules) {
|
|
181
|
+
packagesToExternalize.push(/node_modules/);
|
|
182
|
+
if (verbose) {
|
|
183
|
+
log(`Externalized node_modules.`);
|
|
184
|
+
}
|
|
157
185
|
}
|
|
158
186
|
return {
|
|
159
187
|
name: "vite-plugin-lib:build",
|
|
@@ -171,29 +199,60 @@ function buildConfig({
|
|
|
171
199
|
fileName: (format) => formatToFileName(entry, format)
|
|
172
200
|
},
|
|
173
201
|
rollupOptions: {
|
|
174
|
-
external:
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
]
|
|
202
|
+
external: (source, _importer, _isResolved) => {
|
|
203
|
+
const shouldBeExternalized = packagesToExternalize.some((rule) => matchesRule(source, rule));
|
|
204
|
+
const shouldBeBundled = bundleWithDefaults.include.some((rule) => matchesRule(source, rule));
|
|
205
|
+
return shouldBeExternalized && !shouldBeBundled;
|
|
206
|
+
}
|
|
180
207
|
}
|
|
181
208
|
}
|
|
182
209
|
};
|
|
183
210
|
}
|
|
184
211
|
};
|
|
185
212
|
}
|
|
186
|
-
function
|
|
213
|
+
function matchesRule(source, rule) {
|
|
214
|
+
return typeof rule === "string" ? rule === source : rule.test(source);
|
|
215
|
+
}
|
|
216
|
+
function getDependencies(manifest, bundle, verbose) {
|
|
187
217
|
try {
|
|
188
218
|
const content = readFileSync(manifest, { encoding: "utf-8" });
|
|
189
|
-
const { dependencies = {}, peerDependencies = {} } = JSON.parse(content);
|
|
190
|
-
|
|
219
|
+
const { dependencies = {}, devDependencies = {}, peerDependencies = {} } = JSON.parse(content);
|
|
220
|
+
const dependenciesToExternalize = [];
|
|
221
|
+
if (!bundle.dependencies) {
|
|
222
|
+
const names = Object.keys(dependencies);
|
|
223
|
+
dependenciesToExternalize.push(...names);
|
|
224
|
+
if (verbose) {
|
|
225
|
+
log(`Externalized ${names.length} dependencies.`);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
if (!bundle.devDependencies) {
|
|
229
|
+
const names = Object.keys(devDependencies);
|
|
230
|
+
dependenciesToExternalize.push(...names);
|
|
231
|
+
if (verbose) {
|
|
232
|
+
log(`Externalized ${names.length} devDependencies.`);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
if (!bundle.peerDependencies) {
|
|
236
|
+
const names = Object.keys(peerDependencies);
|
|
237
|
+
dependenciesToExternalize.push(...names);
|
|
238
|
+
if (verbose) {
|
|
239
|
+
log(`Externalized ${names.length} peerDependencies.`);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return dependenciesToExternalize;
|
|
191
243
|
} catch (error) {
|
|
192
244
|
const message = getErrorMessage(error);
|
|
193
245
|
logError(`Could not read ${c.green(manifest)}: ${message}`);
|
|
194
246
|
throw error;
|
|
195
247
|
}
|
|
196
248
|
}
|
|
249
|
+
function getBuiltinModules(bundle) {
|
|
250
|
+
if (bundle.builtin) {
|
|
251
|
+
return [];
|
|
252
|
+
}
|
|
253
|
+
log("Externalized builtin modules.");
|
|
254
|
+
return [...builtinModules, /node:/, /bun:/, /deno:/];
|
|
255
|
+
}
|
|
197
256
|
function logInjectedAliases(aliasOptions, config, verbose) {
|
|
198
257
|
log(`Injected ${c.green(aliasOptions.length)} aliases.`);
|
|
199
258
|
if (!verbose) {
|
|
@@ -201,7 +260,7 @@ function logInjectedAliases(aliasOptions, config, verbose) {
|
|
|
201
260
|
}
|
|
202
261
|
const base = `${path.resolve(config.root ?? ".")}/`;
|
|
203
262
|
aliasOptions.map(
|
|
204
|
-
({ find, replacement }) =>
|
|
263
|
+
({ find, replacement }) => ` ${c.gray(">")} ${c.green(find.toString())} ${c.gray(
|
|
205
264
|
c.bold("->")
|
|
206
265
|
)} ${c.green(replacement.replace(base, ""))}`
|
|
207
266
|
).forEach(log);
|
|
@@ -287,7 +346,7 @@ function library(options = {}) {
|
|
|
287
346
|
})
|
|
288
347
|
];
|
|
289
348
|
if (mergedOptions.cleanup) {
|
|
290
|
-
plugins.push(cleanup());
|
|
349
|
+
plugins.push(cleanup(mergedOptions));
|
|
291
350
|
}
|
|
292
351
|
return plugins;
|
|
293
352
|
}
|
|
@@ -326,7 +385,8 @@ function getErrorMessage(error) {
|
|
|
326
385
|
const isObject = typeof error === "object" && error !== null && "message" in error;
|
|
327
386
|
return isObject ? error.message : String(error);
|
|
328
387
|
}
|
|
329
|
-
function cleanup() {
|
|
388
|
+
function cleanup(options = {}) {
|
|
389
|
+
const verbose = options.verbose ?? COMMON_DEFAULTS.verbose;
|
|
330
390
|
return {
|
|
331
391
|
name: "vite-plugin-lib:cleanup",
|
|
332
392
|
enforce: "post",
|
|
@@ -339,7 +399,9 @@ function cleanup() {
|
|
|
339
399
|
rmSync(`./${file}`);
|
|
340
400
|
deletedCount++;
|
|
341
401
|
});
|
|
342
|
-
|
|
402
|
+
if (verbose) {
|
|
403
|
+
log(`Removed ${deletedCount} temporary files.`);
|
|
404
|
+
}
|
|
343
405
|
}
|
|
344
406
|
};
|
|
345
407
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-lib",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "3.0.0",
|
|
5
5
|
"description": "Vite plugin for build configuration, automatic aliases, and type declarations.",
|
|
6
6
|
"author": "Jan Müller <janmueller3698@gmail.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"@types/node": "22.14.1",
|
|
47
47
|
"typescript": "5.8.3",
|
|
48
48
|
"unbuild": "3.5.0",
|
|
49
|
-
"vite": "6.2
|
|
49
|
+
"vite": "6.3.2",
|
|
50
50
|
"@yeger/tsconfig": "2.1.0"
|
|
51
51
|
},
|
|
52
52
|
"publishConfig": {
|