Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions packages/metro/src/HmrServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import debounceAsyncQueue from './lib/debounceAsyncQueue';
import formatBundlingError from './lib/formatBundlingError';
import getGraphId from './lib/getGraphId';
import parseBundleOptionsFromBundleRequestUrl from './lib/parseBundleOptionsFromBundleRequestUrl';
import RootUrlMap from './lib/RootUrlMap';
import splitBundleOptions from './lib/splitBundleOptions';
import * as transformHelpers from './lib/transformHelpers';
import debugModule from 'debug';
Expand Down Expand Up @@ -71,6 +72,7 @@ export default class HmrServer<TClient extends Client> {
_bundler: IncrementalBundler;
_createModuleId: (path: string) => number;
_clientGroups: Map<RevisionId, ClientGroup>;
_rootUrlMap: RootUrlMap;

constructor(
bundler: IncrementalBundler,
Expand All @@ -81,6 +83,7 @@ export default class HmrServer<TClient extends Client> {
this._bundler = bundler;
this._createModuleId = createModuleId;
this._clientGroups = new Map();
this._rootUrlMap = new RootUrlMap(config);
}

onClientConnect: (
Expand Down Expand Up @@ -121,19 +124,19 @@ export default class HmrServer<TClient extends Client> {
transformOptions.platform,
resolverOptions,
);
const resolvedEntryFilePath = resolutionFn(
(this._config.server.unstable_serverRoot ?? this._config.projectRoot) +
'/.',
{
name: entryFile,
data: {
key: entryFile,
asyncType: null,
isESMImport: false,
locs: [],
},
const absolutePath =
this._rootUrlMap.filePathOfUrlDecodedPathname(entryFile);
const rootDir = absolutePath != null ? '/' : this._rootUrlMap.serverRootDir;
const resolvedEntryFile = absolutePath ?? entryFile;
const resolvedEntryFilePath = resolutionFn(rootDir + '/.', {
name: resolvedEntryFile,
data: {
key: resolvedEntryFile,
asyncType: null,
isESMImport: false,
locs: [],
},
).filePath;
}).filePath;
const graphId = getGraphId(resolvedEntryFilePath, transformOptions, {
resolverOptions,
shallow: graphOptions.shallow,
Expand Down Expand Up @@ -379,8 +382,7 @@ export default class HmrServer<TClient extends Client> {
createModuleId: this._createModuleId,
includeAsyncPaths: group.graphOptions.lazy,
projectRoot: this._config.projectRoot,
serverRoot:
this._config.server.unstable_serverRoot ?? this._config.projectRoot,
serverRoot: this._rootUrlMap.serverRootDir,
});

logger?.point('serialize_end');
Expand Down
55 changes: 55 additions & 0 deletions packages/metro/src/__tests__/HmrServer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ describe('HmrServer', () => {
unstable_allowRequireContext: false,
},
resolver: {platforms: []},
watchFolders: [p('/external/node_modules')],
server: {
rewriteRequestUrl(requrl) {
const rewritten = requrl.replace(
Expand Down Expand Up @@ -650,6 +651,60 @@ describe('HmrServer', () => {
]);
});

test('should resolve [metro-watchFolders] prefix in entry point', async () => {
await connect(
'/hot?bundleEntry=./[metro-watchFolders]/0/expo-router/entry.js&platform=ios',
);

expect(getRevisionByGraphIdMock).toBeCalledWith(
getGraphId(
p('/external/node_modules/expo-router/entry.js'),
{
customTransformOptions: {},
dev: true,
minify: false,
platform: 'ios',
type: 'module',
unstable_transformProfile: 'default',
},
{
shallow: false,
lazy: false,
unstable_allowRequireContext: false,
resolverOptions: {
dev: true,
},
},
),
);
});

test('should resolve [metro-project] prefix in entry point', async () => {
await connect('/hot?bundleEntry=./[metro-project]/src/App.js&platform=ios');

expect(getRevisionByGraphIdMock).toBeCalledWith(
getGraphId(
p('/root/src/App.js'),
{
customTransformOptions: {},
dev: true,
minify: false,
platform: 'ios',
type: 'module',
unstable_transformProfile: 'default',
},
{
shallow: false,
lazy: false,
unstable_allowRequireContext: false,
resolverOptions: {
dev: true,
},
},
),
);
});

test('should return error messages when there is a transform error', async () => {
jest.useRealTimers();
const sendMessage = jest.fn();
Expand Down
Loading