@sveltejs/vite-plugin-svelte 2.0.4 → 2.1.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.
@@ -1,5 +1,17 @@
1
1
  import { SvelteRequest } from './id';
2
2
  import { Code, CompileData } from './compile';
3
+ import { readFileSync } from 'fs';
4
+ import { dirname } from 'path';
5
+ //eslint-disable-next-line node/no-missing-import
6
+ import { findClosestPkgJsonPath } from 'vitefu';
7
+ import { normalizePath } from 'vite';
8
+
9
+ interface PackageInfo {
10
+ name: string;
11
+ version: string;
12
+ svelte?: string;
13
+ path: string;
14
+ }
3
15
 
4
16
  export class VitePluginSvelteCache {
5
17
  private _css = new Map<string, Code>();
@@ -8,6 +20,7 @@ export class VitePluginSvelteCache {
8
20
  private _dependants = new Map<string, Set<string>>();
9
21
  private _resolvedSvelteFields = new Map<string, string>();
10
22
  private _errors = new Map<string, any>();
23
+ private _packageInfos: PackageInfo[] = [];
11
24
 
12
25
  public update(compileData: CompileData) {
13
26
  this._errors.delete(compileData.normalizedFilename);
@@ -110,6 +123,9 @@ export class VitePluginSvelteCache {
110
123
  return this._resolvedSvelteFields.get(this._getResolvedSvelteFieldKey(name, importer));
111
124
  }
112
125
 
126
+ public hasResolvedSvelteField(name: string, importer?: string) {
127
+ return this._resolvedSvelteFields.has(this._getResolvedSvelteFieldKey(name, importer));
128
+ }
113
129
  public setResolvedSvelteField(
114
130
  importee: string,
115
131
  importer: string | undefined = undefined,
@@ -124,4 +140,43 @@ export class VitePluginSvelteCache {
124
140
  private _getResolvedSvelteFieldKey(importee: string, importer?: string): string {
125
141
  return importer ? `${importer} > ${importee}` : importee;
126
142
  }
143
+
144
+ public async getPackageInfo(file: string): Promise<PackageInfo> {
145
+ let info = this._packageInfos.find((pi) => file.startsWith(pi.path));
146
+ if (!info) {
147
+ info = await findPackageInfo(file);
148
+ this._packageInfos.push(info);
149
+ }
150
+ return info;
151
+ }
152
+ }
153
+
154
+ /**
155
+ * utility to get some info from the closest package.json with a "name" set
156
+ *
157
+ * @param {string} file to find info for
158
+ * @returns {PackageInfo}
159
+ */
160
+ async function findPackageInfo(file: string): Promise<PackageInfo> {
161
+ const info: PackageInfo = {
162
+ name: '$unknown',
163
+ version: '0.0.0-unknown',
164
+ path: '$unknown'
165
+ };
166
+ let path = await findClosestPkgJsonPath(file, (pkgPath) => {
167
+ const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
168
+ if (pkg.name != null) {
169
+ info.name = pkg.name;
170
+ if (pkg.version != null) {
171
+ info.version = pkg.version;
172
+ }
173
+ info.svelte = pkg.svelte;
174
+ return true;
175
+ }
176
+ return false;
177
+ });
178
+ // return normalized path with appended '/' so .startsWith works for future file checks
179
+ path = normalizePath(dirname(path ?? file)) + '/';
180
+ info.path = path;
181
+ return info;
127
182
  }
@@ -1,10 +1,7 @@
1
1
  import { log } from './log';
2
- //eslint-disable-next-line node/no-missing-import
3
- import { findClosestPkgJsonPath } from 'vitefu';
4
- import { readFileSync } from 'fs';
5
- import { dirname } from 'path';
6
2
  import { performance } from 'perf_hooks';
7
3
  import { normalizePath } from 'vite';
4
+ import { VitePluginSvelteCache } from './vite-plugin-svelte-cache';
8
5
 
9
6
  interface Stat {
10
7
  file: string;
@@ -87,31 +84,13 @@ function formatPackageStats(pkgStats: PackageStats[]) {
87
84
  return table;
88
85
  }
89
86
 
90
- /**
91
- * utility to get the package name a file belongs to
92
- *
93
- * @param {string} file to find package for
94
- * @returns {path:string,name:string} tuple of path,name where name is the parsed package name and path is the normalized path to it
95
- */
96
- async function getClosestNamedPackage(file: string): Promise<{ name: string; path: string }> {
97
- let name = '$unknown';
98
- let path = await findClosestPkgJsonPath(file, (pkgPath) => {
99
- const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
100
- if (pkg.name != null) {
101
- name = pkg.name;
102
- return true;
103
- }
104
- return false;
105
- });
106
- // return normalized path with appended '/' so .startsWith works for future file checks
107
- path = normalizePath(dirname(path ?? file)) + '/';
108
- return { name, path };
109
- }
110
-
111
87
  export class VitePluginSvelteStats {
112
88
  // package directory -> package name
113
- private _packages: { path: string; name: string }[] = [];
89
+ private _cache: VitePluginSvelteCache;
114
90
  private _collections: StatCollection[] = [];
91
+ constructor(cache: VitePluginSvelteCache) {
92
+ this._cache = cache;
93
+ }
115
94
  startCollection(name: string, opts?: Partial<CollectionOptions>) {
116
95
  const options = {
117
96
  ...defaultCollectionOptions,
@@ -140,7 +119,7 @@ export class VitePluginSvelteStats {
140
119
  stats.push(stat);
141
120
  if (!hasLoggedProgress && options.logInProgress(collection, now)) {
142
121
  hasLoggedProgress = true;
143
- log.info(`${name} in progress ...`);
122
+ log.debug(`${name} in progress ...`, undefined, 'stats');
144
123
  }
145
124
  };
146
125
  },
@@ -164,7 +143,11 @@ export class VitePluginSvelteStats {
164
143
  const logResult = collection.options.logResult(collection);
165
144
  if (logResult) {
166
145
  await this._aggregateStatsResult(collection);
167
- log.info(`${collection.name} done.`, formatPackageStats(collection.packageStats!));
146
+ log.debug(
147
+ `${collection.name} done.\n${formatPackageStats(collection.packageStats!)}`,
148
+ undefined,
149
+ 'stats'
150
+ );
168
151
  }
169
152
  // cut some ties to free it for garbage collection
170
153
  const index = this._collections.indexOf(collection);
@@ -179,19 +162,14 @@ export class VitePluginSvelteStats {
179
162
  collection.finish = () => {};
180
163
  } catch (e) {
181
164
  // this should not happen, but stats taking also should not break the process
182
- log.debug.once(`failed to finish stats for ${collection.name}`, e);
165
+ log.debug.once(`failed to finish stats for ${collection.name}\n`, e, 'stats');
183
166
  }
184
167
  }
185
168
 
186
169
  private async _aggregateStatsResult(collection: StatCollection) {
187
170
  const stats = collection.stats;
188
171
  for (const stat of stats) {
189
- let pkg = this._packages.find((p) => stat.file.startsWith(p.path));
190
- if (!pkg) {
191
- pkg = await getClosestNamedPackage(stat.file);
192
- this._packages.push(pkg);
193
- }
194
- stat.pkg = pkg.name;
172
+ stat.pkg = (await this._cache.getPackageInfo(stat.file)).name;
195
173
  }
196
174
 
197
175
  // group stats