-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathloadModule.ts
109 lines (97 loc) · 2.67 KB
/
loadModule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { createRequire as createNativeRequire } from 'node:module';
import { pathToFileURL } from 'node:url';
import vm from 'node:vm';
import path from 'pathe';
import { logger } from '../../utils/logger';
const isRelativePath = (p: string) => /^\.\.?\//.test(p);
const createRequire = (
filename: string,
distPath: string,
rstestContext: Record<string, any>,
assetFiles: Record<string, string>,
): NodeJS.Require => {
const _require = createNativeRequire(filename);
const require = ((id: string) => {
const currentDirectory = path.dirname(distPath);
const joinedPath = isRelativePath(id)
? path.join(currentDirectory, id)
: id;
const content = assetFiles[joinedPath];
if (content) {
try {
return loadModule({
codeContent: content,
originPath: joinedPath,
distPath: joinedPath,
rstestContext,
assetFiles,
});
} catch (err) {
logger.error(
`load file ${joinedPath} failed:\n`,
err instanceof Error ? err.message : err,
);
}
}
const resolved = _require.resolve(id);
return _require(resolved);
}) as NodeJS.Require;
require.resolve = _require.resolve;
require.main = _require.main;
return require;
};
export const loadModule = ({
codeContent,
distPath,
originPath,
rstestContext,
assetFiles,
}: {
codeContent: string;
distPath: string;
originPath: string;
rstestContext: Record<string, any>;
assetFiles: Record<string, string>;
}): any => {
const fileDir = path.dirname(originPath);
const localModule = {
children: [],
exports: {},
filename: originPath,
id: originPath,
isPreloading: false,
loaded: false,
path: fileDir,
};
const context = {
module: localModule,
exports: localModule.exports,
require: createRequire(originPath, distPath, rstestContext, assetFiles),
__dirname: fileDir,
__filename: originPath,
...rstestContext,
};
const codeDefinition = `'use strict';(${Object.keys(context).join(',')})=>{{`;
const code = `${codeDefinition}${codeContent}\n}}`;
const fn = vm.runInThisContext(code, {
// Used in stack traces produced by this script.
filename: distPath,
lineOffset: 0,
columnOffset: -codeDefinition.length,
importModuleDynamically: async (
specifier,
_referencer,
importAttributes,
) => {
const dependencyAsset = import.meta.resolve(
specifier,
pathToFileURL(originPath),
);
// @ts-expect-error
const res = await import(dependencyAsset, importAttributes);
return res;
},
});
fn(...Object.values(context));
return localModule.exports;
};