From 06643674603227c573b717486cf0d27f889c6024 Mon Sep 17 00:00:00 2001 From: Jia Li Date: Thu, 2 Nov 2017 11:48:49 -0700 Subject: [PATCH] Revert D6210177: react-native: BundleSegments Differential Revision: D6210177 fbshipit-source-id: 8cb4ee37cd03698e9f3980a9819269086a9d6eab --- .../src/JSTransformer/worker/index.js | 12 +-- .../__tests__/collect-dependencies-test.js | 22 +++--- .../worker/collect-dependencies.js | 74 ++++++------------- .../src/Resolver/polyfills/require.js | 14 +--- .../__snapshots__/basic_bundle-test.js.snap | 30 ++------ 5 files changed, 44 insertions(+), 108 deletions(-) diff --git a/packages/metro-bundler/src/JSTransformer/worker/index.js b/packages/metro-bundler/src/JSTransformer/worker/index.js index f995a7d8..505628aa 100644 --- a/packages/metro-bundler/src/JSTransformer/worker/index.js +++ b/packages/metro-bundler/src/JSTransformer/worker/index.js @@ -36,21 +36,13 @@ export type TransformedCode = { map?: ?CompactRawMappings, }; -export type TransformArgs = {| +export type Transform = ({| filename: string, localPath: string, options: ExtraOptions & TransformOptions, plugins?: BabelPlugins, src: string, -|}; -export type TransformResults = { - ast: ?Ast, - code: string, - map: ?MappingsMap | RawMappings, -}; -export type Transform = ( - TransformArgs, -) => TransformResults; +|}) => {ast: ?Ast, code: string, map: ?MappingsMap | RawMappings}; export type Transformer = { transform: Transform, diff --git a/packages/metro-bundler/src/ModuleGraph/worker/__tests__/collect-dependencies-test.js b/packages/metro-bundler/src/ModuleGraph/worker/__tests__/collect-dependencies-test.js index 156d1401..b0bf1c50 100644 --- a/packages/metro-bundler/src/ModuleGraph/worker/__tests__/collect-dependencies-test.js +++ b/packages/metro-bundler/src/ModuleGraph/worker/__tests__/collect-dependencies-test.js @@ -13,16 +13,15 @@ 'use strict'; -const babylon = require('babylon'); const collectDependencies = require('../collect-dependencies'); - +const astFromCode = require('babylon').parse; const {codeFromAst, comparableCode} = require('../../test-helpers'); const {any} = expect; const {InvalidRequireCallError} = collectDependencies; -describe('dependency collection from ASTs', () => { +describe('dependency collection from ASTs:', () => { it('collects dependency identifiers from the code', () => { const ast = astFromCode(` const a = require('b/lib/a'); @@ -43,14 +42,13 @@ describe('dependency collection from ASTs', () => { const ast = astFromCode(` const a = require('b/lib/a'); if (!something) { - import("some/async/module").then(foo => {}); + require.async("some/async/module").then(foo => {}); } `); expect(collectDependencies(ast).dependencies).toEqual([ 'b/lib/a', 'some/async/module', - 'BundleSegments', ]); }); @@ -109,7 +107,7 @@ describe('dependency collection from ASTs', () => { }); }); -describe('Dependency collection from optimized ASTs', () => { +describe('Dependency collection from optimized ASTs:', () => { const dependencyMapName = 'arbitrary'; const {forOptimization} = collectDependencies; let ast, names; @@ -118,11 +116,12 @@ describe('Dependency collection from optimized ASTs', () => { ast = astFromCode(` const a = require(${dependencyMapName}[0], 'b/lib/a'); exports.do = () => require(${dependencyMapName}[1], "do"); + require.async(${dependencyMapName}[2], 'some/async/module').then(foo => {}); if (!something) { - require(${dependencyMapName}[2], "setup/something"); + require(${dependencyMapName}[3], "setup/something"); } `); - names = ['b/lib/a', 'do', 'setup/something']; + names = ['b/lib/a', 'do', 'some/async/module', 'setup/something']; }); it('passes the `dependencyMapName` through', () => { @@ -147,14 +146,11 @@ describe('Dependency collection from optimized ASTs', () => { comparableCode(` const a = require(${dependencyMapName}[0]); exports.do = () => require(${dependencyMapName}[1]); + require.async(${dependencyMapName}[2]).then(foo => {}); if (!something) { - require(${dependencyMapName}[2]); + require(${dependencyMapName}[3]); } `), ); }); }); - -function astFromCode(code) { - return babylon.parse(code, {plugins: ['dynamicImport']}); -} diff --git a/packages/metro-bundler/src/ModuleGraph/worker/collect-dependencies.js b/packages/metro-bundler/src/ModuleGraph/worker/collect-dependencies.js index 86aaa795..6b7c8efd 100644 --- a/packages/metro-bundler/src/ModuleGraph/worker/collect-dependencies.js +++ b/packages/metro-bundler/src/ModuleGraph/worker/collect-dependencies.js @@ -12,7 +12,6 @@ 'use strict'; -const babelTemplate = require('babel-template'); const nullthrows = require('fbjs/lib/nullthrows'); const {traverse, types} = require('babel-core'); @@ -23,7 +22,6 @@ type AST = Object; class Replacement { nameToIndex: Map; nextIndex: number; - replaceImports = true; constructor() { this.nameToIndex = new Map(); @@ -76,7 +74,6 @@ function getInvalidProdRequireMessage(node) { class ProdReplacement { replacement: Replacement; names: Array; - replaceImports = false; constructor(names) { this.replacement = new Replacement(); @@ -132,15 +129,19 @@ function createMapLookup(dependencyMapIdentifier, propertyIdentifier) { ); } -const makeAsyncRequire = babelTemplate( - `require(BUNDLE_SEGMENTS_PATH).loadForModule(MODULE_ID).then( - () => require(MODULE_PATH), - )`, -); - function collectDependencies(ast, replacement, dependencyMapIdentifier) { const visited = new WeakSet(); const traversalState = {dependencyMapIdentifier}; + function processRequireCall(node, state, isAsync) { + const arg = replacement.getRequireCallArg(node); + const index = replacement.getIndex(arg); + node.arguments = replacement.makeArgs( + types.numericLiteral(index), + arg, + state.dependencyMapIdentifier, + ); + visited.add(node); + } traverse( ast, { @@ -151,27 +152,16 @@ function collectDependencies(ast, replacement, dependencyMapIdentifier) { ); } }, - CallExpression(path, state) { const node = path.node; - if (replacement.replaceImports && node.callee.type === 'Import') { - processImportCall(path, node, replacement, state); - return; - } if (visited.has(node)) { return; } - if (!isRequireCall(node.callee)) { - return; + if (isRequireCall(node.callee)) { + processRequireCall(node, state, false); + } else if (isAsyncRequireCall(node.callee)) { + processRequireCall(node, state, true); } - const arg = replacement.getRequireCallArg(node); - const index = replacement.getIndex(arg); - node.arguments = replacement.makeArgs( - types.numericLiteral(index), - arg, - state.dependencyMapIdentifier, - ); - visited.add(node); }, }, null, @@ -184,31 +174,6 @@ function collectDependencies(ast, replacement, dependencyMapIdentifier) { }; } -function processImportCall(path, node, replacement, state) { - const args = node.arguments; - if (args.length !== 1 || !isLiteralString(args[0])) { - throw new InvalidRequireCallError( - 'Calls to import() expect exactly 1 string literal argument, ' + - 'but this was found: ' + - prettyPrint(node).code, - ); - } - const modulePath = args[0]; - const index = replacement.getIndex(modulePath); - const newImport = makeAsyncRequire({ - MODULE_PATH: modulePath, - MODULE_ID: createMapLookup( - state.dependencyMapIdentifier, - types.numericLiteral(index), - ), - BUNDLE_SEGMENTS_PATH: { - type: 'StringLiteral', - value: 'BundleSegments', - }, - }); - path.replaceWith(newImport); -} - function isLiteralString(node) { return ( node.type === 'StringLiteral' || @@ -220,6 +185,15 @@ function isRequireCall(callee) { return callee.type === 'Identifier' && callee.name === 'require'; } +function isAsyncRequireCall(callee) { + return ( + callee.type === 'MemberExpression' && + !callee.computed && + callee.property.name === 'async' && + isRequireCall(callee.object) + ); +} + class InvalidRequireCallError extends Error { constructor(message) { super(message); @@ -227,7 +201,7 @@ class InvalidRequireCallError extends Error { } const xp = (module.exports = (ast: AST) => - collectDependencies(ast, new Replacement(), undefined)); + collectDependencies(ast, new Replacement())); xp.forOptimization = ( ast: AST, diff --git a/packages/metro-bundler/src/Resolver/polyfills/require.js b/packages/metro-bundler/src/Resolver/polyfills/require.js index b6b2e8b1..b3b8ca1d 100644 --- a/packages/metro-bundler/src/Resolver/polyfills/require.js +++ b/packages/metro-bundler/src/Resolver/polyfills/require.js @@ -138,20 +138,12 @@ function guardedLoadModule(moduleId: ModuleID, module) { const ID_MASK_SHIFT = 16; const LOCAL_ID_MASK = ~0 >>> ID_MASK_SHIFT; -function unpackModuleId( - moduleId: ModuleID, -): {segmentId: number, localId: number} { - const segmentId = moduleId >>> ID_MASK_SHIFT; - const localId = moduleId & LOCAL_ID_MASK; - return {segmentId, localId}; -} -require.unpackModuleId = unpackModuleId; - function loadModuleImplementation(moduleId, module) { const nativeRequire = global.nativeRequire; if (!module && nativeRequire) { - const {segmentId, localId} = unpackModuleId(moduleId); - nativeRequire(localId, segmentId); + const bundleId = moduleId >>> ID_MASK_SHIFT; + const localId = moduleId & LOCAL_ID_MASK; + nativeRequire(localId, bundleId); module = modules[moduleId]; } diff --git a/packages/metro-bundler/src/integration_tests/__tests__/__snapshots__/basic_bundle-test.js.snap b/packages/metro-bundler/src/integration_tests/__tests__/__snapshots__/basic_bundle-test.js.snap index 9eadbbb5..82d46f26 100644 --- a/packages/metro-bundler/src/integration_tests/__tests__/__snapshots__/basic_bundle-test.js.snap +++ b/packages/metro-bundler/src/integration_tests/__tests__/__snapshots__/basic_bundle-test.js.snap @@ -56,21 +56,12 @@ function guardedLoadModule(moduleId, module) { var ID_MASK_SHIFT = 16; var LOCAL_ID_MASK = ~0 >>> ID_MASK_SHIFT; -function unpackModuleId(moduleId) { - var segmentId = moduleId >>> ID_MASK_SHIFT; - var localId = moduleId & LOCAL_ID_MASK; - return { segmentId: segmentId, localId: localId }; -} -_require.unpackModuleId = unpackModuleId; - function loadModuleImplementation(moduleId, module) { var nativeRequire = global.nativeRequire; if (!module && nativeRequire) { - var _unpackModuleId = unpackModuleId(moduleId), - segmentId = _unpackModuleId.segmentId, - localId = _unpackModuleId.localId; - - nativeRequire(localId, segmentId); + var bundleId = moduleId >>> ID_MASK_SHIFT; + var localId = moduleId & LOCAL_ID_MASK; + nativeRequire(localId, bundleId); module = modules[moduleId]; } @@ -226,21 +217,12 @@ function guardedLoadModule(moduleId, module) { var ID_MASK_SHIFT = 16; var LOCAL_ID_MASK = ~0 >>> ID_MASK_SHIFT; -function unpackModuleId(moduleId) { - var segmentId = moduleId >>> ID_MASK_SHIFT; - var localId = moduleId & LOCAL_ID_MASK; - return { segmentId: segmentId, localId: localId }; -} -_require.unpackModuleId = unpackModuleId; - function loadModuleImplementation(moduleId, module) { var nativeRequire = global.nativeRequire; if (!module && nativeRequire) { - var _unpackModuleId = unpackModuleId(moduleId), - segmentId = _unpackModuleId.segmentId, - localId = _unpackModuleId.localId; - - nativeRequire(localId, segmentId); + var bundleId = moduleId >>> ID_MASK_SHIFT; + var localId = moduleId & LOCAL_ID_MASK; + nativeRequire(localId, bundleId); module = modules[moduleId]; }