-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(test-ssr): add custom esbuild-plugin to properly resolve TS path…
… aliases which stopped working starting esbuild 0.19 (#31227)
- Loading branch information
Showing
4 changed files
with
199 additions
and
22 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
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,45 @@ | ||
import * as path from 'node:path'; | ||
|
||
import type { Plugin } from 'esbuild'; | ||
import { loadConfig } from 'tsconfig-paths'; | ||
|
||
function assertPathAliasesSetup(paths: Record<string, string[]>): never | void { | ||
for (const [key, mapping] of Object.entries(paths)) { | ||
if (mapping.length > 1) { | ||
throw new Error( | ||
`Multiple TS path mappings are not supported. Please adjust your config. "${key}": [ ${mapping.join()} ]"`, | ||
); | ||
} | ||
} | ||
} | ||
|
||
export function tsConfigPathsPlugin(options: { cwd: string }): Plugin { | ||
const tsConfig = loadConfig(options.cwd); | ||
|
||
if (tsConfig.resultType === 'failed') { | ||
throw new Error(tsConfig.message); | ||
} | ||
|
||
const pathAliases = tsConfig.paths; | ||
|
||
assertPathAliasesSetup(pathAliases); | ||
|
||
const pluginConfig: Plugin = { | ||
name: 'tsconfig-paths', | ||
setup({ onResolve }) { | ||
onResolve({ filter: /.*/ }, args => { | ||
const pathMapping = pathAliases[args.path]; | ||
|
||
if (!pathMapping) { | ||
return null; | ||
} | ||
|
||
const absoluteImportPath = path.join(tsConfig.absoluteBaseUrl, pathMapping[0]); | ||
|
||
return { path: absoluteImportPath }; | ||
}); | ||
}, | ||
}; | ||
|
||
return pluginConfig; | ||
} |