/
script.js
190 lines (160 loc) · 5.05 KB
/
script.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
exports.init = function(grunt) {
var path = require('path');
var ast = require('cmd-util').ast;
var iduri = require('cmd-util').iduri;
var _ = grunt.util._;
var exports = {};
exports.jsParser = function(fileObj, options) {
grunt.log.verbose.writeln('Transport ' + fileObj.src + ' -> ' + fileObj.dest);
var data = fileObj.srcData || grunt.file.read(fileObj.src);
try {
var astCache = ast.getAst(data);
} catch(e) {
grunt.log.error('js parse error ' + fileObj.src.red)
grunt.fail.fatal(e.message + ' [ line:' + e.line + ', col:' + e.col + ', pos:' + e.pos + ' ]');
}
var meta = ast.parseFirst(astCache);
if (!meta) {
grunt.log.warn('found non cmd module "' + fileObj.src + '"');
// do nothing
return;
}
if (meta.id) {
grunt.log.warn('found id in "' + fileObj.src + '"');
grunt.file.write(fileObj.dest, data);
return;
}
var deps = parseDependencies(fileObj.src, options);
if (deps.length) {
grunt.log.verbose.writeln('found dependencies ' + deps);
} else {
grunt.log.verbose.writeln('found no dependencies');
}
astCache = ast.modify(astCache, {
id: unixy(options.idleading + fileObj.name.replace(/\.js$/, '')),
dependencies: deps,
require: function(v) {
return iduri.parseAlias(options, v);
}
});
data = astCache.print_to_string(options.uglify);
grunt.file.write(fileObj.dest, data);
if (!options.debug) {
return;
}
var dest = fileObj.dest.replace(/\.js$/, '-debug.js');
astCache = ast.modify(data, function(v) {
var ext = path.extname(v);
if (ext && options.parsers[ext]) {
return v.replace(new RegExp('\\' + ext + '$'), '-debug' + ext);
} else {
return v + '-debug';
}
});
data = astCache.print_to_string(options.uglify);
grunt.file.write(dest, data);
};
// helpers
// ----------------
function unixy(uri) {
return uri.replace(/\\/g, '/');
}
function moduleDependencies(id, options) {
var alias = iduri.parseAlias(options, id);
if (iduri.isAlias(options, id) && alias === id) {
// usually this is "$"
return [];
}
var file = iduri.appendext(alias);
if (!/\.js$/.test(file)) {
return [];
}
var fpath;
options.paths.some(function(base) {
var filepath = path.join(base, file);
if (grunt.file.exists(filepath)) {
grunt.log.verbose.writeln('find module "' + filepath + '"');
fpath = filepath;
return true;
}
});
if (!fpath) {
grunt.fail.warn("can't find module " + alias);
return [];
}
if (!grunt.file.exists(fpath)) {
grunt.fail.warn("can't find " + fpath);
return [];
}
var data = grunt.file.read(fpath);
var parsed = ast.parse(data);
var deps = [];
var ids = parsed.map(function(meta) {
return meta.id;
});
parsed.forEach(function(meta) {
meta.dependencies.forEach(function(dep) {
dep = iduri.absolute(alias, dep);
if (!_.contains(deps, dep) && !_.contains(ids, dep) && !_.contains(ids, dep.replace(/\.js$/, ''))) {
deps.push(dep);
}
});
});
return deps;
}
function parseDependencies(fpath, options) {
var rootpath = fpath;
function relativeDependencies(fpath, options, basefile) {
if (basefile) {
fpath = path.join(path.dirname(basefile), fpath);
}
fpath = iduri.appendext(fpath);
var deps = [];
var moduleDeps = {};
if (!grunt.file.exists(fpath)) {
if (!/\{\w+\}/.test(fpath)) {
grunt.log.warn("can't find " + fpath);
}
return [];
}
var data = grunt.file.read(fpath);
try {
var parsed = ast.parseFirst(data);
} catch(e) {
grunt.log.error(e.message + ' [ line:' + e.line + ', col:' + e.col + ', pos:' + e.pos + ' ]');
return [];
}
parsed.dependencies.forEach(function(id) {
if (id.charAt(0) === '.') {
// fix nested relative dependencies
if (basefile) {
var altId = path.join(path.dirname(fpath), id);
altId = path.relative(path.dirname(rootpath), altId);
altId = altId.replace(/\\/g, '/');
if (altId.charAt(0) !== '.') {
altId = './' + altId;
}
deps.push(altId);
} else {
deps.push(id);
}
if (/\.js$/.test(iduri.appendext(id))) {
deps = grunt.util._.union(deps, relativeDependencies(id, options, fpath));
}
} else if (!moduleDeps[id]) {
var alias = iduri.parseAlias(options, id);
deps.push(alias);
// don't parse no javascript dependencies
var ext = path.extname(alias);
if (ext && ext !== '.js') return;
var mdeps = moduleDependencies(id, options);
moduleDeps[id] = mdeps;
deps = grunt.util._.union(deps, mdeps);
}
});
return deps;
}
return relativeDependencies(fpath, options);
}
return exports;
};