Skip to content

Commit 3bdf386

Browse files
Alexey Kureevfacebook-github-bot
authored andcommitted
Replace projectRoots with projectRoot + watchRoots
Reviewed By: rafeca Differential Revision: D8450498 fbshipit-source-id: 830c21e847c3236e42d5414a8587508cb73864bd
1 parent 8be0b07 commit 3bdf386

19 files changed

Lines changed: 266 additions & 218 deletions

File tree

packages/metro/src/Bundler.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,14 @@ export type Options = {|
8383
+polyfillModuleNames: Array<string>,
8484
+postMinifyProcess: PostMinifyProcess,
8585
+postProcessBundleSourcemap: PostProcessBundleSourcemap,
86-
+projectRoots: $ReadOnlyArray<string>,
86+
+projectRoot: string,
8787
+providesModuleNodeModules?: Array<string>,
8888
+reporter: Reporter,
8989
+resolveRequest: ?CustomResolver,
9090
+sourceExts: Array<string>,
9191
+transformModulePath: string,
9292
+watch: boolean,
93+
+watchFolders: $ReadOnlyArray<string>,
9394
+workerPath: ?string,
9495
|};
9596

@@ -101,17 +102,17 @@ class Bundler {
101102
_baseHash: string;
102103
_transformer: Transformer;
103104
_depGraphPromise: Promise<DependencyGraph>;
104-
_projectRoots: $ReadOnlyArray<string>;
105+
_projectRoot: string;
105106
_getTransformOptions: void | GetTransformOptions;
106107

107108
constructor(opts: Options) {
108-
opts.projectRoots.forEach(verifyRootExists);
109+
opts.watchFolders.forEach(verifyRootExists);
109110

110111
const getTransformCacheKey = getTransformCacheKeyFn({
111112
asyncRequireModulePath: opts.asyncRequireModulePath,
112113
cacheVersion: opts.cacheVersion,
113114
dynamicDepsInPackages: opts.dynamicDepsInPackages,
114-
projectRoots: opts.projectRoots,
115+
projectRoot: opts.projectRoot,
115116
transformModulePath: opts.transformModulePath,
116117
});
117118

@@ -140,13 +141,14 @@ class Bundler {
140141
mainFields: opts.getResolverMainFields(),
141142
maxWorkers: opts.maxWorkers,
142143
platforms: new Set(opts.platforms),
143-
projectRoots: opts.projectRoots,
144+
projectRoot: opts.projectRoot,
144145
providesModuleNodeModules:
145146
opts.providesModuleNodeModules || defaults.providesModuleNodeModules,
146147
reporter: opts.reporter,
147148
resolveRequest: opts.resolveRequest,
148149
sourceExts: opts.sourceExts,
149150
watch: opts.watch,
151+
watchFolders: opts.watchFolders,
150152
});
151153

152154
this._baseHash = stableHash([
@@ -156,7 +158,7 @@ class Bundler {
156158
opts.minifierPath,
157159
]).toString('binary');
158160

159-
this._projectRoots = opts.projectRoots;
161+
this._projectRoot = opts.projectRoot;
160162
this._getTransformOptions = opts.getTransformOptions;
161163
}
162164

@@ -205,7 +207,7 @@ class Bundler {
205207
} {
206208
return {
207209
enableBabelRCLookup: this._opts.enableBabelRCLookup,
208-
projectRoot: this._projectRoots[0],
210+
projectRoot: this._projectRoot,
209211
};
210212
}
211213

@@ -241,7 +243,7 @@ class Bundler {
241243
}
242244
}
243245

244-
const localPath = toLocalPath(this._projectRoots, filePath);
246+
const localPath = toLocalPath(this._opts.watchFolders, filePath);
245247

246248
const partialKey = stableHash([
247249
// This is the hash related to the global Bundler config.

packages/metro/src/Bundler/__tests__/Bundler-test.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,28 @@ var commonOptions = {
4343
minifierPath: defaults.DEFAULT_METRO_MINIFIER_PATH,
4444
platforms: defaults.platforms,
4545
postMinifyProcess: e => e,
46+
projectRoot: '/root',
4647
resetCache: false,
4748
sourceExts: defaults.sourceExts,
4849
transformModulePath: '/path/to/transformer.js',
4950
watch: false,
51+
watchFolders: ['/root'],
5052
};
5153

5254
describe('Bundler', function() {
5355
let bundler;
5456
let assetServer;
55-
let projectRoots;
57+
let watchFolders;
58+
let projectRoot;
5659

5760
beforeEach(function() {
5861
os.cpus.mockReturnValue({length: 1});
5962
// local directory on purpose, because it should not actually write
6063
// anything to the disk during a unit test!
6164
os.tmpDir.mockReturnValue(path.join(__dirname));
6265

63-
projectRoots = ['/root'];
66+
projectRoot = '/root';
67+
watchFolders = [projectRoot];
6468

6569
mkdirp.sync('/path/to');
6670
mkdirp.sync('/root');
@@ -72,7 +76,7 @@ describe('Bundler', function() {
7276

7377
bundler = new Bundler({
7478
...commonOptions,
75-
projectRoots,
79+
watchFolders,
7680
assetServer,
7781
});
7882

@@ -90,7 +94,7 @@ describe('Bundler', function() {
9094
]);
9195
const b = new Bundler({
9296
...commonOptions,
93-
projectRoots,
97+
watchFolders,
9498
assetServer,
9599
platforms: ['android', 'vr'],
96100
});
@@ -104,7 +108,7 @@ describe('Bundler', function() {
104108
const bundlerInstance = new Bundler({
105109
...commonOptions,
106110
cacheStores: [{get, set}],
107-
projectRoots,
111+
watchFolders,
108112
});
109113

110114
const depGraph = {

packages/metro/src/Config.js

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,22 @@ export type ConfigT = {
9595
*/
9696
getPlatforms: () => Array<string>,
9797

98-
getProjectRoots(): Array<string>,
98+
/**
99+
* Specify a list of project roots
100+
* @deprecated Previousely used to set up a list of watchers (one per
101+
* directory). Discontinued in a favor of getProjectRoot and getWatchFolders
102+
*/
103+
getProjectRoots: ?() => Array<string>,
104+
105+
/**
106+
* Specify a root folder of the user project
107+
*/
108+
getProjectRoot: () => string,
109+
110+
/**
111+
* Specify any additional (to projectRoot) watch folders
112+
*/
113+
getWatchFolders: () => Array<string>,
99114

100115
/**
101116
* Specify any additional node modules that should be processed for
@@ -201,9 +216,11 @@ const DEFAULT = ({
201216
getEnableBabelRCLookup: () => true,
202217
getPlatforms: () => [],
203218
getPolyfillModuleNames: () => [],
219+
getProjectRoots: undefined,
204220
// We assume the default project path is two levels up from
205221
// node_modules/metro/
206-
getProjectRoots: () => [path.resolve(__dirname, '../../..')],
222+
getProjectRoot: () => path.resolve(__dirname, '../../..'),
223+
getWatchFolders: () => [],
207224
getProvidesModuleNodeModules: () => providesModuleNodeModules.slice(),
208225
getRunModuleStatement: (moduleId: number | string) =>
209226
`require(${JSON.stringify(moduleId)});`,
@@ -221,10 +238,37 @@ const DEFAULT = ({
221238
}: ConfigT);
222239

223240
const normalize = (initialConfig: ConfigT, defaults?: ConfigT): ConfigT => {
224-
return {
241+
const normalizedConfig: ConfigT = {
225242
...(defaults || DEFAULT),
226243
...initialConfig,
227244
};
245+
246+
const watchFolders = normalizedConfig.getWatchFolders();
247+
const projectRoot = normalizedConfig.getProjectRoot();
248+
249+
// TODO: Remove me once getProjectRoots is finally deprecated
250+
if (normalizedConfig.getProjectRoots) {
251+
const legacyProjectRoots = normalizedConfig.getProjectRoots();
252+
normalizedConfig.getProjectRoot = () => legacyProjectRoots[0];
253+
normalizedConfig.getWatchFolders = () => legacyProjectRoots;
254+
normalizedConfig.getProjectRoots = undefined;
255+
256+
console.warn(
257+
'Warning! Your metro configuration contains a deprecated function ' +
258+
'"projectRoots". Please, consider changing it to "getProjectRoot" ' +
259+
'with "watchFolders" (if needed)',
260+
);
261+
} else if (watchFolders.includes(projectRoot)) {
262+
console.warn(
263+
'Warning! There is no need to specify your projectRoot (' +
264+
projectRoot +
265+
') in "watchFolders". It will be watched automatically',
266+
);
267+
} else {
268+
normalizedConfig.getWatchFolders = () => [projectRoot, ...watchFolders];
269+
}
270+
271+
return normalizedConfig;
228272
};
229273

230274
const load = (configFile: string, defaults?: ConfigT) =>

packages/metro/src/DeltaBundler/Serializers/__tests__/getAssets-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ it('should return the bundle assets', async () => {
3434
]),
3535
};
3636

37-
expect(await getAssets(graph, {projectRoots: ['/tmp']})).toEqual([
37+
expect(await getAssets(graph, {watchFolders: ['/tmp']})).toEqual([
3838
{path: '/tmp/3.png', localPath: '3.png'},
3939
{path: '/tmp/5.mov', localPath: '5.mov'},
4040
]);

packages/metro/src/DeltaBundler/Serializers/getAssets.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import type {Graph} from '../types.flow';
2121
type Options = {|
2222
assetPlugins: $ReadOnlyArray<string>,
2323
platform: ?string,
24-
projectRoots: $ReadOnlyArray<string>,
24+
watchFolders: $ReadOnlyArray<string>,
2525
|};
2626

2727
async function getAssets(
@@ -35,7 +35,7 @@ async function getAssets(
3535
promises.push(
3636
getAssetData(
3737
module.path,
38-
toLocalPath(options.projectRoots, module.path),
38+
toLocalPath(options.watchFolders, module.path),
3939
options.assetPlugins,
4040
options.platform,
4141
),

0 commit comments

Comments
 (0)