| | |
| | | const path = require('node:path'); |
| | | const ts = require('typescript'); |
| | | // 无服务的公共绘图函数测试;加载器只负责转译本地 TypeScript 模块。 |
| | | module.exports = function load(file) { |
| | | module.exports = function load(file, external = {}) { |
| | | if (file instanceof URL) file = require('node:url').fileURLToPath(file); |
| | | const source = fs.readFileSync(file, 'utf8'); |
| | | const script = ts.transpileModule(source, { compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS } }).outputText; |
| | | const exports = {}; |
| | | new Function('exports', 'require', script)(exports, id => id.startsWith('.') ? load(path.resolve(path.dirname(file), id.endsWith('.ts') ? id : id + '.ts')) : require(id)); |
| | | new Function('exports', 'require', script)(exports, id => Object.hasOwn(external, id) ? external[id] : id.startsWith('.') ? load(path.resolve(path.dirname(file), id.endsWith('.ts') ? id : id + '.ts'), external) : require(id)); |
| | | return exports; |
| | | }; |