vite-plugin-lib 1.1.1 → 1.2.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
@@ -1,5 +1,6 @@
1
1
  'use strict';
2
2
 
3
+ const fs = require('fs');
3
4
  const promises = require('fs/promises');
4
5
  const module$1 = require('module');
5
6
  const path = require('path');
@@ -23,10 +24,13 @@ const dts__namespace = /*#__PURE__*/_interopNamespaceDefault(dts);
23
24
  function log(text) {
24
25
  console.log(`${c.cyan("[vite:lib]")} ${text}`);
25
26
  }
27
+ function logWarn(text) {
28
+ console.warn(`${c.yellow("[vite:lib]")} ${text}`);
29
+ }
26
30
  function logError(text) {
27
31
  console.error(`${c.red("[vite:lib]")} ${text}`);
28
32
  }
29
- const tsconfigPaths = () => {
33
+ const tsconfigPaths = ({ verbose } = {}) => {
30
34
  return {
31
35
  name: "vite-plugin-lib:alias",
32
36
  enforce: "pre",
@@ -34,30 +38,13 @@ const tsconfigPaths = () => {
34
38
  const tsconfigPath = path.resolve(config.root ?? ".", "tsconfig.json");
35
39
  const { baseUrl, paths } = await readConfig(tsconfigPath);
36
40
  if (!baseUrl || !paths) {
41
+ log("No paths found in tsconfig.json.");
37
42
  return config;
38
43
  }
39
- const aliasOptions = Object.entries(paths).map(
40
- ([alias, replacements]) => {
41
- const find = alias.replace("/*", "");
42
- const replacement = path.resolve(
43
- tsconfigPath,
44
- baseUrl,
45
- replacements[0]?.replace("/*", "") ?? find
46
- );
47
- return {
48
- find,
49
- replacement
50
- };
51
- }
52
- );
44
+ const pathToAlias = pathToAliasFactory(tsconfigPath, baseUrl, verbose);
45
+ const aliasOptions = Object.entries(paths).map(pathToAlias).filter(Boolean);
53
46
  if (aliasOptions.length > 0) {
54
- log(`Injected ${c.green(aliasOptions.length)} aliases.`);
55
- const base = `${path.resolve(config.root ?? ".")}/`;
56
- aliasOptions.map(
57
- ({ find, replacement }) => `${c.gray(">")} ${c.green(find.toString())} ${c.gray(
58
- c.bold("->")
59
- )} ${c.green(replacement.replace(base, ""))}`
60
- ).forEach(log);
47
+ logInjectedAliases(aliasOptions, config, verbose);
61
48
  }
62
49
  const existingAlias = transformExistingAlias(config.resolve?.alias);
63
50
  return {
@@ -102,6 +89,64 @@ const buildConfig = ({
102
89
  }
103
90
  };
104
91
  };
92
+ function logInjectedAliases(aliasOptions, config, verbose) {
93
+ log(`Injected ${c.green(aliasOptions.length)} aliases.`);
94
+ if (!verbose) {
95
+ return;
96
+ }
97
+ const base = `${path.resolve(config.root ?? ".")}/`;
98
+ aliasOptions.map(
99
+ ({ find, replacement }) => `${c.gray(">")} ${c.green(find.toString())} ${c.gray(
100
+ c.bold("->")
101
+ )} ${c.green(replacement.replace(base, ""))}`
102
+ ).forEach(log);
103
+ }
104
+ function pathToAliasFactory(tsconfigPath, baseUrl, verbose) {
105
+ return ([alias, replacements]) => {
106
+ if (replacements.length === 0) {
107
+ if (verbose) {
108
+ logWarn(`No replacements for alias ${c.green(alias)}.`);
109
+ }
110
+ return void 0;
111
+ }
112
+ if (verbose && replacements.length > 1) {
113
+ logWarn(`Found more than one replacement for alias ${c.green(alias)}.`);
114
+ logWarn("Using the first existing replacement.");
115
+ }
116
+ const find = alias.replace("/*", "");
117
+ const replacement = getFirstExistingReplacement(
118
+ tsconfigPath,
119
+ baseUrl,
120
+ replacements,
121
+ find
122
+ );
123
+ if (!replacement) {
124
+ if (verbose) {
125
+ logWarn(`No replacement found for alias ${c.green(alias)}.`);
126
+ }
127
+ return void 0;
128
+ }
129
+ return {
130
+ find,
131
+ replacement
132
+ };
133
+ };
134
+ }
135
+ function getFirstExistingReplacement(tsconfigPath, baseUrl, replacements, find, verbose) {
136
+ for (const replacement of replacements) {
137
+ const resolvedReplacement = path.resolve(
138
+ tsconfigPath,
139
+ baseUrl,
140
+ replacement.replace("/*", "") ?? find
141
+ );
142
+ if (fs.existsSync(resolvedReplacement)) {
143
+ return resolvedReplacement;
144
+ } else if (verbose) {
145
+ logWarn(`Path ${c.green(replacement)} does not exist.`);
146
+ }
147
+ }
148
+ return void 0;
149
+ }
105
150
  function formatToFileName(entry, format) {
106
151
  const entryFileName = entry.substring(
107
152
  entry.lastIndexOf("/") + 1,
package/dist/index.d.ts CHANGED
@@ -7,8 +7,9 @@ interface Options {
7
7
  entry: string;
8
8
  formats?: LibraryFormats[];
9
9
  externalPackages?: (string | RegExp)[];
10
+ verbose?: boolean;
10
11
  }
11
- declare const tsconfigPaths: () => Plugin;
12
+ declare const tsconfigPaths: ({ verbose }?: Partial<Options>) => Plugin;
12
13
  declare function library(options: Options): Plugin[];
13
14
 
14
15
  export { Options, library, tsconfigPaths };
package/dist/index.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ import { existsSync } from 'fs';
1
2
  import { readFile } from 'fs/promises';
2
3
  import { builtinModules } from 'module';
3
4
  import path from 'path';
@@ -10,10 +11,13 @@ export { dts };
10
11
  function log(text) {
11
12
  console.log(`${c.cyan("[vite:lib]")} ${text}`);
12
13
  }
14
+ function logWarn(text) {
15
+ console.warn(`${c.yellow("[vite:lib]")} ${text}`);
16
+ }
13
17
  function logError(text) {
14
18
  console.error(`${c.red("[vite:lib]")} ${text}`);
15
19
  }
16
- const tsconfigPaths = () => {
20
+ const tsconfigPaths = ({ verbose } = {}) => {
17
21
  return {
18
22
  name: "vite-plugin-lib:alias",
19
23
  enforce: "pre",
@@ -21,30 +25,13 @@ const tsconfigPaths = () => {
21
25
  const tsconfigPath = path.resolve(config.root ?? ".", "tsconfig.json");
22
26
  const { baseUrl, paths } = await readConfig(tsconfigPath);
23
27
  if (!baseUrl || !paths) {
28
+ log("No paths found in tsconfig.json.");
24
29
  return config;
25
30
  }
26
- const aliasOptions = Object.entries(paths).map(
27
- ([alias, replacements]) => {
28
- const find = alias.replace("/*", "");
29
- const replacement = path.resolve(
30
- tsconfigPath,
31
- baseUrl,
32
- replacements[0]?.replace("/*", "") ?? find
33
- );
34
- return {
35
- find,
36
- replacement
37
- };
38
- }
39
- );
31
+ const pathToAlias = pathToAliasFactory(tsconfigPath, baseUrl, verbose);
32
+ const aliasOptions = Object.entries(paths).map(pathToAlias).filter(Boolean);
40
33
  if (aliasOptions.length > 0) {
41
- log(`Injected ${c.green(aliasOptions.length)} aliases.`);
42
- const base = `${path.resolve(config.root ?? ".")}/`;
43
- aliasOptions.map(
44
- ({ find, replacement }) => `${c.gray(">")} ${c.green(find.toString())} ${c.gray(
45
- c.bold("->")
46
- )} ${c.green(replacement.replace(base, ""))}`
47
- ).forEach(log);
34
+ logInjectedAliases(aliasOptions, config, verbose);
48
35
  }
49
36
  const existingAlias = transformExistingAlias(config.resolve?.alias);
50
37
  return {
@@ -89,6 +76,64 @@ const buildConfig = ({
89
76
  }
90
77
  };
91
78
  };
79
+ function logInjectedAliases(aliasOptions, config, verbose) {
80
+ log(`Injected ${c.green(aliasOptions.length)} aliases.`);
81
+ if (!verbose) {
82
+ return;
83
+ }
84
+ const base = `${path.resolve(config.root ?? ".")}/`;
85
+ aliasOptions.map(
86
+ ({ find, replacement }) => `${c.gray(">")} ${c.green(find.toString())} ${c.gray(
87
+ c.bold("->")
88
+ )} ${c.green(replacement.replace(base, ""))}`
89
+ ).forEach(log);
90
+ }
91
+ function pathToAliasFactory(tsconfigPath, baseUrl, verbose) {
92
+ return ([alias, replacements]) => {
93
+ if (replacements.length === 0) {
94
+ if (verbose) {
95
+ logWarn(`No replacements for alias ${c.green(alias)}.`);
96
+ }
97
+ return void 0;
98
+ }
99
+ if (verbose && replacements.length > 1) {
100
+ logWarn(`Found more than one replacement for alias ${c.green(alias)}.`);
101
+ logWarn("Using the first existing replacement.");
102
+ }
103
+ const find = alias.replace("/*", "");
104
+ const replacement = getFirstExistingReplacement(
105
+ tsconfigPath,
106
+ baseUrl,
107
+ replacements,
108
+ find
109
+ );
110
+ if (!replacement) {
111
+ if (verbose) {
112
+ logWarn(`No replacement found for alias ${c.green(alias)}.`);
113
+ }
114
+ return void 0;
115
+ }
116
+ return {
117
+ find,
118
+ replacement
119
+ };
120
+ };
121
+ }
122
+ function getFirstExistingReplacement(tsconfigPath, baseUrl, replacements, find, verbose) {
123
+ for (const replacement of replacements) {
124
+ const resolvedReplacement = path.resolve(
125
+ tsconfigPath,
126
+ baseUrl,
127
+ replacement.replace("/*", "") ?? find
128
+ );
129
+ if (existsSync(resolvedReplacement)) {
130
+ return resolvedReplacement;
131
+ } else if (verbose) {
132
+ logWarn(`Path ${c.green(replacement)} does not exist.`);
133
+ }
134
+ }
135
+ return void 0;
136
+ }
92
137
  function formatToFileName(entry, format) {
93
138
  const entryFileName = entry.substring(
94
139
  entry.lastIndexOf("/") + 1,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-lib",
3
- "version": "1.1.1",
3
+ "version": "1.2.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",