chore: cleanup
This commit is contained in:
@@ -12,7 +12,7 @@ if (process.env.DISABLE_SWC !== 'true') {
|
||||
const dirname = path.dirname(filename)
|
||||
const url = pathToFileURL(dirname).toString() + '/'
|
||||
|
||||
register('./dist/bin/register/index.js', url)
|
||||
register('./dist/bin/loader/index.js', url)
|
||||
}
|
||||
|
||||
bin()
|
||||
|
||||
40
packages/payload/src/bin/loader/compile.ts
Normal file
40
packages/payload/src/bin/loader/compile.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import type * as ts from 'typescript'
|
||||
|
||||
import { transform } from '@swc-node/core'
|
||||
import { SourcemapMap } from '@swc-node/sourcemap-support'
|
||||
|
||||
import { tsCompilerOptionsToSwcConfig } from './read-default-tsconfig.js'
|
||||
|
||||
const injectInlineSourceMap = ({
|
||||
code,
|
||||
filename,
|
||||
map,
|
||||
}: {
|
||||
code: string
|
||||
filename: string
|
||||
map: string | undefined
|
||||
}): string => {
|
||||
if (map) {
|
||||
SourcemapMap.set(filename, map)
|
||||
const base64Map = Buffer.from(map, 'utf8').toString('base64')
|
||||
const sourceMapContent = `//# sourceMappingURL=data:application/json;charset=utf-8;base64,${base64Map}`
|
||||
return `${code}\n${sourceMapContent}`
|
||||
}
|
||||
return code
|
||||
}
|
||||
|
||||
export async function compile(
|
||||
sourcecode: string,
|
||||
filename: string,
|
||||
options: ts.CompilerOptions & { fallbackToTs?: (filename: string) => boolean },
|
||||
): Promise<string> {
|
||||
if (filename.endsWith('.d.ts')) {
|
||||
return ''
|
||||
}
|
||||
|
||||
const swcRegisterConfig = tsCompilerOptionsToSwcConfig(options, filename)
|
||||
|
||||
return transform(sourcecode, filename, swcRegisterConfig).then(({ code, map }) => {
|
||||
return injectInlineSourceMap({ code, filename, map })
|
||||
})
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import ts from 'typescript'
|
||||
import { fileURLToPath, pathToFileURL } from 'url'
|
||||
|
||||
import { CLIENT_EXTENSIONS } from './clientExtensions.js'
|
||||
import { compile } from './register.js'
|
||||
import { compile } from './compile.js'
|
||||
|
||||
interface ResolveContext {
|
||||
conditions: string[]
|
||||
@@ -44,10 +44,15 @@ const host: ts.ModuleResolutionHost = {
|
||||
fileExists: ts.sys.fileExists,
|
||||
readFile: ts.sys.readFile,
|
||||
}
|
||||
const EXTENSIONS: string[] = [ts.Extension.Ts, ts.Extension.Tsx, ts.Extension.Dts, ts.Extension.Mts]
|
||||
const TS_EXTENSIONS: string[] = [
|
||||
ts.Extension.Ts,
|
||||
ts.Extension.Tsx,
|
||||
ts.Extension.Dts,
|
||||
ts.Extension.Mts,
|
||||
]
|
||||
|
||||
export const resolve: ResolveFn = async (specifier, context, nextResolve) => {
|
||||
const isTS = EXTENSIONS.some((ext) => specifier.endsWith(ext))
|
||||
const isTS = TS_EXTENSIONS.some((ext) => specifier.endsWith(ext))
|
||||
const isClient = CLIENT_EXTENSIONS.some((ext) => specifier.endsWith(ext))
|
||||
|
||||
if (isClient) {
|
||||
@@ -86,7 +91,7 @@ export const resolve: ResolveFn = async (specifier, context, nextResolve) => {
|
||||
// import from local project to local project TS file
|
||||
if (resolvedModule) {
|
||||
const resolvedIsNodeModule = resolvedModule.resolvedFileName.includes('/node_modules/')
|
||||
const resolvedIsTS = EXTENSIONS.includes(resolvedModule.extension)
|
||||
const resolvedIsTS = TS_EXTENSIONS.includes(resolvedModule.extension)
|
||||
|
||||
if (!resolvedIsNodeModule && resolvedIsTS) {
|
||||
return {
|
||||
@@ -151,7 +156,7 @@ 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, fileURLToPath(url), swcOptions, true)
|
||||
const compiled = await compile(code, fileURLToPath(url), swcOptions)
|
||||
return {
|
||||
format: 'module',
|
||||
shortCircuit: true,
|
||||
@@ -1,125 +0,0 @@
|
||||
import type { Options } from '@swc-node/core'
|
||||
|
||||
import { transform, transformSync } from '@swc-node/core'
|
||||
import { SourcemapMap, installSourceMapSupport } from '@swc-node/sourcemap-support'
|
||||
import { getTsconfig } from 'get-tsconfig'
|
||||
import { platform } from 'os'
|
||||
import { resolve } from 'path'
|
||||
import { addHook } from 'pirates'
|
||||
import * as ts from 'typescript'
|
||||
|
||||
import { tsCompilerOptionsToSwcConfig } from './read-default-tsconfig.js'
|
||||
|
||||
const DEFAULT_EXTENSIONS = ['.js', '.jsx', '.es6', '.es', '.mjs', '.ts', '.tsx']
|
||||
const PLATFORM = platform()
|
||||
|
||||
const injectInlineSourceMap = ({
|
||||
code,
|
||||
filename,
|
||||
map,
|
||||
}: {
|
||||
code: string
|
||||
filename: string
|
||||
map: string | undefined
|
||||
}): string => {
|
||||
if (map) {
|
||||
SourcemapMap.set(filename, map)
|
||||
const base64Map = Buffer.from(map, 'utf8').toString('base64')
|
||||
const sourceMapContent = `//# sourceMappingURL=data:application/json;charset=utf-8;base64,${base64Map}`
|
||||
return `${code}\n${sourceMapContent}`
|
||||
}
|
||||
return code
|
||||
}
|
||||
|
||||
export function compile(
|
||||
sourcecode: string,
|
||||
filename: string,
|
||||
options: ts.CompilerOptions & { fallbackToTs?: (filename: string) => boolean },
|
||||
): string
|
||||
|
||||
export function compile(
|
||||
sourcecode: string,
|
||||
filename: string,
|
||||
options: ts.CompilerOptions & { fallbackToTs?: (filename: string) => boolean },
|
||||
async: false,
|
||||
): string
|
||||
|
||||
export function compile(
|
||||
sourcecode: string,
|
||||
filename: string,
|
||||
options: ts.CompilerOptions & { fallbackToTs?: (filename: string) => boolean },
|
||||
async: true,
|
||||
): Promise<string>
|
||||
|
||||
export function compile(
|
||||
sourcecode: string,
|
||||
filename: string,
|
||||
options: ts.CompilerOptions & { fallbackToTs?: (filename: string) => boolean },
|
||||
async: boolean,
|
||||
): Promise<string> | string
|
||||
|
||||
export function compile(
|
||||
sourcecode: string,
|
||||
filename: string,
|
||||
options: ts.CompilerOptions & { fallbackToTs?: (filename: string) => boolean },
|
||||
async = false,
|
||||
) {
|
||||
if (filename.endsWith('.d.ts')) {
|
||||
return ''
|
||||
}
|
||||
if (options.files && (options.files as string[]).length) {
|
||||
if (
|
||||
PLATFORM === 'win32' &&
|
||||
(options.files as string[]).every((file) => filename !== resolve(process.cwd(), file))
|
||||
) {
|
||||
return sourcecode
|
||||
}
|
||||
if (
|
||||
PLATFORM !== 'win32' &&
|
||||
(options.files as string[]).every((file) => !filename.endsWith(file))
|
||||
) {
|
||||
return sourcecode
|
||||
}
|
||||
}
|
||||
if (options && typeof options.fallbackToTs === 'function' && options.fallbackToTs(filename)) {
|
||||
delete options.fallbackToTs
|
||||
const { outputText, sourceMapText } = ts.transpileModule(sourcecode, {
|
||||
compilerOptions: options,
|
||||
fileName: filename,
|
||||
})
|
||||
return injectInlineSourceMap({ code: outputText, filename, map: sourceMapText })
|
||||
}
|
||||
|
||||
let swcRegisterConfig: Options
|
||||
if (process.env.SWCRC) {
|
||||
// when SWCRC environment variable is set to true it will use swcrc file
|
||||
swcRegisterConfig = {
|
||||
swc: {
|
||||
swcrc: true,
|
||||
},
|
||||
}
|
||||
} else {
|
||||
swcRegisterConfig = tsCompilerOptionsToSwcConfig(options, filename)
|
||||
}
|
||||
|
||||
if (async) {
|
||||
return transform(sourcecode, filename, swcRegisterConfig).then(({ code, map }) => {
|
||||
return injectInlineSourceMap({ code, filename, map })
|
||||
})
|
||||
} else {
|
||||
const { code, map } = transformSync(sourcecode, filename, swcRegisterConfig)
|
||||
return injectInlineSourceMap({ code, filename, map })
|
||||
}
|
||||
}
|
||||
|
||||
export function register(options: Partial<ts.CompilerOptions> = {}, hookOpts = {}) {
|
||||
const locatedConfig = getTsconfig()
|
||||
const tsconfig = locatedConfig.config.compilerOptions as unknown as ts.CompilerOptions
|
||||
options = tsconfig
|
||||
// options.module = ts.ModuleKind.CommonJS
|
||||
installSourceMapSupport()
|
||||
return addHook((code, filename) => compile(code, filename, options), {
|
||||
exts: DEFAULT_EXTENSIONS,
|
||||
...hookOpts,
|
||||
})
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
import type pino from 'pino'
|
||||
|
||||
import { createRequire } from 'module'
|
||||
import path from 'path'
|
||||
|
||||
import type { SanitizedConfig } from './types.js'
|
||||
|
||||
import { CLIENT_EXTENSIONS } from '../bin/register/clientExtensions.js'
|
||||
import Logger from '../utilities/logger.js'
|
||||
import { findConfig } from './find.js'
|
||||
import { validateSchema } from './validate.js'
|
||||
|
||||
const require = createRequire(import.meta.url)
|
||||
|
||||
const loadConfig = async (logger?: pino.Logger): Promise<SanitizedConfig> => {
|
||||
const localLogger = logger ?? Logger()
|
||||
|
||||
const configPath = findConfig()
|
||||
|
||||
CLIENT_EXTENSIONS.forEach((ext) => {
|
||||
require.extensions[ext] = () => null
|
||||
})
|
||||
|
||||
const configPromise = await import(configPath)
|
||||
|
||||
let config = await configPromise
|
||||
|
||||
if ('default' in config) config = await config.default
|
||||
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
config = validateSchema(config, localLogger)
|
||||
}
|
||||
|
||||
return {
|
||||
...config,
|
||||
paths: {
|
||||
config: configPath,
|
||||
configDir: path.dirname(configPath),
|
||||
rawConfig: configPath,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export default loadConfig
|
||||
@@ -1,6 +1,6 @@
|
||||
import { load } from './load.js'
|
||||
|
||||
describe('Config Loader', () => {
|
||||
describe('Loader', () => {
|
||||
it('should load using TS moduleResolution: bundler', async () => {
|
||||
const file = await load('./next-navigation-test.js')
|
||||
expect(typeof file.redirect).toStrictEqual('function')
|
||||
@@ -8,7 +8,7 @@ export const load = async (filePath) => {
|
||||
const url = pathToFileURL(dirname).toString() + '/'
|
||||
|
||||
// Need to register loader from payload/dist for a true test of functionality
|
||||
register('../../packages/payload/dist/bin/register/index.js', url)
|
||||
register('../../packages/payload/dist/bin/loader/index.js', url)
|
||||
|
||||
const result = await import(filePath)
|
||||
|
||||
Reference in New Issue
Block a user