forked from apache/cordova-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowserify.js
179 lines (153 loc) · 7.84 KB
/
browserify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
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 path = require('path');
var aliasify = require('aliasify');
var common = require('./platforms/common');
var fs = require('fs-extra');
var childProcess = require('child_process');
var events = require('cordova-common').events;
var bundle = require('cordova-js/tasks/lib/bundle-browserify');
var writeLicenseHeader = require('cordova-js/tasks/lib/write-license-header');
var Q = require('q');
var computeCommitId = require('cordova-js/tasks/lib/compute-commit-id');
var Readable = require('stream').Readable;
var PlatformJson = require('cordova-common').PlatformJson;
var PluginInfoProvider = require('cordova-common').PluginInfoProvider;
function generateFinalBundle (platform, libraryRelease, outReleaseFile, commitId, platformVersion) {
var deferred = Q.defer();
var outReleaseFileStream = fs.createWriteStream(outReleaseFile);
var time = new Date().valueOf();
writeLicenseHeader(outReleaseFileStream, platform, commitId, platformVersion);
var releaseBundle = libraryRelease.bundle();
releaseBundle.pipe(outReleaseFileStream);
outReleaseFileStream.on('finish', function () {
var newtime = new Date().valueOf() - time;
events.emit('verbose', 'generated cordova.' + platform + '.js @ ' + commitId + ' in ' + newtime + 'ms');
deferred.resolve();
// TODO clean up all the *.browserify files
});
outReleaseFileStream.on('error', function (err) {
events.emit('warn', 'Error while generating cordova.js');
deferred.reject(err);
});
return deferred.promise;
}
function computeCommitIdSync () {
var deferred = Q.defer();
computeCommitId(function (cId) {
deferred.resolve(cId);
});
return deferred.promise;
}
function getPlatformVersion (cId, project_dir) {
var deferred = Q.defer();
// run version script for each platform to get platformVersion
var versionPath = path.join(project_dir, '/cordova/version');
childProcess.exec('"' + versionPath + '"', function (err, stdout, stderr) {
if (err) {
err.message = 'Failed to get platform version (will use \'N/A\' instead).\n' + err.message;
events.emit('warn', err);
deferred.resolve('N/A');
} else {
deferred.resolve(stdout.trim());
}
});
return deferred.promise;
}
module.exports = function doBrowserify (project, platformApi, pluginInfoProvider) {
// Process:
// - Do config munging by calling into config-changes module
// - List all plugins in plugins_dir
// - Load and parse their plugin.xml files.
// - Skip those without support for this platform. (No <platform> tags means JS-only!)
// - Build a list of all their js-modules, including platform-specific js-modules.
// - For each js-module (general first, then platform) build up an object storing the path and any clobbers, merges and runs for it.
// Write this object into www/cordova_plugins.json.
// This file is not really used. Maybe cordova app harness
var platform = platformApi.platform;
events.emit('verbose', 'Preparing ' + platform + ' browserify project');
pluginInfoProvider = pluginInfoProvider || new PluginInfoProvider(); // Allow null for backwards-compat.
var platformJson = PlatformJson.load(project.locations.plugins, platform);
var wwwDir = platformApi.getPlatformInfo().locations.www;
var commitId;
return computeCommitIdSync()
.then(function (cId) {
commitId = cId;
return getPlatformVersion(commitId, platformApi.root);
}).then(function (platformVersion) {
var libraryRelease = bundle(platform, false, commitId, platformVersion, platformApi.getPlatformInfo().locations.platformWww);
var pluginMetadata = {};
var modulesMetadata = [];
var plugins = Object.keys(platformJson.root.installed_plugins).concat(Object.keys(platformJson.root.dependent_plugins));
events.emit('verbose', 'Iterating over plugins in project:', plugins);
plugins.forEach(function (plugin) {
var pluginDir = path.join(project.locations.plugins, plugin);
var pluginInfo = pluginInfoProvider.get(pluginDir);
// pluginMetadata is a mapping from plugin IDs to versions.
pluginMetadata[pluginInfo.id] = pluginInfo.version;
// Copy www assets described in <asset> tags.
pluginInfo.getAssets(platform)
.forEach(function (asset) {
common.asset.install(asset, pluginDir, wwwDir);
});
pluginInfo.getJsModules(platform)
.forEach(function (jsModule) {
var moduleName = jsModule.name ? jsModule.name : path.basename(jsModule.src, '.js');
var moduleId = pluginInfo.id + '.' + moduleName;
var moduleMetadata = {
file: jsModule.src,
id: moduleId,
name: moduleName,
pluginId: pluginInfo.id
};
if (jsModule.clobbers.length > 0) {
moduleMetadata.clobbers = jsModule.clobbers.map(function (o) { return o.target; });
}
if (jsModule.merges.length > 0) {
moduleMetadata.merges = jsModule.merges.map(function (o) { return o.target; });
}
if (jsModule.runs) {
moduleMetadata.runs = true;
}
modulesMetadata.push(moduleMetadata);
libraryRelease.require(path.join(pluginDir, jsModule.src), { expose: moduleId });
});
});
events.emit('verbose', 'Writing out cordova_plugins.js...');
// Create a stream and write plugin metadata into it
// instead of generating intermediate file on FS
var cordova_plugins = new Readable();
cordova_plugins.push(
'module.exports = ' + JSON.stringify(modulesMetadata, null, 2) + ';\n' +
'module.exports.metadata = ' + JSON.stringify(pluginMetadata, null, 2) + ';\n', 'utf8');
cordova_plugins.push(null);
var bootstrap = new Readable();
bootstrap.push('require(\'cordova/init\');\n', 'utf8');
bootstrap.push(null);
var moduleAliases = modulesMetadata
.reduce(function (accum, meta) {
accum['./' + meta.name] = meta.id;
return accum;
}, {});
libraryRelease
.add(cordova_plugins, {file: path.join(wwwDir, 'cordova_plugins.js'), expose: 'cordova/plugin_list'})
.add(bootstrap)
.transform(aliasify, {aliases: moduleAliases});
var outReleaseFile = path.join(wwwDir, 'cordova.js');
return generateFinalBundle(platform, libraryRelease, outReleaseFile, commitId, platformVersion);
});
};