Skip to content

Commit

Permalink
fix: normalize require.context paths to posix only
Browse files Browse the repository at this point in the history
  • Loading branch information
byCedric committed Sep 28, 2022
1 parent 9329b82 commit db96843
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ exports[`getContextModuleTemplate creates a lazy template 1`] = `
const map = Object.defineProperties({}, {
\\"./foo.js\\": { enumerable: true, get() { return import(\\"/path/to/project/src/foo.js\\"); } },
});
function metroContext(request) {
return map[request];
}
// Return the keys that can be resolved.
metroContext.keys = function metroContextKeys() {
return Object.keys(map);
Expand All @@ -29,11 +29,11 @@ const map = Object.defineProperties({}, {
\\"./another/bar.js\\": { enumerable: true, get() { return import(\\"/path/to/project/src/another/bar.js\\"); } },
\\"./foo.js\\": { enumerable: true, get() { return import(\\"/path/to/project/src/foo.js\\"); } },
});
function metroContext(request) {
return map[request];
}
// Return the keys that can be resolved.
metroContext.keys = function metroContextKeys() {
return Object.keys(map);
Expand All @@ -52,11 +52,11 @@ exports[`getContextModuleTemplate creates a sync template 1`] = `
const map = Object.defineProperties({}, {
\\"./foo.js\\": { enumerable: true, get() { return require(\\"/path/to/project/src/foo.js\\"); } },
});
function metroContext(request) {
return map[request];
}
// Return the keys that can be resolved.
metroContext.keys = function metroContextKeys() {
return Object.keys(map);
Expand All @@ -75,13 +75,13 @@ exports[`getContextModuleTemplate creates an eager template 1`] = `
const map = Object.defineProperties({}, {
\\"./foo.js\\": { enumerable: true, get() { return require(\\"/path/to/project/src/foo.js\\"); } },
});
function metroContext(request) {
// Here Promise.resolve().then() is used instead of new Promise() to prevent
// uncaught exception popping up in devtools
return Promise.resolve().then(() => map[request]);
}
// Return the keys that can be resolved.
metroContext.keys = function metroContextKeys() {
return Object.keys(map);
Expand Down
8 changes: 6 additions & 2 deletions packages/metro/src/lib/contextModuleTemplates.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ function createFileMap(
if (!filePath.startsWith('.')) {
filePath = `.${path.sep}` + filePath;
}
// NOTE(byCedric): On Windows, normalize the backslashes to forward slashes to match the behavior in Webpack.
if (path.sep === '\\') {
filePath = filePath.replace(/\\/g, '/');
}
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
Expand Down Expand Up @@ -79,11 +83,11 @@ const map = ${createFileMap(
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);
Expand Down

0 comments on commit db96843

Please sign in to comment.