Commit 8d75e603 authored by devjiwonchoi's avatar devjiwonchoi

lets break

parent ee164dae
...@@ -6,8 +6,7 @@ import type { ...@@ -6,8 +6,7 @@ import type {
} from 'module' } from 'module'
import type { CompilerOptions } from 'typescript' import type { CompilerOptions } from 'typescript'
import path from 'path' import { extname } from 'path'
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' import { resolveSWCOptions } from './transpile-config.js'
...@@ -37,40 +36,7 @@ export const resolve: ResolveHook = async (specifier, context, nextResolve) => { ...@@ -37,40 +36,7 @@ export const resolve: ResolveHook = async (specifier, context, nextResolve) => {
return nextResolve(specifier, context) return nextResolve(specifier, context)
} }
const ext = path.extname(specifier) const ext = extname(specifier)
// If the specifier has no extension, try to resolve it as a TS file.
// This is mainly to prevent a breaking change for ESM projects that use
// "next.config.ts", and also to keep the TS file behavior consistent with
// the rest of the project files.
if (ext === '') {
const possibleTsFileURL = new URL(specifier + '.ts', context.parentURL)
if (existsSync(possibleTsFileURL)) {
return {
format: 'typescript' as ModuleFormat,
shortCircuit: true,
url: possibleTsFileURL.href,
}
}
if (existsSync(new URL(specifier + '.js', context.parentURL))) {
return nextResolve(specifier + '.js', context)
}
return nextResolve(specifier, context)
}
// This allows not to write type: "with" assertions for JSON imports.
// This is mainly to prevent a breaking change for ESM projects that use
// "next.config.ts", and also to keep the TS file behavior consistent with
// the rest of the project files.
if (ext === '.json') {
return {
format: 'json' as ModuleFormat,
shortCircuit: true,
url: new URL(specifier, context.parentURL).href,
}
}
// Node.js resolver can take care of the rest of the non-TS files. // Node.js resolver can take care of the rest of the non-TS files.
if (!tsExts.has(ext)) { if (!tsExts.has(ext)) {
return nextResolve(specifier, context) return nextResolve(specifier, context)
...@@ -84,10 +50,7 @@ export const resolve: ResolveHook = async (specifier, context, nextResolve) => { ...@@ -84,10 +50,7 @@ export const resolve: ResolveHook = async (specifier, context, nextResolve) => {
} }
export const load: LoadHook = async (url, context, nextLoad) => { export const load: LoadHook = async (url, context, nextLoad) => {
if ( if (context.format !== ('typescript' as ModuleFormat)) {
context.format !== ('typescript' as ModuleFormat) &&
context.format !== ('json' as ModuleFormat)
) {
return nextLoad(url, context) return nextLoad(url, context)
} }
...@@ -104,26 +67,6 @@ export const load: LoadHook = async (url, context, nextLoad) => { ...@@ -104,26 +67,6 @@ export const load: LoadHook = async (url, context, nextLoad) => {
) )
} }
if (context.format === ('json' as ModuleFormat)) {
const rawSource = await readFile(fileURLToPath(url), 'utf-8')
const jsonData = JSON.parse(rawSource)
let source = `export default (${rawSource});`
// Add named exports for object properties
if (!Array.isArray(jsonData)) {
const namedExports = Object.keys(jsonData)
.map((key) => `export const ${key} = (${rawSource}).${key};`)
.join('\n')
source = `${namedExports}\n${source}`
}
return {
format: 'module',
shortCircuit: true,
source,
}
}
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')
......
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