Skip to content

Commit ee00675

Browse files
committed
[react-packager] Add dev option to CLI | James Ide
Summary: Exposes the dev option that is already there to the CLI so that you can turn off invariant checks, etc. I also made it omit the inlined source map when dev=false which made it a lot faster to run on a phone, both due to smaller download size and fewer bytes to copy from Obj-C to JS and evaluate. Closes facebook/react-native#112 Github Author: James Ide <ide@jameside.com> Test Plan: * ./runJestTests.sh * test bundle creation with `bundle.sh` * test `load_dependencies.js` script * start the server and click around shell app
1 parent 68b02a8 commit ee00675

File tree

4 files changed

+23
-11
lines changed

4 files changed

+23
-11
lines changed

packager.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ var options = parseCommandLine([{
2929
}, {
3030
command: 'root',
3131
description: 'add another root(s) to be used by the packager in this project',
32+
}, {
33+
command: 'dev',
34+
default: true,
35+
description: 'produce development packages with extra warnings enabled',
3236
}]);
3337

3438
if (!options.projectRoots) {
@@ -93,7 +97,7 @@ function openStackFrameInEditor(req, res, next) {
9397

9498
function getAppMiddleware(options) {
9599
return ReactPackager.middleware({
96-
dev: true,
100+
dev: options.dev,
97101
projectRoots: options.projectRoots,
98102
blacklistRE: blacklist(false),
99103
cacheVersion: '2',

react-packager/src/Packager/Package.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,17 @@ Package.prototype.finalize = function(options) {
4040
Object.seal(this._modules);
4141
};
4242

43-
Package.prototype.getSource = function() {
44-
return this._source || (
45-
this._source = _.pluck(this._modules, 'transformedCode').join('\n') + '\n' +
46-
'RAW_SOURCE_MAP = ' + JSON.stringify(this.getSourceMap({excludeSource: true})) +
47-
';\n' + '\/\/@ sourceMappingURL=' + this._sourceMapUrl
48-
);
43+
Package.prototype.getSource = function(options) {
44+
if (!this._source) {
45+
options = options || {};
46+
this._source = _.pluck(this._modules, 'transformedCode').join('\n');
47+
if (options.inlineSourceMap) {
48+
var sourceMap = this.getSourceMap({excludeSource: true});
49+
this._source += '\nRAW_SOURCE_MAP = ' + JSON.stringify(sourceMap) + ';';
50+
}
51+
this._source += '\n\/\/@ sourceMappingURL=' + this._sourceMapUrl;
52+
}
53+
return this._source;
4954
};
5055

5156
Package.prototype.getSourceMap = function(options) {

react-packager/src/Packager/__tests__/Package-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('Package', function() {
2121
ppackage.addModule('transformed foo;', 'source foo', 'foo path');
2222
ppackage.addModule('transformed bar;', 'source bar', 'bar path');
2323
ppackage.finalize({});
24-
expect(ppackage.getSource()).toBe([
24+
expect(ppackage.getSource({inlineSourceMap: true})).toBe([
2525
'transformed foo;',
2626
'transformed bar;',
2727
'RAW_SOURCE_MAP = "test-source-map";',
@@ -34,7 +34,7 @@ describe('Package', function() {
3434
ppackage.addModule('transformed bar;', 'source bar', 'bar path');
3535
ppackage.setMainModuleId('foo');
3636
ppackage.finalize({runMainModule: true});
37-
expect(ppackage.getSource()).toBe([
37+
expect(ppackage.getSource({inlineSourceMap: true})).toBe([
3838
'transformed foo;',
3939
'transformed bar;',
4040
';require("foo");',

react-packager/src/Server/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ var validateOpts = declareOpts({
5151

5252
function Server(options) {
5353
var opts = validateOpts(options);
54+
this._dev = opts.dev;
5455
this._projectRoots = opts.projectRoots;
5556
this._packages = Object.create(null);
5657
this._packager = new Packager(opts);
@@ -72,13 +73,14 @@ Server.prototype._onFileChange = function(type, filepath, root) {
7273
};
7374

7475
Server.prototype._rebuildPackages = function() {
76+
var dev = this._dev;
7577
var buildPackage = this._buildPackage.bind(this);
7678
var packages = this._packages;
7779
Object.keys(packages).forEach(function(key) {
7880
var options = getOptionsFromPath(url.parse(key).pathname);
7981
packages[key] = buildPackage(options).then(function(p) {
8082
// Make a throwaway call to getSource to cache the source string.
81-
p.getSource();
83+
p.getSource({inlineSourceMap: dev});
8284
return p;
8385
});
8486
});
@@ -159,10 +161,11 @@ Server.prototype.processRequest = function(req, res, next) {
159161
var options = getOptionsFromPath(url.parse(req.url).pathname);
160162
var building = this._packages[req.url] || this._buildPackage(options);
161163
this._packages[req.url] = building;
164+
var dev = this._dev;
162165
building.then(
163166
function(p) {
164167
if (requestType === 'bundle') {
165-
res.end(p.getSource());
168+
res.end(p.getSource({inlineSourceMap: dev}));
166169
Activity.endEvent(startReqEventId);
167170
} else if (requestType === 'map') {
168171
res.end(JSON.stringify(p.getSourceMap()));

0 commit comments

Comments
 (0)