solid-optimizer 1.0.0-next.4 → 1.0.0-next.5

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/src/vite/usage.ts CHANGED
@@ -123,12 +123,26 @@ class Usage implements UsageIndex {
123
123
  const users = new Set([user]);
124
124
  for (const [importer, containers] of usage.importers) {
125
125
  const moved = absorbed.get(importer);
126
- users.add([...containers].every((name) => moved?.has(name) === true) ? user : importer);
126
+ users.add(hasEvery(containers, moved) ? user : importer);
127
127
  }
128
128
  // A module the component's module imports never gets a copy of it, so
129
129
  // the original stays for it.
130
- return [...users].some((id) => this.reaches(module, id)) ? 0 : users.size;
130
+ for (const id of users) {
131
+ if (this.reaches(module, id)) {
132
+ return 0;
133
+ }
134
+ }
135
+ return users.size;
136
+ }
137
+ }
138
+
139
+ function hasEvery(names: Iterable<string>, set: ReadonlySet<string> | undefined): boolean {
140
+ for (const name of names) {
141
+ if (set?.has(name) !== true) {
142
+ return false;
143
+ }
131
144
  }
145
+ return true;
132
146
  }
133
147
 
134
148
  function isTagName(reference: Node, parents: Parents): boolean {
@@ -147,9 +161,13 @@ function declaredNames(declaration: Node | null | undefined): string[] {
147
161
  return [declaration.id.name];
148
162
  }
149
163
  if (declaration?.type === 'VariableDeclaration') {
150
- return declaration.declarations.flatMap((declarator) =>
151
- declarator.id.type === 'Identifier' ? [declarator.id.name] : [],
152
- );
164
+ const names: string[] = [];
165
+ for (const declarator of declaration.declarations) {
166
+ if (declarator.id.type === 'Identifier') {
167
+ names.push(declarator.id.name);
168
+ }
169
+ }
170
+ return names;
153
171
  }
154
172
  return [];
155
173
  }
@@ -206,7 +224,7 @@ function recordModule(
206
224
  // A module that uses its own export keeps it, whoever else copies it.
207
225
  const exported = exportedBindings(program, scopes);
208
226
  // The export names of the top-level declaration a use sits in.
209
- const containersOf = (reference: Node): string[] => {
227
+ const findContainers = (reference: Node): string[] => {
210
228
  let node: Node = reference;
211
229
  let parent = parents.get(node);
212
230
  while (parent && parent.type !== 'Program') {
@@ -232,7 +250,12 @@ function recordModule(
232
250
  }
233
251
  }
234
252
  }
235
- const names = bindings.flatMap((binding) => (binding ? (exported.get(binding) ?? []) : []));
253
+ const names: string[] = [];
254
+ for (const binding of bindings) {
255
+ if (binding) {
256
+ names.push(...(exported.get(binding) ?? []));
257
+ }
258
+ }
236
259
  return names.length > 0 ? names : [UNEXPORTED];
237
260
  };
238
261
 
@@ -244,7 +267,7 @@ function recordModule(
244
267
  }
245
268
  if (imported !== '*') {
246
269
  if (isTagName(reference, parents)) {
247
- usage.tag(target, imported, id, containersOf(reference));
270
+ usage.tag(target, imported, id, findContainers(reference));
248
271
  } else {
249
272
  usage.other(target, imported);
250
273
  }
@@ -254,7 +277,7 @@ function recordModule(
254
277
  const member = parents.get(reference);
255
278
  if (member?.type === 'JSXMemberExpression' && member.object === reference) {
256
279
  if (isTagName(member, parents)) {
257
- usage.tag(target, member.property.name, id, containersOf(reference));
280
+ usage.tag(target, member.property.name, id, findContainers(reference));
258
281
  } else {
259
282
  usage.other(target, member.property.name);
260
283
  }
@@ -299,14 +322,18 @@ function recordModule(
299
322
  }
300
323
 
301
324
  for (const [binding, names] of exported) {
302
- const used = binding.references.some((reference) => {
325
+ let used = false;
326
+ for (const reference of binding.references) {
303
327
  const parent = parents.get(reference);
304
- return (
328
+ if (
305
329
  parent?.type !== 'ExportSpecifier' &&
306
330
  parent?.type !== 'ExportDefaultDeclaration' &&
307
331
  !isClosingTagName(reference, parents)
308
- );
309
- });
332
+ ) {
333
+ used = true;
334
+ break;
335
+ }
336
+ }
310
337
  if (used) {
311
338
  for (const name of names) {
312
339
  usage.other(id, name);
@@ -316,7 +343,7 @@ function recordModule(
316
343
  }
317
344
 
318
345
  /** The import specifiers a module loads, statically or with `import()`. */
319
- function specifiersOf(program: Program): { static: Set<string>; dynamic: Set<string> } {
346
+ function collectSpecifiers(program: Program): { static: Set<string>; dynamic: Set<string> } {
320
347
  const statics = new Set<string>();
321
348
  const dynamics = new Set<string>();
322
349
  for (const statement of program.body) {
@@ -360,46 +387,60 @@ export async function buildUsageIndex(
360
387
  usage.all(entry);
361
388
  }
362
389
  const seen = new Set<string>();
363
- let frontier = entries.filter((id) => SCRIPT.test(id.replace(/\?.*$/, '')));
390
+ let frontier: string[] = [];
391
+ for (const id of entries) {
392
+ if (SCRIPT.test(id.replace(/\?.*$/, ''))) {
393
+ frontier.push(id);
394
+ }
395
+ }
364
396
  while (frontier.length > 0) {
365
397
  for (const id of frontier) {
366
398
  seen.add(id);
367
399
  }
368
- // oxlint-disable-next-line no-await-in-loop
369
- const next = await Promise.all(
370
- frontier.map(async (id) => {
371
- const code = await host.read(id);
372
- let program: Program;
373
- try {
374
- program = parse(id.replace(/\?.*$/, ''), code ?? '');
375
- } catch {
376
- return [];
400
+ async function readModule(id: string): Promise<string[]> {
401
+ const code = await host.read(id);
402
+ let program: Program;
403
+ try {
404
+ program = parse(id.replace(/\?.*$/, ''), code ?? '');
405
+ } catch {
406
+ return [];
407
+ }
408
+ const { static: statics, dynamic } = collectSpecifiers(program);
409
+ const targets = new Map<string, string>();
410
+ async function resolveSource(source: string): Promise<void> {
411
+ const target = await host.resolve(source, id);
412
+ if (target !== undefined) {
413
+ targets.set(source, target);
377
414
  }
378
- const { static: statics, dynamic } = specifiersOf(program);
379
- const targets = new Map<string, string>();
380
- await Promise.all(
381
- [...statics, ...dynamic].map(async (source) => {
382
- const target = await host.resolve(source, id);
383
- if (target !== undefined) {
384
- targets.set(source, target);
385
- }
386
- }),
387
- );
388
- // A module loaded with `import()` is used as a whole.
389
- for (const source of dynamic) {
390
- const target = targets.get(source);
391
- if (target !== undefined) {
392
- usage.all(target);
393
- }
415
+ }
416
+ const resolving: Promise<void>[] = [];
417
+ for (const source of [...statics, ...dynamic]) {
418
+ resolving.push(resolveSource(source));
419
+ }
420
+ await Promise.all(resolving);
421
+ // A module loaded with `import()` is used as a whole.
422
+ for (const source of dynamic) {
423
+ const target = targets.get(source);
424
+ if (target !== undefined) {
425
+ usage.all(target);
394
426
  }
395
- recordModule(usage, id, program, targets);
396
- usage.imports.set(id, [...targets.values()]);
397
- return [...targets.values()];
398
- }),
399
- );
400
- frontier = [...new Set(next.flat())].filter(
401
- (id) => !seen.has(id) && SCRIPT.test(id.replace(/\?.*$/, '')),
402
- );
427
+ }
428
+ recordModule(usage, id, program, targets);
429
+ usage.imports.set(id, [...targets.values()]);
430
+ return [...targets.values()];
431
+ }
432
+ const reading: Promise<string[]>[] = [];
433
+ for (const id of frontier) {
434
+ reading.push(readModule(id));
435
+ }
436
+ // oxlint-disable-next-line no-await-in-loop
437
+ const next = await Promise.all(reading);
438
+ frontier = [];
439
+ for (const id of new Set(next.flat())) {
440
+ if (!seen.has(id) && SCRIPT.test(id.replace(/\?.*$/, ''))) {
441
+ frontier.push(id);
442
+ }
443
+ }
403
444
  }
404
445
  return usage;
405
446
  }
@@ -1 +0,0 @@
1
- {"version":3,"file":"compile-CwH9mvHD.d.mts","names":[],"sources":["../src/value.ts","../src/context.ts","../src/constants.ts","../src/compile.ts"],"mappings":";;;;KAUY;;;;KC4DA;;;;;UAMK;WACN,MAAM;;WAEN;;WAEA;;;;;;;;KC9DC,oBAAoB,SAAS,eAAe,SAAS,eAAe;;;UCF/D;;;;;;;EAOf;;;;;;;EAOA;;;;;;EAMA;;;;;;;EAOA;;;;;;;EAOA;;;;;;;EAOA;;;;;;;EAOA;;;;;;EAMA,iBAAiB,SAAS;;;;;;;EAO1B;;;;;;EAMA,oBAAoB;;;;;;EAMpB,kBAAkB,SAAS,eAAe;;;;;;EAM1C,mBAAmB,SAAS;;;;;;;;EAQ5B,mBAAmB,SAAS;;;;;;;;EAQ5B;;;;;;EAMA;;;;;;;;EAQA;;;;;;EAMA;;;;;;;EAOA;;UAGe;EACf;;EAEA,KAAK;;;;;EAKL,UAAU;;;;;;;;;;;;;iBAoGI,QAAQ,cAAc,UAAS,iBAAsB;UA0DpD;;;;;EAKf;;;;;;EAMA,SAAS,eAAe;;;;;;;;iBASV,oBACd,cACA,UAAS,KAAK,wEACb"}