-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.js
301 lines (255 loc) · 10.1 KB
/
convert.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*jshint node: true */
'use strict';
var fs = require('fs'),
path = require('path'),
onrequirejs = require('./lib/onrequirejs'),
builtins = require('browserify/lib/builtins'),
walk = require('./walk'),
toAmd = require('./lib/toAmd'),
jsonAdapterPath = require.resolve('./adapters/json'),
jsonBuilderAdapterPath = require.resolve('./adapters/json-builder'),
browserifyPath = path.dirname(require.resolve('browserify')),
browserifyPackageDir = path.join(browserifyPath, 'node_modules'),
internalBrowserifyNative = /browserify\/lib$/,
readableStreamDepRegExp = /require\s*\(\s*["']readable-stream[^\)]*\)/g,
streamRegExp = /(require\s*\(\s*["'])stream(["']\s*\))/g,
jsonPluginRegExp = /^json!/,
jsSuffixRegExp = /\.js$/;
//console.log(JSON.stringify(builtins, null, ' '));
var streamMainAdapter = 'define([\'./stream/browser-main\'], ' +
'function(m) { return m; });';
function getPackageJson(filePath) {
if (fs.existsSync(filePath)) {
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
} else {
return {};
}
}
function isJson(id) {
return jsonPluginRegExp.test(id);
}
function isNativeModule(id) {
return builtins.hasOwnProperty(id) || id === 'process';
}
function getNativePath(id) {
if (id === 'process') {
id = '_' + id;
}
var nativePath = builtins[id];
if (!nativePath) {
throw new Error('Unknown native ID: ' + id);
}
// Have a path to a specific module, but really need the full package
// directory since the module can have internal package dependencies. However,
// some are just mapped to "empty" files in browserify. So for those, just
// return those files.
var dirName = path.dirname(nativePath);
if (internalBrowserifyNative.test(dirName)) {
return nativePath;
} else {
return dirName;
}
}
function convertWithFile(baseUrl, options, file) {
if (!options.onDep) {
options.onDep = onDep;
}
var nativeWalked = {};
function installNativeShim(nativeId, nativePath) {
nativePath = nativePath || getNativePath(nativeId);
var destPrefix = path.join(baseUrl, nativeId);
// Only do the work if there is not already a module at the expected
// baseUrl location.
if (!nativeWalked.hasOwnProperty(nativeId) &&
!fs.existsSync(destPrefix + '.js')) {
var nativeStat = fs.statSync(nativePath);
if (nativeStat.isFile()) {
var nativeContents = fs.readFileSync(builtins[nativeId], 'utf8');
// If the native shim is just an empty file, write out an empty AMD
// module.
if (!nativeContents.trim()) {
nativeContents = 'define(function(){});';
}
var converted = toAmd(nativePath, nativeContents);
fs.writeFileSync(nativePath, converted.contents, 'utf8');
nativeWalked[nativeId] = {};
// The builtin adapter could itself have dependencies on other
// built-ins and if so, make sure to add them.
if (converted.deps) {
converted.deps.forEach(function(dep) {
if (isNativeModule(dep) && !nativeWalked.hasOwnProperty(dep)) {
installNativeShim(dep);
}
});
}
} else if (nativeStat.isDirectory()) {
// Copy the directory over.
file.copyDir(nativePath, destPrefix);
// Walk/convert it. Create the entry in nativeWalked before receiving
// the walked values to avoid cycles where a native depends on itself.
nativeWalked[nativeId] = {};
nativeWalked[nativeId] = walk.topPackage(nativeId,
destPrefix,
options);
// Need to fix up the stream-browserify and readable-stream relations.
if (nativeId === 'stream') {
var mainPath = path.join(destPrefix,
nativeWalked[nativeId].main + '.js');
if (fs.existsSync(mainPath)) {
var mainContents = file.readFile(mainPath)
.replace(readableStreamDepRegExp, 'undefined');
file.saveFile(mainPath, mainContents);
file.copyFile(path.join(__dirname,
'adapters', 'stream-browser-main.js'),
path.join(destPrefix, 'browser-main.js'));
file.saveFile(destPrefix + '.js', streamMainAdapter);
}
} else if (nativeId === 'readable-stream') {
var files = file.getFilteredFileList(destPrefix, jsSuffixRegExp);
files.forEach(function(filePath) {
var fileContents = file.readFile(filePath);
fileContents = fileContents.replace(streamRegExp,
function(match, prefix, suffix) {
return prefix + 'stream/index' + suffix;
});
if (/_stream_writable.js$/.test(filePath)) {
// It is a complicated web that we weave for backcompat
fileContents = fileContents.replace(
/require\('.\/_stream_duplex'\)/g,
'require(\'./_stream\' + \'_duplex\')'
);
}
file.saveFile(filePath, fileContents);
});
}
// Some of the native shims depend on packages installed in browserify's
// node_modules. So if a package.json dependency is not in the nested
// node_modules for that shim, get it from the browserify node_modules.
var nativePackageJsonPath = path.join(destPrefix, 'package.json');
var deps = getPackageJson(nativePackageJsonPath).dependencies;
if (deps) {
Object.keys(deps).forEach(function(depName) {
var nestedPath = path.join(destPrefix, 'node_modules', depName);
if (!fs.existsSync(nestedPath)) {
var altPath = path.join(browserifyPackageDir, depName);
if (!nativeWalked.hasOwnProperty(depName) &&
fs.existsSync(altPath)) {
installNativeShim(depName, altPath);
}
}
});
}
}
}
}
function amdDir(dir) {
fs.readdirSync(dir).forEach(function(baseName) {
var fullPath = path.join(dir, baseName),
stat = fs.statSync(fullPath);
if (jsSuffixRegExp.test(baseName) && stat.isFile()) {
var contents = fs.readFileSync(fullPath, 'utf8');
// converted has .contents and .deps array.
var converted = toAmd(fullPath, contents);
// Write out file if it is different.
if (converted.contents !== contents) {
fs.writeFileSync(fullPath, converted.contents, 'utf8');
}
if (converted.deps) {
converted.deps.forEach(function(dep) {
// Browserify throws garbage in the require API by using numbers
// sometimes, so skip those.
if (typeof dep !== 'string') {
return;
}
// Find the native modules needed.
if (isNativeModule(dep) && !nativeWalked.hasOwnProperty(dep)) {
installNativeShim(dep);
} else {
if (isJson(dep)) {
// Write out json plugin and its builder.
var jsonDest = path.join(baseUrl, 'json.js');
if (!fs.existsSync(jsonDest)) {
file.copyFile(jsonAdapterPath, jsonDest);
}
jsonDest = path.join(baseUrl, 'json-builder.js');
if (!fs.existsSync(jsonDest)) {
file.copyFile(jsonBuilderAdapterPath, jsonDest);
}
}
}
});
}
} else if (stat.isDirectory() && baseName !== 'node_modules') {
// recurse, but only if not the node_modules, that will be handled
// by other calls to convert.
amdDir(fullPath);
}
});
}
// walkData has: packageName, main, normalizedId, fullPath
function convertPackage(walkData) {
var fullPath = walkData.fullPath,
packageName = walkData.packageName,
mainId = walkData.main,
map = walkData.map;
// If fullPath ends in a .js, rename the directory, and adjust the full
// path.
if (jsSuffixRegExp.test(walkData.fullPath)) {
var oldPath = fullPath;
fullPath = walkData.fullPath = fullPath.replace(jsSuffixRegExp, '');
// This could be a re-run, so remove any existing target first.
if (fs.existsSync(fullPath)) {
file.deleteFile(fullPath);
}
file.copyDir(oldPath, fullPath);
file.deleteFile(oldPath);
}
if (mainId) {
// Write main module adapter
var adapterPath = fullPath + '.js';
// This should work multiple times over the same directory. So if the
// adapter already exists, do not bother doing the work.
if (!fs.existsSync(adapterPath)) {
fs.writeFileSync(adapterPath,
'define([\'./' +
packageName + '/' + mainId +
'\'], function(m) { return m; });',
'utf8');
}
}
// If any of the map property values is 'notobo-empty', write it out
// to the baseUrl area.
if (map) {
Object.keys(map).some(function(prop) {
if (map[prop] === 'notobo-empty') {
var targetPath = path.join(baseUrl, 'notobo-empty.js');
if (!fs.existsSync(targetPath)) {
file.copyFile(path.join(__dirname, 'adapters', 'notobo-empty.js'),
targetPath);
}
return true;
}
});
}
// Scan for .js files, and convert to AMD.
amdDir(fullPath);
}
function onDep(walkData) {
convertPackage(walkData);
}
var walked = walk(baseUrl, options);
Object.keys(nativeWalked).forEach(function(nativeId) {
walked[nativeId] = nativeWalked[nativeId];
});
return walked;
}
module.exports = function convert(baseUrl, options, callback) {
if (typeof options === 'function' || !options) {
callback = options;
options = {};
}
onrequirejs(['env!env/file'], callback, function(file, callback) {
var walked = convertWithFile(baseUrl, options, file);
callback(undefined, walked);
});
};