-
Notifications
You must be signed in to change notification settings - Fork 598
/
contextModuleTemplates.js
155 lines (137 loc) Β· 4.77 KB
/
contextModuleTemplates.js
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
* @oncall react_native
*/
import * as path from 'path';
import * as os from 'os';
import type {ContextMode} from '../ModuleGraph/worker/collectDependencies';
function createFileMap(
modulePath: string,
files: string[],
processModule: (moduleId: string) => string,
): string {
let mapString = '\n';
files
.slice()
// Sort for deterministic output
.sort()
.forEach(file => {
let filePath = path.relative(modulePath, file);
if (os.platform() === 'win32') {
filePath = filePath.replace(/\\/g, '/');
}
// NOTE(EvanBacon): I'd prefer we prevent the ability for a module to require itself (`require.context('./')`)
// but Webpack allows this, keeping it here provides better parity between bundlers.
// Ensure relative file paths start with `./` so they match the
// patterns (filters) used to include them.
if (!filePath.startsWith('.')) {
filePath = `./${filePath}`;
}
const key = JSON.stringify(filePath);
// NOTE(EvanBacon): Webpack uses `require.resolve` in order to load modules on demand,
// Metro doesn't have this functionality so it will use getters instead. Modules need to
// be loaded on demand because if we imported directly then users would get errors from importing
// a file without exports as soon as they create a new file and the context module is updated.
// NOTE: The values are set to `enumerable` so the `context.keys()` method works as expected.
mapString += ` ${key}: { enumerable: true, get() { return ${processModule(
file,
)}; } },\n`;
});
return `Object.defineProperties({}, {${mapString}})`;
}
function getEmptyContextModuleTemplate(modulePath: string): string {
return `
function metroEmptyContext(request) {
let e = new Error('No modules in context');
e.code = 'MODULE_NOT_FOUND';
throw e;
}
// Return the keys that can be resolved.
metroEmptyContext.keys = () => ([]);
// Return the module identifier for a user request.
metroEmptyContext.resolve = function metroContextResolve(request) {
throw new Error('Unimplemented Metro module context functionality');
}
module.exports = metroEmptyContext;`;
}
function getLoadableContextModuleTemplate(
modulePath: string,
files: string[],
importSyntax: string,
getContextTemplate: string,
): string {
return `// All of the requested modules are loaded behind enumerable getters.
const map = ${createFileMap(
modulePath,
files,
moduleId => `${importSyntax}(${JSON.stringify(moduleId)})`,
)};
function metroContext(request) {
${getContextTemplate}
}
// Return the keys that can be resolved.
metroContext.keys = function metroContextKeys() {
return Object.keys(map);
};
// Return the module identifier for a user request.
metroContext.resolve = function metroContextResolve(request) {
throw new Error('Unimplemented Metro module context functionality');
}
module.exports = metroContext;`;
}
/**
* Generate a context module as a virtual file string.
*
* @prop {ContextMode} mode indicates how the modules should be loaded.
* @prop {string} modulePath virtual file path for the virtual module. Example: `require.context('./src')` -> `'/path/to/project/src'`.
* @prop {string[]} files list of absolute file paths that must be exported from the context module. Example: `['/path/to/project/src/index.js']`.
*
* @returns a string representing a context module (virtual file contents).
*/
export function getContextModuleTemplate(
mode: ContextMode,
modulePath: string,
files: string[],
): string {
if (!files.length) {
return getEmptyContextModuleTemplate(modulePath);
}
switch (mode) {
case 'eager':
return getLoadableContextModuleTemplate(
modulePath,
files,
// NOTE(EvanBacon): It's unclear if we should use `import` or `require` here so sticking
// with the more stable option (`require`) for now.
'require',
[
' // Here Promise.resolve().then() is used instead of new Promise() to prevent',
' // uncaught exception popping up in devtools',
' return Promise.resolve().then(() => map[request]);',
].join('\n'),
);
case 'sync':
return getLoadableContextModuleTemplate(
modulePath,
files,
'require',
' return map[request];',
);
case 'lazy':
case 'lazy-once':
return getLoadableContextModuleTemplate(
modulePath,
files,
'import',
' return map[request];',
);
default:
throw new Error(`Metro context mode "${mode}" is unimplemented`);
}
}