fix(runjs): resolve source exports in subprocesses

This commit is contained in:
gchust
2026-08-15 10:51:01 +00:00
parent cd3f83bf40
commit b66e71fec5
6 changed files with 34 additions and 3 deletions
+1
View File
@@ -5,3 +5,4 @@
!/es/**
/src/completion-catalog/generated.ts
/src/generated/
/register-source.cjs
+3 -1
View File
@@ -161,6 +161,8 @@
},
"devDependencies": {
"ajv": "^6.12.6",
"cross-spawn": "^7.0.6"
"cross-spawn": "^7.0.6",
"tsconfig-paths": "^4.2.0",
"tsx": "^4.19.0"
}
}
+14
View File
@@ -0,0 +1,14 @@
const path = require('node:path');
require('tsx/cjs');
const { register } = require('tsconfig-paths');
register({
baseUrl: path.resolve(__dirname, 'src'),
paths: {
'@nocobase/runjs': ['index.ts'],
'@nocobase/runjs/package.json': ['../package.json'],
'@nocobase/runjs/*': ['*'],
},
});
@@ -137,7 +137,8 @@ function registerCompileContractTests() {
});
function readCompilerBuildId(script: string): string {
return execFileSync(process.execPath, ['--import', 'tsx', '--eval', script], {
const runJSSourceRegisterPath = path.resolve(__dirname, '../../../../../../core/runjs/register-source.cjs');
return execFileSync(process.execPath, ['--require', runJSSourceRegisterPath, '--import', 'tsx', '--eval', script], {
cwd: process.cwd(),
encoding: 'utf8',
}).trim();
@@ -30,10 +30,13 @@ import PluginJsTemplateServer from '../plugin';
describe('plugin-js-template bootstrap', () => {
it('keeps the RunJS compiler unloaded until the first compile', () => {
const serverEntryUrl = pathToFileURL(path.resolve(__dirname, '../index.ts')).href;
const runJSSourceRegisterPath = path.resolve(__dirname, '../../../../../../core/runjs/register-source.cjs');
expect(() =>
execFileSync(
process.execPath,
[
'--require',
runJSSourceRegisterPath,
'--import',
'tsx',
'--input-type=module',
@@ -8,6 +8,7 @@
*/
import path from 'node:path';
import { createRequire } from 'node:module';
import { performance } from 'node:perf_hooks';
import { deserialize, serialize } from 'node:v8';
import { Worker } from 'node:worker_threads';
@@ -798,9 +799,18 @@ function assertIntegerLimit(label: string, value: number, minimum: number, maxim
function createDefaultWorker(): JsTemplateCompileWorkerHandle {
const isTypeScriptRuntime = __filename.endsWith('.ts');
const workerPath = path.join(__dirname, `JsTemplateCompileWorker.${isTypeScriptRuntime ? 'ts' : 'js'}`);
const sourceRuntimeExecArgv = isTypeScriptRuntime
? [
'--require',
path.join(
path.dirname(createRequire(__filename).resolve('@nocobase/runjs/package.json')),
'register-source.cjs',
),
]
: undefined;
return new NodeCompileWorkerHandle(
new Worker(workerPath, {
execArgv: isTypeScriptRuntime ? ['--require', 'tsx/cjs'] : undefined,
execArgv: sourceRuntimeExecArgv,
}),
);
}