Skip to content

Commit 1e5fb49

Browse files
committed
Cherry pick: Use relative paths for module verboseNames
Summary: This diff changes the verboseName assigned to modules to be the full relative path of the module, instead of just the file name. This fixes #204 Reviewed By: mjesun Differential Revision: D9013798 fbshipit-source-id: 306be4890628dfa6bfa5cc22d1bf0d8119314636
1 parent 9cf605d commit 1e5fb49

File tree

12 files changed

+64
-31
lines changed

12 files changed

+64
-31
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const options = {
4444
createModuleId: createModuleIdFactory(),
4545
dev: true,
4646
getRunModuleStatement: moduleId => `require(${JSON.stringify(moduleId)});`,
47+
projectRoot: '/root',
4748
runBeforeMainModule: [],
4849
runModule: true,
4950
sourceMapUrl: 'http://localhost/bundle.map',

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ it('should return the RAM bundle info', async () => {
5757
ramGroups: [],
5858
}),
5959
dev: true,
60+
projectRoot: '/root',
6061
runBeforeMainModule: [],
6162
runModule: true,
6263
sourceMapUrl: 'http://localhost/bundle.map',
@@ -76,6 +77,7 @@ it('should use the preloadedModules and ramGroup configs to build a RAM bundle',
7677
getRunModuleStatement,
7778
getTransformOptions,
7879
dev: true,
80+
projectRoot: '/root',
7981
runBeforeMainModule: [],
8082
runModule: true,
8183
sourceMapUrl: 'http://localhost/bundle.map',

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

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
'use strict';
1212

1313
const createModuleIdFactory = require('../../../lib/createModuleIdFactory');
14+
const path = require('path');
1415
const plainJSBundle = require('../plainJSBundle');
1516

1617
const polyfill = {
@@ -23,15 +24,15 @@ const polyfill = {
2324
};
2425

2526
const fooModule = {
26-
path: 'foo',
27-
dependencies: new Map([['./bar', {absolutePath: 'bar', data: {}}]]),
27+
path: '/root/foo',
28+
dependencies: new Map([['./bar', {absolutePath: '/root/bar', data: {}}]]),
2829
output: [
2930
{type: 'js/module', data: {code: '__d(function() {/* code for foo */});'}},
3031
],
3132
};
3233

3334
const barModule = {
34-
path: 'bar',
35+
path: '/root/bar',
3536
dependencies: new Map(),
3637
output: [
3738
{type: 'js/module', data: {code: '__d(function() {/* code for bar */});'}},
@@ -44,16 +45,20 @@ const getRunModuleStatement = moduleId =>
4445
it('should serialize a very simple bundle', () => {
4546
expect(
4647
plainJSBundle(
47-
'foo',
48+
'/root/foo',
4849
[polyfill],
4950
{
50-
dependencies: new Map([['foo', fooModule], ['bar', barModule]]),
51+
dependencies: new Map([
52+
['/root/foo', fooModule],
53+
['/root/bar', barModule],
54+
]),
5155
entryPoints: ['foo'],
5256
},
5357
{
54-
createModuleId: path => path,
58+
createModuleId: filePath => path.basename(filePath),
5559
dev: true,
5660
getRunModuleStatement,
61+
projectRoot: '/root',
5762
runBeforeMainModule: [],
5863
runModule: true,
5964
sourceMapUrl: 'http://localhost/bundle.map',
@@ -73,17 +78,21 @@ it('should serialize a very simple bundle', () => {
7378
it('should add runBeforeMainModule statements if found in the graph', () => {
7479
expect(
7580
plainJSBundle(
76-
'foo',
81+
'/root/foo',
7782
[polyfill],
7883
{
79-
dependencies: new Map([['foo', fooModule], ['bar', barModule]]),
80-
entryPoints: ['foo'],
84+
dependencies: new Map([
85+
['/root/foo', fooModule],
86+
['/root/bar', barModule],
87+
]),
88+
entryPoints: ['/root/foo'],
8189
},
8290
{
83-
createModuleId: path => path,
91+
createModuleId: filePath => path.basename(filePath),
8492
dev: true,
8593
getRunModuleStatement,
86-
runBeforeMainModule: ['bar', 'non-existant'],
94+
projectRoot: '/root',
95+
runBeforeMainModule: ['/root/bar', 'non-existant'],
8796
runModule: true,
8897
sourceMapUrl: 'http://localhost/bundle.map',
8998
},
@@ -103,17 +112,21 @@ it('should add runBeforeMainModule statements if found in the graph', () => {
103112
it('should handle numeric module ids', () => {
104113
expect(
105114
plainJSBundle(
106-
'foo',
115+
'/root/foo',
107116
[polyfill],
108117
{
109-
dependencies: new Map([['foo', fooModule], ['bar', barModule]]),
110-
entryPoints: ['foo'],
118+
dependencies: new Map([
119+
['/root/foo', fooModule],
120+
['/root/bar', barModule],
121+
]),
122+
entryPoints: ['/root/foo'],
111123
},
112124
{
113125
createModuleId: createModuleIdFactory(),
114126
dev: true,
115127
getRunModuleStatement,
116-
runBeforeMainModule: ['bar', 'non-existant'],
128+
projectRoot: '/root',
129+
runBeforeMainModule: ['/root/bar', 'non-existant'],
117130
runModule: true,
118131
sourceMapUrl: 'http://localhost/bundle.map',
119132
},
@@ -133,18 +146,22 @@ it('should handle numeric module ids', () => {
133146
it('outputs custom runModule statements', () => {
134147
expect(
135148
plainJSBundle(
136-
'foo',
149+
'/root/foo',
137150
[polyfill],
138151
{
139-
dependencies: new Map([['foo', fooModule], ['bar', barModule]]),
140-
entryPoints: ['foo'],
152+
dependencies: new Map([
153+
['/root/foo', fooModule],
154+
['/root/bar', barModule],
155+
]),
156+
entryPoints: ['/root/foo'],
141157
},
142158
{
143-
createModuleId: path => path,
159+
createModuleId: filePath => path.basename(filePath),
144160
dev: true,
145161
getRunModuleStatement: moduleId =>
146162
`export default require(${JSON.stringify(moduleId)}).default;`,
147-
runBeforeMainModule: ['bar'],
163+
projectRoot: '/root',
164+
runBeforeMainModule: ['/root/bar'],
148165
runModule: true,
149166
},
150167
),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type Options = {|
2121
+createModuleId: string => number | string,
2222
+dev: boolean,
2323
+getRunModuleStatement: (number | string) => string,
24+
+projectRoot: string,
2425
+runBeforeMainModule: $ReadOnlyArray<string>,
2526
+runModule: boolean,
2627
+sourceMapUrl: ?string,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Options = {|
3030
+getRunModuleStatement: number => string,
3131
+getTransformOptions: ?GetTransformOptions,
3232
+platform: ?string,
33+
+projectRoot: string,
3334
+runBeforeMainModule: $ReadOnlyArray<string>,
3435
+runModule: boolean,
3536
+sourceMapUrl: ?string,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ describe('wrapModule()', () => {
4545
wrapModule(myModule, {
4646
createModuleId: createModuleIdFactory(),
4747
dev: false,
48+
projectRoot: '/root',
4849
}),
4950
).toEqual('__d(function() { console.log("foo") },0,[1,2]);');
5051
});
@@ -54,6 +55,7 @@ describe('wrapModule()', () => {
5455
wrapModule(myModule, {
5556
createModuleId: createModuleIdFactory(),
5657
dev: true,
58+
projectRoot: '/root',
5759
}),
5860
).toEqual('__d(function() { console.log("foo") },0,[1,2],"foo.js");');
5961
});
@@ -65,6 +67,7 @@ describe('wrapModule()', () => {
6567
wrapModule(myModule, {
6668
createModuleId: createModuleIdFactory(),
6769
dev: true,
70+
projectRoot: '/root',
6871
}),
6972
).toEqual(myModule.output[0].data.code);
7073
});
@@ -75,6 +78,7 @@ describe('wrapModule()', () => {
7578
wrapModule(myModule, {
7679
createModuleId: path => path,
7780
dev: false,
81+
projectRoot: '/root',
7882
}),
7983
).toEqual(
8084
'__d(function() { console.log("foo") },"/root/foo.js",["/bar","/baz"]);',

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import type {MixedOutput, Module} from '../../types.flow';
2020
export type Options = {
2121
+createModuleId: string => number | string,
2222
+dev: boolean,
23+
+projectRoot: string,
2324
};
2425

2526
// Used to include paths in production bundles for traces of performance tuned runs,
@@ -42,12 +43,10 @@ function wrapModule(module: Module<>, options: Options) {
4243
}),
4344
];
4445

45-
// Add the module name as the last parameter (to make it easier to do
46+
// Add the module relative path as the last parameter (to make it easier to do
4647
// requires by name when debugging).
47-
// TODO (t26853986): Switch this to use the relative file path (once we have
48-
// as single project root).
4948
if (PASS_MODULE_PATHS_TO_DEFINE || options.dev) {
50-
params.push(path.basename(module.path));
49+
params.push(path.relative(options.projectRoot, module.path));
5150
if (PASS_MODULE_PATHS_TO_DEFINE) {
5251
params.push(module.path);
5352
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ const {isJsModule, wrapModule} = require('./helpers/js');
1717
import type {DeltaResult, Graph, Module} from '../types.flow';
1818

1919
type Options = {
20-
createModuleId: string => number,
20+
+createModuleId: string => number,
21+
+projectRoot: string,
2122
};
2223

2324
export type Result = {
@@ -58,7 +59,7 @@ function _prepareModule(
5859
options: Options,
5960
): {|+id: number, +code: string|} {
6061
const code = wrapModule(module, {
61-
createModuleId: options.createModuleId,
62+
...options,
6263
dev: true,
6364
});
6465

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type Options = {|
2020
+createModuleId: string => number | string,
2121
+dev: boolean,
2222
+getRunModuleStatement: (number | string) => string,
23+
+projectRoot: string,
2324
+runBeforeMainModule: $ReadOnlyArray<string>,
2425
+runModule: boolean,
2526
+sourceMapUrl: ?string,

packages/metro/src/HmrServer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ class HmrServer<TClient: Client> {
127127

128128
return hmrJSBundle(delta, client.graph, {
129129
createModuleId: this._packagerServer._opts.createModuleId,
130+
projectRoot: this._packagerServer.getProjectRoots()[0],
130131
});
131132
} catch (error) {
132133
const formattedError = formatBundlingError(error);

0 commit comments

Comments
 (0)