-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjs.js
103 lines (88 loc) · 2.97 KB
/
js.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
'use strict';
var requires = require('crequire');
var util = require('../util');
var rename = require('rename');
var template = util.template;
var common = require('../common');
var createStream = common.createStream;
var transportDeps = common.transportDeps;
var transportId = common.transportId;
var getStyleId = common.getStyleId;
var debug = require('debug')('transport:js');
var extraDepsKeys = [];
for (var i in common.extraDeps) {
extraDepsKeys.push(common.extraDeps[i]);
}
module.exports = jsParser;
function jsParser(options) {
return createStream(options, 'js', parser);
}
var headerTpl = 'define("{{id}}", [{{deps}}], function(require, exports, module){';
var footerTpl = '});\n';
function parser(gfile, options) {
gfile.contents = new Buffer(transport(gfile, options));
var fileObj = rename.parse(gfile.path);
// change extname of .handlebars.js/.json.js/.tpl.js/.css.js
var m = fileObj.basename.match(/\.(handlebars|json|tpl|css)$/);
if (m) {
fileObj.basename = fileObj.basename.replace(m[0], '');
fileObj.extname = m[0] + fileObj.extname;
}
var renameOpts = util.getRenameOpts(gfile.file, options.rename);
gfile.path = rename(fileObj, renameOpts);
return gfile;
}
function transport(gfile, options) {
var file = gfile.file;
var code = gfile.contents.toString();
var id = transportId(file, options);
var deps = transportDeps(file, options);
debug('filepath:%s id(%s) deps(%s)', file.path, id, deps);
return [
template(headerTpl, {id: id, deps: arr2str(deps)}),
replace(code, file, options) + getStyleBox(file, options),
footerTpl
].join('\n');
}
function replace(code, file, options) {
return requires(code, function(require) {
var id = getId(require.path);
if (id === require.path && options.global[id]) {
return wrapGlobal(options.global[id]);
}
return template('require("{{id}}")', {id: id});
});
function getId(id) {
var depFile = file.getDeps(id);
if (depFile) {
return depFile.ignore ? id : transportId(depFile, options);
}
// for extra deps: handlebars-runtime, import-style and so on
if (extraDepsKeys.indexOf(id) > -1) {
var pkg_ = options.pkg.dependencies[id];
// it's a skip package when pkg is not found
if (!pkg_) return id;
// ignore package
if (options.ignore.indexOf(pkg_.name) > -1) return id;
depFile = pkg_.files[pkg_.main];
if (!depFile) return id;
return transportId(depFile, options);
}
return id;
}
}
function getStyleBox(file, options) {
var styleId = getStyleId(file, options);
debug('filepath: %s, styleBox: %s, styleId: %s', file.fullpath, options.styleBox, styleId);
return options.styleBox === true && styleId ?
'module.exports.outerBoxClass="' + styleId + '";\n' : '';
}
function arr2str(arr) {
return arr.map(function(item) {
return '"' + item + '"';
}).join(',');
}
// return window.xx if not contain
function wrapGlobal(id) {
return id.substring(0, 7) === 'window.' ? id : ('window.' + id);
}