Files
web_hgdjc_h5/tests/helpers/shared-modules.mjs
T
2026-11-06 00:13:27 +07:00

24 lines
1.4 KiB
JavaScript

import {readFile} from 'node:fs/promises';
import ts from 'typescript';
// Dependency-free site modules (brand, contact, footer filtering, download address, visitor headers)
// are linked into transpiled test modules by specifier, whatever relative depth they are imported from.
const compile=async path=>ts.transpileModule(await readFile(new URL('../../src/lib/'+path,import.meta.url),'utf8'),{compilerOptions:{target:ts.ScriptTarget.ES2022,module:ts.ModuleKind.ESNext}}).outputText;
const url=source=>'data:text/javascript;base64,'+Buffer.from(source).toString('base64');
const protocol=url(await compile('api/protocol.ts'));
const userAgent=url(await compile('api/user-agent.ts'));
const modules={
brand:url(await compile('brand.ts')),
contact:url(await compile('contact.ts')),
'footer-links':url(await compile('footer-links.ts')),
'app-download':url((await compile('app-download.ts')).replace(/(['"])\.\/api\/protocol\1/g,JSON.stringify(protocol))),
'visitor-headers':url((await compile('client/visitor-headers.ts')).replace(/(['"])\.\.\/api\/user-agent\1/g,JSON.stringify(userAgent))),
};
export function linkShared(code){
return code.replace(/(['"])(?:\.\.?\/)+(brand|contact|footer-links|app-download|visitor-headers)\1/g,(_,quote,name)=>JSON.stringify(modules[name]));
}
/** Namespace of a shared module, for CommonJS test harnesses that resolve `require` by hand. */
export const sharedModule=name=>import(modules[name]);