-
Notifications
You must be signed in to change notification settings - Fork 24.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[react-packager] Introduce
require.ensure
Summary: This is the first step to add support for splitting the JS bundle into multiple ones. This diff adds support for keeping track of the async dependencies each module has. To do so we introduce the following syntax: require.ensure(['dep1', 'dep2, ..., 'depN'], callback); Where the callback function is asynchronously invoked once all the indicated modules are loaded. Internally, the packager keeps track of every set of async dependencies a module has. So for instance if a module looks like this: require.ensure(['dep1'], () => {...}); require.ensure(['dep2'], () => {...}); the `Module` object will keep track of each set of dependencies separately (because we might want to put them on separate bundles).
- Loading branch information
1 parent
309326d
commit cfcf604
Showing
8 changed files
with
227 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
packager/react-packager/src/DependencyResolver/__tests__/Module-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
'use strict'; | ||
|
||
jest | ||
.dontMock('absolute-path') | ||
.dontMock('../fastfs') | ||
.dontMock('../replacePatterns') | ||
.dontMock('../DependencyGraph/docblock') | ||
.dontMock('../../FileWatcher') | ||
.dontMock('../Module'); | ||
|
||
jest | ||
.mock('fs'); | ||
|
||
describe('Module', () => { | ||
var Fastfs; | ||
var Module; | ||
var ModuleCache; | ||
var Promise; | ||
var fs; | ||
|
||
const FileWatcher = require('../../FileWatcher'); | ||
const fileWatcher = new FileWatcher(['/root']); | ||
|
||
beforeEach(function() { | ||
Fastfs = require('../fastfs'); | ||
Module = require('../Module'); | ||
ModuleCache = require('../ModuleCache'); | ||
Promise = require('promise'); | ||
fs = require('fs'); | ||
}); | ||
|
||
describe('Async Dependencies', () => { | ||
function expectAsyncDependenciesToEqual(expected) { | ||
var fastfs = new Fastfs( | ||
['/root'], | ||
fileWatcher, | ||
{crawling: Promise.resolve(['/root/index.js']), ignore: []}, | ||
); | ||
|
||
return fastfs.build().then(() => { | ||
var module = new Module('/root/index.js', fastfs, new ModuleCache(fastfs)); | ||
|
||
return module.getAsyncDependencies().then(actual => | ||
expect(actual).toEqual(expected) | ||
); | ||
}); | ||
} | ||
|
||
pit('should recognize single dependency', () => { | ||
fs.__setMockFilesystem({ | ||
'root': { | ||
'index.js': 'require.ensure(["dep1"], function() {});', | ||
} | ||
}); | ||
|
||
return expectAsyncDependenciesToEqual([['dep1']]); | ||
}); | ||
|
||
pit('should parse single quoted dependencies', () => { | ||
fs.__setMockFilesystem({ | ||
'root': { | ||
'index.js': 'require.ensure([\'dep1\'], function() {});', | ||
} | ||
}); | ||
|
||
return expectAsyncDependenciesToEqual([['dep1']]); | ||
}); | ||
|
||
pit('should recognize multiple dependencies on the same statement', () => { | ||
fs.__setMockFilesystem({ | ||
'root': { | ||
'index.js': 'require.ensure(["dep1", "dep2"], function() {});', | ||
} | ||
}); | ||
|
||
return expectAsyncDependenciesToEqual([['dep1', 'dep2']]); | ||
}); | ||
|
||
pit('should group async dependencies', () => { | ||
fs.__setMockFilesystem({ | ||
'root': { | ||
'index.js': [ | ||
'require.ensure(["dep1", "dep2"], function() {});', | ||
'require.ensure(["dep3", "dep4"], function() {});', | ||
].join('\n'), | ||
} | ||
}); | ||
|
||
return expectAsyncDependenciesToEqual([ | ||
['dep1', 'dep2'], | ||
['dep3', 'dep4'] | ||
]); | ||
}); | ||
|
||
pit('shouldn\'t throw with ES6 arrow functions', () => { | ||
fs.__setMockFilesystem({ | ||
'root': { | ||
'index.js': 'require.ensure(["dep1", "dep2"], () => {});', | ||
} | ||
}); | ||
|
||
return expectAsyncDependenciesToEqual([['dep1', 'dep2']]); | ||
}); | ||
|
||
pit('parse fine new lines', () => { | ||
fs.__setMockFilesystem({ | ||
'root': { | ||
'index.js': 'require.ensure(["dep1", \n"dep2"], () => {});', | ||
} | ||
}); | ||
|
||
return expectAsyncDependenciesToEqual([['dep1', 'dep2']]); | ||
}); | ||
|
||
pit('ignore comments', () => { | ||
fs.__setMockFilesystem({ | ||
'root': { | ||
'index.js': 'require.ensure(["dep1", /*comment*/"dep2"], () => {});', | ||
} | ||
}); | ||
|
||
return expectAsyncDependenciesToEqual([['dep1', 'dep2']]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters