-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
352 lines (297 loc) · 10.5 KB
/
index.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
'use strict';
require('dumber').ensureParserSet();
var fsReadFile = require('dumber/lib/shared').fsReadFile;
var idUtils = require('dumber-module-loader/dist/id-utils');
var ext = idUtils.ext;
var astMatcher = require('ast-matcher');
var depFinder = astMatcher.depFinder;
var ensureParsed = astMatcher.ensureParsed;
var htmlparser = require('htmlparser2');
var jsdelivr_regext = /^(?:https?:)?\/\/cdn.jsdelivr.net\/npm\/(.[^@]*)@([^/]+)\/(.+)$/;
function buildFileSet(files, folder) {
var set = new Set();
var prefix = folder ? (folder + '/') : '';
files.forEach(function(node) {
if (node.type === 'directory') {
buildFileSet(node.files, prefix + node.name)
.forEach(function(f) { set.add(f); });
} else if (node.type === 'file') {
set.add(prefix + node.name);
}
});
return set;
}
var fileLists = {};
function whenFileListReady(packageName, version, _fetch) {
var key = packageName + '@' + version;
if (!fileLists.hasOwnProperty(key)) {
fileLists[key] = _fetch('//data.jsdelivr.com/v1/package/npm/' + key)
.then(function(response) {
if (response.ok) return response.json();
return {files: []};
})
.then(function(result) {
return buildFileSet(result.files);
});
}
return fileLists[key];
}
var readFile;
if (process.browser) {
readFile = function (filepath, _fetch) {
var m = filepath.match(jsdelivr_regext);
if (m) {
var packageName = m[1];
var version = m[2];
var fpath = m[3];
return whenFileListReady(packageName, version, _fetch)
.then(function(files) {
if (!files.has(fpath)) {
throw new Error('no file "' + fpath + '" in ' + packageName + '@' + version);
}
// file exist, don't care about file content
});
} else {
return fsReadFile(filepath);
}
}
} else {
readFile = fsReadFile;
}
var auJsDepFinder = depFinder(
'PLATFORM.moduleName(__dep)',
'__any.PLATFORM.moduleName(__dep)',
'PLATFORM.moduleName(__dep, __any)',
'__any.PLATFORM.moduleName(__dep, __any)',
// any babel master? pls tell me
// why babel put (0, ) in front.
// for babel compiled code
'(__any, __any.useView)(__dep)',
'(__any, __any.noView)([__deps])',
'(__any, __any.inlineView)(__any, [__deps])',
// for TypeScript compiled code
'__any.useView(__dep)',
'__any.noView([__deps])',
'__any.inlineView(__any, [__deps])'
// there is a feature on noView and inlineView that
// supports optional base url.
// that feature DOES NOT work, so I don't support it now.
// https://github.com/aurelia/templating/issues/605
//
// even if we need to support it,
// I can go down to astMatcher to support it, no sweat.
// 1. ignores deps if base url starts with https:// or http://
// 2. use path.resolve('/', baseUrl, dep).slice(1) to get real deps
);
var _checkConfigureFunc = [
astMatcher('function configure(__any_auVar) {__anl_body}'),
astMatcher('function configure(__any_auVar, __any) {__anl_body}'),
astMatcher('async function configure(__any_auVar) {__anl_body}'),
astMatcher('async function configure(__any_auVar, __any) {__anl_body}'),
astMatcher('exports.configure = function (__any_auVar) {__anl_body};'),
astMatcher('exports.configure = function(__any_auVar, __any) {__anl_body};'),
astMatcher('exports.configure = async function (__any_auVar) {__anl_body};'),
astMatcher('exports.configure = async function(__any_auVar, __any) {__anl_body};')
];
var _auConfigureDeps = depFinder(
// forgive users don't know about PLATFORM.moduleName
'__any.plugin(__dep)',
'__any.plugin(__dep, __any)',
'__any.feature(__dep)',
'__any.feature(__dep, __any)',
'__any.globalResources(__dep)',
'__any.globalResources([__deps])'
);
var _methodCall = astMatcher('__any.__any_method()');
var auConfigModuleNames = {
defaultBindingLanguage: ['aurelia-templating-binding'],
router: ['aurelia-templating-router'],
history: ['aurelia-history-browser'],
defaultResources: ['aurelia-templating-resources'],
eventAggregator: ['aurelia-event-aggregator'],
developmentLogging: ['aurelia-logging-console'],
basicConfiguration: [
'aurelia-templating-binding',
'aurelia-templating-resources',
'aurelia-event-aggregator'
],
standardConfiguration: [
'aurelia-templating-binding',
'aurelia-templating-resources',
'aurelia-event-aggregator',
'aurelia-history-browser',
'aurelia-templating-router'
]
};
var entryDeps = [
'aurelia-bootstrapper',
'aurelia-loader-default',
'aurelia-pal-browser'
];
// https://github.com/aurelia/framework/pull/851
var auDevLogWithOptionalLevel = astMatcher('__any.developmentLogging(__any)');
function auConfigureDepFinder(contents) {
// the way to find configure function is not waterproof
var configFunc;
_checkConfigureFunc.find(function(check) {
var m = check(contents);
// only want single configure func
if (m && m.length === 1) {
configFunc = m[0];
return true; // break find loop
}
});
if (!configFunc) return [];
var auVar = configFunc.match.auVar.name;
var configureFuncBody = {
type: 'BlockStatement',
// The matched body is an array, wrap them under single node,
// so that I don't need to call forEach to deal with them.
body: configFunc.match.body
};
var isLikelyAureliaConfigFile;
var isAureliaMainFile = !!(astMatcher(auVar + '.start()')(contents));
if (!isAureliaMainFile) {
// an aurelia plugin entry file is likely to call one of
// 'globalResources', 'feature', or 'plugin'
isLikelyAureliaConfigFile = !!(astMatcher(auVar + '.globalResources(__anl)')(contents) ||
astMatcher(auVar + '.feature(__anl)')(contents) ||
astMatcher(auVar + '.plugin(__anl)')(contents));
}
var deps = new Set();
var add = _add.bind(deps);
if (isAureliaMainFile) {
var match = _methodCall(configureFuncBody);
if (match) {
// track aurelia dependency based on user configuration.
match.forEach(function(m) {
var methodName = m.match.method.name;
if (auConfigModuleNames.hasOwnProperty(methodName)) {
entryDeps.forEach(add);
auConfigModuleNames[methodName].forEach(add);
}
});
}
if (auDevLogWithOptionalLevel(configureFuncBody)) {
auConfigModuleNames.developmentLogging.forEach(add);
}
}
if (isAureliaMainFile || isLikelyAureliaConfigFile) {
_auConfigureDeps(configureFuncBody).forEach(add);
}
return Array.from(deps);
}
var inlineViewExtract = depFinder(
// for babel compiled code
'(__any, __any.inlineView)(__dep)',
'(__any, __any.inlineView)(__dep, __any)',
// for TypeScript compiled code
'__any.inlineView(__dep)',
'__any.inlineView(__dep, __any)'
);
var auInlineViewDepsFinder = function(contents) {
var match = inlineViewExtract(contents);
if (match.length === 0) return [];
// If user accidentally calls inlineView more than once,
// aurelia renders first inlineView without any complain.
// But this assumes there is only one custom element
// class implementation in current js file.
return findHtmlDeps('', match[0]);
};
// helper to add deps to a set
// accepts string, or array, or set.
function _add(deps) {
if (!deps) return;
if (typeof deps === 'string') deps = [deps];
var that = this;
deps.forEach(function(d) {
if (!d) return;
// ignore string interpolation
// <compose view-model="./foo/${bar}"></compose>
if (d.indexOf('$') >= 0) return;
var clean = d.trim();
// There is some npm package call itself like "popper.js",
// cannot strip .js from it.
// Also don't remove .js from /remote/file.js
if (!isPackageName(clean) && clean[0] !== '/') {
// strip off tailing .js
clean = clean.replace(/\.js$/ig, '');
}
that.add(clean);
});
}
function auDep(dep) {
if (!dep) return dep;
var _ext = ext(dep);
if (_ext === '.html' || _ext === '.css') {
return 'text!' + dep;
}
return dep;
}
function isPackageName(id) {
if (id.startsWith('.')) return false;
var parts = id.split('/');
// package name, or scope package name
return parts.length === 1 || (parts.length === 2 && parts[0].startsWith('@'));
}
function findJsDeps(filename, contents, mock) {
var _readFile = (mock && mock.readFile) || readFile;
var _fetch = (mock && mock.fetch) || global.fetch;
var deps = new Set();
var add = _add.bind(deps);
// for all following static analysis,
// only parse once for efficiency
var parsed = ensureParsed(contents);
// aurelia dependencies PLATFORM.moduleName and some others
add(auJsDepFinder(parsed).map(function(d) { return auDep(d); }));
// aurelia deps in configure func without PLATFORM.moduleName
add(auConfigureDepFinder(parsed).map(function(d) { return auDep(d); }));
// aurelia deps in inlineView template
add(auInlineViewDepsFinder(parsed));
// aurelia view convention, try foo.html for every foo.js
var htmlPair = filename.slice(0, -3) + '.html';
var sep = filename.lastIndexOf('/') + 1;
var localHtmlPair = htmlPair.slice(sep);
return _readFile(htmlPair, _fetch).then(
function() {
// got html file
add('text!./' + localHtmlPair);
},
function() {}
).then(function() {return Array.from(deps);});
}
function findHtmlDeps(filename, contents) {
var deps = new Set();
var add = _add.bind(deps);
var parser = new htmlparser.Parser({
onopentag: function(name, attrs) {
// <require from="dep"></require>
if (name === 'require' && attrs.from) {
add(auDep(attrs.from));
// <compose view-model="vm" view="view"></compose>
// <any as-element="compose" view-model="vm" view="view"></any>
} else if (name === 'compose' || attrs['as-element'] === 'compose') {
add([auDep(attrs['view-model']), auDep(attrs.view)]);
// <router-view layout-view-model="lvm" layout-view="ly"></router-view>
// <any as-element === 'router-view' layout-view-model="lvm" layout-view="ly"></any>
} else if (name === 'router-view' || attrs['as-element'] === 'router-view') {
add([auDep(attrs['layout-view-model']), auDep(attrs['layout-view'])]);
}
}
});
parser.write(contents);
parser.end();
return Array.from(deps);
}
function findDeps(filename, contents, mock) {
var _ext = ext(filename);
if (_ext === '.js' || _ext === '.cjs' || _ext === '.mjs') {
return findJsDeps(filename, contents, mock);
} else if (_ext === '.html' || _ext === '.htm') {
return findHtmlDeps(filename, contents);
}
return [];
}
findDeps.findJsDeps = findJsDeps;
findDeps.findHtmlDeps = findHtmlDeps;
module.exports = findDeps;