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
141 changes: 33 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,11 @@ 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 (
this._routeMap.filePathOfUrlDecodedPathname(entryFile) ??
path.resolve(this._routeMap.serverRootDir, entryFile)
);
}

// Wait for the server to finish initializing.
Expand All @@ -1771,29 +1718,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
83 changes: 0 additions & 83 deletions packages/metro/src/Server/__tests__/Server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1436,87 +1436,4 @@ describe('processRequest', () => {
},
);
});

describe('watchFolder prefix resolution', () => {
let watchFolderServer: $FlowFixMe;

beforeEach(() => {
watchFolderServer = new Server(
mergeConfig(getDefaultValues('/'), {
projectRoot: '/project',
watchFolders: ['/project', '/external/packages'],
resolver: {blockList: []},
cacheVersion: '',
serializer: {
getRunModuleStatement: moduleId =>
`require(${JSON.stringify(moduleId)});`,
polyfillModuleNames: [],
getModulesRunBeforeMainModule: () => ['InitializeCore'],
},
reporter: require('../../lib/reporting').nullReporter,
} as InputConfigT),
);
});

test('resolves [metro-watchFolders]/N/ prefix against the Nth watch folder', () => {
expect(
watchFolderServer._resolveWatchFolderPrefix(
'./[metro-watchFolders]/1/expo-router/entry',
),
).toEqual({
rootDir: '/external/packages',
filePath: './expo-router/entry',
});
});

test('resolves [metro-watchFolders]/0/ prefix against the first watch folder', () => {
expect(
watchFolderServer._resolveWatchFolderPrefix(
'./[metro-watchFolders]/0/app/index',
),
).toEqual({
rootDir: '/project',
filePath: './app/index',
});
});

test('resolves [metro-project]/ prefix against projectRoot', () => {
expect(
watchFolderServer._resolveWatchFolderPrefix(
'./[metro-project]/src/App',
),
).toEqual({
rootDir: '/project',
filePath: './src/App',
});
});

test('returns null for paths without a recognized prefix', () => {
expect(
watchFolderServer._resolveWatchFolderPrefix('./mybundle'),
).toBeNull();
});

test('returns null for out-of-bounds watchFolder index', () => {
expect(
watchFolderServer._resolveWatchFolderPrefix(
'./[metro-watchFolders]/99/mybundle',
),
).toBeNull();
});

test('_getEntryPointAbsolutePath resolves prefixed entry against the corresponding watch folder', () => {
expect(
watchFolderServer._getEntryPointAbsolutePath(
'./[metro-watchFolders]/1/expo-router/entry',
),
).toBe('/external/packages/expo-router/entry');
});

test('_getEntryPointAbsolutePath resolves non-prefixed entry against server root', () => {
expect(watchFolderServer._getEntryPointAbsolutePath('./mybundle')).toBe(
'/project/mybundle',
);
});
});
});
35 changes: 35 additions & 0 deletions packages/metro/src/integration_tests/__tests__/build-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,41 @@ test('allows specifying paths to save bundle and maps', async () => {
);
});

// $FlowFixMe[prop-missing] - test.failing is not in Flow's Jest types
test.failing(
'builds a bundle from a file in a directory literally named [metro-project]',
async () => {
const config = await Metro.loadConfig({
config: require.resolve('../metro.config.js'),
});

const result = await Metro.runBuild(config, {
entry: './[metro-project]/LiteralDir.js',
});

expect(execBundle(result.code)).toBe('from-literal-dir');
},
);

// $FlowFixMe[prop-missing] - test.failing is not in Flow's Jest types
test.failing(
'runBuild resolves entry against projectRoot, not unstable_serverRoot',
async () => {
const baseConfig = await Metro.loadConfig({
config: require.resolve('../metro.config.js'),
});
const config = MetroConfig.mergeConfig(baseConfig, {
server: {unstable_serverRoot: path.resolve(INPUT_PATH, '..')},
});

const result = await Metro.runBuild(config, {
entry: 'TestBundle.js',
});

expect(execBundle(result.code)).toBeDefined();
},
);

test('(unstable) allows specifying a transform profile', async () => {
const config = await Metro.loadConfig({
config: require.resolve('../metro.config.js'),
Expand Down
Loading
Loading