Skip to content

Commit 8d7480f

Browse files
committed
fix(#8, other-resolver): fix to fallback to other resolve if needed to
1 parent 3cdbc81 commit 8d7480f

File tree

8 files changed

+16336
-17
lines changed

8 files changed

+16336
-17
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
3232

3333
# Finder (MacOS) folder config
3434
.DS_Store
35+
36+
examples/facetpack-uniwind-repro

bun.lock

Lines changed: 169 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/benchmark/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"@babel/core": "^7.24.0",
1616
"@babel/preset-react": "^7.24.0",
1717
"@babel/preset-typescript": "^7.24.0",
18-
"@ecrindigital/facetpack-native": "^0.1.0",
18+
"@ecrindigital/facetpack-native": "workspace:*",
1919
"enhanced-resolve": "^5.15.0",
2020
"terser": "^5.27.0"
2121
}

examples/expo-facetpack/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
"dependencies": {
1212
"expo": "~54.0.30",
1313
"expo-status-bar": "~3.0.9",
14-
"@ecrindigital/facetpack": "^latest",
15-
"@ecrindigital/facetpack-native": "^latest",
14+
"@ecrindigital/facetpack": "workspace:*",
15+
"@ecrindigital/facetpack-native": "workspace:*",
1616
"react": "19.1.0",
1717
"react-native": "0.81.5"
1818
},

packages/facetpack/src/transformer.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { transformSync, JsxRuntime, resolveBatchSync } from '@ecrindigital/facetpack-native'
1+
import { transformSync, JsxRuntime, resolveBatchSync, parseSync } from '@ecrindigital/facetpack-native'
22
import { parse } from '@babel/parser'
33
import type { TransformParams, TransformResult, FacetpackOptions } from './types'
44
import { setCachedResolutions } from './cache'
@@ -124,10 +124,22 @@ const BABEL_REQUIRED_PATTERNS = [
124124
/runOnJS/,
125125
]
126126

127+
const HERMES_COMPAT_PATTERNS = [
128+
/\basync\s+function\b/,
129+
/\basync\s*\(/,
130+
/\basync\s+\w+\s*\(/,
131+
/=\s*async\s*\(/,
132+
/=\s*async\s+function\b/,
133+
]
134+
127135
function requiresBabelTransform(src: string): boolean {
128136
return BABEL_REQUIRED_PATTERNS.some(pattern => pattern.test(src))
129137
}
130138

139+
function requiresHermesCompat(src: string): boolean {
140+
return HERMES_COMPAT_PATTERNS.some(pattern => pattern.test(src))
141+
}
142+
131143
function shouldTransform(filename: string, src: string, options: Required<FacetpackOptions>): boolean {
132144
if (isNodeModules(filename)) {
133145
return false
@@ -140,6 +152,13 @@ function shouldTransform(filename: string, src: string, options: Required<Facetp
140152
return false
141153
}
142154

155+
if (requiresHermesCompat(src)) {
156+
if (process.env.FACETPACK_DEBUG) {
157+
console.log(`[Facetpack] Babel required for async/await (Hermes compat): ${filename}`)
158+
}
159+
return false
160+
}
161+
143162
const ext = filename.split('.').pop()?.toLowerCase()
144163
if (!ext) return false
145164
return options.sourceExts.includes(ext)
@@ -165,6 +184,29 @@ export function transform(params: TransformParams): TransformResult {
165184
}
166185

167186
try {
187+
const parseResult = parseSync(filename, src)
188+
if (parseResult.errors.length > 0 && parseResult.diagnostics.length > 0) {
189+
const formattedErrors = parseResult.diagnostics
190+
.map(d => {
191+
let output = d.formatted || ''
192+
if (!output && d.message) {
193+
output = `\n × ${d.message}\n`
194+
if (d.snippet) {
195+
output += ` ╭─[${d.filename}:${d.line}:${d.column}]\n`
196+
output += ` ${d.line}${d.snippet}\n`
197+
output += ` ╰────\n`
198+
}
199+
if (d.help) {
200+
output += ` help: ${d.help}\n`
201+
}
202+
}
203+
return output
204+
})
205+
.join('\n')
206+
207+
throw new Error(`\n${formattedErrors}`)
208+
}
209+
168210
const isClassic = opts.jsxRuntime === 'classic'
169211
const result = transformSync(filename, src, {
170212
jsx: opts.jsx,

packages/facetpack/src/withFacetpack.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export function withFacetpack(
4848
? options.minifier
4949
: {}
5050

51-
const useTreeShake = options.treeShake !== false
51+
const useTreeShake = options.treeShake === true
5252
const existingSerializer = (config as any).serializer?.customSerializer as CustomSerializer | undefined
5353
const customSerializer = useTreeShake
5454
? createFacetpackSerializer(existingSerializer, { treeShake: true })
@@ -60,6 +60,8 @@ export function withFacetpack(
6060

6161
storeTransformerOptions(options, fallbackTransformerPath)
6262

63+
const existingResolver = config.resolver?.resolveRequest
64+
6365
return {
6466
...config,
6567
transformer: {
@@ -97,16 +99,18 @@ export function withFacetpack(
9799
]),
98100
],
99101
resolveRequest: (context: any, moduleName: string, platform: string | null) => {
102+
const fallbackResolver = existingResolver ?? context.resolveRequest
103+
100104
if (context.originModulePath.includes('node_modules')) {
101-
return context.resolveRequest(context, moduleName, platform)
105+
return fallbackResolver(context, moduleName, platform)
102106
}
103107

104108
const cached = getCachedResolution(context.originModulePath, moduleName)
105109
if (cached !== undefined) {
106110
if (cached) {
107111
return { type: 'sourceFile', filePath: cached }
108112
}
109-
return context.resolveRequest(context, moduleName, platform)
113+
return fallbackResolver(context, moduleName, platform)
110114
}
111115

112116
const directory = context.originModulePath.substring(
@@ -124,7 +128,7 @@ export function withFacetpack(
124128
return { type: 'sourceFile', filePath: result.path }
125129
}
126130

127-
return context.resolveRequest(context, moduleName, platform)
131+
return fallbackResolver(context, moduleName, platform)
128132
},
129133
},
130134
serializer: {

0 commit comments

Comments
 (0)