Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: esm module resolve issues #754

Merged
merged 4 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading