Skip to content
Permalink
Browse files
CB-9436 Removes require-tr bundle transformation
  • Loading branch information
Vladimir Kotikov committed Jul 31, 2015
1 parent 2c29e18 commit d02896617847ce83b4d768cb3ad8ac348189d8b4
Showing 8 changed files with 170 additions and 309 deletions.
@@ -73,9 +73,7 @@
"through2": "^2.0.0"
},
"dependencies": {
"browserify": "10.1.3",
"through": "2.3.4",
"uglify-js": "^2.4.15"
"browserify": "10.1.3"
},
"cordova-platforms" : {
"cordova-android" : "../cordova-android",
@@ -21,6 +21,7 @@

var channel = require('cordova/channel');
var cordova = require('cordova');
var modulemapper = require('cordova/modulemapper');
var platform = require('cordova/platform');
var utils = require('cordova/utils');

@@ -113,6 +114,7 @@ platform.bootstrap && platform.bootstrap();
* Create all cordova objects once native side is ready.
*/
channel.join(function() {
modulemapper.mapModules(window);

platform.initialize && platform.initialize();

@@ -0,0 +1,111 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/

var builder = require('cordova/builder'),
symbolList = [],
deprecationMap;

exports.reset = function() {
symbolList = [];
deprecationMap = {};
};

function addEntry(strategy, moduleName, symbolPath, opt_deprecationMessage) {
symbolList.push(strategy, moduleName, symbolPath);
if (opt_deprecationMessage) {
deprecationMap[symbolPath] = opt_deprecationMessage;
}
}

// Note: Android 2.3 does have Function.bind().
exports.clobbers = function(moduleName, symbolPath, opt_deprecationMessage) {
addEntry('c', moduleName, symbolPath, opt_deprecationMessage);
};

exports.merges = function(moduleName, symbolPath, opt_deprecationMessage) {
addEntry('m', moduleName, symbolPath, opt_deprecationMessage);
};

exports.defaults = function(moduleName, symbolPath, opt_deprecationMessage) {
addEntry('d', moduleName, symbolPath, opt_deprecationMessage);
};

exports.runs = function(moduleName) {
addEntry('r', moduleName, null);
};

function prepareNamespace(symbolPath, context) {
if (!symbolPath) {
return context;
}
var parts = symbolPath.split('.');
var cur = context;
for (var i = 0, part; part = parts[i]; ++i) {
cur = cur[part] = cur[part] || {};
}
return cur;
}

exports.mapModules = function(context) {
var origSymbols = {};
context.CDV_origSymbols = origSymbols;
for (var i = 0, len = symbolList.length; i < len; i += 3) {
var strategy = symbolList[i];
var moduleName = symbolList[i + 1];
var module = require(moduleName);
// <runs/>
if (strategy == 'r') {
continue;
}
var symbolPath = symbolList[i + 2];
var lastDot = symbolPath.lastIndexOf('.');
var namespace = symbolPath.substr(0, lastDot);
var lastName = symbolPath.substr(lastDot + 1);

var deprecationMsg = symbolPath in deprecationMap ? 'Access made to deprecated symbol: ' + symbolPath + '. ' + deprecationMsg : null;
var parentObj = prepareNamespace(namespace, context);
var target = parentObj[lastName];

if (strategy == 'm' && target) {
builder.recursiveMerge(target, module);
} else if ((strategy == 'd' && !target) || (strategy != 'd')) {
if (!(symbolPath in origSymbols)) {
origSymbols[symbolPath] = target;
}
builder.assignOrWrapInDeprecateGetter(parentObj, lastName, module, deprecationMsg);
}
}
};

exports.getOriginalSymbol = function(context, symbolPath) {
var origSymbols = context.CDV_origSymbols;
if (origSymbols && (symbolPath in origSymbols)) {
return origSymbols[symbolPath];
}
var parts = symbolPath.split('.');
var obj = context;
for (var i = 0; i < parts.length; ++i) {
obj = obj && obj[parts[i]];
}
return obj;
};

exports.reset();

@@ -98,19 +98,7 @@ function createEvent(type, data) {
var cordova = {
platformVersion:PLATFORM_VERSION_BUILD_LABEL,
version:PLATFORM_VERSION_BUILD_LABEL,
require: function(module) {
console.log(module);
if(symbolList) {
for(var i = 0 ; i < symbolList.length ; i++) {
if(module === symbolList[i].symbol) {
return require(symbolList[i].path);
}
}
} else {
console.log("else");
return require(module);
}
},
require: require,
platformId:platform.id,
/**
* Methods to add/remove your own addEventListener hijacking on document + window.
@@ -55,6 +55,6 @@ module.exports = function(grunt) {
platformVersion = 'N/A';
}
}
generate(platformName, useWindowsLineEndings, platformVersion, done);
generate(platformName, useWindowsLineEndings, platformVersion, platformPath, done);
});
}
@@ -9,7 +9,7 @@
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to
* Unless required by applicable law or agreed to
* software distributed under the License is distr
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
* KIND, either express or implied. See the Licen
@@ -19,53 +19,63 @@
var fs = require('fs');
var path = require('path');
var browserify = require('browserify');
var require_tr = require('./require-tr');
var root = path.join(__dirname, '..', '..');
var pkgJson = require('../../package.json');
var collectFiles = require('./collect-files');
var copyProps = require('./copy-props');

module.exports = function bundle(platform, debug, commitId, platformVersion) {
require_tr.platform = platform;
// FIXME: need to find a way to void ignore missing
var b = browserify({debug: debug});
// XXX plugin_list is not present at this stage
b.ignore(path.join(root, 'src', 'common', 'plugin_list'));
module.exports = function bundle(platform, debug, commitId, platformVersion, platformPath) {
platformPath = fs.existsSync(platformPath) && fs.existsSync(path.join(platformPath, 'cordova-js-src')) ?
path.join(platformPath, 'cordova-js-src') :
path.resolve(root, 'src', 'legacy-exec', platform);

b.transform(require_tr.transform, {'platform': platform});
var platformDirname = platform === 'amazon-fireos' ? 'android' : platform;

var cordovajssrc = path.join(process.cwd(), 'platforms', platform, 'platform_www', 'cordova-js-src');
//checks to see if browserify is run in a cli project and
//if the platform has a cordova-js-src to build cordova.js with
if(fs.existsSync(cordovajssrc)){
b.add(path.join(cordovajssrc, 'exec.js'));
b.add(path.join(cordovajssrc, 'platform.js'));
} else {
b.add(path.join(root, 'src', 'legacy-exec', platform, 'exec.js'));
b.add(path.join(root, 'src', 'legacy-exec', platform, 'platform.js'));
}
var modules = {'cordova': path.resolve(root, 'src', 'cordova_b.js')};
copyProps(modules, collectFiles(path.resolve(root, 'src', 'common'), 'cordova'));
copyProps(modules, collectFiles(platformPath, 'cordova'));

if (platform === 'test') {
// Add tests to bundle
// TODO: Also need to include android/ios tests
fs.readdirSync('test').forEach(function (item) {
var itemPath = path.resolve('test', item);
if (fs.statSync(itemPath).isFile()) b.add(itemPath);
});
// Replace standart initialization script with browserify's one
delete modules['cordova/init_b'];
delete modules['cordova/modulemapper_b'];
delete modules['cordova/pluginloader'];
modules['cordova/init'] = path.resolve(root, 'src', 'common', 'init_b.js');
modules['cordova/modulemapper'] = path.resolve(root, 'src', 'common', 'modulemapper_b.js');

// Add rest of modules from cordova-js-src/legacy-exec directory
// TODO: this probably should be done for all platforms?
fs.readdirSync(path.join(root, 'src', 'legacy-exec', platform, platform)).forEach(function (item) {
var itemPath = path.resolve(root, 'src', 'legacy-exec', platform, platform, item);
if (fs.statSync(itemPath).isFile()) b.add(itemPath);
});
// test doesn't support custom paths
if (platform === 'test') {
var testFilesPath;
var androidPath = path.resolve(pkgJson['cordova-platforms']['cordova-android']);
var iosPath = path.resolve(pkgJson['cordova-platforms']['cordova-ios']);
// Add android platform-specific modules that have tests to the test bundle.
if(fs.existsSync(androidPath)) {
testFilesPath = path.resolve(androidPath, 'cordova-js-src', 'android');
modules['cordova/android/exec'] = path.resolve(androidPath, 'cordova-js-src', 'exec.js');
} else {
testFilesPath = path.resolve('src', 'legacy-exec', 'android', 'android');
modules['cordova/android/exec'] = path.resolve(root, 'src', 'legacy-exec', 'android', 'exec.js');
}
copyProps(modules, collectFiles(testFilesPath, 'cordova/android'));

// Ignore fake modules from tests, otherwise browserify fails to generate bundle
['your mom', 'dino', 'a', 'ModuleA', 'ModuleB', 'ModuleC']
.forEach(b.ignore.bind(b));
//Add iOS platform-specific modules that have tests for the test bundle.
if(fs.existsSync(iosPath)) {
modules['cordova/ios/exec'] = path.join(iosPath, 'cordova-js-src', 'exec.js');
} else {
modules['cordova/ios/exec'] = path.resolve(root, 'src', 'legacy-exec', 'ios', 'exec.js');
}
copyProps(modules, collectFiles(testFilesPath, 'cordova/ios'));
}

b.add(path.join(root, 'src', 'scripts', 'bootstrap.js'));
modules = Object.keys(modules)
.map(function (moduleId) {
return {
file: modules[moduleId],
expose: moduleId
};
});

return b;
}
return browserify({debug: !!debug, detectGlobals: false})
.require(modules)
.exclude('cordova/plugin_list')
.exclude('cordova/pluginloader');
};
@@ -9,7 +9,7 @@
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to
* Unless required by applicable law or agreed to
* software distributed under the License is distr
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
* KIND, either express or implied. See the Licen
@@ -19,52 +19,32 @@
var fs = require('fs');
var path = require('path');
var util = require('util');
var through = require('through2');
var requireTr = require('./require-tr');
var bundle = require('./bundle-browserify');
var computeCommitId = require('./compute-commit-id');
var writeLicenseHeader = require('./write-license-header');

module.exports = function generate(platform, useWindowsLineEndings, platformVersion, done) {
module.exports = function generate(platform, useWindowsLineEndings, platformVersion, platformPath, done) {
computeCommitId(function(commitId) {
var outReleaseFile, outReleaseFileStream,
outDebugFile, outDebugFileStream,
releaseBundle, debugBundle;
var time = new Date().valueOf();

var libraryRelease = bundle(platform, false, commitId, platformVersion);
// if we are using windows line endings, we will also add the BOM
// if(useWindowsLineEndings) {
// libraryRelease = "\ufeff" + libraryRelease.split(/\r?\n/).join("\r\n");
// }
// var libraryDebug = bundle(platform, true, commitId);

if (!fs.existsSync('pkg')) {
fs.mkdirSync('pkg');
}

outReleaseFile = path.join('pkg', 'cordova.' + platform + '.js');
outReleaseFileStream = fs.createWriteStream(outReleaseFile);

var symbolList = null;
var addSymbolList = through.obj(function(row, enc, next) {
if(symbolList === null) {
symbolList = requireTr.getModules(platform);
this.push(util.format('var symbolList = %s;\n%s\n', JSON.stringify(symbolList, null, 4), row));
} else {
this.push(row);
}
next();
});

libraryRelease.pipeline.get('wrap').push(addSymbolList);

// write license header
writeLicenseHeader(outReleaseFileStream, platform, commitId, platformVersion);

releaseBundle = libraryRelease.bundle();

releaseBundle.pipe(outReleaseFileStream);
bundle(platform, false, commitId, platformVersion, platformPath)
.add(path.resolve(__dirname, '..', '..', 'src/scripts/bootstrap.js'))
.bundle()
.pipe(outReleaseFileStream);

outReleaseFileStream.on('finish', function() {
var newtime = new Date().valueOf() - time;

0 comments on commit d028966

Please sign in to comment.