import path from 'node:path' import type { LoadFnOutput, LoadHookContext, ResolveFnOutput, ResolveHookContext } from "node:module" export class PlataRequire { public static isPlata(value: string): value is PlataFormts { return value.startsWith('plata') } public static resolver(pathRequire: string, context: ResolveHookContext, next: (specifier: string, context?: Partial) => ResolveFnOutput): ResolveFnOutput { // Converte @@ e ++ em seus valores corretos switch (pathRequire.slice(0, 2)) { case '@@': pathRequire = pathRequire.replace('@@', `${Plata.ProjectDir}/__BUILD__`) break case '++': pathRequire = pathRequire.replace('++', `${Plata.PlataDir}/__BUILD__`) break } pathRequire = pathRequire.replace(/\\/g, '/') // Verifica se é para usar o loader custom switch (path.extname(pathRequire)) { case '.env': return { url: pathRequire, format: 'plata-env', importAttributes: undefined, shortCircuit: true, } case '.*': return { url: pathRequire, format: 'plata-dir', importAttributes: undefined, shortCircuit: true } } return next(pathRequire, context) } public static loader(pathRequire: string, context: LoadHookContext, next: (url: string, context?: Partial) => LoadFnOutput): LoadFnOutput { const format = context.format ?? '' if (!PlataRequire.isPlata(format)) { return next(pathRequire, context) } switch (format) { case 'plata-env': return { format: 'commonjs', shortCircuit: true, source: `module.exports=Plata.ReadEnvFileToPromise("${pathRequire}")`, } case 'plata-dir': return { format: 'commonjs', shortCircuit: true, source: `module.exports=Plata.RequireFolder("${path.dirname(pathRequire)}")` } default: { ((): never => format) return next(pathRequire, context) } } } private static getMajorVersion() { return +(process.version.split('.')[0]?.replace('v', '') || 0) } public static setup() { // Fallback para antigo if (PlataRequire.getMajorVersion() < 22) { module.constructor.prototype.require = function (pathRequire) { const self = this if (typeof pathRequire !== 'string') { throw new Error('path must be a string') } if (!pathRequire) { throw new Error('missing path') } try { switch (pathRequire.slice(0, 2)) { case '@@': pathRequire = pathRequire.replace('@@', `${Plata.ProjectDir}/__BUILD__`) break case '++': pathRequire = pathRequire.replace('++', `${Plata.PlataDir}/__BUILD__`) break } pathRequire = pathRequire.replace(/\\/g, '/') switch (path.extname(pathRequire)) { case '.env': return Plata.FastPromise(() => Plata.ReadEnvFileToPromise(pathRequire)) break case '.*': return Plata.FastPromise(() => Plata.RequireFolder(path.dirname(pathRequire))) break default: return self.constructor._load(pathRequire, self) break } } catch (err) { // if module not found, we have nothing to do, simply throw it back. if (err.code === 'MODULE_NOT_FOUND') { throw err } // resolve the path to get absolute path pathRequire = path.resolve(__dirname, pathRequire) // Write to log or whatever console.log('Error in file: ' + pathRequire) } } return } const { registerHooks } = require('node:module') registerHooks({ resolve: PlataRequire.resolver, load: PlataRequire.loader, }) } } type PlataFormts = | 'plata-env' | 'plata-dir'