diff --git a/react-packager/src/Resolver/__tests__/Resolver-test.js b/react-packager/src/Resolver/__tests__/Resolver-test.js index 17a6de6e..80f70de8 100644 --- a/react-packager/src/Resolver/__tests__/Resolver-test.js +++ b/react-packager/src/Resolver/__tests__/Resolver-test.js @@ -366,19 +366,19 @@ describe('Resolver', function() { dev: false, }).then(({code: processedCode}) => { expect(processedCode).toEqual([ - `__d(${resolutionResponse.getModuleId(module)} /* test module */, function(global, require, module, exports) {` + + '__d(/* test module */function(global, require, module, exports) {' + // require `require(${moduleIds.get('x')} /* x */)`, `require(${moduleIds.get('y')} /* y */)`, 'require( \'z\' )', 'require( "a")', 'require("b" )', - '});', + `}, ${resolutionResponse.getModuleId(module)});`, ].join('\n')); }); }); - pit('should add module transport names as third argument to `__d`', () => { + pit('should add module transport names as fourth argument to `__d`', () => { const module = createModule('test module'); const code = 'arbitrary(code)' const resolutionResponse = new ResolutionResponseMock({ @@ -393,9 +393,9 @@ describe('Resolver', function() { dev: true, }).then(({code: processedCode}) => expect(processedCode).toEqual([ - `__d(${resolutionResponse.getModuleId(module)} /* test module */, function(global, require, module, exports) {` + + '__d(/* test module */function(global, require, module, exports) {' + code, - '}, "test module");' + `}, ${resolutionResponse.getModuleId(module)}, null, "test module");` ].join('\n')) ); }); @@ -455,8 +455,8 @@ describe('Resolver', function() { .wrapModule({resolutionResponse, module, name: id, code, dev: false}) .then(({code: processedCode}) => expect(processedCode).toEqual([ - `__d(${resolutionResponse.getModuleId(module)} /* ${id} */, function(global, require, module, exports) {`, - `module.exports = ${code}\n});`, + `__d(/* ${id} */function(global, require, module, exports) {`, + `module.exports = ${code}\n}, ${resolutionResponse.getModuleId(module)});`, ].join(''))); }); }); @@ -483,7 +483,9 @@ describe('Resolver', function() { }); pit('should invoke the minifier with the wrapped code', () => { - const wrappedCode = `__d(${resolutionResponse.getModuleId(module)} /* ${id} */, function(global, require, module, exports) {${code}\n});` + const wrappedCode = + `__d(/* ${id} */function(global, require, module, exports) {${ + code}\n}, ${resolutionResponse.getModuleId(module)});` return depResolver .wrapModule({ resolutionResponse, diff --git a/react-packager/src/Resolver/index.js b/react-packager/src/Resolver/index.js index 40a00d9e..1e8f4f49 100644 --- a/react-packager/src/Resolver/index.js +++ b/react-packager/src/Resolver/index.js @@ -259,12 +259,12 @@ class Resolver { function defineModuleCode(moduleName, code, verboseName = '', dev = true) { return [ - '__d(', - `${JSON.stringify(moduleName)} /* ${verboseName} */, `, - 'function(global, require, module, exports) {', + `__d(/* ${verboseName} */`, + 'function(global, require, module, exports) {', // module factory code, - '\n}', - dev ? `, ${JSON.stringify(verboseName)}` : '', + '\n}, ', + `${JSON.stringify(moduleName)}`, // module id, null = id map. used in ModuleGraph + dev ? `, null, ${JSON.stringify(verboseName)}` : '', ');', ].join(''); } diff --git a/react-packager/src/Resolver/polyfills/require.js b/react-packager/src/Resolver/polyfills/require.js index 88fdbb86..fb2fe0e7 100644 --- a/react-packager/src/Resolver/polyfills/require.js +++ b/react-packager/src/Resolver/polyfills/require.js @@ -11,12 +11,14 @@ 'use strict'; +type DependencyMap = Array; type Exports = any; type FactoryFn = ( global: Object, require: RequireFn, moduleObject: {exports: {}}, exports: {}, + dependencyMap: ?DependencyMap, ) => void; type HotModuleReloadingAcceptFn = Function; type HotModuleReloadingData = {| @@ -29,12 +31,13 @@ type Module = { }; type ModuleID = number; type ModuleDefinition = {| + dependencyMap: ?DependencyMap, + exports: Exports, factory: FactoryFn, hasError: boolean, - isInitialized: boolean, - exports: Exports, - verboseName?: string, hot?: HotModuleReloadingData, + isInitialized: boolean, + verboseName?: string, |}; type ModuleMap = {[key: ModuleID]: (ModuleDefinition)}; @@ -50,8 +53,9 @@ if (__DEV__) { } function define( - moduleId: number, factory: FactoryFn, + moduleId: number, + dependencyMap?: DependencyMap, ) { if (moduleId in modules) { // prevent repeated calls to `global.nativeRequire` to overwrite modules @@ -59,18 +63,20 @@ function define( return; } modules[moduleId] = { + dependencyMap, + exports: undefined, factory, hasError: false, isInitialized: false, - exports: undefined, }; if (__DEV__) { // HMR modules[moduleId].hot = createHotReloadingObject(); // DEBUGGABLE MODULES NAMES - // avoid unnecessary parameter in prod - const verboseName: string | void = arguments[2]; + // we take `verboseName` from `arguments` to avoid an unused named parameter + // in `define` in production. + const verboseName: string | void = arguments[3]; if (verboseName) { modules[moduleId].verboseName = verboseName; verboseNamesToModuleIds[verboseName] = moduleId; @@ -90,14 +96,14 @@ function require(moduleId: ModuleID | VerboseModuleNameForDev) { 'debugging purposes and will BREAK IN PRODUCTION!' ); } - } else { - moduleId = +moduleId; } - const module = modules[moduleId]; + //$FlowFixMe: at this point we know that moduleId is a number + const moduleIdReallyIsNumber: number = moduleId; + const module = modules[moduleIdReallyIsNumber]; return module && module.isInitialized ? module.exports - : guardedLoadModule(moduleId, module); + : guardedLoadModule(moduleIdReallyIsNumber, module); } let inGuard = false; @@ -146,7 +152,7 @@ function loadModuleImplementation(moduleId, module) { // infinite require loop. module.isInitialized = true; const exports = module.exports = {}; - const {factory} = module; + const {factory, dependencyMap} = module; try { if (__DEV__) { // $FlowFixMe: we know that __DEV__ is const and `Systrace` exists @@ -161,7 +167,7 @@ function loadModuleImplementation(moduleId, module) { // keep args in sync with with defineModuleCode in // packager/react-packager/src/Resolver/index.js // and packager/react-packager/src/ModuleGraph/worker.js - factory(global, require, moduleObject, exports); + factory(global, require, moduleObject, exports, dependencyMap); // avoid removing factory in DEV mode as it breaks HMR if (!__DEV__) { @@ -239,7 +245,7 @@ if (__DEV__) { const mod = modules[id]; if (!mod && factory) { // new modules need a factory - define(id, factory); + define(factory, id); return true; // new modules don't need to be accepted }