-
-
Notifications
You must be signed in to change notification settings - Fork 633
Expand file tree
/
Copy pathdynamic-packages-manager.js
More file actions
58 lines (48 loc) 路 1.79 KB
/
Copy pathdynamic-packages-manager.js
File metadata and controls
58 lines (48 loc) 路 1.79 KB
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
import { existsSync, readFileSync } from 'fs';
import { join } from 'path';
import { DYNAMIC_PACKAGES_ID, DYNAMIC_REGISTER_SUFFIX, HELPERS_ID, wrapId } from './helpers';
import { getVirtualPathForDynamicRequirePath, normalizePathSlashes } from './utils';
export function getPackageEntryPoint(dirPath) {
let entryPoint = 'index.js';
try {
if (existsSync(join(dirPath, 'package.json'))) {
entryPoint =
JSON.parse(readFileSync(join(dirPath, 'package.json'), { encoding: 'utf8' })).main ||
entryPoint;
}
} catch (ignored) {
// ignored
}
return entryPoint;
}
export function getDynamicPackagesModule(dynamicRequireModuleDirPaths, commonDir) {
let code = `const commonjsRegister = require('${HELPERS_ID}?commonjsRegister');`;
for (const dir of dynamicRequireModuleDirPaths) {
const entryPoint = getPackageEntryPoint(dir);
code += `\ncommonjsRegister(${JSON.stringify(
getVirtualPathForDynamicRequirePath(dir, commonDir)
)}, function (module, exports) {
module.exports = require(${JSON.stringify(normalizePathSlashes(join(dir, entryPoint)))});
});`;
}
return code;
}
export function getDynamicPackagesEntryIntro(
dynamicRequireModuleDirPaths,
dynamicRequireModuleSet
) {
let dynamicImports = Array.from(
dynamicRequireModuleSet,
(dynamicId) => `require(${JSON.stringify(wrapId(dynamicId, DYNAMIC_REGISTER_SUFFIX))});`
).join('\n');
if (dynamicRequireModuleDirPaths.length) {
dynamicImports += `require(${JSON.stringify(
wrapId(DYNAMIC_PACKAGES_ID, DYNAMIC_REGISTER_SUFFIX)
)});`;
}
return dynamicImports;
}
export function isDynamicModuleImport(id, dynamicRequireModuleSet) {
const normalizedPath = normalizePathSlashes(id);
return dynamicRequireModuleSet.has(normalizedPath) && !normalizedPath.endsWith('.json');
}