solid-optimizer 1.0.0-next.7 → 1.0.0-next.8

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
@@ -8,6 +8,8 @@
8
8
 
9
9
  ![A small app compiled by Solid's JSX transform alone and with solid-optimizer first. The optimized build has 1 template instead of 5, and its server HTML has 1 hydration key instead of 4.](https://github.com/lxsmnsyc/solid-optimizer/blob/main/examples/showcase/comparison.png?raw=true)
10
10
 
11
+ The website, [solid-optimizer.vercel.app](https://solid-optimizer.vercel.app), shows each pass and the benchmarks for the example apps.
12
+
11
13
  ## Install
12
14
 
13
15
  The Solid 2.0 version is published under the `next` tag. The `latest` version is for Solid 1.x, which only gets fixes now, from the [`0.x` branch](https://github.com/lxsmnsyc/solid-optimizer/tree/0.x).
@@ -140,7 +142,7 @@ Each line is a pass, a reason, how many times it happened, and the names it happ
140
142
  - `no-host` means the component has statements, and the call is not in JSX that a component or one of Solid's callbacks returns. A `<Show>` child or a `fallback` is one such place.
141
143
  - `size` means the copies would be larger than the calls.
142
144
  - `lazy`, `imported`, `nested`, and `not-a-function` mean the tag is not a component declared at the top of the same module or chunk.
143
- - A reason that starts with `copy-` is about copying a component from another module. `copy-package` means it comes from a package in `node_modules` that `optimizer.packages` does not list, `copy-cycle` means the two modules import each other, `copy-wrapper` means the component only returns a call of another component in its module, and `copy-size` means the copy was larger.
145
+ - A reason that starts with `copy-` is about copying a component from another module. `copy-package` means it comes from a package in `node_modules` that `optimizer.packages` does not list, `copy-cycle` means the component's module imports the importer back, directly or through other modules, a listed package, or a virtual module, `copy-virtual` means the copy would import a virtual module that its specifier does not reach from the importer, `copy-wrapper` means the component only returns a call of another component in its module, and `copy-size` means the copy was larger.
144
146
 
145
147
  The provider pass reports `open-component`, `open-call`, `open-new`, `open-spread`, and `open-outer` for code under the provider that it cannot see into, and `provider-value`, `provider-undefined`, and `provider-shape` for a value or provider it cannot handle. The memo pass reports `memo-reads`, `memo-read-place`, `memo-constant`, and a few more for a memo it keeps, and `signal-constant` for a signal that nothing writes but that is used other than by calling it.
146
148
 
@@ -199,7 +201,7 @@ Solid then creates one template for `<main>` instead of two.
199
201
 
200
202
  A component inlines when all of these hold:
201
203
 
202
- - It is declared once at the top level, with a capitalized name. A call through an alias, like `const Separator = SeparatorRoot`, is a call of the component it names.
204
+ - It is declared once at the top level, with a capitalized name. A call through an alias, like `const Separator = SeparatorRoot`, is a call of the component it names. So is a call through a compound component, like Kobalte's `const Dialog = Object.assign(DialogRoot, { Trigger: DialogTrigger })`: `<Dialog>` calls `DialogRoot` and `<Dialog.Trigger>` calls `DialogTrigger`, as long as nothing writes to a member of either.
203
205
  - Its parameter is one identifier that is only read as `props.name`, spread onto an element as `{...props}`, or passed to `merge()` and `omit()`. See below.
204
206
  - Its body ends in the only `return`, and that `return` returns JSX.
205
207
  - It does not use `this`, `arguments`, `super`, or `new.target`.
@@ -924,6 +924,16 @@ function namedImportSources(program, scopes = analyzeScopes(program)) {
924
924
  return [...sources];
925
925
  }
926
926
  /**
927
+ * The modules a module still imports once its types are stripped: the
928
+ * sources of `namedImportSources`, and imports for side effects only, like
929
+ * `import './setup'`.
930
+ */
931
+ function runtimeImportSources(program, scopes = analyzeScopes(program)) {
932
+ const sources = new Set(namedImportSources(program, scopes));
933
+ for (const statement of program.body) if (statement.type === "ImportDeclaration" && statement.importKind !== "type" && statement.specifiers.length === 0) sources.add(statement.source.value);
934
+ return [...sources];
935
+ }
936
+ /**
927
937
  * Whether the module reads one of these import bindings as a value. Scope
928
938
  * analysis skips type positions, so a binding used only in types has no
929
939
  * references.
@@ -4279,16 +4289,82 @@ var Inliner = class {
4279
4289
  return target && target !== binding && target.scope.parent === void 0 && !target.mutated && isCapitalized(target.name) ? target : void 0;
4280
4290
  }
4281
4291
  /**
4292
+ * The parts of a compound component like Kobalte's
4293
+ * `const Dialog = Object.assign(DialogRoot, { Trigger: DialogTrigger })`,
4294
+ * or `undefined` when the binding is not one. `<Dialog>` is `<DialogRoot>`,
4295
+ * since `Object.assign` returns its first argument, and `<Dialog.Trigger>`
4296
+ * is `<DialogTrigger>` while nothing writes to a member of either.
4297
+ */
4298
+ compoundTarget(binding) {
4299
+ const { declaration } = binding;
4300
+ if (!isCapitalized(binding.name) || !isConstantDeclaration(binding, this.context.options) || declaration.type !== "VariableDeclarator" || declaration.id !== binding.identifier || !declaration.init) return;
4301
+ const init = unwrap(declaration.init);
4302
+ if (init.type !== "CallExpression" || init.optional || init.arguments.length !== 2 || !this.isObjectAssign(init.callee)) return;
4303
+ const [first, second] = init.arguments;
4304
+ if (first.type !== "Identifier" || second.type !== "ObjectExpression") return;
4305
+ const root = this.topLevelComponent(first);
4306
+ if (!root || root === binding || this.hasMemberWrite(binding) || this.hasMemberWrite(root)) return;
4307
+ const parts = /* @__PURE__ */ new Map();
4308
+ for (const property of second.properties) {
4309
+ if (property.type !== "Property" || property.kind !== "init" || property.computed || property.method || property.value.type !== "Identifier") return;
4310
+ const key = propertyKeyName(property.key);
4311
+ if (key === void 0) return;
4312
+ const part = this.topLevelComponent(property.value);
4313
+ if (part) parts.set(key, part);
4314
+ }
4315
+ return {
4316
+ root,
4317
+ parts
4318
+ };
4319
+ }
4320
+ /** Whether `callee` is the global `Object.assign`. */
4321
+ isObjectAssign(callee) {
4322
+ return callee.type === "MemberExpression" && !callee.computed && !callee.optional && callee.object.type === "Identifier" && callee.object.name === "Object" && !this.context.scopes.references.get(callee.object) && callee.property.type === "Identifier" && callee.property.name === "assign";
4323
+ }
4324
+ /** The top-level binding with a capitalized name that `reference` reads, when nothing reassigns it. */
4325
+ topLevelComponent(reference) {
4326
+ const target = this.context.scopes.references.get(reference);
4327
+ return target && target.scope.parent === void 0 && !target.mutated && isCapitalized(target.name) ? target : void 0;
4328
+ }
4329
+ /** Whether anything assigns to, updates, or deletes a member of the binding. */
4330
+ hasMemberWrite(binding) {
4331
+ for (const reference of binding.references) {
4332
+ const member = this.context.parents.get(reference);
4333
+ if (member?.type !== "MemberExpression" || member.object !== reference) continue;
4334
+ const use = this.context.parents.get(member);
4335
+ if (use?.type === "AssignmentExpression" && use.left === member || use?.type === "UpdateExpression" || use?.type === "UnaryExpression" && use.operator === "delete") return true;
4336
+ }
4337
+ return false;
4338
+ }
4339
+ /** Whether the code reads a member of the binding, like `DialogRoot.Trigger`. */
4340
+ hasMemberRead(binding) {
4341
+ for (const reference of binding.references) {
4342
+ const member = this.context.parents.get(reference);
4343
+ if ((member?.type === "MemberExpression" || member?.type === "JSXMemberExpression") && member.object === reference) return true;
4344
+ }
4345
+ return false;
4346
+ }
4347
+ /**
4282
4348
  * The statement of an alias nothing uses that `reference` is the value
4283
4349
  * of, like `const Separator = SeparatorRoot` once its tags are renamed.
4284
- * It does not keep the component, and goes away with it.
4350
+ * It does not keep the component, and goes away with it. A compound
4351
+ * component counts too, when its tags are renamed and nothing reads the
4352
+ * members `Object.assign` gave its root.
4285
4353
  */
4286
4354
  unusedAlias(reference) {
4287
- const declarator = this.context.parents.get(reference);
4355
+ let declarator = this.context.parents.get(reference);
4356
+ if (declarator?.type === "Property") declarator = this.context.parents.get(declarator);
4357
+ if (declarator?.type === "ObjectExpression" || declarator?.type === "CallExpression") {
4358
+ const call = declarator.type === "CallExpression" ? declarator : this.context.parents.get(declarator);
4359
+ declarator = call ? this.context.parents.get(call) : void 0;
4360
+ }
4288
4361
  if (declarator?.type !== "VariableDeclarator" || declarator.id.type !== "Identifier") return;
4289
4362
  const alias = this.context.scopes.root.bindings.get(declarator.id.name);
4363
+ if (alias?.declaration !== declarator || alias.references.length > 0) return;
4364
+ const compound = this.compoundTarget(alias);
4365
+ if (this.aliasTarget(alias) === void 0 && (!compound || this.hasMemberRead(compound.root))) return;
4290
4366
  const statement = this.context.parents.get(declarator);
4291
- return alias?.declaration === declarator && this.aliasTarget(alias) !== void 0 && alias.references.length === 0 && statement?.type === "VariableDeclaration" && statement.declarations.length === 1 && this.context.parents.get(statement)?.type === "Program" ? statement : void 0;
4367
+ return statement?.type === "VariableDeclaration" && statement.declarations.length === 1 && this.context.parents.get(statement)?.type === "Program" ? statement : void 0;
4292
4368
  }
4293
4369
  /**
4294
4370
  * Renames each tag of an alias to the component it names, where that name
@@ -4298,13 +4374,23 @@ var Inliner = class {
4298
4374
  renameAliasTags() {
4299
4375
  let renamed = false;
4300
4376
  for (const binding of this.context.scopes.root.bindings.values()) {
4301
- const target = this.aliasTarget(binding);
4377
+ const compound = this.compoundTarget(binding);
4378
+ const target = this.aliasTarget(binding) ?? compound?.root;
4302
4379
  if (!target) continue;
4303
4380
  for (const reference of binding.references) {
4304
4381
  const parent = this.context.parents.get(reference);
4305
- if (parent?.type !== "JSXOpeningElement" && parent?.type !== "JSXClosingElement" || parent.name !== reference) continue;
4306
- if (lookup(scopeAt(this.context.scopes, this.context.parents, reference), target.name) !== target) continue;
4307
- this.context.s.overwrite(reference.start, reference.end, target.name);
4382
+ let tag = reference;
4383
+ let renamedTo = target;
4384
+ if (parent?.type === "JSXMemberExpression" && parent.object === reference) {
4385
+ const part = compound?.parts.get(parent.property.name);
4386
+ if (!part) continue;
4387
+ tag = parent;
4388
+ renamedTo = part;
4389
+ }
4390
+ const element = this.context.parents.get(tag);
4391
+ if (element?.type !== "JSXOpeningElement" && element?.type !== "JSXClosingElement" || element.name !== tag) continue;
4392
+ if (lookup(scopeAt(this.context.scopes, this.context.parents, reference), renamedTo.name) !== renamedTo) continue;
4393
+ this.context.s.overwrite(tag.start, tag.end, renamedTo.name);
4308
4394
  renamed = true;
4309
4395
  }
4310
4396
  }
@@ -4521,6 +4607,11 @@ function hasChild(children, child) {
4521
4607
  for (const item of children) if (item === child) return true;
4522
4608
  return false;
4523
4609
  }
4610
+ /** The name a non-computed object key spells, like `Trigger` or `'Trigger'`. */
4611
+ function propertyKeyName(key) {
4612
+ if (key.type === "Identifier") return key.name;
4613
+ return key.type === "Literal" && typeof key.value === "string" ? key.value : void 0;
4614
+ }
4524
4615
  //#endregion
4525
4616
  //#region src/memo.ts
4526
4617
  /**
@@ -5494,6 +5585,58 @@ function expressionText(context, attribute) {
5494
5585
  }
5495
5586
  }
5496
5587
  //#endregion
5588
+ //#region src/terminate.ts
5589
+ const TERMINATED = /* @__PURE__ */ new Set([
5590
+ "BreakStatement",
5591
+ "ContinueStatement",
5592
+ "DebuggerStatement",
5593
+ "DoWhileStatement",
5594
+ "ExportAllDeclaration",
5595
+ "ExpressionStatement",
5596
+ "ImportDeclaration",
5597
+ "ReturnStatement",
5598
+ "ThrowStatement",
5599
+ "TSTypeAliasDeclaration",
5600
+ "VariableDeclaration"
5601
+ ]);
5602
+ const LOOP_HEADS = /* @__PURE__ */ new Set([
5603
+ "ForStatement",
5604
+ "ForInStatement",
5605
+ "ForOfStatement"
5606
+ ]);
5607
+ function isTerminated(node, context) {
5608
+ if (node.type === "VariableDeclaration") {
5609
+ const parent = context.parents.get(node);
5610
+ return !(parent && LOOP_HEADS.has(parent.type) && Reflect.get(parent, "body") !== node);
5611
+ }
5612
+ if (node.type === "ExportNamedDeclaration") return !node.declaration;
5613
+ if (node.type === "ExportDefaultDeclaration") {
5614
+ const { type } = node.declaration;
5615
+ return type !== "FunctionDeclaration" && type !== "ClassDeclaration";
5616
+ }
5617
+ return TERMINATED.has(node.type);
5618
+ }
5619
+ const TRIVIA = /(?:\s|\/\/[^\n\r\u2028\u2029]*|\/\*[\s\S]*?\*\/)*/y;
5620
+ /** Whether code other than the closing brace of a block follows `position`. */
5621
+ function hasCodeAfter(code, position) {
5622
+ TRIVIA.lastIndex = position;
5623
+ TRIVIA.exec(code);
5624
+ return TRIVIA.lastIndex < code.length && code[TRIVIA.lastIndex] !== "}";
5625
+ }
5626
+ function terminateStatements(context) {
5627
+ const { code, s } = context;
5628
+ let changed = false;
5629
+ const visit = (node) => {
5630
+ if (isTerminated(node, context) && code[node.end - 1] !== ";" && hasCodeAfter(code, node.end)) {
5631
+ s.appendLeft(node.end, ";");
5632
+ changed = true;
5633
+ }
5634
+ forEachChild(node, visit);
5635
+ };
5636
+ visit(context.program);
5637
+ return changed;
5638
+ }
5639
+ //#endregion
5497
5640
  //#region src/compile.ts
5498
5641
  const DEFAULT_BUILT_INS = [
5499
5642
  "For",
@@ -5585,6 +5728,12 @@ function compile(code, options = {}) {
5585
5728
  if (parsed?.code !== current) parsed = parseCode(current, filename);
5586
5729
  return parsed;
5587
5730
  };
5731
+ const terminated = runPass(parsedCurrent(), filename, resolved, sourceMap, terminateStatements, void 0);
5732
+ if (terminated) {
5733
+ current = terminated.code;
5734
+ if (terminated.map !== void 0) maps.push(terminated.map);
5735
+ }
5736
+ const start = current;
5588
5737
  const maxPasses = options.maxPasses ?? 10;
5589
5738
  let skipped;
5590
5739
  for (let round = 0; round < maxPasses; round += 1) {
@@ -5606,7 +5755,7 @@ function compile(code, options = {}) {
5606
5755
  current = result.code;
5607
5756
  if (result.map !== void 0) maps.push(result.map);
5608
5757
  }
5609
- if (current !== code) {
5758
+ if (current !== start) {
5610
5759
  const result = runPass(parsedCurrent(), filename, resolved, sourceMap, repairNesting, void 0);
5611
5760
  if (result) {
5612
5761
  current = result.code;
@@ -5614,6 +5763,11 @@ function compile(code, options = {}) {
5614
5763
  }
5615
5764
  }
5616
5765
  const report = skipped ? { skipped } : {};
5766
+ if (current === start) return {
5767
+ code,
5768
+ map: null,
5769
+ ...report
5770
+ };
5617
5771
  if (maps.length === 0) return {
5618
5772
  code: current,
5619
5773
  map: null,
@@ -5640,6 +5794,6 @@ function readModuleConstants(code, options = {}) {
5640
5794
  };
5641
5795
  }
5642
5796
  //#endregion
5643
- export { lookup as a, forEachChild as c, unwrap as d, analyzeScopes as i, isIntrinsicTag as l, readModuleConstants as n, scopeAt as o, CONTEXT_SAFE_PRIMITIVES as r, collectParents as s, compile as t, parse$1 as u };
5797
+ export { analyzeScopes as a, collectParents as c, parse$1 as d, unwrap as f, runtimeImportSources as i, forEachChild as l, readModuleConstants as n, lookup as o, CONTEXT_SAFE_PRIMITIVES as r, scopeAt as s, compile as t, isIntrinsicTag as u };
5644
5798
 
5645
- //# sourceMappingURL=compile-BuPR3pVT.mjs.map
5799
+ //# sourceMappingURL=compile-O1Z60VsW.mjs.map