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
29 changes: 15 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 ProjectRouteMap from './lib/ProjectRouteMap';
import splitBundleOptions from './lib/splitBundleOptions';
import * as transformHelpers from './lib/transformHelpers';
import {Logger} from 'metro-core';
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>;
_routeMap: ProjectRouteMap;

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._routeMap = new ProjectRouteMap(config);
}

onClientConnect: (
Expand Down Expand Up @@ -121,19 +124,18 @@ 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._routeMap.filePathOfUrlDecodedPathname(entryFile);
const rootDir = absolutePath != null ? '/' : this._routeMap.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 +381,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._routeMap.serverRootDir,
});

logger?.point('serialize_end');
Expand Down
138 changes: 30 additions & 108 deletions packages/metro/src/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import formatBundlingError from './lib/formatBundlingError';
import getGraphId from './lib/getGraphId';
import parseBundleOptionsFromBundleRequestUrl from './lib/parseBundleOptionsFromBundleRequestUrl';
import parseJsonBody from './lib/parseJsonBody';
import ProjectRouteMap from './lib/ProjectRouteMap';
import splitBundleOptions from './lib/splitBundleOptions';
import * as transformHelpers from './lib/transformHelpers';
import {UnableToResolveError} from './node-haste/DependencyGraph/ModuleResolution';
Expand Down Expand Up @@ -149,9 +150,7 @@ export default class Server {
_reporter: Reporter;
_serverOptions: ServerOptions | void;
_allowedSuffixesForSourceRequests: ReadonlyArray<string>;
_sourceRequestRoutingMap: ReadonlyArray<
[pathnamePrefix: string, normalizedRootDir: string],
>;
_routeMap: ProjectRouteMap;
_fetchTimings: Array<FetchTiming>;
_activeFetchCount: number;

Expand All @@ -178,13 +177,7 @@ export default class Server {
].map(ext => '.' + ext),
),
];
this._sourceRequestRoutingMap = [
['/[metro-project]/', path.resolve(this._config.projectRoot)],
...this._config.watchFolders.map((watchFolder, index) => [
`/[metro-watchFolders]/${index}/`,
path.resolve(watchFolder),
]),
];
this._routeMap = new ProjectRouteMap(config);
this._isEnded = false;
this._fetchTimings = [];
this._activeFetchCount = 0;
Expand Down Expand Up @@ -260,8 +253,7 @@ export default class Server {
sourceMapUrl: serializerOptions.sourceMapUrl,
sourceUrl: serializerOptions.sourceUrl,
inlineSourceMap: serializerOptions.inlineSourceMap,
serverRoot:
this._config.server.unstable_serverRoot ?? this._config.projectRoot,
serverRoot: this._routeMap.serverRootDir,
shouldAddToIgnoreList: (module: Module<>) =>
this._shouldAddModuleToIgnoreList(module),
getSourceUrl: (module: Module<>) =>
Expand Down Expand Up @@ -365,6 +357,8 @@ export default class Server {
transformOptions,
} = splitBundleOptions(options);

const entryPoint = this._getEntryPointAbsolutePath(entryFile);

const {prepend, graph} = await this._bundler.buildGraph(
entryFile,
transformOptions,
Expand All @@ -376,8 +370,6 @@ export default class Server {
},
);

const entryPoint = this._getEntryPointAbsolutePath(entryFile);

return await getRamBundleInfo(entryPoint, prepend, graph, {
asyncRequireModulePath: await this._resolveRelativePath(
this._config.transformer.asyncRequireModulePath,
Expand Down Expand Up @@ -406,8 +398,7 @@ export default class Server {
sourceMapUrl: serializerOptions.sourceMapUrl,
sourceUrl: serializerOptions.sourceUrl,
inlineSourceMap: serializerOptions.inlineSourceMap,
serverRoot:
this._config.server.unstable_serverRoot ?? this._config.projectRoot,
serverRoot: this._routeMap.serverRootDir,
shouldAddToIgnoreList: (module: Module<>) =>
this._shouldAddModuleToIgnoreList(module),
getSourceUrl: (module: Module<>) =>
Expand Down Expand Up @@ -440,7 +431,7 @@ export default class Server {
processModuleFilter: this._config.serializer.processModuleFilter,
assetPlugins: this._config.transformer.assetPlugins,
platform,
projectRoot: this._getServerRootDir(),
projectRoot: this._routeMap.serverRootDir,
publicPath: this._config.transformer.publicPath,
});
}
Expand Down Expand Up @@ -700,52 +691,38 @@ export default class Server {
} else if (pathname === '/symbolicate') {
await this._symbolicate(req, res);
} else {
let handled = false;
for (const [pathnamePrefix, normalizedRootDir] of this
._sourceRequestRoutingMap) {
if (filePathname.startsWith(pathnamePrefix)) {
const relativeFilePathname = filePathname.substr(
pathnamePrefix.length,
);
await this._processSourceRequest(
relativeFilePathname,
normalizedRootDir,
res,
);
handled = true;
break;
}
}
if (!handled) {
const sourceFilePath =
this._routeMap.filePathOfUrlDecodedPathname(filePathname);
if (sourceFilePath != null) {
await this._processSourceRequest(sourceFilePath, res);
} else {
next();
}
}
}

async _processSourceRequest(
relativeFilePathname: string,
rootDir: string,
filePath: string,
res: ServerResponse,
): Promise<void> {
if (
!this._allowedSuffixesForSourceRequests.some(suffix =>
relativeFilePathname.endsWith(suffix),
filePath.endsWith(suffix),
)
) {
res.writeHead(404);
res.end();
return;
}
const depGraph = await this._bundler.getBundler().getDependencyGraph();
const filePath = path.join(rootDir, relativeFilePathname);
try {
await depGraph.getOrComputeSha1(filePath);
} catch {
res.writeHead(404);
res.end();
return;
}
const mimeType = mime.lookup(path.basename(relativeFilePathname));
const mimeType = mime.lookup(path.basename(filePath));
res.setHeader('Content-Type', mimeType);
const stream = fs.createReadStream(filePath);
stream.pipe(res);
Expand Down Expand Up @@ -1150,8 +1127,7 @@ export default class Server {
sourceMapUrl: serializerOptions.sourceMapUrl,
sourceUrl: serializerOptions.sourceUrl,
inlineSourceMap: serializerOptions.inlineSourceMap,
serverRoot:
this._config.server.unstable_serverRoot ?? this._config.projectRoot,
serverRoot: this._routeMap.serverRootDir,
shouldAddToIgnoreList: (module: Module<>) =>
this._shouldAddModuleToIgnoreList(module),
getSourceUrl: (module: Module<>) =>
Expand Down Expand Up @@ -1622,33 +1598,6 @@ export default class Server {
);
}

_resolveWatchFolderPrefix(
filePath: string,
): {rootDir: string, filePath: string} | null {
const watchFolderMatch = filePath.match(
/^\.\/\[metro-watchFolders\]\/(\d+)\/(.*)/,
);
if (watchFolderMatch != null) {
const index = parseInt(watchFolderMatch[1], 10);
const watchFolder = this._config.watchFolders[index];
if (watchFolder != null) {
return {
rootDir: path.resolve(watchFolder),
filePath:
'.' + path.sep + watchFolderMatch[2].split('/').join(path.sep),
};
}
}
const projectMatch = filePath.match(/^\.\/\[metro-project\]\/(.*)/);
if (projectMatch != null) {
return {
rootDir: path.resolve(this._config.projectRoot),
filePath: '.' + path.sep + projectMatch[1].split('/').join(path.sep),
};
}
return null;
}

async _resolveRelativePath(
filePath: string,
{
Expand All @@ -1666,14 +1615,17 @@ export default class Server {
transformOptions.platform,
resolverOptions,
);
const resolved = this._resolveWatchFolderPrefix(filePath);
const rootDir =
resolved != null
? resolved.rootDir
: relativeTo === 'server'
? this._getServerRootDir()
: this._config.projectRoot;
const resolvedFilePath = resolved != null ? resolved.filePath : filePath;
let rootDir;
let resolvedFilePath;
if (relativeTo === 'project') {
rootDir = this._config.projectRoot;
resolvedFilePath = filePath;
} else {
const absolutePath =
this._routeMap.filePathOfUrlDecodedPathname(filePath);
rootDir = absolutePath != null ? '/' : this._routeMap.serverRootDir;
resolvedFilePath = absolutePath ?? filePath;
}
return resolutionFn(`${rootDir}/.`, {
name: resolvedFilePath,
data: {
Expand Down Expand Up @@ -1737,16 +1689,8 @@ export default class Server {
sourcePaths: SourcePathsMode.Absolute,
};

_getServerRootDir(): string {
return this._config.server.unstable_serverRoot ?? this._config.projectRoot;
}

_getEntryPointAbsolutePath(entryFile: string): string {
const resolved = this._resolveWatchFolderPrefix(entryFile);
if (resolved != null) {
return path.resolve(resolved.rootDir, resolved.filePath);
}
return path.resolve(this._getServerRootDir(), entryFile);
return path.resolve(this._routeMap.serverRootDir, entryFile);
}

// Wait for the server to finish initializing.
Expand All @@ -1771,29 +1715,7 @@ export default class Server {
_getModuleSourceUrl(module: Module<>, mode: SourcePathsMode): string {
switch (mode) {
case SourcePathsMode.ServerUrl:
for (const [pathnamePrefix, normalizedRootDir] of this
._sourceRequestRoutingMap) {
if (module.path.startsWith(normalizedRootDir + path.sep)) {
const relativePath = module.path.slice(
normalizedRootDir.length + 1,
);
const relativePathPosix = relativePath
.split(path.sep)
.map(segment => encodeURIComponent(segment))
.join('/');
return pathnamePrefix + relativePathPosix;
}
}
// Ordinarily all files should match one of the roots above. If they
// don't, try to preserve useful information, even if fetching the path
// from Metro might fail.
const modulePathPosix = module.path
.split(path.sep)
.map(segment => encodeURIComponent(segment))
.join('/');
return modulePathPosix.startsWith('/')
? modulePathPosix
: '/' + modulePathPosix;
return this._routeMap.urlPathnameOfFilePath(module.path);
case SourcePathsMode.Absolute:
return module.path;
}
Expand Down
Loading
Loading