diff --git a/react-packager/src/Bundler/__tests__/Bundler-test.js b/react-packager/src/Bundler/__tests__/Bundler-test.js index ac8494be..9ce3f7c4 100644 --- a/react-packager/src/Bundler/__tests__/Bundler-test.js +++ b/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/react-packager/src/Bundler/index.js b/react-packager/src/Bundler/index.js index 5f5d7959..e097667f 100644 --- a/react-packager/src/Bundler/index.js +++ b/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/react-packager/src/Resolver/__tests__/Resolver-test.js b/react-packager/src/Resolver/__tests__/Resolver-test.js index f5858921..bf6363c5 100644 --- a/react-packager/src/Resolver/__tests__/Resolver-test.js +++ b/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/react-packager/src/Resolver/index.js b/react-packager/src/Resolver/index.js index 60820faa..a4e546e0 100644 --- a/react-packager/src/Resolver/index.js +++ b/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'),