@vxrn/compiler 2.9.1 → 2.10.0

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.
@@ -232,6 +232,7 @@ async function transformMixedNativeModule(code, filename, sourceMaps = false) {
232
232
  let analysis = analyze(program);
233
233
  let mixed = false;
234
234
  function inspect(node, parent, key) {
235
+ if (node.declare || node.importKind === "type" || node.exportKind === "type" || /^(?:TSTypeAliasDeclaration|TSInterfaceDeclaration)$/.test(node.type) || /^(?:typeAnnotation|typeArguments|typeParameters|returnType|superTypeArguments|implements)$/.test(key)) return;
235
236
  if (node.type === "Identifier" && /^(?:module|exports)$/.test(node.name) && isReference(node, parent, key, analysis.bindings) && !analysis.binding(node)) mixed = true;
236
237
  children(node, (child, key2) => inspect(child, node, key2));
237
238
  }
@@ -274,7 +275,7 @@ async function transformMixedNativeModule(code, filename, sourceMaps = false) {
274
275
  syntax: "ecmascript",
275
276
  jsx: true
276
277
  },
277
- transform: { react: { runtime: "preserve" } }
278
+ transform: { react: { runtime: "classic" } }
278
279
  },
279
280
  module: {
280
281
  type: "commonjs",
@@ -204,6 +204,7 @@ async function transformMixedNativeModule(code, filename, sourceMaps = false) {
204
204
  let analysis = analyze(program);
205
205
  let mixed = false;
206
206
  function inspect(node, parent, key) {
207
+ if (node.declare || node.importKind === "type" || node.exportKind === "type" || /^(?:TSTypeAliasDeclaration|TSInterfaceDeclaration)$/.test(node.type) || /^(?:typeAnnotation|typeArguments|typeParameters|returnType|superTypeArguments|implements)$/.test(key)) return;
207
208
  if (node.type === "Identifier" && /^(?:module|exports)$/.test(node.name) && isReference(node, parent, key, analysis.bindings) && !analysis.binding(node)) mixed = true;
208
209
  children(node, (child, key2) => inspect(child, node, key2));
209
210
  }
@@ -246,7 +247,7 @@ async function transformMixedNativeModule(code, filename, sourceMaps = false) {
246
247
  syntax: "ecmascript",
247
248
  jsx: true
248
249
  },
249
- transform: { react: { runtime: "preserve" } }
250
+ transform: { react: { runtime: "classic" } }
250
251
  },
251
252
  module: {
252
253
  type: "commonjs",
@@ -1 +1 @@
1
- {"version":3,"file":"transformMixedNativeModule.js","names":[],"sources":["esm/transformMixedNativeModule.js"],"sourcesContent":["import { parseSync } from \"oxc-parser\";\nimport MagicString from \"magic-string\";\nimport remapping from \"@jridgewell/remapping\";\nfunction analyze(program) {\n const scopes = /* @__PURE__ */ new WeakMap();\n const bindings = /* @__PURE__ */ new WeakSet();\n const root = { bindings: /* @__PURE__ */ new Set(), function: true };\n function bind(pattern, scope) {\n if (!pattern) return;\n if (pattern.type === \"Identifier\") {\n scope.bindings.add(pattern.name);\n bindings.add(pattern);\n } else if (pattern.type === \"RestElement\") bind(pattern.argument, scope);\n else if (pattern.type === \"AssignmentPattern\") bind(pattern.left, scope);\n else if (pattern.type === \"ArrayPattern\") {\n for (const item of pattern.elements) bind(item, scope);\n } else if (pattern.type === \"ObjectPattern\") {\n for (const item of pattern.properties)\n bind(item.type === \"RestElement\" ? item.argument : item.value, scope);\n }\n }\n function visit(node, parentScope) {\n let scope = parentScope;\n const fn = /^(?:FunctionDeclaration|FunctionExpression|ArrowFunctionExpression)$/.test(\n node.type\n );\n if (node.type === \"FunctionDeclaration\" || node.type === \"ClassDeclaration\")\n bind(node.id, parentScope);\n if (fn || /^(?:BlockStatement|CatchClause|ForStatement|ForInStatement|ForOfStatement|SwitchStatement|ClassExpression|ClassDeclaration|StaticBlock)$/.test(\n node.type\n )) {\n scope = {\n parent: parentScope,\n bindings: /* @__PURE__ */ new Set(),\n function: fn || node.type === \"StaticBlock\"\n };\n }\n scopes.set(node, scope);\n if (fn) {\n if (node.type === \"FunctionExpression\") bind(node.id, scope);\n for (const param of node.params) bind(param, scope);\n }\n if (node.type === \"ClassExpression\" || node.type === \"ClassDeclaration\")\n bind(node.id, scope);\n if (node.type === \"CatchClause\") bind(node.param, scope);\n if (node.type === \"VariableDeclaration\") {\n let target = scope;\n if (node.kind === \"var\") while (!target.function) target = target.parent;\n for (const declaration of node.declarations) bind(declaration.id, target);\n }\n if (node.type === \"ImportDeclaration\")\n for (const specifier of node.specifiers) bind(specifier.local, scope);\n children(node, (child) => visit(child, scope));\n if ((node.type === \"FunctionDeclaration\" || node.type === \"ClassDeclaration\") && node.id)\n scopes.set(node.id, parentScope);\n }\n visit(program, root);\n function binding(node) {\n let scope = scopes.get(node);\n while (scope && !scope.bindings.has(node.name)) scope = scope.parent;\n return scope;\n }\n return { bindings, binding, rootBindings: root.bindings };\n}\nfunction children(node, visit) {\n for (const key of Object.keys(node)) {\n if (key === \"parent\") continue;\n const value = node[key];\n if (Array.isArray(value)) {\n for (const child of value) if (child?.type) visit(child, key);\n } else if (value?.type) visit(value, key);\n }\n}\nfunction isReference(node, parent, key, bindings) {\n if (bindings.has(node)) return false;\n if ((parent.type === \"MemberExpression\" || parent.type === \"OptionalMemberExpression\") && key === \"property\" && !parent.computed)\n return false;\n if (/^(?:Property|MethodDefinition|PropertyDefinition)$/.test(parent.type) && key === \"key\" && !parent.computed)\n return false;\n if (/^(?:Import.*Specifier|ExportSpecifier|LabeledStatement|BreakStatement|ContinueStatement|MetaProperty)$/.test(\n parent.type\n ))\n return false;\n return true;\n}\nfunction lowerLocalExports(program, code, analysis) {\n const edited = new MagicString(code);\n let exportAccess = \"__mixedExports\";\n while (code.includes(exportAccess)) exportAccess += \"_\";\n const exportBinding = analysis.rootBindings.has(\"exports\") ? `const ${exportAccess} = module.exports;` : `const ${exportAccess} = () => exports;`;\n const exportObject = analysis.rootBindings.has(\"exports\") ? exportAccess : `${exportAccess}()`;\n const exported = /* @__PURE__ */ new Map();\n const imported = /* @__PURE__ */ new Set();\n for (const node of program.body)\n if (node.type === \"ImportDeclaration\")\n for (const specifier of node.specifiers) imported.add(specifier.local.name);\n const hoisted = [];\n const defaultFunctions = [];\n function names(pattern) {\n if (!pattern) return [];\n if (pattern.type === \"Identifier\") return [pattern];\n if (pattern.type === \"RestElement\") return names(pattern.argument);\n if (pattern.type === \"AssignmentPattern\") return names(pattern.left);\n if (pattern.type === \"ArrayPattern\") return pattern.elements.flatMap(names);\n if (pattern.type === \"ObjectPattern\")\n return pattern.properties.flatMap(\n (item) => names(item.type === \"RestElement\" ? item.argument : item.value)\n );\n return [];\n }\n function add(id, name) {\n const entry = exported.get(id.name) || {\n scope: analysis.binding(id),\n names: []\n };\n entry.names.push(name);\n exported.set(id.name, entry);\n }\n const assignments = (id) => exported.get(id).names.map((name) => `${exportObject}[${JSON.stringify(name)}] = `).join(\"\") + id;\n for (const node of program.body) {\n if (node.type === \"ExportDefaultDeclaration\") {\n const declaration = node.declaration;\n if (declaration.type === \"FunctionDeclaration\" || declaration.type === \"ClassDeclaration\" && declaration.id) {\n edited.remove(node.start, declaration.start);\n if (declaration.id) {\n add(declaration.id, \"default\");\n if (declaration.type === \"FunctionDeclaration\")\n hoisted.push(declaration.id.name);\n else\n edited.appendLeft(\n node.end,\n `;${exportObject}.default = ${declaration.id.name};`\n );\n } else {\n let name = \"__mixedDefault\";\n while (code.includes(name)) name += \"_\";\n const prefix = code.slice(declaration.start, declaration.body.start).match(/^(?:async\\s+)?function\\s*\\*?/)[0];\n edited.prependRight(declaration.start + prefix.length, ` ${name}`);\n defaultFunctions.push(`${exportObject}.default = ${name};`);\n }\n } else if (declaration.type !== \"TSInterfaceDeclaration\") {\n edited.overwrite(node.start, declaration.start, `${exportObject}.default = (`);\n edited.appendLeft(declaration.end, \")\");\n }\n }\n if (node.type === \"ExportNamedDeclaration\" && !node.source && node.exportKind !== \"type\") {\n const declaration = node.declaration;\n if (declaration) {\n if (/^(?:TS|Interface|Type)/.test(declaration.type)) continue;\n const ids = declaration.type === \"VariableDeclaration\" ? declaration.declarations.flatMap((item) => names(item.id)) : names(declaration.id);\n for (const id of ids) add(id, id.name);\n edited.remove(node.start, declaration.start);\n if (declaration.type === \"FunctionDeclaration\")\n hoisted.push(...ids.map((id) => id.name));\n } else {\n const locals = node.specifiers.filter(\n (item) => item.exportKind !== \"type\" && !imported.has(item.local.name)\n );\n for (const item of locals)\n add(item.local, item.exported.name ?? item.exported.value);\n if (locals.length === node.specifiers.length) edited.remove(node.start, node.end);\n else if (locals.length) {\n const retained = node.specifiers.filter(\n (other) => !locals.includes(other)\n );\n edited.overwrite(\n node.start,\n node.end,\n `export { ${retained.map((other) => code.slice(other.start, other.end)).join(\", \")} };`\n );\n }\n }\n }\n }\n for (const node of program.body) {\n const declaration = node.type === \"ExportNamedDeclaration\" ? node.declaration : node;\n if (!declaration) continue;\n if (declaration.type === \"VariableDeclaration\") {\n for (const item of declaration.declarations)\n for (const id of names(item.id)) {\n if (exported.has(id.name)) {\n if (item.id.type === \"Identifier\" && item.init) {\n const prefix = exported.get(id.name).names.map((name) => `${exportObject}[${JSON.stringify(name)}] = `).join(\"\");\n edited.prependRight(item.init.start, prefix + \"(\");\n edited.appendLeft(item.init.end, \")\");\n } else if (item.id.type !== \"Identifier\")\n edited.appendLeft(node.end, `;${assignments(id.name)};`);\n }\n }\n } else if (declaration.type === \"FunctionDeclaration\" && exported.has(declaration.id?.name)) {\n if (!hoisted.includes(declaration.id.name)) hoisted.push(declaration.id.name);\n } else if (declaration.type === \"ClassDeclaration\" && exported.has(declaration.id?.name)) {\n edited.appendLeft(node.end, `;${assignments(declaration.id.name)};`);\n }\n }\n let temporary = \"__mixedExportResult\";\n while (code.includes(temporary)) temporary += \"_\";\n function mutations(node) {\n if (node.type === \"ForOfStatement\" || node.type === \"ForInStatement\") {\n const ids = (node.left.type === \"VariableDeclaration\" ? node.left.declarations.flatMap((item) => names(item.id)) : names(node.left)).filter(\n (id) => exported.has(id.name) && analysis.binding(id) === exported.get(id.name).scope\n );\n if (ids.length) {\n const publish = ids.map((id) => assignments(id.name) + \";\").join(\"\");\n if (node.body.type === \"BlockStatement\")\n edited.prependRight(node.body.start + 1, publish);\n else {\n edited.prependRight(node.body.start, \"{\" + publish);\n edited.appendLeft(node.body.end, \"}\");\n }\n }\n }\n if (node.type === \"AssignmentExpression\" || node.type === \"UpdateExpression\") {\n const ids = names(\n node.type === \"AssignmentExpression\" ? node.left : node.argument\n ).filter(\n (id) => exported.has(id.name) && analysis.binding(id) === exported.get(id.name).scope\n );\n if (ids.length) {\n edited.prependRight(\n node.start,\n `((${temporary}) => (${ids.map((id) => assignments(id.name)).join(\", \")}, ${temporary}))(`\n );\n edited.appendLeft(node.end, \")\");\n }\n }\n children(node, (child) => mutations(child));\n }\n mutations(program);\n const first = program.body.find(\n (node) => !(node.type === \"ExpressionStatement\" && node.directive)\n );\n const initialization = [...exported.values()].flatMap((entry) => entry.names).map((name) => `${exportObject}[${JSON.stringify(name)}] = void 0;`).join(\"\");\n edited.prependRight(\n first?.start ?? code.length,\n `export {};${exportBinding}${initialization}${hoisted.map((id) => assignments(id) + \";\").join(\"\")}${defaultFunctions.join(\"\")}`\n );\n return edited;\n}\nasync function transformMixedNativeModule(code, filename, sourceMaps = false) {\n if (!/\\b(?:module|exports)\\b/.test(code) || !/\\b(?:import|export)\\b/.test(code)) return;\n const typescript = /\\.[cm]?tsx?$/.test(filename);\n const parsed = parseSync(filename, code, {\n lang: typescript ? filename.endsWith(\"tsx\") ? \"tsx\" : \"ts\" : \"jsx\"\n });\n if (parsed.errors.length) throw new Error(parsed.errors[0].message);\n let program = parsed.program;\n if (!program.body.some((node) => /^(?:Import|Export)/.test(node.type))) return;\n let analysis = analyze(program);\n let mixed = false;\n function inspect(node, parent, key) {\n if (node.type === \"Identifier\" && /^(?:module|exports)$/.test(node.name) && isReference(node, parent, key, analysis.bindings) && !analysis.binding(node))\n mixed = true;\n children(node, (child, key2) => inspect(child, node, key2));\n }\n inspect(program, program, \"\");\n if (!mixed) return;\n const { transform } = await import(\"@swc/core\");\n let typeMap;\n if (typescript) {\n const stripped = await transform(code, {\n filename,\n swcrc: false,\n configFile: false,\n sourceMaps,\n jsc: {\n target: \"esnext\",\n parser: { syntax: \"typescript\", tsx: filename.endsWith(\".tsx\") },\n transform: { react: { runtime: \"preserve\" } }\n },\n module: { type: \"es6\" }\n });\n code = stripped.code;\n typeMap = stripped.map ? JSON.parse(stripped.map) : void 0;\n const javascript = parseSync(filename, code, { lang: \"jsx\" });\n if (javascript.errors.length) throw new Error(javascript.errors[0].message);\n program = javascript.program;\n analysis = analyze(program);\n }\n const edited = lowerLocalExports(program, code, analysis);\n const result = await transform(edited.toString(), {\n filename,\n swcrc: false,\n configFile: false,\n sourceMaps,\n jsc: {\n target: \"esnext\",\n parser: { syntax: \"ecmascript\", jsx: true },\n transform: { react: { runtime: \"preserve\" } }\n },\n module: { type: \"commonjs\", strictMode: false, allowTopLevelThis: true }\n });\n return {\n code: result.code,\n map: result.map ? remapping(\n [\n JSON.parse(result.map),\n edited.generateMap({\n source: filename,\n includeContent: true,\n hires: true\n }),\n ...typeMap ? [typeMap] : []\n ],\n () => null\n ) : void 0\n };\n}\nexport {\n transformMixedNativeModule\n};\n//# sourceMappingURL=transformMixedNativeModule.js.map\n"],"mappings":";;;;;AAGA,SAAS,QAAQ,SAAS;CACxB,MAAM,yBAAyB,IAAI,QAAQ;CAC3C,MAAM,2BAA2B,IAAI,QAAQ;CAC7C,MAAM,OAAO;EAAE,0BAA0B,IAAI,IAAI;EAAG,UAAU;CAAK;CACnE,SAAS,KAAK,SAAS,OAAO;EAC5B,IAAI,CAAC,SAAS;EACd,IAAI,QAAQ,SAAS,cAAc;GACjC,MAAM,SAAS,IAAI,QAAQ,IAAI;GAC/B,SAAS,IAAI,OAAO;EACtB,OAAO,IAAI,QAAQ,SAAS,eAAe,KAAK,QAAQ,UAAU,KAAK;OAClE,IAAI,QAAQ,SAAS,qBAAqB,KAAK,QAAQ,MAAM,KAAK;OAClE,IAAI,QAAQ,SAAS,gBAAgB;GACxC,KAAK,MAAM,QAAQ,QAAQ,UAAU,KAAK,MAAM,KAAK;EACvD,OAAO,IAAI,QAAQ,SAAS,iBAAiB;GAC3C,KAAK,MAAM,QAAQ,QAAQ,YACzB,KAAK,KAAK,SAAS,gBAAgB,KAAK,WAAW,KAAK,OAAO,KAAK;EACxE;CACF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Q;KAAE,QAAQ;KAAc,KAAK,SAAS,SAAS,MAAM;IAAE;IAC/D,WAAW,EAAE,OAAO,EAAE,SAAS,WAAW,EAAE;GAC9C;GACA,QAAQ,EAAE,MAAM,MAAM;EACxB,CAAC;EACD,OAAO,SAAS;EAChB,UAAU,SAAS,MAAM,KAAK,MAAM,SAAS,GAAG,IAAI,KAAK;EACzD,MAAM,aAAa,UAAU,UAAU,MAAM,EAAE,MAAM,MAAM,CAAC;EAC5D,IAAI,WAAW,OAAO,QAAQ,MAAM,IAAI,MAAM,WAAW,OAAO,EAAE,CAAC,OAAO;EAC1E,UAAU,WAAW;EACrB,WAAW,QAAQ,OAAO;CAC5B;CACA,MAAM,SAAS,kBAAkB,SAAS,MAAM,QAAQ;CACxD,MAAM,SAAS,MAAM,UAAU,OAAO,SAAS,GAAG;EAChD;EACA,OAAO;EACP,YAAY;EACZ;EACA,KAAK;GACH,QAAQ;GACR,QAAQ;IAAE,QAAQ;IAAc,KAAK;GAAK;GAC1C,WAAW,EAAE,OAAO,EAAE,SAAS,WAAW,EAAE;EAC9C;EACA,QAAQ;GAAE,MAAM;GAAY,YAAY;GAAO,mBAAmB;EAAK;CACzE,CAAC;CACD,OAAO;EACL,MAAM,OAAO;EACb,KAAK,OAAO,MAAM,UAChB;GACE,KAAK,MAAM,OAAO,GAAG;GACrB,OAAO,YAAY;IACjB,QAAQ;IACR,gBAAgB;IAChB,OAAO;GACT,CAAC;GACD,GAAG,UAAU,CAAC,OAAO,IAAI,CAAC;EAC5B,SACM,IACR,IAAI,KAAK;CACX;AACF"}
1
+ {"version":3,"file":"transformMixedNativeModule.js","names":[],"sources":["esm/transformMixedNativeModule.js"],"sourcesContent":["import { parseSync } from \"oxc-parser\";\nimport MagicString from \"magic-string\";\nimport remapping from \"@jridgewell/remapping\";\nfunction analyze(program) {\n const scopes = /* @__PURE__ */ new WeakMap();\n const bindings = /* @__PURE__ */ new WeakSet();\n const root = { bindings: /* @__PURE__ */ new Set(), function: true };\n function bind(pattern, scope) {\n if (!pattern) return;\n if (pattern.type === \"Identifier\") {\n scope.bindings.add(pattern.name);\n bindings.add(pattern);\n } else if (pattern.type === \"RestElement\") bind(pattern.argument, scope);\n else if (pattern.type === \"AssignmentPattern\") bind(pattern.left, scope);\n else if (pattern.type === \"ArrayPattern\") {\n for (const item of pattern.elements) bind(item, scope);\n } else if (pattern.type === \"ObjectPattern\") {\n for (const item of pattern.properties)\n bind(item.type === \"RestElement\" ? item.argument : item.value, scope);\n }\n }\n function visit(node, parentScope) {\n let scope = parentScope;\n const fn = /^(?:FunctionDeclaration|FunctionExpression|ArrowFunctionExpression)$/.test(\n node.type\n );\n if (node.type === \"FunctionDeclaration\" || node.type === \"ClassDeclaration\")\n bind(node.id, parentScope);\n if (fn || /^(?:BlockStatement|CatchClause|ForStatement|ForInStatement|ForOfStatement|SwitchStatement|ClassExpression|ClassDeclaration|StaticBlock)$/.test(\n node.type\n )) {\n scope = {\n parent: parentScope,\n bindings: /* @__PURE__ */ new Set(),\n function: fn || node.type === \"StaticBlock\"\n };\n }\n scopes.set(node, scope);\n if (fn) {\n if (node.type === \"FunctionExpression\") bind(node.id, scope);\n for (const param of node.params) bind(param, scope);\n }\n if (node.type === \"ClassExpression\" || node.type === \"ClassDeclaration\")\n bind(node.id, scope);\n if (node.type === \"CatchClause\") bind(node.param, scope);\n if (node.type === \"VariableDeclaration\") {\n let target = scope;\n if (node.kind === \"var\") while (!target.function) target = target.parent;\n for (const declaration of node.declarations) bind(declaration.id, target);\n }\n if (node.type === \"ImportDeclaration\")\n for (const specifier of node.specifiers) bind(specifier.local, scope);\n children(node, (child) => visit(child, scope));\n if ((node.type === \"FunctionDeclaration\" || node.type === \"ClassDeclaration\") && node.id)\n scopes.set(node.id, parentScope);\n }\n visit(program, root);\n function binding(node) {\n let scope = scopes.get(node);\n while (scope && !scope.bindings.has(node.name)) scope = scope.parent;\n return scope;\n }\n return { bindings, binding, rootBindings: root.bindings };\n}\nfunction children(node, visit) {\n for (const key of Object.keys(node)) {\n if (key === \"parent\") continue;\n const value = node[key];\n if (Array.isArray(value)) {\n for (const child of value) if (child?.type) visit(child, key);\n } else if (value?.type) visit(value, key);\n }\n}\nfunction isReference(node, parent, key, bindings) {\n if (bindings.has(node)) return false;\n if ((parent.type === \"MemberExpression\" || parent.type === \"OptionalMemberExpression\") && key === \"property\" && !parent.computed)\n return false;\n if (/^(?:Property|MethodDefinition|PropertyDefinition)$/.test(parent.type) && key === \"key\" && !parent.computed)\n return false;\n if (/^(?:Import.*Specifier|ExportSpecifier|LabeledStatement|BreakStatement|ContinueStatement|MetaProperty)$/.test(\n parent.type\n ))\n return false;\n return true;\n}\nfunction lowerLocalExports(program, code, analysis) {\n const edited = new MagicString(code);\n let exportAccess = \"__mixedExports\";\n while (code.includes(exportAccess)) exportAccess += \"_\";\n const exportBinding = analysis.rootBindings.has(\"exports\") ? `const ${exportAccess} = module.exports;` : `const ${exportAccess} = () => exports;`;\n const exportObject = analysis.rootBindings.has(\"exports\") ? exportAccess : `${exportAccess}()`;\n const exported = /* @__PURE__ */ new Map();\n const imported = /* @__PURE__ */ new Set();\n for (const node of program.body)\n if (node.type === \"ImportDeclaration\")\n for (const specifier of node.specifiers) imported.add(specifier.local.name);\n const hoisted = [];\n const defaultFunctions = [];\n function names(pattern) {\n if (!pattern) return [];\n if (pattern.type === \"Identifier\") return [pattern];\n if (pattern.type === \"RestElement\") return names(pattern.argument);\n if (pattern.type === \"AssignmentPattern\") return names(pattern.left);\n if (pattern.type === \"ArrayPattern\") return pattern.elements.flatMap(names);\n if (pattern.type === \"ObjectPattern\")\n return pattern.properties.flatMap(\n (item) => names(item.type === \"RestElement\" ? item.argument : item.value)\n );\n return [];\n }\n function add(id, name) {\n const entry = exported.get(id.name) || {\n scope: analysis.binding(id),\n names: []\n };\n entry.names.push(name);\n exported.set(id.name, entry);\n }\n const assignments = (id) => exported.get(id).names.map((name) => `${exportObject}[${JSON.stringify(name)}] = `).join(\"\") + id;\n for (const node of program.body) {\n if (node.type === \"ExportDefaultDeclaration\") {\n const declaration = node.declaration;\n if (declaration.type === \"FunctionDeclaration\" || declaration.type === \"ClassDeclaration\" && declaration.id) {\n edited.remove(node.start, declaration.start);\n if (declaration.id) {\n add(declaration.id, \"default\");\n if (declaration.type === \"FunctionDeclaration\")\n hoisted.push(declaration.id.name);\n else\n edited.appendLeft(\n node.end,\n `;${exportObject}.default = ${declaration.id.name};`\n );\n } else {\n let name = \"__mixedDefault\";\n while (code.includes(name)) name += \"_\";\n const prefix = code.slice(declaration.start, declaration.body.start).match(/^(?:async\\s+)?function\\s*\\*?/)[0];\n edited.prependRight(declaration.start + prefix.length, ` ${name}`);\n defaultFunctions.push(`${exportObject}.default = ${name};`);\n }\n } else if (declaration.type !== \"TSInterfaceDeclaration\") {\n edited.overwrite(node.start, declaration.start, `${exportObject}.default = (`);\n edited.appendLeft(declaration.end, \")\");\n }\n }\n if (node.type === \"ExportNamedDeclaration\" && !node.source && node.exportKind !== \"type\") {\n const declaration = node.declaration;\n if (declaration) {\n if (/^(?:TS|Interface|Type)/.test(declaration.type)) continue;\n const ids = declaration.type === \"VariableDeclaration\" ? declaration.declarations.flatMap((item) => names(item.id)) : names(declaration.id);\n for (const id of ids) add(id, id.name);\n edited.remove(node.start, declaration.start);\n if (declaration.type === \"FunctionDeclaration\")\n hoisted.push(...ids.map((id) => id.name));\n } else {\n const locals = node.specifiers.filter(\n (item) => item.exportKind !== \"type\" && !imported.has(item.local.name)\n );\n for (const item of locals)\n add(item.local, item.exported.name ?? item.exported.value);\n if (locals.length === node.specifiers.length) edited.remove(node.start, node.end);\n else if (locals.length) {\n const retained = node.specifiers.filter(\n (other) => !locals.includes(other)\n );\n edited.overwrite(\n node.start,\n node.end,\n `export { ${retained.map((other) => code.slice(other.start, other.end)).join(\", \")} };`\n );\n }\n }\n }\n }\n for (const node of program.body) {\n const declaration = node.type === \"ExportNamedDeclaration\" ? node.declaration : node;\n if (!declaration) continue;\n if (declaration.type === \"VariableDeclaration\") {\n for (const item of declaration.declarations)\n for (const id of names(item.id)) {\n if (exported.has(id.name)) {\n if (item.id.type === \"Identifier\" && item.init) {\n const prefix = exported.get(id.name).names.map((name) => `${exportObject}[${JSON.stringify(name)}] = `).join(\"\");\n edited.prependRight(item.init.start, prefix + \"(\");\n edited.appendLeft(item.init.end, \")\");\n } else if (item.id.type !== \"Identifier\")\n edited.appendLeft(node.end, `;${assignments(id.name)};`);\n }\n }\n } else if (declaration.type === \"FunctionDeclaration\" && exported.has(declaration.id?.name)) {\n if (!hoisted.includes(declaration.id.name)) hoisted.push(declaration.id.name);\n } else if (declaration.type === \"ClassDeclaration\" && exported.has(declaration.id?.name)) {\n edited.appendLeft(node.end, `;${assignments(declaration.id.name)};`);\n }\n }\n let temporary = \"__mixedExportResult\";\n while (code.includes(temporary)) temporary += \"_\";\n function mutations(node) {\n if (node.type === \"ForOfStatement\" || node.type === \"ForInStatement\") {\n const ids = (node.left.type === \"VariableDeclaration\" ? node.left.declarations.flatMap((item) => names(item.id)) : names(node.left)).filter(\n (id) => exported.has(id.name) && analysis.binding(id) === exported.get(id.name).scope\n );\n if (ids.length) {\n const publish = ids.map((id) => assignments(id.name) + \";\").join(\"\");\n if (node.body.type === \"BlockStatement\")\n edited.prependRight(node.body.start + 1, publish);\n else {\n edited.prependRight(node.body.start, \"{\" + publish);\n edited.appendLeft(node.body.end, \"}\");\n }\n }\n }\n if (node.type === \"AssignmentExpression\" || node.type === \"UpdateExpression\") {\n const ids = names(\n node.type === \"AssignmentExpression\" ? node.left : node.argument\n ).filter(\n (id) => exported.has(id.name) && analysis.binding(id) === exported.get(id.name).scope\n );\n if (ids.length) {\n edited.prependRight(\n node.start,\n `((${temporary}) => (${ids.map((id) => assignments(id.name)).join(\", \")}, ${temporary}))(`\n );\n edited.appendLeft(node.end, \")\");\n }\n }\n children(node, (child) => mutations(child));\n }\n mutations(program);\n const first = program.body.find(\n (node) => !(node.type === \"ExpressionStatement\" && node.directive)\n );\n const initialization = [...exported.values()].flatMap((entry) => entry.names).map((name) => `${exportObject}[${JSON.stringify(name)}] = void 0;`).join(\"\");\n edited.prependRight(\n first?.start ?? code.length,\n `export {};${exportBinding}${initialization}${hoisted.map((id) => assignments(id) + \";\").join(\"\")}${defaultFunctions.join(\"\")}`\n );\n return edited;\n}\nasync function transformMixedNativeModule(code, filename, sourceMaps = false) {\n if (!/\\b(?:module|exports)\\b/.test(code) || !/\\b(?:import|export)\\b/.test(code)) return;\n const typescript = /\\.[cm]?tsx?$/.test(filename);\n const parsed = parseSync(filename, code, {\n lang: typescript ? filename.endsWith(\"tsx\") ? \"tsx\" : \"ts\" : \"jsx\"\n });\n if (parsed.errors.length) throw new Error(parsed.errors[0].message);\n let program = parsed.program;\n if (!program.body.some((node) => /^(?:Import|Export)/.test(node.type))) return;\n let analysis = analyze(program);\n let mixed = false;\n function inspect(node, parent, key) {\n if (node.declare || node.importKind === \"type\" || node.exportKind === \"type\" || /^(?:TSTypeAliasDeclaration|TSInterfaceDeclaration)$/.test(node.type) || /^(?:typeAnnotation|typeArguments|typeParameters|returnType|superTypeArguments|implements)$/.test(\n key\n ))\n return;\n if (node.type === \"Identifier\" && /^(?:module|exports)$/.test(node.name) && isReference(node, parent, key, analysis.bindings) && !analysis.binding(node))\n mixed = true;\n children(node, (child, key2) => inspect(child, node, key2));\n }\n inspect(program, program, \"\");\n if (!mixed) return;\n const { transform } = await import(\"@swc/core\");\n let typeMap;\n if (typescript) {\n const stripped = await transform(code, {\n filename,\n swcrc: false,\n configFile: false,\n sourceMaps,\n jsc: {\n target: \"esnext\",\n parser: { syntax: \"typescript\", tsx: filename.endsWith(\".tsx\") },\n transform: { react: { runtime: \"preserve\" } }\n },\n module: { type: \"es6\" }\n });\n code = stripped.code;\n typeMap = stripped.map ? JSON.parse(stripped.map) : void 0;\n const javascript = parseSync(filename, code, { lang: \"jsx\" });\n if (javascript.errors.length) throw new Error(javascript.errors[0].message);\n program = javascript.program;\n analysis = analyze(program);\n }\n const edited = lowerLocalExports(program, code, analysis);\n const result = await transform(edited.toString(), {\n filename,\n swcrc: false,\n configFile: false,\n sourceMaps,\n jsc: {\n target: \"esnext\",\n parser: { syntax: \"ecmascript\", jsx: true },\n transform: { react: { runtime: \"classic\" } }\n },\n module: { type: \"commonjs\", strictMode: false, allowTopLevelThis: true }\n });\n return {\n code: result.code,\n map: result.map ? remapping(\n [\n JSON.parse(result.map),\n edited.generateMap({\n source: filename,\n includeContent: true,\n hires: true\n }),\n ...typeMap ? [typeMap] : []\n ],\n () => null\n ) : void 0\n };\n}\nexport {\n transformMixedNativeModule\n};\n//# sourceMappingURL=transformMixedNativeModule.js.map\n"],"mappings":";;;;;AAGA,SAAS,QAAQ,SAAS;CACxB,MAAM,yBAAyB,IAAI,QAAQ;CAC3C,MAAM,2BAA2B,IAAI,QAAQ;CAC7C,MAAM,OAAO;EAAE,0BAA0B,IAAI,IAAI;EAAG,UAAU;CAAK;CACnE,SAAS,KAAK,SAAS,OAAO;EAC5B,IAAI,CAAC,SAAS;EACd,IAAI,QAAQ,SAAS,cAAc;GACjC,MAAM,SAAS,IAAI,QAAQ,IAAI;GAC/B,SAAS,IAAI,OAAO;EACtB,OAAO,IAAI,QAAQ,SAAS,eAAe,KAAK,QAAQ,UAAU,KAAK;OAClE,IAAI,QAAQ,SAAS,qBAAqB,KAAK,QAAQ,MAAM,KAAK;OAClE,IAAI,QAAQ,SAAS,gBAAgB;GACxC,KAAK,MAAM,QAAQ,QAAQ,UAAU,KAAK,MAAM,KAAK;EACvD,OAAO,IAAI,QAAQ,SAAS,iBAAiB;GAC3C,KAAK,MAAM,QAAQ,QAAQ,YACzB,KAAK,KAAK,SAAS,gBAAgB,KAAK,WAAW,KAAK,OAAO,KAAK;EACxE;CACF;CACA,SAAS,MAAM,MAAM,aAAa;EAChC,IAAI,QAAQ;EACZ,MAAM,KAAK,uEAAuE,KAChF,KAAK,IACP;EACA,IAAI,KAAK,SAAS,yBAAyB,KAAK,SAAS,oBACvD,KAAK,KAAK,IAA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AM;IAAE;IAC/D,WAAW,EAAE,OAAO,EAAE,SAAS,WAAW,EAAE;GAC9C;GACA,QAAQ,EAAE,MAAM,MAAM;EACxB,CAAC;EACD,OAAO,SAAS;EAChB,UAAU,SAAS,MAAM,KAAK,MAAM,SAAS,GAAG,IAAI,KAAK;EACzD,MAAM,aAAa,UAAU,UAAU,MAAM,EAAE,MAAM,MAAM,CAAC;EAC5D,IAAI,WAAW,OAAO,QAAQ,MAAM,IAAI,MAAM,WAAW,OAAO,EAAE,CAAC,OAAO;EAC1E,UAAU,WAAW;EACrB,WAAW,QAAQ,OAAO;CAC5B;CACA,MAAM,SAAS,kBAAkB,SAAS,MAAM,QAAQ;CACxD,MAAM,SAAS,MAAM,UAAU,OAAO,SAAS,GAAG;EAChD;EACA,OAAO;EACP,YAAY;EACZ;EACA,KAAK;GACH,QAAQ;GACR,QAAQ;IAAE,QAAQ;IAAc,KAAK;GAAK;GAC1C,WAAW,EAAE,OAAO,EAAE,SAAS,UAAU,EAAE;EAC7C;EACA,QAAQ;GAAE,MAAM;GAAY,YAAY;GAAO,mBAAmB;EAAK;CACzE,CAAC;CACD,OAAO;EACL,MAAM,OAAO;EACb,KAAK,OAAO,MAAM,UAChB;GACE,KAAK,MAAM,OAAO,GAAG;GACrB,OAAO,YAAY;IACjB,QAAQ;IACR,gBAAgB;IAChB,OAAO;GACT,CAAC;GACD,GAAG,UAAU,CAAC,OAAO,IAAI,CAAC;EAC5B,SACM,IACR,IAAI,KAAK;CACX;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vxrn/compiler",
3
- "version": "2.9.1",
3
+ "version": "2.10.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  "./package.json": "./package.json",
@@ -42,8 +42,8 @@
42
42
  "@babel/plugin-transform-regenerator": "^7.28.4",
43
43
  "@jridgewell/remapping": "^2.3.0",
44
44
  "@swc/core": "1.16.18",
45
- "@vxrn/utils": "2.9.1",
46
- "@vxrn/vite-native-client": "2.9.1",
45
+ "@vxrn/utils": "2.10.0",
46
+ "@vxrn/vite-native-client": "2.10.0",
47
47
  "astring": "1.9.0",
48
48
  "babel-plugin-react-compiler": "^1.0.0",
49
49
  "babel-plugin-syntax-hermes-parser": "^0.37.0",
@@ -58,7 +58,7 @@
58
58
  },
59
59
  "devDependencies": {
60
60
  "@jridgewell/trace-mapping": "^0.3.25",
61
- "@tamagui/build": "3.1.0",
61
+ "@tamagui/build": "3.1.3",
62
62
  "depcheck": "^1.4.7",
63
63
  "react-native-worklets": "~0.13.0",
64
64
  "rolldown": "^1.2.8",
@@ -76,7 +76,7 @@
76
76
  "publishConfig": {
77
77
  "access": "public"
78
78
  },
79
- "releaseSourceCommit": "ebf7b0fe59a565f6cd824c8c0a295d600bf107ac",
79
+ "releaseSourceCommit": "0f637d42dcaf1ee86cb153a1fe98426b091a87fc",
80
80
  "repository": {
81
81
  "type": "git",
82
82
  "url": "git+https://github.com/onejs/one.git",
@@ -0,0 +1,83 @@
1
+ import { runInNewContext } from 'node:vm'
2
+ import { transformSync } from 'oxc-transform'
3
+ import { expect, it } from 'vitest'
4
+ import { transformMixedNativeModule } from './transformMixedNativeModule'
5
+
6
+ it.each(['probe.js', 'probe.jsx', 'probe.tsx'])(
7
+ 'preserves imported JSX component identity in mixed native %s',
8
+ async (filename) => {
9
+ const source = `import { View } from 'react-native';
10
+ exports.extra = 1;
11
+ export default () => <View />;`
12
+ const result = await transformMixedNativeModule(source, filename, true)
13
+ const View = {}
14
+ const module = { exports: {} as any }
15
+ const native = transformSync('native.jsx', result!.code, {
16
+ jsx: { runtime: 'classic' },
17
+ })
18
+ expect(native.errors).toEqual([])
19
+ runInNewContext(native.code, {
20
+ module,
21
+ exports: module.exports,
22
+ require(name: string) {
23
+ expect(name).toBe('react-native')
24
+ return { View }
25
+ },
26
+ // the native bundler injects react; this probe only checks the view binding.
27
+ React: { createElement: (component: unknown) => component },
28
+ })
29
+ expect(module.exports.default()).toBe(View)
30
+ expect(module.exports.extra).toBe(1)
31
+ }
32
+ )
33
+
34
+ it('ignores type-only CommonJS names while preserving runtime TypeScript references', async () => {
35
+ const prefix = 'import { x } from "y"; '
36
+ for (const source of [
37
+ 'type Result = { module: unknown }; export const a = x;',
38
+ 'type Result = { exports: unknown }; export const a = x;',
39
+ 'type Result = typeof module; export const a = x;',
40
+ 'type Result = typeof module.exports; export const a = x;',
41
+ 'interface Result { module: unknown; exports: typeof module.exports }; export const a = x;',
42
+ 'export const a: typeof module.exports = x;',
43
+ 'export const a = x as typeof module.exports;',
44
+ 'export const a = x as { module: unknown };',
45
+ 'export const a = x satisfies typeof module.exports;',
46
+ 'export const a = <typeof module.exports>x;',
47
+ 'export const a = x<typeof module.exports>;',
48
+ 'export function a<T extends typeof module>(value: T): typeof module.exports { return value };',
49
+ 'export class A extends x<typeof module.exports> {};',
50
+ 'namespace N { export type Result = { module: unknown }; export const a = x }; export { N };',
51
+ 'declare namespace module {}; export const a = x;',
52
+ ]) {
53
+ expect(
54
+ await transformMixedNativeModule(prefix + source, 'probe.ts'),
55
+ source
56
+ ).toBeUndefined()
57
+ }
58
+
59
+ for (const source of [
60
+ 'export const actual = module.exports.initial;',
61
+ 'export const actual = exports.initial;',
62
+ 'export const actual = (module.exports.initial as number);',
63
+ 'export const actual = (<number>module.exports.initial);',
64
+ 'export const actual = (module.exports.initial satisfies number);',
65
+ 'export const actual = module!.exports.initial;',
66
+ 'const read = module.exports.read<number>; export const actual = read();',
67
+ 'export namespace N { export const value = module.exports.initial }; export const actual = N.value;',
68
+ 'export enum E { Value = module.exports.initial }; export const actual = E.Value;',
69
+ 'class Box { constructor(public value = module.exports.initial) {} }; export const actual = new Box().value;',
70
+ 'export const actual = typeof module === "object" ? 42 : 0;',
71
+ 'export const actual = typeof module.exports === "object" ? 42 : 0;',
72
+ ]) {
73
+ const result = await transformMixedNativeModule(prefix + source, 'probe.ts')
74
+ expect(result, source).toBeDefined()
75
+ const module = { exports: { initial: 42, read: () => 42 } as any }
76
+ runInNewContext(result!.code, {
77
+ module,
78
+ exports: module.exports,
79
+ require: () => ({ x: 42 }),
80
+ })
81
+ expect(module.exports.actual, source).toBe(42)
82
+ }
83
+ })
@@ -333,6 +333,17 @@ export async function transformMixedNativeModule(
333
333
  let analysis = analyze(program)
334
334
  let mixed = false
335
335
  function inspect(node: Node, parent: Node, key: string) {
336
+ // types do not reference the runtime commonjs wrapper.
337
+ if (
338
+ node.declare ||
339
+ node.importKind === 'type' ||
340
+ node.exportKind === 'type' ||
341
+ /^(?:TSTypeAliasDeclaration|TSInterfaceDeclaration)$/.test(node.type) ||
342
+ /^(?:typeAnnotation|typeArguments|typeParameters|returnType|superTypeArguments|implements)$/.test(
343
+ key
344
+ )
345
+ )
346
+ return
336
347
  if (
337
348
  node.type === 'Identifier' &&
338
349
  /^(?:module|exports)$/.test(node.name) &&
@@ -378,7 +389,7 @@ export async function transformMixedNativeModule(
378
389
  jsc: {
379
390
  target: 'esnext',
380
391
  parser: { syntax: 'ecmascript', jsx: true },
381
- transform: { react: { runtime: 'preserve' } },
392
+ transform: { react: { runtime: 'classic' } },
382
393
  },
383
394
  module: { type: 'commonjs', strictMode: false, allowTopLevelThis: true },
384
395
  })
@@ -1 +1 @@
1
- {"version":3,"file":"transformMixedNativeModule.d.ts","sourceRoot":"","sources":["../src/transformMixedNativeModule.ts"],"names":[],"mappings":"AA+TA,wBAAsB,0BAA0B,CAC9C,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,MAAM,EAChB,UAAU,UAAQ,GACjB,OAAO,CAAC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,GAAG,CAAA;CAAE,GAAG,SAAS,CAAC,CA8ElD"}
1
+ {"version":3,"file":"transformMixedNativeModule.d.ts","sourceRoot":"","sources":["../src/transformMixedNativeModule.ts"],"names":[],"mappings":"AA+TA,wBAAsB,0BAA0B,CAC9C,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,MAAM,EAChB,UAAU,UAAQ,GACjB,OAAO,CAAC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,GAAG,CAAA;CAAE,GAAG,SAAS,CAAC,CAyFlD"}