From 0e11379dbb2ffef1ac3dcc2a70d0014c162ba286 Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Wed, 25 Nov 2015 17:34:55 -0800 Subject: [PATCH] Extract the Require polyfill from the default list of polyfills Summary: Extracts the module system from the list of dependencies and adds it back to the bundling process. Unbundling and Prepack has their own module systems. jest does as well. This is not normally part of the resolver, nor polyfills. In fact, I think we'll eventually start treating polyfills as normal modules just like core-js. The prelude is the weird part right now but I think that we'll eventually move the DEV flag to be module level instead of global and we can just get rid of this prelude. public Reviewed By: davidaurelio Differential Revision: D2693701 fb-gh-sync-id: a59ccda0fa15fcfcda52897e8290805eed1b92b3 --- .../src/Bundler/__tests__/Bundler-test.js | 7 +++++ packager/react-packager/src/Bundler/index.js | 11 ++++++-- .../src/Resolver/__tests__/Resolver-test.js | 26 +---------------- packager/react-packager/src/Resolver/index.js | 28 +++++++++++++++---- 4 files changed, 39 insertions(+), 33 deletions(-) diff --git a/packager/react-packager/src/Bundler/__tests__/Bundler-test.js b/packager/react-packager/src/Bundler/__tests__/Bundler-test.js index ac8494bec..9ce3f7c46 100644 --- a/packager/react-packager/src/Bundler/__tests__/Bundler-test.js +++ b/packager/react-packager/src/Bundler/__tests__/Bundler-test.js @@ -46,6 +46,7 @@ describe('Bundler', function() { } var getDependencies; + var getModuleSystemDependencies; var wrapModule; var bundler; var assetServer; @@ -53,10 +54,12 @@ describe('Bundler', function() { beforeEach(function() { getDependencies = jest.genMockFn(); + getModuleSystemDependencies = jest.genMockFn(); wrapModule = jest.genMockFn(); Resolver.mockImpl(function() { return { getDependencies: getDependencies, + getModuleSystemDependencies: getModuleSystemDependencies, wrapModule: wrapModule, }; }); @@ -112,6 +115,10 @@ describe('Bundler', function() { }); }); + getModuleSystemDependencies.mockImpl(function() { + return []; + }); + JSTransformer.prototype.loadFileAndTransform .mockImpl(function(path) { return Promise.resolve({ diff --git a/packager/react-packager/src/Bundler/index.js b/packager/react-packager/src/Bundler/index.js index 5f5d79590..e097667ff 100644 --- a/packager/react-packager/src/Bundler/index.js +++ b/packager/react-packager/src/Bundler/index.js @@ -146,23 +146,30 @@ class Bundler { const findEventId = Activity.startEvent('find dependencies'); let transformEventId; + const moduleSystem = this._resolver.getModuleSystemDependencies( + { dev: isDev, platform } + ); + return this.getDependencies(entryFile, isDev, platform).then((response) => { Activity.endEvent(findEventId); transformEventId = Activity.startEvent('transform'); + // Prepend the module system polyfill to the top of dependencies + var dependencies = moduleSystem.concat(response.dependencies); + let bar; if (process.stdout.isTTY) { bar = new ProgressBar('transforming [:bar] :percent :current/:total', { complete: '=', incomplete: ' ', width: 40, - total: response.dependencies.length, + total: dependencies.length, }); } bbundle.setMainModuleId(response.mainModuleId); return Promise.all( - response.dependencies.map( + dependencies.map( module => this._transformModule( bbundle, response, diff --git a/packager/react-packager/src/Resolver/__tests__/Resolver-test.js b/packager/react-packager/src/Resolver/__tests__/Resolver-test.js index f58589215..bf6363c52 100644 --- a/packager/react-packager/src/Resolver/__tests__/Resolver-test.js +++ b/packager/react-packager/src/Resolver/__tests__/Resolver-test.js @@ -79,27 +79,15 @@ describe('Resolver', function() { expect(result.mainModuleId).toEqual('index'); expect(result.dependencies[result.dependencies.length - 1]).toBe(module); expect(_.pluck(Polyfill.mock.calls, 0)).toEqual([ - { path: 'polyfills/prelude.js', - id: 'polyfills/prelude.js', - isPolyfill: true, - dependencies: [] - }, - { path: 'polyfills/require.js', - id: 'polyfills/require.js', - isPolyfill: true, - dependencies: ['polyfills/prelude.js'] - }, { path: 'polyfills/polyfills.js', id: 'polyfills/polyfills.js', isPolyfill: true, - dependencies: ['polyfills/prelude.js', 'polyfills/require.js'] + dependencies: [] }, { id: 'polyfills/console.js', isPolyfill: true, path: 'polyfills/console.js', dependencies: [ - 'polyfills/prelude.js', - 'polyfills/require.js', 'polyfills/polyfills.js' ], }, @@ -107,8 +95,6 @@ describe('Resolver', function() { isPolyfill: true, path: 'polyfills/error-guard.js', dependencies: [ - 'polyfills/prelude.js', - 'polyfills/require.js', 'polyfills/polyfills.js', 'polyfills/console.js' ], @@ -117,8 +103,6 @@ describe('Resolver', function() { isPolyfill: true, path: 'polyfills/String.prototype.es6.js', dependencies: [ - 'polyfills/prelude.js', - 'polyfills/require.js', 'polyfills/polyfills.js', 'polyfills/console.js', 'polyfills/error-guard.js' @@ -128,8 +112,6 @@ describe('Resolver', function() { isPolyfill: true, path: 'polyfills/Array.prototype.es6.js', dependencies: [ - 'polyfills/prelude.js', - 'polyfills/require.js', 'polyfills/polyfills.js', 'polyfills/console.js', 'polyfills/error-guard.js', @@ -140,8 +122,6 @@ describe('Resolver', function() { isPolyfill: true, path: 'polyfills/Array.es6.js', dependencies: [ - 'polyfills/prelude.js', - 'polyfills/require.js', 'polyfills/polyfills.js', 'polyfills/console.js', 'polyfills/error-guard.js', @@ -153,8 +133,6 @@ describe('Resolver', function() { isPolyfill: true, path: 'polyfills/babelHelpers.js', dependencies: [ - 'polyfills/prelude.js', - 'polyfills/require.js', 'polyfills/polyfills.js', 'polyfills/console.js', 'polyfills/error-guard.js', @@ -218,8 +196,6 @@ describe('Resolver', function() { id: 'some module', isPolyfill: true, dependencies: [ - 'polyfills/prelude.js', - 'polyfills/require.js', 'polyfills/polyfills.js', 'polyfills/console.js', 'polyfills/error-guard.js', diff --git a/packager/react-packager/src/Resolver/index.js b/packager/react-packager/src/Resolver/index.js index 60820faa1..a4e546e07 100644 --- a/packager/react-packager/src/Resolver/index.js +++ b/packager/react-packager/src/Resolver/index.js @@ -99,7 +99,7 @@ class Resolver { return this._depGraph.getDependencies(main, opts.platform).then( resolutionResponse => { - this._getPolyfillDependencies(opts.dev).reverse().forEach( + this._getPolyfillDependencies().reverse().forEach( polyfill => resolutionResponse.prependDependency(polyfill) ); @@ -108,12 +108,28 @@ class Resolver { ); } - _getPolyfillDependencies(isDev) { - const polyfillModuleNames = [ - isDev + getModuleSystemDependencies(options) { + const opts = getDependenciesValidateOpts(options); + + const prelude = opts.dev ? path.join(__dirname, 'polyfills/prelude_dev.js') - : path.join(__dirname, 'polyfills/prelude.js'), - path.join(__dirname, 'polyfills/require.js'), + : path.join(__dirname, 'polyfills/prelude.js'); + + const moduleSystem = path.join(__dirname, 'polyfills/require.js'); + + return [ + prelude, + moduleSystem + ].map(moduleName => new Polyfill({ + path: moduleName, + id: moduleName, + dependencies: [], + isPolyfill: true, + })); + } + + _getPolyfillDependencies() { + const polyfillModuleNames = [ path.join(__dirname, 'polyfills/polyfills.js'), path.join(__dirname, 'polyfills/console.js'), path.join(__dirname, 'polyfills/error-guard.js'),