Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1bf7887

Browse files
Amjad Masadfacebook-github-bot-2
Amjad Masad
authored and
facebook-github-bot-2
committed
Refactor DependencyResolver into request/response
Reviewed By: @martinbigio Differential Revision: D2425842
1 parent e9e3cd3 commit 1bf7887

File tree

13 files changed

+946
-673
lines changed

13 files changed

+946
-673
lines changed

packager/react-packager/src/Bundler/__tests__/Bundler-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ describe('Bundler', function() {
125125
});
126126
});
127127

128-
wrapModule.mockImpl(function(module, code) {
128+
wrapModule.mockImpl(function(response, module, code) {
129129
return Promise.resolve('lol ' + code + ' lol');
130130
});
131131

packager/react-packager/src/Bundler/index.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class Bundler {
137137
const findEventId = Activity.startEvent('find dependencies');
138138
let transformEventId;
139139

140-
return this.getDependencies(main, isDev, platform).then((result) => {
140+
return this.getDependencies(main, isDev, platform).then((response) => {
141141
Activity.endEvent(findEventId);
142142
transformEventId = Activity.startEvent('transform');
143143

@@ -147,14 +147,19 @@ class Bundler {
147147
complete: '=',
148148
incomplete: ' ',
149149
width: 40,
150-
total: result.dependencies.length,
150+
total: response.dependencies.length,
151151
});
152152
}
153153

154-
bundle.setMainModuleId(result.mainModuleId);
154+
bundle.setMainModuleId(response.mainModuleId);
155155
return Promise.all(
156-
result.dependencies.map(
157-
module => this._transformModule(bundle, module, platform).then(transformed => {
156+
response.dependencies.map(
157+
module => this._transformModule(
158+
bundle,
159+
response,
160+
module,
161+
platform
162+
).then(transformed => {
158163
if (bar) {
159164
bar.tick();
160165
}
@@ -182,7 +187,7 @@ class Bundler {
182187
return this._resolver.getDependencies(main, { dev: isDev, platform });
183188
}
184189

185-
_transformModule(bundle, module, platform = null) {
190+
_transformModule(bundle, response, module, platform = null) {
186191
let transform;
187192

188193
if (module.isAsset_DEPRECATED()) {
@@ -199,7 +204,11 @@ class Bundler {
199204

200205
const resolver = this._resolver;
201206
return transform.then(
202-
transformed => resolver.wrapModule(module, transformed.code).then(
207+
transformed => resolver.wrapModule(
208+
response,
209+
module,
210+
transformed.code
211+
).then(
203212
code => new ModuleTransport({
204213
code: code,
205214
map: transformed.map,

packager/react-packager/src/BundlesLayout/__tests__/BundlesLayoutIntegration-test.js

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,8 @@
99
'use strict';
1010

1111
jest
12-
.dontMock('absolute-path')
13-
.dontMock('crypto')
14-
.dontMock('underscore')
15-
.dontMock('path')
16-
.dontMock('../index')
17-
.dontMock('../../lib/getAssetDataFromName')
18-
.dontMock('../../DependencyResolver/crawlers')
19-
.dontMock('../../DependencyResolver/crawlers/node')
20-
.dontMock('../../DependencyResolver/DependencyGraph/docblock')
21-
.dontMock('../../DependencyResolver/fastfs')
22-
.dontMock('../../DependencyResolver/replacePatterns')
23-
.dontMock('../../DependencyResolver')
24-
.dontMock('../../DependencyResolver/DependencyGraph')
25-
.dontMock('../../DependencyResolver/AssetModule_DEPRECATED')
26-
.dontMock('../../DependencyResolver/AssetModule')
27-
.dontMock('../../DependencyResolver/Module')
28-
.dontMock('../../DependencyResolver/Package')
29-
.dontMock('../../DependencyResolver/Polyfill')
30-
.dontMock('../../DependencyResolver/ModuleCache');
12+
.autoMockOff()
13+
.mock('../../Cache');
3114

3215
const Promise = require('promise');
3316
const path = require('path');
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
'use strict';
10+
11+
const Activity = require('../../Activity');
12+
const AssetModule_DEPRECATED = require('../AssetModule_DEPRECATED');
13+
const Fastfs = require('../fastfs');
14+
const debug = require('debug')('ReactPackager:DependencyGraph');
15+
const path = require('path');
16+
17+
class DeprecatedAssetMap {
18+
constructor({ fsCrawl, roots, assetExts, fileWatcher, ignoreFilePath, helpers }) {
19+
if (roots == null || roots.length === 0) {
20+
this._disabled = true;
21+
return;
22+
}
23+
24+
this._helpers = helpers;
25+
this._map = Object.create(null);
26+
this._assetExts = assetExts;
27+
this._fastfs = new Fastfs(
28+
'Assets',
29+
roots,
30+
fileWatcher,
31+
{ ignore: ignoreFilePath, crawling: fsCrawl }
32+
);
33+
34+
this._fastfs.on('change', this._processFileChange.bind(this));
35+
}
36+
37+
build() {
38+
if (this._disabled) {
39+
return Promise.resolve();
40+
}
41+
42+
return this._fastfs.build().then(
43+
() => {
44+
const processAsset_DEPRECATEDActivity = Activity.startEvent(
45+
'Building (deprecated) Asset Map',
46+
);
47+
48+
this._fastfs.findFilesByExts(this._assetExts).forEach(
49+
file => this._processAsset(file)
50+
);
51+
52+
Activity.endEvent(processAsset_DEPRECATEDActivity);
53+
}
54+
);
55+
}
56+
57+
resolve(fromModule, toModuleName) {
58+
if (this._disabled) {
59+
return null;
60+
}
61+
62+
const assetMatch = toModuleName.match(/^image!(.+)/);
63+
if (assetMatch && assetMatch[1]) {
64+
if (!this._map[assetMatch[1]]) {
65+
debug('WARINING: Cannot find asset:', assetMatch[1]);
66+
return null;
67+
}
68+
return this._map[assetMatch[1]];
69+
}
70+
}
71+
72+
_processAsset(file) {
73+
let ext = this._helpers.extname(file);
74+
if (this._assetExts.indexOf(ext) !== -1) {
75+
let name = assetName(file, ext);
76+
if (this._map[name] != null) {
77+
debug('Conflcting assets', name);
78+
}
79+
80+
this._map[name] = new AssetModule_DEPRECATED(file);
81+
}
82+
}
83+
84+
_processFileChange(type, filePath, root, fstat) {
85+
const name = assetName(filePath);
86+
if (type === 'change' || type === 'delete') {
87+
delete this._map[name];
88+
}
89+
90+
if (type === 'change' || type === 'add') {
91+
this._processAsset(path.join(root, filePath));
92+
}
93+
}
94+
}
95+
96+
function assetName(file, ext) {
97+
return path.basename(file, '.' + ext).replace(/@[\d\.]+x/, '');
98+
}
99+
100+
module.exports = DeprecatedAssetMap;
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
'use strict';
10+
11+
const path = require('path');
12+
const getPontentialPlatformExt = require('../../lib/getPlatformExtension');
13+
14+
class HasteMap {
15+
constructor({ fastfs, moduleCache, helpers }) {
16+
this._fastfs = fastfs;
17+
this._moduleCache = moduleCache;
18+
this._helpers = helpers;
19+
this._map = Object.create(null);
20+
}
21+
22+
build() {
23+
let promises = this._fastfs.findFilesByExt('js', {
24+
ignore: (file) => this._helpers.isNodeModulesDir(file)
25+
}).map(file => this._processHasteModule(file));
26+
27+
promises = promises.concat(
28+
this._fastfs.findFilesByName('package.json', {
29+
ignore: (file) => this._helpers.isNodeModulesDir(file)
30+
}).map(file => this._processHastePackage(file))
31+
);
32+
33+
return Promise.all(promises);
34+
}
35+
36+
processFileChange(type, absPath) {
37+
return Promise.resolve().then(() => {
38+
/*eslint no-labels: 0 */
39+
if (type === 'delete' || type === 'change') {
40+
loop: for (let name in this._map) {
41+
let modules = this._map[name];
42+
for (var i = 0; i < modules.length; i++) {
43+
if (modules[i].path === absPath) {
44+
modules.splice(i, 1);
45+
break loop;
46+
}
47+
}
48+
}
49+
50+
if (type === 'delete') {
51+
return;
52+
}
53+
}
54+
55+
if (this._helpers.extname(absPath) === 'js' ||
56+
this._helpers.extname(absPath) === 'json') {
57+
if (path.basename(absPath) === 'package.json') {
58+
return this._processHastePackage(absPath);
59+
} else {
60+
return this._processHasteModule(absPath);
61+
}
62+
}
63+
});
64+
}
65+
66+
getModule(name, platform = null) {
67+
if (this._map[name]) {
68+
const modules = this._map[name];
69+
if (platform != null) {
70+
for (let i = 0; i < modules.length; i++) {
71+
if (getPontentialPlatformExt(modules[i].path) === platform) {
72+
return modules[i];
73+
}
74+
}
75+
}
76+
77+
return modules[0];
78+
}
79+
return null;
80+
}
81+
82+
_processHasteModule(file) {
83+
const module = this._moduleCache.getModule(file);
84+
return module.isHaste().then(
85+
isHaste => isHaste && module.getName()
86+
.then(name => this._updateHasteMap(name, module))
87+
);
88+
}
89+
90+
_processHastePackage(file) {
91+
file = path.resolve(file);
92+
const p = this._moduleCache.getPackage(file, this._fastfs);
93+
return p.isHaste()
94+
.then(isHaste => isHaste && p.getName()
95+
.then(name => this._updateHasteMap(name, p)))
96+
.catch(e => {
97+
if (e instanceof SyntaxError) {
98+
// Malformed package.json.
99+
return;
100+
}
101+
throw e;
102+
});
103+
}
104+
105+
_updateHasteMap(name, mod) {
106+
if (this._map[name] == null) {
107+
this._map[name] = [];
108+
}
109+
110+
if (mod.type === 'Module') {
111+
// Modules takes precendence over packages.
112+
this._map[name].unshift(mod);
113+
} else {
114+
this._map[name].push(mod);
115+
}
116+
}
117+
}
118+
119+
module.exports = HasteMap;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
'use strict';
10+
11+
const path = require('path');
12+
13+
class Helpers {
14+
constructor({ providesModuleNodeModules, assetExts }) {
15+
this._providesModuleNodeModules = providesModuleNodeModules;
16+
this._assetExts = assetExts;
17+
}
18+
19+
isNodeModulesDir(file) {
20+
let parts = path.normalize(file).split(path.sep);
21+
const indexOfNodeModules = parts.lastIndexOf('node_modules');
22+
23+
if (indexOfNodeModules === -1) {
24+
return false;
25+
}
26+
27+
parts = parts.slice(indexOfNodeModules + 1);
28+
29+
const dirs = this._providesModuleNodeModules;
30+
31+
for (let i = 0; i < dirs.length; i++) {
32+
if (parts.indexOf(dirs[i]) > -1) {
33+
return false;
34+
}
35+
}
36+
37+
return true;
38+
}
39+
40+
isAssetFile(file) {
41+
return this._assetExts.indexOf(this.extname(file)) !== -1;
42+
}
43+
44+
extname(name) {
45+
return path.extname(name).replace(/^\./, '');
46+
}
47+
}
48+
49+
module.exports = Helpers;

0 commit comments

Comments
 (0)