From f434d43f543caf6b06aae53d020fbf86241c0f90 Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Thu, 23 Nov 2017 12:02:25 -0800 Subject: [PATCH] metro-bundler: RAM bundles cannot contain scripts masquerading as modules Reviewed By: davidaurelio Differential Revision: D6405614 fbshipit-source-id: 6c21fd30d6246f50c34e9f23a193452737a3b67b --- .../output/__tests__/indexed-ram-bundle-test.js | 2 +- .../output/__tests__/multiple-files-ram-bundle-test.js | 2 +- .../src/ModuleGraph/output/indexed-ram-bundle.js | 7 +++++++ packages/metro-bundler/src/ModuleGraph/output/util.js | 3 ++- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/metro-bundler/src/ModuleGraph/output/__tests__/indexed-ram-bundle-test.js b/packages/metro-bundler/src/ModuleGraph/output/__tests__/indexed-ram-bundle-test.js index 64f6b6ad..ab768c23 100644 --- a/packages/metro-bundler/src/ModuleGraph/output/__tests__/indexed-ram-bundle-test.js +++ b/packages/metro-bundler/src/ModuleGraph/output/__tests__/indexed-ram-bundle-test.js @@ -34,7 +34,7 @@ const idsForPath = ({path}) => { beforeAll(() => { modules = [ - makeModule('a', [], 'script'), + makeModule('a', []), makeModule('b', ['c']), makeModule('c', ['f']), makeModule('d', ['e']), diff --git a/packages/metro-bundler/src/ModuleGraph/output/__tests__/multiple-files-ram-bundle-test.js b/packages/metro-bundler/src/ModuleGraph/output/__tests__/multiple-files-ram-bundle-test.js index db008437..8c78bf5d 100644 --- a/packages/metro-bundler/src/ModuleGraph/output/__tests__/multiple-files-ram-bundle-test.js +++ b/packages/metro-bundler/src/ModuleGraph/output/__tests__/multiple-files-ram-bundle-test.js @@ -35,7 +35,7 @@ const idsForPath = ({path}) => { beforeAll(() => { modules = [ - makeModule('a', [], 'script'), + makeModule('a', []), makeModule('b'), makeModule('c', ['f']), makeModule('d', ['e']), diff --git a/packages/metro-bundler/src/ModuleGraph/output/indexed-ram-bundle.js b/packages/metro-bundler/src/ModuleGraph/output/indexed-ram-bundle.js index e2b3b8a3..bcf54b38 100644 --- a/packages/metro-bundler/src/ModuleGraph/output/indexed-ram-bundle.js +++ b/packages/metro-bundler/src/ModuleGraph/output/indexed-ram-bundle.js @@ -13,6 +13,7 @@ 'use strict'; const buildSourceMapWithMetaData = require('../../shared/output/unbundle/build-unbundle-sourcemap-with-metadata.js'); +const invariant = require('invariant'); const {createRamBundleGroups} = require('../../Bundler/util'); const { @@ -36,6 +37,12 @@ function asIndexedRamBundle({ const [startup, deferred] = partition(modules, preloadedModules); const startupModules = Array.from(concat(startup, requireCalls)); const deferredModules = deferred.map(m => toModuleTransport(m, idsForPath)); + for (const m of deferredModules) { + invariant( + m.id >= 0, + 'A script (non-module) cannot be part of the deferred modules of a RAM bundle', + ); + } const ramGroups = createRamBundleGroups( ramGroupHeads || [], deferredModules, diff --git a/packages/metro-bundler/src/ModuleGraph/output/util.js b/packages/metro-bundler/src/ModuleGraph/output/util.js index d5e30648..ca2aa8cd 100644 --- a/packages/metro-bundler/src/ModuleGraph/output/util.js +++ b/packages/metro-bundler/src/ModuleGraph/output/util.js @@ -117,7 +117,8 @@ exports.toModuleTransport = (module: Module, idsForPath: IdsForPathFn) => { return { code: getModuleCode(module, x => idsForPath(x).moduleId), dependencies, - id: idsForPath(file).localId, + // ID is required but we provide an invalid one for "script"s. + id: file.type === 'module' ? idsForPath(file).localId : -1, map: file.map, name: file.path, sourcePath: file.path,