solid-optimizer 0.4.0 → 0.4.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/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import { n as readModuleConstants, t as compile } from "./compile-D3caSUtw.mjs";
1
+ import { n as readModuleConstants, t as compile } from "./compile-CqhOfBvG.mjs";
2
2
  export { compile, readModuleConstants };
package/dist/vite.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { a as lookup, c as forEachChild, d as unwrap, i as analyzeScopes, l as isIntrinsicTag, n as readModuleConstants, o as scopeAt, r as CONTEXT_SAFE_PRIMITIVES, s as collectParents, t as compile, u as parse } from "./compile-D3caSUtw.mjs";
1
+ import { a as lookup, c as forEachChild, d as unwrap, i as analyzeScopes, l as isIntrinsicTag, n as readModuleConstants, o as scopeAt, r as CONTEXT_SAFE_PRIMITIVES, s as collectParents, t as compile, u as parse } from "./compile-CqhOfBvG.mjs";
2
2
  import { createRequire } from "node:module";
3
3
  import remapping from "@jridgewell/remapping";
4
4
  import MagicString from "magic-string";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solid-optimizer",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "Experimental compile-time optimizer for SolidJS 1.x",
5
5
  "keywords": [
6
6
  "compiler",
package/src/compile.ts CHANGED
@@ -348,8 +348,9 @@ export function readModuleConstants(
348
348
  options: Pick<CompileOptions, 'filename' | 'importedConstants'> = {},
349
349
  ): ModuleConstants {
350
350
  const program = parse(options.filename ?? 'input.jsx', code);
351
+ const scopes = analyzeScopes(program);
351
352
  return {
352
- imports: namedImportSources(program),
353
- exports: constantExports(program, resolveOptions(options)),
353
+ imports: namedImportSources(program, scopes),
354
+ exports: constantExports(program, resolveOptions(options), scopes),
354
355
  };
355
356
  }
package/src/constants.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * from `importedConstants`, which a caller fills with `constantExports` of
6
6
  * each imported module.
7
7
  */
8
- import type { MemberExpression, Node, Program } from 'oxc-parser';
8
+ import type { ImportDeclaration, MemberExpression, Node, Program } from 'oxc-parser';
9
9
  import type { ResolvedOptions } from './context';
10
10
  import { isConstantDeclaration } from './context';
11
11
  import type { Binding, ScopeAnalysis } from './scope';
@@ -131,17 +131,19 @@ export class ConstantTable {
131
131
  /**
132
132
  * The modules `program` imports or re-exports bindings from. Their
133
133
  * constant exports are what `importedConstants` can hold.
134
+ *
135
+ * An import counts only when one of its bindings is read as a value. A
136
+ * binding read only in types has no constant to fold, and TypeScript drops its
137
+ * import, so the module it names may not even build for this target.
134
138
  */
135
- export function namedImportSources(program: Program): string[] {
139
+ export function namedImportSources(
140
+ program: Program,
141
+ scopes: ScopeAnalysis = analyzeScopes(program),
142
+ ): string[] {
136
143
  const sources = new Set<string>();
137
144
  for (const statement of program.body) {
138
145
  if (statement.type === 'ImportDeclaration') {
139
- if (
140
- statement.importKind !== 'type' &&
141
- statement.specifiers.some(
142
- (specifier) => !(specifier.type === 'ImportSpecifier' && specifier.importKind === 'type'),
143
- )
144
- ) {
146
+ if (statement.importKind !== 'type' && readsAsValue(statement.specifiers, scopes)) {
145
147
  sources.add(statement.source.value);
146
148
  }
147
149
  } else if (
@@ -156,6 +158,23 @@ export function namedImportSources(program: Program): string[] {
156
158
  return [...sources];
157
159
  }
158
160
 
161
+ /**
162
+ * Whether the module reads one of these import bindings as a value. Scope
163
+ * analysis skips type positions, so a binding used only in types has no
164
+ * references.
165
+ */
166
+ function readsAsValue(specifiers: ImportDeclaration['specifiers'], scopes: ScopeAnalysis): boolean {
167
+ for (const specifier of specifiers) {
168
+ if (specifier.type === 'ImportSpecifier' && specifier.importKind === 'type') {
169
+ continue;
170
+ }
171
+ if (scopes.root.bindings.get(specifier.local.name)?.references.length) {
172
+ return true;
173
+ }
174
+ }
175
+ return false;
176
+ }
177
+
159
178
  function exportName(node: Node): string | undefined {
160
179
  if (node.type === 'Identifier') {
161
180
  return node.name;