From e595178fc859263cc2d88de08650d99c52bcc0d9 Mon Sep 17 00:00:00 2001 From: Rafael Oleza Date: Fri, 5 Oct 2018 11:31:30 -0700 Subject: [PATCH] Fix HMR plugin on the OpenSource React Native transformer Summary: This fixes https://github.com/facebook/react-native/issues/21490 Reviewed By: jeanlauliac Differential Revision: D10216508 fbshipit-source-id: d857918e281e0d1c02644bb7fcbe201026d2224a --- .../__snapshots__/server-test.js.snap | 31 ++++++++++- .../__tests__/server-test.js | 52 +++++++++++++------ packages/metro/src/reactNativeTransformer.js | 5 +- 3 files changed, 71 insertions(+), 17 deletions(-) diff --git a/packages/metro/src/integration_tests/__tests__/__snapshots__/server-test.js.snap b/packages/metro/src/integration_tests/__tests__/__snapshots__/server-test.js.snap index 06ee1f6d03..98587b5f0c 100644 --- a/packages/metro/src/integration_tests/__tests__/__snapshots__/server-test.js.snap +++ b/packages/metro/src/integration_tests/__tests__/__snapshots__/server-test.js.snap @@ -1,6 +1,35 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`should create a server 1`] = ` +exports[`Metro development server serves bundles via HTTP should serve deveopment bundles 1`] = ` +Object { + "Bar": Object { + "foo": "foo", + "type": "bar", + }, + "Foo": Object { + "asset": Object { + "__packager_asset": true, + "hash": "77d45c1f7fa73c0f6c444a830dc42f67", + "height": 8, + "httpServerLocation": "/assets", + "name": "test", + "scales": Array [ + 1, + ], + "type": "png", + "width": 8, + }, + "type": "foo", + }, + "TypeScript": Object { + "B": [Function], + "test": true, + "type": "TypeScript", + }, +} +`; + +exports[`Metro development server serves bundles via HTTP should serve production bundles 1`] = ` Object { "Bar": Object { "foo": "foo", diff --git a/packages/metro/src/integration_tests/__tests__/server-test.js b/packages/metro/src/integration_tests/__tests__/server-test.js index 6601e7f594..0d670f2b39 100644 --- a/packages/metro/src/integration_tests/__tests__/server-test.js +++ b/packages/metro/src/integration_tests/__tests__/server-test.js @@ -19,33 +19,55 @@ jest.unmock('cosmiconfig'); jasmine.DEFAULT_TIMEOUT_INTERVAL = 60 * 1000; -it('should create a server', async () => { - const config = await Metro.loadConfig({ - config: require.resolve('../metro.config.js'), - }); - - const httpServer = await Metro.runServer(config, { - reporter: {update() {}}, - }); +describe('Metro development server serves bundles via HTTP', () => { + let config; + let httpServer; - try { + async function downloadAndExec(path: string): mixed { const response = await fetch( - `http://localhost:${ - config.server.port - }/TestBundle.bundle?platform=ios&dev=false&minify=true`, + 'http://localhost:' + config.server.port + path, ); const body = await response.text(); if (!response.ok) { console.error(body); + throw new Error( 'Metro server responded with status code: ' + response.status, ); } - expect(execBundle(body)).toMatchSnapshot(); - } finally { - httpServer.close(); + return execBundle(body); } + + beforeEach(async () => { + config = await Metro.loadConfig({ + config: require.resolve('../metro.config.js'), + }); + + httpServer = await Metro.runServer(config, { + reporter: {update() {}}, + }); + }); + + afterEach(() => { + httpServer.close(); + }); + + it('should serve deveopment bundles', async () => { + expect( + await downloadAndExec( + '/TestBundle.bundle?platform=ios&dev=true&minify=false', + ), + ).toMatchSnapshot(); + }); + + it('should serve production bundles', async () => { + expect( + await downloadAndExec( + '/TestBundle.bundle?platform=ios&dev=false&minify=true', + ), + ).toMatchSnapshot(); + }); }); diff --git a/packages/metro/src/reactNativeTransformer.js b/packages/metro/src/reactNativeTransformer.js index 53cbe387bc..bbbcf76bb1 100644 --- a/packages/metro/src/reactNativeTransformer.js +++ b/packages/metro/src/reactNativeTransformer.js @@ -129,7 +129,10 @@ function buildBabelConfig(filename, options, plugins?: BabelPlugins = []) { config.plugins = extraPlugins.concat(config.plugins, plugins); if (options.dev && options.hot) { - const hmrConfig = makeHMRConfig(options, filename); + const hmrConfig = makeHMRConfig( + options, + path.resolve(options.projectRoot, filename), + ); config = Object.assign({}, config, hmrConfig); }