Commit 2a73ab0f authored by devjiwonchoi's avatar devjiwonchoi

resolve swc option

- no need with assertion handling
- strict pass module type, no isModule unknown
parent d76502aa
......@@ -4,13 +4,13 @@ import type {
ModuleFormat,
ResolveHook,
} from 'module'
import type { Options as SWCOptions } from '@swc/core'
import type { CompilerOptions } from 'typescript'
import path from 'path'
import { existsSync } from 'fs'
import { readFile } from 'fs/promises'
import { fileURLToPath } from 'url'
import { resolveSWCOptions } from './transpile-config.js'
const tsExts = new Set(['.ts', '.mts', '.cts'])
const localContext = new Map<string, any>()
......@@ -127,34 +127,12 @@ export const load: LoadHook = async (url, context, nextLoad) => {
const rawSource = await readFile(fileURLToPath(url), 'utf-8')
// Lazy load swc to reduce the initial loader registration time.
const { transform } = await import('../swc/index.js')
const { code } = await transform(rawSource, {
jsc: {
parser: {
syntax: 'typescript',
},
...(compilerOptions.paths ? { paths: compilerOptions.paths } : {}),
...(compilerOptions.baseUrl
? // Needs to be an absolute path.
{ baseUrl: path.resolve(cwd, compilerOptions.baseUrl) }
: compilerOptions.paths
? // If paths is given, baseUrl is required.
{ baseUrl: cwd }
: {}),
experimental: {
keepImportAttributes: true,
// Without this option, `assert` assertion also transpiles to `with` attribute,
// which will throw if in Node.js version that does not support `with`.
// Switch from Import Assertions to Import Attributes held at v21.0.0, v20.10.0, v18.20.0.
emitAssertForImportAttributes: true,
},
},
env: {
targets: {
// Setting the Node.js version can reduce unnecessary code generation.
node: process?.versions?.node ?? '20.19.0',
},
},
} satisfies SWCOptions)
const swcOptions = resolveSWCOptions({
cwd,
compilerOptions,
type: 'es6',
})
const { code } = await transform(rawSource, swcOptions)
return {
format: 'module',
......
import type { Options as SWCOptions } from '@swc/core'
import type { CompilerOptions } from 'typescript'
import path from 'path'
import { join, resolve } from 'path'
import { readFile } from 'fs/promises'
import { register } from 'module'
import { pathToFileURL } from 'url'
......@@ -10,10 +10,15 @@ import { deregisterHook, registerHook, requireFromString } from './require-hook'
import { warn } from '../output/log'
import { installDependencies } from '../../lib/install-dependencies'
function resolveSWCOptions(
cwd: string,
export function resolveSWCOptions({
cwd,
compilerOptions,
type,
}: {
cwd: string
compilerOptions: CompilerOptions
): SWCOptions {
type: 'commonjs' | 'es6'
}): SWCOptions {
return {
jsc: {
parser: {
......@@ -22,16 +27,15 @@ function resolveSWCOptions(
...(compilerOptions.paths ? { paths: compilerOptions.paths } : {}),
...(compilerOptions.baseUrl
? // Needs to be an absolute path.
{ baseUrl: path.resolve(cwd, compilerOptions.baseUrl) }
{ baseUrl: resolve(cwd, compilerOptions.baseUrl) }
: compilerOptions.paths
? // If paths is given, baseUrl is required.
{ baseUrl: cwd }
: {}),
},
module: {
type: 'commonjs',
type,
},
isModule: 'unknown',
env: {
targets: {
// Setting the Node.js version can reduce unnecessary code generation.
......@@ -124,9 +128,7 @@ export async function transpileConfig({
let pkgJson: Record<string, string> = {}
try {
pkgJson = JSON.parse(
await readFile(path.join(cwd, 'package.json'), 'utf8')
)
pkgJson = JSON.parse(await readFile(join(cwd, 'package.json'), 'utf8'))
} catch {}
if (configFileName.endsWith('.mts') || pkgJson.type === 'module') {
......@@ -150,7 +152,11 @@ async function handleCJS({
nextConfigPath: string
compilerOptions: CompilerOptions
}) {
const swcOptions = resolveSWCOptions(cwd, compilerOptions)
const swcOptions = resolveSWCOptions({
cwd,
compilerOptions,
type: 'commonjs',
})
let hasRequire = false
try {
const nextConfigString = await readFile(nextConfigPath, 'utf8')
......@@ -166,7 +172,7 @@ async function handleCJS({
}
// filename & extension don't matter here
return requireFromString(code, path.resolve(cwd, 'next.config.compiled.js'))
return requireFromString(code, resolve(cwd, 'next.config.compiled.js'))
} catch (error) {
throw error
} finally {
......@@ -186,7 +192,7 @@ async function handleESM(workerData: {
}) {
try {
if (!hasRegistered) {
register(pathToFileURL(path.join(__dirname, 'loader.js')).href, {
register(pathToFileURL(join(__dirname, 'loader.js')).href, {
parentURL: pathToFileURL(workerData.cwd).href,
data: {
cwd: workerData.cwd,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment