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
This commit is contained in:
Sebastian Markbage 2015-11-25 17:34:55 -08:00 committed by facebook-github-bot-9
parent e88322d3a8
commit df5d178115
4 changed files with 39 additions and 33 deletions

View File

@ -46,6 +46,7 @@ describe('Bundler', function() {
} }
var getDependencies; var getDependencies;
var getModuleSystemDependencies;
var wrapModule; var wrapModule;
var bundler; var bundler;
var assetServer; var assetServer;
@ -53,10 +54,12 @@ describe('Bundler', function() {
beforeEach(function() { beforeEach(function() {
getDependencies = jest.genMockFn(); getDependencies = jest.genMockFn();
getModuleSystemDependencies = jest.genMockFn();
wrapModule = jest.genMockFn(); wrapModule = jest.genMockFn();
Resolver.mockImpl(function() { Resolver.mockImpl(function() {
return { return {
getDependencies: getDependencies, getDependencies: getDependencies,
getModuleSystemDependencies: getModuleSystemDependencies,
wrapModule: wrapModule, wrapModule: wrapModule,
}; };
}); });
@ -112,6 +115,10 @@ describe('Bundler', function() {
}); });
}); });
getModuleSystemDependencies.mockImpl(function() {
return [];
});
JSTransformer.prototype.loadFileAndTransform JSTransformer.prototype.loadFileAndTransform
.mockImpl(function(path) { .mockImpl(function(path) {
return Promise.resolve({ return Promise.resolve({

View File

@ -146,23 +146,30 @@ class Bundler {
const findEventId = Activity.startEvent('find dependencies'); const findEventId = Activity.startEvent('find dependencies');
let transformEventId; let transformEventId;
const moduleSystem = this._resolver.getModuleSystemDependencies(
{ dev: isDev, platform }
);
return this.getDependencies(entryFile, isDev, platform).then((response) => { return this.getDependencies(entryFile, isDev, platform).then((response) => {
Activity.endEvent(findEventId); Activity.endEvent(findEventId);
transformEventId = Activity.startEvent('transform'); transformEventId = Activity.startEvent('transform');
// Prepend the module system polyfill to the top of dependencies
var dependencies = moduleSystem.concat(response.dependencies);
let bar; let bar;
if (process.stdout.isTTY) { if (process.stdout.isTTY) {
bar = new ProgressBar('transforming [:bar] :percent :current/:total', { bar = new ProgressBar('transforming [:bar] :percent :current/:total', {
complete: '=', complete: '=',
incomplete: ' ', incomplete: ' ',
width: 40, width: 40,
total: response.dependencies.length, total: dependencies.length,
}); });
} }
bbundle.setMainModuleId(response.mainModuleId); bbundle.setMainModuleId(response.mainModuleId);
return Promise.all( return Promise.all(
response.dependencies.map( dependencies.map(
module => this._transformModule( module => this._transformModule(
bbundle, bbundle,
response, response,

View File

@ -79,27 +79,15 @@ describe('Resolver', function() {
expect(result.mainModuleId).toEqual('index'); expect(result.mainModuleId).toEqual('index');
expect(result.dependencies[result.dependencies.length - 1]).toBe(module); expect(result.dependencies[result.dependencies.length - 1]).toBe(module);
expect(_.pluck(Polyfill.mock.calls, 0)).toEqual([ 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', { path: 'polyfills/polyfills.js',
id: 'polyfills/polyfills.js', id: 'polyfills/polyfills.js',
isPolyfill: true, isPolyfill: true,
dependencies: ['polyfills/prelude.js', 'polyfills/require.js'] dependencies: []
}, },
{ id: 'polyfills/console.js', { id: 'polyfills/console.js',
isPolyfill: true, isPolyfill: true,
path: 'polyfills/console.js', path: 'polyfills/console.js',
dependencies: [ dependencies: [
'polyfills/prelude.js',
'polyfills/require.js',
'polyfills/polyfills.js' 'polyfills/polyfills.js'
], ],
}, },
@ -107,8 +95,6 @@ describe('Resolver', function() {
isPolyfill: true, isPolyfill: true,
path: 'polyfills/error-guard.js', path: 'polyfills/error-guard.js',
dependencies: [ dependencies: [
'polyfills/prelude.js',
'polyfills/require.js',
'polyfills/polyfills.js', 'polyfills/polyfills.js',
'polyfills/console.js' 'polyfills/console.js'
], ],
@ -117,8 +103,6 @@ describe('Resolver', function() {
isPolyfill: true, isPolyfill: true,
path: 'polyfills/String.prototype.es6.js', path: 'polyfills/String.prototype.es6.js',
dependencies: [ dependencies: [
'polyfills/prelude.js',
'polyfills/require.js',
'polyfills/polyfills.js', 'polyfills/polyfills.js',
'polyfills/console.js', 'polyfills/console.js',
'polyfills/error-guard.js' 'polyfills/error-guard.js'
@ -128,8 +112,6 @@ describe('Resolver', function() {
isPolyfill: true, isPolyfill: true,
path: 'polyfills/Array.prototype.es6.js', path: 'polyfills/Array.prototype.es6.js',
dependencies: [ dependencies: [
'polyfills/prelude.js',
'polyfills/require.js',
'polyfills/polyfills.js', 'polyfills/polyfills.js',
'polyfills/console.js', 'polyfills/console.js',
'polyfills/error-guard.js', 'polyfills/error-guard.js',
@ -140,8 +122,6 @@ describe('Resolver', function() {
isPolyfill: true, isPolyfill: true,
path: 'polyfills/Array.es6.js', path: 'polyfills/Array.es6.js',
dependencies: [ dependencies: [
'polyfills/prelude.js',
'polyfills/require.js',
'polyfills/polyfills.js', 'polyfills/polyfills.js',
'polyfills/console.js', 'polyfills/console.js',
'polyfills/error-guard.js', 'polyfills/error-guard.js',
@ -153,8 +133,6 @@ describe('Resolver', function() {
isPolyfill: true, isPolyfill: true,
path: 'polyfills/babelHelpers.js', path: 'polyfills/babelHelpers.js',
dependencies: [ dependencies: [
'polyfills/prelude.js',
'polyfills/require.js',
'polyfills/polyfills.js', 'polyfills/polyfills.js',
'polyfills/console.js', 'polyfills/console.js',
'polyfills/error-guard.js', 'polyfills/error-guard.js',
@ -218,8 +196,6 @@ describe('Resolver', function() {
id: 'some module', id: 'some module',
isPolyfill: true, isPolyfill: true,
dependencies: [ dependencies: [
'polyfills/prelude.js',
'polyfills/require.js',
'polyfills/polyfills.js', 'polyfills/polyfills.js',
'polyfills/console.js', 'polyfills/console.js',
'polyfills/error-guard.js', 'polyfills/error-guard.js',

View File

@ -99,7 +99,7 @@ class Resolver {
return this._depGraph.getDependencies(main, opts.platform).then( return this._depGraph.getDependencies(main, opts.platform).then(
resolutionResponse => { resolutionResponse => {
this._getPolyfillDependencies(opts.dev).reverse().forEach( this._getPolyfillDependencies().reverse().forEach(
polyfill => resolutionResponse.prependDependency(polyfill) polyfill => resolutionResponse.prependDependency(polyfill)
); );
@ -108,12 +108,28 @@ class Resolver {
); );
} }
_getPolyfillDependencies(isDev) { getModuleSystemDependencies(options) {
const polyfillModuleNames = [ const opts = getDependenciesValidateOpts(options);
isDev
const prelude = opts.dev
? path.join(__dirname, 'polyfills/prelude_dev.js') ? path.join(__dirname, 'polyfills/prelude_dev.js')
: path.join(__dirname, 'polyfills/prelude.js'), : path.join(__dirname, 'polyfills/prelude.js');
path.join(__dirname, 'polyfills/require.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/polyfills.js'),
path.join(__dirname, 'polyfills/console.js'), path.join(__dirname, 'polyfills/console.js'),
path.join(__dirname, 'polyfills/error-guard.js'), path.join(__dirname, 'polyfills/error-guard.js'),