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.
@@ -868,16 +868,32 @@ var ConstantTable = class {
868
868
  /**
869
869
  * The modules `program` imports or re-exports bindings from. Their
870
870
  * constant exports are what `importedConstants` can hold.
871
+ *
872
+ * An import counts only when one of its bindings is read as a value. A
873
+ * binding read only in types has no constant to fold, and TypeScript drops its
874
+ * import, so the module it names may not even build for this target.
871
875
  */
872
- function namedImportSources(program) {
876
+ function namedImportSources(program, scopes = analyzeScopes(program)) {
873
877
  const sources = /* @__PURE__ */ new Set();
874
878
  for (const statement of program.body) if (statement.type === "ImportDeclaration") {
875
- if (statement.importKind !== "type" && statement.specifiers.some((specifier) => !(specifier.type === "ImportSpecifier" && specifier.importKind === "type"))) sources.add(statement.source.value);
879
+ if (statement.importKind !== "type" && readsAsValue(statement.specifiers, scopes)) sources.add(statement.source.value);
876
880
  } else if (statement.type === "ExportNamedDeclaration" && statement.source || statement.type === "ExportAllDeclaration" && !statement.exported) {
877
881
  if (statement.exportKind !== "type" && statement.source) sources.add(statement.source.value);
878
882
  }
879
883
  return [...sources];
880
884
  }
885
+ /**
886
+ * Whether the module reads one of these import bindings as a value. Scope
887
+ * analysis skips type positions, so a binding used only in types has no
888
+ * references.
889
+ */
890
+ function readsAsValue(specifiers, scopes) {
891
+ for (const specifier of specifiers) {
892
+ if (specifier.type === "ImportSpecifier" && specifier.importKind === "type") continue;
893
+ if (scopes.root.bindings.get(specifier.local.name)?.references.length) return true;
894
+ }
895
+ return false;
896
+ }
881
897
  function exportName(node) {
882
898
  if (node.type === "Identifier") return node.name;
883
899
  if (node.type === "Literal" && typeof node.value === "string") return node.value;
@@ -4233,12 +4249,13 @@ function compile(code, options = {}) {
4233
4249
  */
4234
4250
  function readModuleConstants(code, options = {}) {
4235
4251
  const program = parse(options.filename ?? "input.jsx", code);
4252
+ const scopes = analyzeScopes(program);
4236
4253
  return {
4237
- imports: namedImportSources(program),
4238
- exports: constantExports(program, resolveOptions(options))
4254
+ imports: namedImportSources(program, scopes),
4255
+ exports: constantExports(program, resolveOptions(options), scopes)
4239
4256
  };
4240
4257
  }
4241
4258
  //#endregion
4242
4259
  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 as u };
4243
4260
 
4244
- //# sourceMappingURL=compile-D3caSUtw.mjs.map
4261
+ //# sourceMappingURL=compile-CqhOfBvG.mjs.map