-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
69 lines (52 loc) · 1.74 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
'use strict';
const compiler = require('@vue/compiler-sfc');
const detectiveTypeScript = require('detective-typescript');
const detectiveEs6 = require('detective-es6');
const detectiveScss = require('detective-scss');
const detectiveStylus = require('detective-stylus');
const detectiveSass = require('detective-sass');
const detectiveLess = require('@dependents/detective-less');
function detectiveVue(content, options) {
if (content === undefined) throw new Error('content not given');
if (typeof content !== 'string') throw new Error('content is not a string');
const result = compiler.parse(content, { sourceMap: false });
const { styles, script, scriptSetup } = result.descriptor;
const dependencies = [];
for (const usedScript of [script, scriptSetup]) {
if (usedScript?.content) {
if (usedScript.attrs?.lang === 'ts') {
dependencies.push(...detectiveTypeScript(usedScript.content, options));
} else {
dependencies.push(...detectiveEs6(usedScript.content, options));
}
}
}
if (!styles) return dependencies;
for (const style of styles) {
switch (style.attrs.lang) {
case 'scss': {
dependencies.push(...detectiveScss(style.content, options));
break;
}
case 'stylus': {
dependencies.push(...detectiveStylus(style.content, options));
break;
}
case 'sass': {
dependencies.push(...detectiveSass(style.content, options));
break;
}
case 'less': {
dependencies.push(...detectiveLess(style.content, options));
break;
}
default:
// css has no deps
}
}
return dependencies;
}
/**
* Extracts the dependencies of the supplied Vue module
*/
module.exports = detectiveVue;