Skip to content

Commit

Permalink
fix: esm module resolve issues (swc-project#754)
Browse files Browse the repository at this point in the history
* fix: convert fileUrl to path before compile

this commit convert fileUrl to path before compile in esm, this may cause path alias resolve issue

close swc-project#753

* Create changeset

* chore: update test:module to use node --import

* fix: remove baseUrl from esm to keep module import specifier, cause it use tsc resolver
  • Loading branch information
yeliex authored Mar 4, 2024
1 parent 28c0a99 commit d35ddf1
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/spotty-mails-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@fake-scope/fake-pkg': patch
---

fix: convert fileUrl to path before compile, close #753
fix: remove baseUrl from esm to keep module import specifier, cause it use tsc resolver.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"lint": "eslint -c ./.eslintrc.yml .",
"test": "ava",
"test:jest": "jest --config jest.config.js",
"test:module": "cross-env SWC_NODE_PROJECT=packages/integrate-module/tsconfig.json node --loader=@swc-node/register/esm packages/integrate-module/src/index.ts",
"test:module": "cross-env SWC_NODE_PROJECT=packages/integrate-module/tsconfig.json node --import=@swc-node/register/esm-register packages/integrate-module/src/index.ts",
"version": "pnpm install && git add .",
"postinstall": "husky install"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test('default values', (t) => {
const filename = 'some-file.tsx'
const swcConfig = tsCompilerOptionsToSwcConfig(options, filename)
const expected = {
baseUrl: process.cwd(),
baseUrl: undefined,
module: 'es6',
sourcemap: false,
experimentalDecorators: false,
Expand Down
8 changes: 7 additions & 1 deletion packages/register/esm.mts
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,17 @@ interface LoadResult {
type LoadArgs = [url: string, context: LoadContext, nextLoad?: (...args: LoadArgs) => Promise<LoadResult>]
type LoadFn = (...args: Required<LoadArgs>) => Promise<LoadResult>

const tsconfigForSWCNode = {
...tsconfig,
paths: undefined,
baseUrl: undefined,
}

export const load: LoadFn = async (url, context, nextLoad) => {
if (context.format === 'ts') {
const { source } = await nextLoad(url, context)
const code = typeof source === 'string' ? source : Buffer.from(source).toString()
const compiled = await compile(code, url, tsconfig, true)
const compiled = await compile(code, fileURLToPath(url), tsconfigForSWCNode, true)
return {
format: 'module',
source: compiled,
Expand Down
2 changes: 1 addition & 1 deletion packages/register/read-default-tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export function tsCompilerOptionsToSwcConfig(options: ts.CompilerOptions, filena
useBuiltins: true,
}
: undefined,
baseUrl: resolve(options.baseUrl ?? './'),
baseUrl: options.baseUrl ? resolve(options.baseUrl) : undefined,
paths: Object.fromEntries(
Object.entries(options.paths ?? {}).map(([aliasKey, aliasPaths]) => [
aliasKey,
Expand Down

0 comments on commit d35ddf1

Please sign in to comment.