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 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({

View File

@ -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,

View File

@ -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',

View File

@ -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'),