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: support compile js files. close #761 #767

Merged
merged 3 commits into from
May 1, 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/smooth-ties-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@fake-scope/fake-pkg': patch
---

fix: support compile js files. close #761
fix: update minimal `@swc/core` to `1.4.13` because of https://github.com/swc-project/swc/pull/8784
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
},
"ava": {
"extensions": [
"js",
"ts",
"tsx"
],
Expand All @@ -95,7 +96,7 @@
],
"cache": false,
"files": [
"packages/**/*.spec.{ts,tsx}"
"packages/**/*.spec.{js,ts,tsx}"
],
"environmentVariables": {
"SWC_NODE_PROJECT": "./tsconfig.test.json"
Expand Down
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
"url": "https://github.com/swc-project/swc-node/issues"
},
"peerDependencies": {
"@swc/core": ">= 1.3",
"@swc/core": ">= 1.4.13",
"@swc/types": ">= 0.1"
},
"devDependencies": {
"@swc/core": "^1.3.107",
"@swc/core": "^1.4.13",
"@swc/types": "^0.1.5"
},
"funding": {
Expand Down
2 changes: 1 addition & 1 deletion packages/integrate-module/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"p-timeout": "^6.1.2"
},
"devDependencies": {
"@swc/core": "^1.3.107",
"@swc/core": "^1.4.13",
"@swc-node/register": "workspace:*",
"typescript": "^5.3.3"
}
Expand Down
6 changes: 6 additions & 0 deletions packages/integrate-module/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { supportedExtensions } from 'file-type'
import { foo } from './foo.mjs'
import { bar } from './subdirectory/bar.mjs'
import { baz } from './subdirectory/index.mjs'
import { bar as subBar } from '@subdirectory/bar.mjs'
import './js-module.mjs'

await test('file-type should work', () => {
assert.ok(supportedExtensions.has('jpg'))
Expand All @@ -23,3 +25,7 @@ await test('resolve nested file path', () => {
await test('resolve nested entry point', () => {
assert.equal(baz(), 'baz')
})

await test('resolve paths', () => {
assert.equal(subBar(), 'bar')
})
30 changes: 30 additions & 0 deletions packages/integrate-module/src/js-module.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* eslint import/order: off */
import assert from 'node:assert'
import test from 'node:test'

import { supportedExtensions } from 'file-type'

import { foo } from './foo.mts'
import { bar } from './subdirectory/bar.mts'
import { baz } from './subdirectory/index.mts'
import { bar as subBar } from '@subdirectory/bar.mts'

await test('js:file-type should work', () => {
assert.ok(supportedExtensions.has('jpg'))
})

await test('js:resolve adjacent file path', () => {
assert.equal(foo(), 'foo')
})

await test('js:resolve nested file path', () => {
assert.equal(bar(), 'bar')
})

await test('js:resolve nested entry point', () => {
assert.equal(baz(), 'baz')
})

await test('js:resolve paths', () => {
assert.equal(subBar(), 'bar')
})
8 changes: 6 additions & 2 deletions packages/integrate-module/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
"target": "ESNext",
"module": "ESNext",
"composite": true,
"outDir": "dist"
"outDir": "dist",
"baseUrl": "./",
"paths": {
"@subdirectory/*": ["./src/subdirectory/*"],
},
},
"include": ["src"]
"include": ["src"],
}
6 changes: 6 additions & 0 deletions packages/integrate/__tests__/paths/js-paths.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { FIXTURE } from '@integrate/fixture'
import test from 'ava'

test('js should transpile paths', (t) => {
t.is(FIXTURE, 'fixture')
})
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Generated by [AVA](https://avajs.dev).
`"use strict";␊
const Button = ({ text })=>/*#__PURE__*/ h("div", null, text);␊
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QudHN4Il0sInNvdXJjZXNDb250ZW50IjpbIlxuICBjb25zdCBCdXR0b24gPSAoeyB0ZXh0IH0pID0+IChcbiAgICA8ZGl2PlxuICAgICAge3RleHR9XG4gICAgPC9kaXY+XG4gIClcbiJdLCJuYW1lcyI6WyJCdXR0b24iLCJ0ZXh0IiwiZGl2Il0sIm1hcHBpbmdzIjoiO0FBQ0UsTUFBTUEsU0FBUyxDQUFDLEVBQUVDLElBQUksRUFBRSxpQkFDdEIsRUFBQ0MsYUFDRUQifQ==`
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QudHN4Il0sInNvdXJjZXNDb250ZW50IjpbIlxuICBjb25zdCBCdXR0b24gPSAoeyB0ZXh0IH0pID0+IChcbiAgICA8ZGl2PlxuICAgICAge3RleHR9XG4gICAgPC9kaXY+XG4gIClcbiJdLCJuYW1lcyI6WyJCdXR0b24iLCJ0ZXh0IiwiZGl2Il0sInJhbmdlTWFwcGluZ3MiOiI7IiwibWFwcGluZ3MiOiI7QUFDRSxNQUFNQSxTQUFTLENBQUMsRUFBRUMsSUFBSSxFQUFFLGlCQUN0QixFQUFDQyxhQUNFRCJ9`

## should transform jsx into new jsx runtime

Expand All @@ -26,4 +26,4 @@ Generated by [AVA](https://avajs.dev).
children: text␊
});␊
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QudHN4Il0sInNvdXJjZXNDb250ZW50IjpbIlxuICBjb25zdCBCdXR0b24gPSAoeyB0ZXh0IH0pID0+IChcbiAgICA8ZGl2PlxuICAgICAge3RleHR9XG4gICAgPC9kaXY+XG4gIClcbiJdLCJuYW1lcyI6WyJCdXR0b24iLCJ0ZXh0IiwiZGl2Il0sIm1hcHBpbmdzIjoiOzs7OztBQUNFLE1BQU1BLFNBQVMsQ0FBQyxFQUFFQyxJQUFJLEVBQUUsaUJBQ3RCLHFCQUFDQztrQkFDRUQifQ==`
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QudHN4Il0sInNvdXJjZXNDb250ZW50IjpbIlxuICBjb25zdCBCdXR0b24gPSAoeyB0ZXh0IH0pID0+IChcbiAgICA8ZGl2PlxuICAgICAge3RleHR9XG4gICAgPC9kaXY+XG4gIClcbiJdLCJuYW1lcyI6WyJCdXR0b24iLCJ0ZXh0IiwiZGl2Il0sInJhbmdlTWFwcGluZ3MiOiI7Ozs7OzsiLCJtYXBwaW5ncyI6Ijs7Ozs7QUFDRSxNQUFNQSxTQUFTLENBQUMsRUFBRUMsSUFBSSxFQUFFLGlCQUN0QixxQkFBQ0M7a0JBQ0VEIn0=`
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/jest/__test__/hoist-top-level.spec.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ Generated by [AVA](https://avajs.dev).
jest.unmock('./bar/foo').dontMock('./bar/bar');␊
};␊
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImplc3Quc3BlYy50cyJdLCJzb3VyY2VzQ29udGVudCI6WyJcbmNvbnN0IGZvbyA9ICdmb28nXG5jb25zb2xlLmxvZyhmb28pXG5qZXN0LmVuYWJsZUF1dG9tb2NrKClcbmplc3QuZGlzYWJsZUF1dG9tb2NrKClcbmplc3QubW9jaygnLi9mb28nKVxuamVzdC5tb2NrKCcuL2Zvby9iYXInLCAoKSA9PiAnYmFyJylcbmplc3QudW5tb2NrKCcuL2Jhci9mb28nKS5kb250TW9jaygnLi9iYXIvYmFyJylcbmplc3QuZGVlcFVubW9jaygnLi9mb28nKVxuamVzdC5tb2NrKCcuL2ZvbycpLm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O0lBQ2hCSixLQUFLRyxJQUFJLENBQUMsU0FBU0EsSUFBSSxDQUFDO0lBUnhCLE1BQU1RLE1BQU07SUFDWkwsUUFBUUMsR0FBRyxDQUFDSTtJQUtaWCxLQUFLUSxNQUFNLENBQUMsYUFBYUMsUUFBUSxDQUFDO0FBR3BDIn0=`
Binary file modified packages/jest/__test__/hoist-top-level.spec.ts.snap
Binary file not shown.
4 changes: 2 additions & 2 deletions packages/jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
"@swc-node/register": "^1.9.0"
},
"peerDependencies": {
"@swc/core": ">= 1.3",
"@swc/core": ">= 1.4.13",
"@swc/types": ">= 0.1",
"typescript": ">= 5.0"
},
"devDependencies": {
"@swc/core": "^1.3.107",
"@swc/core": "^1.4.13",
"@swc/types": "^0.1.5"
},
"repository": {
Expand Down
84 changes: 30 additions & 54 deletions packages/register/esm.mts
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
import type { LoadHook, ResolveHook } from 'node:module'
import { fileURLToPath, pathToFileURL } from 'url'

import ts from 'typescript'

// @ts-expect-error
import { readDefaultTsConfig } from '../lib/read-default-tsconfig.js'
// @ts-expect-error
import { compile } from '../lib/register.js'

interface ResolveContext {
conditions: string[]
parentURL: string | undefined
}

interface ResolveResult {
format?: string
shortCircuit?: boolean
url: string
}

type ResolveArgs = [
specifier: string,
context?: ResolveContext,
nextResolve?: (...args: ResolveArgs) => Promise<ResolveResult>,
]
type ResolveFn = (...args: Required<ResolveArgs>) => Promise<ResolveResult>
import { AVAILABLE_EXTENSION_PATTERN, AVAILABLE_TS_EXTENSION_PATTERN, compile } from '../lib/register.js'

const tsconfig: ts.CompilerOptions = readDefaultTsConfig()
tsconfig.module = ts.ModuleKind.ESNext
Expand All @@ -33,25 +16,24 @@ const host: ts.ModuleResolutionHost = {
fileExists: ts.sys.fileExists,
readFile: ts.sys.readFile,
}
const EXTENSIONS: string[] = [ts.Extension.Ts, ts.Extension.Tsx, ts.Extension.Mts]

export const resolve: ResolveFn = async (specifier, context, nextResolve) => {
const isTS = EXTENSIONS.some((ext) => specifier.endsWith(ext))
export const resolve: ResolveHook = async (specifier, context, nextResolve) => {
if (!AVAILABLE_EXTENSION_PATTERN.test(specifier)) {
return nextResolve(specifier)
}

// entrypoint
if (!context.parentURL) {
return {
format: isTS ? 'ts' : undefined,
importAttributes: {
...context.importAttributes,
swc: 'entrypoint',
},
url: specifier,
shortCircuit: true,
}
}

// import/require from external library
if (context.parentURL.includes('/node_modules/') && !isTS) {
return nextResolve(specifier)
}

const { resolvedModule } = ts.resolveModuleName(
specifier.startsWith('file:') ? fileURLToPath(specifier) : specifier,
fileURLToPath(context.parentURL),
Expand All @@ -60,50 +42,44 @@ export const resolve: ResolveFn = async (specifier, context, nextResolve) => {
moduleResolutionCache,
)

// import from local project to local project TS file
// local project file
if (
resolvedModule &&
!resolvedModule.resolvedFileName.includes('/node_modules/') &&
EXTENSIONS.includes(resolvedModule.extension)
(!resolvedModule.resolvedFileName.includes('/node_modules/') ||
AVAILABLE_TS_EXTENSION_PATTERN.test(resolvedModule.resolvedFileName))
) {
return {
format: 'ts',
url: pathToFileURL(resolvedModule.resolvedFileName).href,
shortCircuit: true,
importAttributes: {
...context.importAttributes,
swc: resolvedModule.resolvedFileName,
},
}
}

// import from local project to either:
// - something TS couldn't resolve
// - external library
// - local project non-TS file
// files could not resolved by typescript
return nextResolve(specifier)
}

interface LoadContext {
conditions: string[]
format: string | null | undefined
}

interface LoadResult {
format: string
shortCircuit?: boolean
source: string | ArrayBuffer | SharedArrayBuffer | Uint8Array
}

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()
export const load: LoadHook = async (url, context, nextLoad) => {
const swcAttribute = context.importAttributes.swc

if (swcAttribute) {
delete context.importAttributes.swc

const { source } = await nextLoad(url, {
...context,
format: 'ts' as any,
})

const code = !source || typeof source === 'string' ? source : Buffer.from(source).toString()
const compiled = await compile(code, fileURLToPath(url), tsconfigForSWCNode, true)
return {
format: 'module',
Expand Down
4 changes: 2 additions & 2 deletions packages/register/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@
"tslib": "^2.6.2"
},
"peerDependencies": {
"@swc/core": ">= 1.3",
"@swc/core": ">= 1.4.13",
"typescript": ">= 4.3"
},
"devDependencies": {
"@swc/core": "^1.3.107",
"@swc/core": "^1.4.13",
"@swc/helpers": "^0.5.3",
"@types/debug": "^4.1.12",
"lodash": "^4.17.21",
Expand Down
Loading
Loading