metro-bundler: RAM bundles cannot contain scripts masquerading as modules

Reviewed By: davidaurelio

Differential Revision: D6405614

fbshipit-source-id: 6c21fd30d6246f50c34e9f23a193452737a3b67b
This commit is contained in:
Jean Lauliac 2017-11-23 12:02:25 -08:00 committed by Facebook Github Bot
parent ccd8eae0b0
commit f434d43f54
4 changed files with 11 additions and 3 deletions

View File

@ -34,7 +34,7 @@ const idsForPath = ({path}) => {
beforeAll(() => { beforeAll(() => {
modules = [ modules = [
makeModule('a', [], 'script'), makeModule('a', []),
makeModule('b', ['c']), makeModule('b', ['c']),
makeModule('c', ['f']), makeModule('c', ['f']),
makeModule('d', ['e']), makeModule('d', ['e']),

View File

@ -35,7 +35,7 @@ const idsForPath = ({path}) => {
beforeAll(() => { beforeAll(() => {
modules = [ modules = [
makeModule('a', [], 'script'), makeModule('a', []),
makeModule('b'), makeModule('b'),
makeModule('c', ['f']), makeModule('c', ['f']),
makeModule('d', ['e']), makeModule('d', ['e']),

View File

@ -13,6 +13,7 @@
'use strict'; 'use strict';
const buildSourceMapWithMetaData = require('../../shared/output/unbundle/build-unbundle-sourcemap-with-metadata.js'); const buildSourceMapWithMetaData = require('../../shared/output/unbundle/build-unbundle-sourcemap-with-metadata.js');
const invariant = require('invariant');
const {createRamBundleGroups} = require('../../Bundler/util'); const {createRamBundleGroups} = require('../../Bundler/util');
const { const {
@ -36,6 +37,12 @@ function asIndexedRamBundle({
const [startup, deferred] = partition(modules, preloadedModules); const [startup, deferred] = partition(modules, preloadedModules);
const startupModules = Array.from(concat(startup, requireCalls)); const startupModules = Array.from(concat(startup, requireCalls));
const deferredModules = deferred.map(m => toModuleTransport(m, idsForPath)); 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( const ramGroups = createRamBundleGroups(
ramGroupHeads || [], ramGroupHeads || [],
deferredModules, deferredModules,

View File

@ -117,7 +117,8 @@ exports.toModuleTransport = (module: Module, idsForPath: IdsForPathFn) => {
return { return {
code: getModuleCode(module, x => idsForPath(x).moduleId), code: getModuleCode(module, x => idsForPath(x).moduleId),
dependencies, 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, map: file.map,
name: file.path, name: file.path,
sourcePath: file.path, sourcePath: file.path,