import fs from 'node:fs' import { createRequire } from 'node:module' import path from 'path' import type { PluginItem, TransformOptions } from '@babel/core' import type { RegisterOptions } from '@babel/register' import { parseConfigFileTextToJson } from 'typescript' import { getPaths } from '@cedarjs/project-config' import type { Flags as WebFlags } from './web.js' // `@babel/register` patches Node's CJS `require()` to compile files on the // fly. This package is ESM-only, so `require` isn't a global here — use // `createRequire` to get a real one for loading the (CJS) npm package. const require = createRequire(import.meta.url) export interface RegisterHookOptions { /** * Be careful: plugins are a nested array e.g. [[plug1, x, x], [plug2, y, y]]. * These are in addition to the default RW plugins */ plugins?: PluginItem[] overrides?: TransformOptions['overrides'] options?: WebFlags } /** @deprecated Please use `RegisterOptions` from `@babel/register` */ export interface BabelRegisterOptions extends TransformOptions { extensions?: string[] cache?: boolean } // NOTE: // We do this so we still get types, but don't import babel/register // Importing babel/register in typescript (instead of requiring) has dire // consequences.. // // Lets say we use the import syntax: // `import babelRequireHook from '@babel/register'` // - if you import in a JS file (like we used to in the cli project) - not a // problem, and it would only invoke the register function when you called // babelRequireHook // - if you import in a TS file, the transpile process modifies it when we build // the framework – so it will invoke it once as soon as you import, and // another time when you use babelRequireHook... // BUTTT!!! you won't notice it if your project is TS because by default it // ignores .ts and .tsx files, but if its a JS project, it would try to // transpile twice export const registerBabel = (options: RegisterOptions) => { // One of the ways you can use Babel is through the require hook. The require // hook will bind itself to node's require and automatically compile files on // the fly. After this `require`, all subsequent files required by node with // the extensions .es6, .es, .jsx, .mjs, and .js will be transformed by Babel // (unless options.extensions override the default exensions). require('@babel/register')(options) } /** * Finds, reads and parses the [ts|js]config.json file * @returns The config object */ export const parseTypeScriptConfigFiles = () => { const rwPaths = getPaths() const parseConfigFile = (basePath: string) => { let configPath = path.join(basePath, 'tsconfig.json') if (!fs.existsSync(configPath)) { configPath = path.join(basePath, 'jsconfig.json') if (!fs.existsSync(configPath)) { return null } } return parseConfigFileTextToJson( configPath, fs.readFileSync(configPath, 'utf-8'), ) } const apiConfig = parseConfigFile(rwPaths.api.base) const webConfig = parseConfigFile(rwPaths.web.base) return { api: apiConfig?.config ?? null, web: webConfig?.config ?? null, } } type CompilerOptionsForPaths = { compilerOptions: { baseUrl: string; paths: Record } } /** * Extracts and formats the paths from the [ts|js]config.json file * @param config The config object * @param rootDir {string} Where the jsconfig/tsconfig is loaded from * @returns {Record} The paths object */ export const getPathsFromTypeScriptConfig = ( config: CompilerOptionsForPaths, rootDir: string, ): Record => { if (!config) { return {} } if (!config.compilerOptions?.paths) { return {} } const { baseUrl, paths } = config.compilerOptions let absoluteBase: string if (baseUrl) { // Convert it to absolute path - on windows the baseUrl is already absolute absoluteBase = path.isAbsolute(baseUrl) ? baseUrl : path.join(rootDir, baseUrl) } else { absoluteBase = rootDir } const pathsObj: Record = {} for (const [key, value] of Object.entries(paths)) { // exclude the default paths that are included in the tsconfig.json file // "src/*" // "$api/*" // "types/*" // "@cedarjs/testing" if (key.match(/src\/|\$api\/\*|types\/\*|\@cedarjs\/.*/g)) { continue } const aliasKey = key.replace('/*', '') const aliasValue = path.join(absoluteBase, value[0].replace('/*', '')) pathsObj[aliasKey] = aliasValue } return pathsObj }