vite-plugin-lib 2.2.1 → 3.0.1

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
@@ -24,6 +24,7 @@ This highly opinionated all-in one Vite plugin enables automatic alias configura
24
24
 
25
25
  ```ts
26
26
  import { defineConfig } from 'vite'
27
+
27
28
  import { tsconfigPaths } from 'vite-plugin-lib'
28
29
 
29
30
  export default defineConfig({
@@ -37,6 +38,7 @@ The `library` plugin includes the `alias` plugin, configures build settings, and
37
38
 
38
39
  ```ts
39
40
  import { defineConfig } from 'vite'
41
+
40
42
  import { library } from 'vite-plugin-lib'
41
43
 
42
44
  export default defineConfig({
@@ -45,7 +47,16 @@ export default defineConfig({
45
47
  entry: 'src/index.ts', // file name determines output file names, default is 'src/index.ts'
46
48
  formats: ['es'], // optional, default is ['es']
47
49
  name: 'YourGlobalUMDName', // optional if format does not include 'umd' or 'iife'
48
- external: ['some-package'], // optional, default is all node_modules and builtin modules
50
+ // optional, default externalizes all builtin modules, node_modules, dependencies, and peerDependencies
51
+ bundle: {
52
+ builtin: false,
53
+ dependencies: false,
54
+ devDependencies: true,
55
+ peerDependencies: false,
56
+ exclude: [], // individual packages or modules to externalize
57
+ include: [], // override the default externalization for individual packages or modules
58
+ nodeModules: false,
59
+ },
49
60
  manifest: 'package.json', // relative path to package.json, default is package.json,
50
61
  tsconfig: 'tsconfig.json', // relative path to tsconfig.json, default is tsconfig.json
51
62
  }),
package/dist/index.d.mts CHANGED
@@ -8,26 +8,48 @@ declare const coverage: {
8
8
  include: string[];
9
9
  provider: "v8";
10
10
  };
11
- interface Options {
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
- externalPackages?: (string | RegExp)[];
37
+ /** Bundle configuration for packages and modules. See {@link BundleOptions} for defaults. */
38
+ bundle: Partial<BundleOptions>;
15
39
  /** Defaults to `['es']`. */
16
- formats?: LibraryFormats[];
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?: boolean;
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<Options>): Plugin;
27
- declare function library(options?: Partial<Options>): Plugin[];
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 Options, cleanup, coverage, library, tsconfigPaths };
54
+ export { cleanup, coverage, library, tsconfigPaths };
55
+ export type { BundleOptions, CommonOptions, LibraryOptions, TSConfigPathsOptions };
package/dist/index.d.ts CHANGED
@@ -8,26 +8,48 @@ declare const coverage: {
8
8
  include: string[];
9
9
  provider: "v8";
10
10
  };
11
- interface Options {
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
- externalPackages?: (string | RegExp)[];
37
+ /** Bundle configuration for packages and modules. See {@link BundleOptions} for defaults. */
38
+ bundle: Partial<BundleOptions>;
15
39
  /** Defaults to `['es']`. */
16
- formats?: LibraryFormats[];
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?: boolean;
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<Options>): Plugin;
27
- declare function library(options?: Partial<Options>): Plugin[];
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 Options, cleanup, coverage, library, tsconfigPaths };
54
+ export { cleanup, coverage, library, tsconfigPaths };
55
+ export type { BundleOptions, CommonOptions, LibraryOptions, TSConfigPathsOptions };
package/dist/index.mjs CHANGED
@@ -104,21 +104,39 @@ const coverage = {
104
104
  include: ["src/**/*.*"],
105
105
  provider: "v8"
106
106
  };
107
- const defaults = {
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
- ...defaults,
133
+ ...LIBRARY_DEFAULTS,
117
134
  ...options
118
135
  };
119
136
  }
120
137
  function tsconfigPaths(options = {}) {
121
- const { verbose, tsconfig } = mergeWithDefaults(options);
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
- externalPackages
171
+ bundle,
172
+ verbose
154
173
  }) {
155
- if (!externalPackages) {
156
- log("Externalized all packages.");
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: externalPackages ?? [
175
- /node_modules/,
176
- ...builtinModules,
177
- /node:/,
178
- ...getDependencies(manifest)
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 getDependencies(manifest) {
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
- return Object.keys({ ...dependencies, ...peerDependencies });
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 }) => `${c.gray(">")} ${c.green(find.toString())} ${c.gray(
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
- log(`Removed ${deletedCount} temporary files.`);
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": "2.2.1",
4
+ "version": "3.0.1",
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",
@@ -36,18 +36,18 @@
36
36
  ],
37
37
  "peerDependencies": {
38
38
  "typescript": "*",
39
- "vite": "^2.0.0 || ^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0"
39
+ "vite": "2.0.0 || ^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0"
40
40
  },
41
41
  "dependencies": {
42
42
  "picocolors": "1.1.1",
43
- "vite-plugin-dts": "4.5.3"
43
+ "vite-plugin-dts": "4.5.4"
44
44
  },
45
45
  "devDependencies": {
46
- "@types/node": "22.14.1",
46
+ "@types/node": "22.15.29",
47
47
  "typescript": "5.8.3",
48
48
  "unbuild": "3.5.0",
49
- "vite": "6.2.6",
50
- "@yeger/tsconfig": "2.1.0"
49
+ "vite": "6.3.5",
50
+ "@yeger/tsconfig": "2.1.1"
51
51
  },
52
52
  "publishConfig": {
53
53
  "access": "public"