-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: π ε端樑ε ε node 樑εεεΌθ§£ζ (#9976)
* fix: π ε端樑ε ε node 樑εεεΌθ§£ζ * refactor: π¨ use for loop to try resolvers Co-authored-by: pshu <pishu.spf@antfin.com>
- Loading branch information
1 parent
5407aa9
commit af538e7
Showing
21 changed files
with
167 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
24 changes: 24 additions & 0 deletions
24
packages/mfsu/fixtures/resolvesContexts/axios-like/node_modules/axios/package.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
21 changes: 21 additions & 0 deletions
21
...tures/resolvesContexts/broadcast-channel-like/node_modules/broadcast-channel/package.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Empty file.
Empty file.
Empty file.
8 changes: 8 additions & 0 deletions
8
...resolvesContexts/broadcast-channel-no-exports/node_modules/broadcast-channel/package.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { join } from 'path'; | ||
import { resolveFromContexts } from './resolveUtils'; | ||
|
||
const FIXTURE_BASE = join(__dirname, '../../fixtures/resolvesContexts'); | ||
|
||
test('resolve axios like', async () => { | ||
const path = await resolveFromContexts( | ||
[join(FIXTURE_BASE, 'axios-like')], | ||
'axios', | ||
); | ||
expect(path).toMatch(/browser-default.js$/); | ||
}); | ||
|
||
test('resolve broadcast-channel like', async () => { | ||
const path = await resolveFromContexts( | ||
[join(FIXTURE_BASE, 'broadcast-channel-like')], | ||
'broadcast-channel', | ||
); | ||
expect(path).toMatch(/browser-index.js$/); | ||
}); | ||
|
||
test('resolve broadcast-channel no-exports', async () => { | ||
const path = await resolveFromContexts( | ||
[join(FIXTURE_BASE, 'broadcast-channel-no-exports')], | ||
'broadcast-channel', | ||
); | ||
expect(path).toMatch(/legacy-browser-index.js$/); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import enhancedResolve from 'enhanced-resolve'; | ||
|
||
type Resolver = ReturnType<typeof enhancedResolve.create>; | ||
|
||
const ORDERED_MAIN_FIELDS = ['browser', 'module', 'main']; | ||
const SUPPORTED_EXTS = ['.wasm', '.mjs', '.js', '.jsx', '.ts', '.tsx', '.json']; | ||
const EXPORTS_FIELDS = ['exports']; | ||
|
||
const browserResolver = enhancedResolve.create({ | ||
mainFields: ORDERED_MAIN_FIELDS, | ||
extensions: SUPPORTED_EXTS, | ||
exportsFields: EXPORTS_FIELDS, | ||
conditionNames: ['browser', 'import'], | ||
symlinks: false, | ||
}); | ||
|
||
const esmResolver = enhancedResolve.create({ | ||
mainFields: ORDERED_MAIN_FIELDS, | ||
extensions: SUPPORTED_EXTS, | ||
exportsFields: EXPORTS_FIELDS, | ||
conditionNames: ['module'], | ||
symlinks: false, | ||
}); | ||
|
||
const cjsResolver = enhancedResolve.create({ | ||
mainFields: ORDERED_MAIN_FIELDS, | ||
extensions: SUPPORTED_EXTS, | ||
exportsFields: EXPORTS_FIELDS, | ||
conditionNames: ['require', 'node'], | ||
symlinks: false, | ||
}); | ||
|
||
async function resolveWith( | ||
resolver: Resolver, | ||
context: string, | ||
path: string, | ||
): Promise<string> { | ||
return new Promise((resolve, reject) => { | ||
resolver(context, path, (err: Error, result: string) => | ||
err ? reject(err) : resolve(result), | ||
); | ||
}); | ||
} | ||
|
||
async function tryResolvers(rs: Resolver[], context: string, path: string) { | ||
let result = ''; | ||
let lastError: any = null; | ||
for (const r of rs) { | ||
try { | ||
result = await resolveWith(r, context, path); | ||
return result; | ||
} catch (e) { | ||
lastError = e; | ||
} | ||
} | ||
if (!result) { | ||
throw lastError || Error(`can't resolve ${path} from ${context}`); | ||
} | ||
return result; | ||
} | ||
|
||
async function resolve(context: string, path: string): Promise<string> { | ||
return await tryResolvers( | ||
[browserResolver, esmResolver, cjsResolver], | ||
context, | ||
path, | ||
); | ||
} | ||
|
||
export async function resolveFromContexts( | ||
contexts: string[], | ||
path: string, | ||
): Promise<string> { | ||
for (const context of contexts) { | ||
try { | ||
return await resolve(context, path); | ||
} catch (e) { | ||
// ignore | ||
} | ||
} | ||
throw new Error(`Can't resolve ${path} from ${contexts.join(', ')}`); | ||
} |