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 { ...@@ -4,13 +4,13 @@ import type {
ModuleFormat, ModuleFormat,
ResolveHook, ResolveHook,
} from 'module' } from 'module'
import type { Options as SWCOptions } from '@swc/core'
import type { CompilerOptions } from 'typescript' import type { CompilerOptions } from 'typescript'
import path from 'path' import path from 'path'
import { existsSync } from 'fs' import { existsSync } from 'fs'
import { readFile } from 'fs/promises' import { readFile } from 'fs/promises'
import { fileURLToPath } from 'url' import { fileURLToPath } from 'url'
import { resolveSWCOptions } from './transpile-config.js'
const tsExts = new Set(['.ts', '.mts', '.cts']) const tsExts = new Set(['.ts', '.mts', '.cts'])
const localContext = new Map<string, any>() const localContext = new Map<string, any>()
...@@ -127,34 +127,12 @@ export const load: LoadHook = async (url, context, nextLoad) => { ...@@ -127,34 +127,12 @@ export const load: LoadHook = async (url, context, nextLoad) => {
const rawSource = await readFile(fileURLToPath(url), 'utf-8') const rawSource = await readFile(fileURLToPath(url), 'utf-8')
// Lazy load swc to reduce the initial loader registration time. // Lazy load swc to reduce the initial loader registration time.
const { transform } = await import('../swc/index.js') const { transform } = await import('../swc/index.js')
const { code } = await transform(rawSource, { const swcOptions = resolveSWCOptions({
jsc: { cwd,
parser: { compilerOptions,
syntax: 'typescript', type: 'es6',
}, })
...(compilerOptions.paths ? { paths: compilerOptions.paths } : {}), const { code } = await transform(rawSource, swcOptions)
...(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)
return { return {
format: 'module', format: 'module',
......
import type { Options as SWCOptions } from '@swc/core' import type { Options as SWCOptions } from '@swc/core'
import type { CompilerOptions } from 'typescript' import type { CompilerOptions } from 'typescript'
import path from 'path' import { join, resolve } from 'path'
import { readFile } from 'fs/promises' import { readFile } from 'fs/promises'
import { register } from 'module' import { register } from 'module'
import { pathToFileURL } from 'url' import { pathToFileURL } from 'url'
...@@ -10,10 +10,15 @@ import { deregisterHook, registerHook, requireFromString } from './require-hook' ...@@ -10,10 +10,15 @@ import { deregisterHook, registerHook, requireFromString } from './require-hook'
import { warn } from '../output/log' import { warn } from '../output/log'
import { installDependencies } from '../../lib/install-dependencies' import { installDependencies } from '../../lib/install-dependencies'
function resolveSWCOptions( export function resolveSWCOptions({
cwd: string, cwd,
compilerOptions,
type,
}: {
cwd: string
compilerOptions: CompilerOptions compilerOptions: CompilerOptions
): SWCOptions { type: 'commonjs' | 'es6'
}): SWCOptions {
return { return {
jsc: { jsc: {
parser: { parser: {
...@@ -22,16 +27,15 @@ function resolveSWCOptions( ...@@ -22,16 +27,15 @@ function resolveSWCOptions(
...(compilerOptions.paths ? { paths: compilerOptions.paths } : {}), ...(compilerOptions.paths ? { paths: compilerOptions.paths } : {}),
...(compilerOptions.baseUrl ...(compilerOptions.baseUrl
? // Needs to be an absolute path. ? // Needs to be an absolute path.
{ baseUrl: path.resolve(cwd, compilerOptions.baseUrl) } { baseUrl: resolve(cwd, compilerOptions.baseUrl) }
: compilerOptions.paths : compilerOptions.paths
? // If paths is given, baseUrl is required. ? // If paths is given, baseUrl is required.
{ baseUrl: cwd } { baseUrl: cwd }
: {}), : {}),
}, },
module: { module: {
type: 'commonjs', type,
}, },
isModule: 'unknown',
env: { env: {
targets: { targets: {
// Setting the Node.js version can reduce unnecessary code generation. // Setting the Node.js version can reduce unnecessary code generation.
...@@ -124,9 +128,7 @@ export async function transpileConfig({ ...@@ -124,9 +128,7 @@ export async function transpileConfig({
let pkgJson: Record<string, string> = {} let pkgJson: Record<string, string> = {}
try { try {
pkgJson = JSON.parse( pkgJson = JSON.parse(await readFile(join(cwd, 'package.json'), 'utf8'))
await readFile(path.join(cwd, 'package.json'), 'utf8')
)
} catch {} } catch {}
if (configFileName.endsWith('.mts') || pkgJson.type === 'module') { if (configFileName.endsWith('.mts') || pkgJson.type === 'module') {
...@@ -150,7 +152,11 @@ async function handleCJS({ ...@@ -150,7 +152,11 @@ async function handleCJS({
nextConfigPath: string nextConfigPath: string
compilerOptions: CompilerOptions compilerOptions: CompilerOptions
}) { }) {
const swcOptions = resolveSWCOptions(cwd, compilerOptions) const swcOptions = resolveSWCOptions({
cwd,
compilerOptions,
type: 'commonjs',
})
let hasRequire = false let hasRequire = false
try { try {
const nextConfigString = await readFile(nextConfigPath, 'utf8') const nextConfigString = await readFile(nextConfigPath, 'utf8')
...@@ -166,7 +172,7 @@ async function handleCJS({ ...@@ -166,7 +172,7 @@ async function handleCJS({
} }
// filename & extension don't matter here // 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) { } catch (error) {
throw error throw error
} finally { } finally {
...@@ -186,7 +192,7 @@ async function handleESM(workerData: { ...@@ -186,7 +192,7 @@ async function handleESM(workerData: {
}) { }) {
try { try {
if (!hasRegistered) { if (!hasRegistered) {
register(pathToFileURL(path.join(__dirname, 'loader.js')).href, { register(pathToFileURL(join(__dirname, 'loader.js')).href, {
parentURL: pathToFileURL(workerData.cwd).href, parentURL: pathToFileURL(workerData.cwd).href,
data: { data: {
cwd: workerData.cwd, 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