Skip to content

Commit e595178

Browse files
rafecafacebook-github-bot
authored andcommitted
Fix HMR plugin on the OpenSource React Native transformer
Summary: This fixes facebook/react-native#21490 Reviewed By: jeanlauliac Differential Revision: D10216508 fbshipit-source-id: d857918e281e0d1c02644bb7fcbe201026d2224a
1 parent a3dec63 commit e595178

File tree

3 files changed

+71
-17
lines changed

3 files changed

+71
-17
lines changed

packages/metro/src/integration_tests/__tests__/__snapshots__/server-test.js.snap

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,35 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`should create a server 1`] = `
3+
exports[`Metro development server serves bundles via HTTP should serve deveopment bundles 1`] = `
4+
Object {
5+
"Bar": Object {
6+
"foo": "foo",
7+
"type": "bar",
8+
},
9+
"Foo": Object {
10+
"asset": Object {
11+
"__packager_asset": true,
12+
"hash": "77d45c1f7fa73c0f6c444a830dc42f67",
13+
"height": 8,
14+
"httpServerLocation": "/assets",
15+
"name": "test",
16+
"scales": Array [
17+
1,
18+
],
19+
"type": "png",
20+
"width": 8,
21+
},
22+
"type": "foo",
23+
},
24+
"TypeScript": Object {
25+
"B": [Function],
26+
"test": true,
27+
"type": "TypeScript",
28+
},
29+
}
30+
`;
31+
32+
exports[`Metro development server serves bundles via HTTP should serve production bundles 1`] = `
433
Object {
534
"Bar": Object {
635
"foo": "foo",

packages/metro/src/integration_tests/__tests__/server-test.js

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,55 @@ jest.unmock('cosmiconfig');
1919

2020
jasmine.DEFAULT_TIMEOUT_INTERVAL = 60 * 1000;
2121

22-
it('should create a server', async () => {
23-
const config = await Metro.loadConfig({
24-
config: require.resolve('../metro.config.js'),
25-
});
26-
27-
const httpServer = await Metro.runServer(config, {
28-
reporter: {update() {}},
29-
});
22+
describe('Metro development server serves bundles via HTTP', () => {
23+
let config;
24+
let httpServer;
3025

31-
try {
26+
async function downloadAndExec(path: string): mixed {
3227
const response = await fetch(
33-
`http://localhost:${
34-
config.server.port
35-
}/TestBundle.bundle?platform=ios&dev=false&minify=true`,
28+
'http://localhost:' + config.server.port + path,
3629
);
3730

3831
const body = await response.text();
3932

4033
if (!response.ok) {
4134
console.error(body);
35+
4236
throw new Error(
4337
'Metro server responded with status code: ' + response.status,
4438
);
4539
}
4640

47-
expect(execBundle(body)).toMatchSnapshot();
48-
} finally {
49-
httpServer.close();
41+
return execBundle(body);
5042
}
43+
44+
beforeEach(async () => {
45+
config = await Metro.loadConfig({
46+
config: require.resolve('../metro.config.js'),
47+
});
48+
49+
httpServer = await Metro.runServer(config, {
50+
reporter: {update() {}},
51+
});
52+
});
53+
54+
afterEach(() => {
55+
httpServer.close();
56+
});
57+
58+
it('should serve deveopment bundles', async () => {
59+
expect(
60+
await downloadAndExec(
61+
'/TestBundle.bundle?platform=ios&dev=true&minify=false',
62+
),
63+
).toMatchSnapshot();
64+
});
65+
66+
it('should serve production bundles', async () => {
67+
expect(
68+
await downloadAndExec(
69+
'/TestBundle.bundle?platform=ios&dev=false&minify=true',
70+
),
71+
).toMatchSnapshot();
72+
});
5173
});

packages/metro/src/reactNativeTransformer.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ function buildBabelConfig(filename, options, plugins?: BabelPlugins = []) {
129129
config.plugins = extraPlugins.concat(config.plugins, plugins);
130130

131131
if (options.dev && options.hot) {
132-
const hmrConfig = makeHMRConfig(options, filename);
132+
const hmrConfig = makeHMRConfig(
133+
options,
134+
path.resolve(options.projectRoot, filename),
135+
);
133136
config = Object.assign({}, config, hmrConfig);
134137
}
135138

0 commit comments

Comments
 (0)