|
1 |
| -import path from 'path'; |
2 |
| -import util from 'util'; |
3 |
| -import r from 'resolve'; |
4 |
| -import readFileCode from './readFileCode.js'; |
5 |
| -import konan from 'konan'; |
6 |
| -import { parseToAst } from '../parsers/parse_to_ast.js'; |
7 |
| - |
8 |
| -// const parseExst = ['.js', '.mjs', '.jsx', '.vue', '.ts', '.tsx']; |
9 |
| -const resolveExst = ['.json', '.css', '.less', '.sass']; |
10 |
| -const resolve = util.promisify(r); |
11 |
| - |
12 |
| -class Deps { |
13 |
| - constructor(opts = {}) { |
14 |
| - this.fileCache = opts.fileCache || {}; |
15 |
| - this.visited = {}; |
16 |
| - this.res = []; |
17 |
| - |
18 |
| - this.options = { ...opts }; |
19 |
| - } |
20 |
| - |
21 |
| - async flush(input) { |
22 |
| - const promises = input.map(file => { |
23 |
| - const dir = path.dirname(file); |
24 |
| - return this.walk(file, { |
25 |
| - basedir: dir, |
26 |
| - filename: 'root' |
27 |
| - }); |
28 |
| - }); |
29 |
| - await Promise.all(promises); |
30 |
| - |
31 |
| - return this.res; |
32 |
| - } |
33 |
| - |
34 |
| - async readFile(file) { |
35 |
| - if (this.fileCache[file]) { |
36 |
| - return this.fileCache[file]; |
37 |
| - } |
38 |
| - |
39 |
| - return readFileCode(file); |
40 |
| - } |
41 |
| - |
42 |
| - async walk(id, parent) { |
43 |
| - const extensions = this.options.extensions; |
44 |
| - const sortKey = parent.sortKey || ''; |
45 |
| - let file = null; |
46 |
| - |
47 |
| - try { |
48 |
| - file = await resolve(id, { ...parent, extensions }); |
49 |
| - } catch (err) { |
50 |
| - if (err.code === 'MODULE_NOT_FOUND') { |
51 |
| - console.warn(`module not found: "${id}" from file ${parent.filename}`); |
52 |
| - return; |
53 |
| - } |
54 |
| - throw err; |
55 |
| - } |
56 |
| - |
57 |
| - if (this.visited[file] || resolveExst.includes(path.extname(file))) { |
58 |
| - return file; |
59 |
| - } |
60 |
| - this.visited[file] = true; |
61 |
| - |
62 |
| - const source = await this.readFile(file); |
63 |
| - const depsArray = this.parseDeps(file, source); |
64 |
| - if (!depsArray) { |
65 |
| - return file; |
66 |
| - } |
67 |
| - |
68 |
| - const deps = {}; |
69 |
| - const promises = depsArray.map(async (id, i) => { |
70 |
| - const filter = this.options.filter; |
71 |
| - if (filter && !filter(id)) { |
72 |
| - deps[id] = false; |
73 |
| - return; |
74 |
| - } |
75 |
| - const number = i.toString().padStart(8, '0'); |
76 |
| - deps[id] = await this.walk(id, { |
77 |
| - filename: file, |
78 |
| - basedir: path.dirname(file), |
79 |
| - sortKey: sortKey + '!' + file + ':' + number |
80 |
| - }); |
81 |
| - }); |
82 |
| - |
83 |
| - await Promise.all(promises); |
84 |
| - |
85 |
| - this.res.push({ |
86 |
| - id: file, |
87 |
| - source, |
88 |
| - deps, |
89 |
| - file, |
90 |
| - sortKey: sortKey + '!' + file |
91 |
| - }); |
92 |
| - return file; |
93 |
| - } |
94 |
| - |
95 |
| - parseDeps(file, src) { |
96 |
| - try { |
97 |
| - const ast = parseToAst(src, file); |
98 |
| - return konan(ast).strings; |
99 |
| - } catch (ex) { |
100 |
| - console.error(`Parsing file ${file}: ${ex}`); |
101 |
| - } |
102 |
| - } |
103 |
| -} |
104 |
| - |
105 |
| -export default async function (input = [], opts = {}) { |
106 |
| - const dep = new Deps(opts); |
107 |
| - return dep.flush(Array.from(new Set(input))); |
108 |
| -} |
| 1 | +import path from 'path'; |
| 2 | +import util from 'util'; |
| 3 | +import r from 'resolve'; |
| 4 | +import readFileCode from './readFileCode.js'; |
| 5 | +import konan from 'konan'; |
| 6 | +import { parseToAst } from '../parsers/parse_to_ast.js'; |
| 7 | + |
| 8 | +// const parseExst = ['.js', '.mjs', '.jsx', '.vue', '.ts', '.tsx']; |
| 9 | +const resolveExst = ['.json', '.css', '.less', '.sass']; |
| 10 | +const resolve = util.promisify(r); |
| 11 | + |
| 12 | +class Deps { |
| 13 | + constructor(opts = {}) { |
| 14 | + this.fileCache = opts.fileCache || {}; |
| 15 | + this.visited = {}; |
| 16 | + this.res = []; |
| 17 | + |
| 18 | + this.options = { ...opts }; |
| 19 | + } |
| 20 | + |
| 21 | + async flush(input) { |
| 22 | + const promises = input.map(file => { |
| 23 | + const dir = path.dirname(file); |
| 24 | + return this.walk(file, { |
| 25 | + basedir: dir, |
| 26 | + filename: 'root' |
| 27 | + }); |
| 28 | + }); |
| 29 | + await Promise.all(promises); |
| 30 | + |
| 31 | + return this.res; |
| 32 | + } |
| 33 | + |
| 34 | + async readFile(file) { |
| 35 | + if (this.fileCache[file]) { |
| 36 | + return this.fileCache[file]; |
| 37 | + } |
| 38 | + |
| 39 | + return readFileCode(file); |
| 40 | + } |
| 41 | + |
| 42 | + async walk(id, parent) { |
| 43 | + const extensions = this.options.extensions; |
| 44 | + const sortKey = parent.sortKey || ''; |
| 45 | + let file = null; |
| 46 | + |
| 47 | + try { |
| 48 | + file = await resolve(id, { ...parent, extensions }); |
| 49 | + } catch (err) { |
| 50 | + if (err.code === 'MODULE_NOT_FOUND') { |
| 51 | + console.warn(`module not found: "${id}" from file ${parent.filename}`); |
| 52 | + return; |
| 53 | + } |
| 54 | + throw err; |
| 55 | + } |
| 56 | + |
| 57 | + if (this.visited[file] || resolveExst.includes(path.extname(file))) { |
| 58 | + return file; |
| 59 | + } |
| 60 | + this.visited[file] = true; |
| 61 | + |
| 62 | + const source = await this.readFile(file); |
| 63 | + const depsArray = this.parseDeps(file, source); |
| 64 | + if (!depsArray) { |
| 65 | + return file; |
| 66 | + } |
| 67 | + |
| 68 | + const deps = {}; |
| 69 | + const promises = depsArray.map(async (id, i) => { |
| 70 | + const filter = this.options.filter; |
| 71 | + if (filter && !filter(id)) { |
| 72 | + deps[id] = false; |
| 73 | + return; |
| 74 | + } |
| 75 | + const number = i.toString().padStart(8, '0'); |
| 76 | + deps[id] = await this.walk(id, { |
| 77 | + filename: file, |
| 78 | + basedir: path.dirname(file), |
| 79 | + sortKey: sortKey + '!' + file + ':' + number |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + await Promise.all(promises); |
| 84 | + |
| 85 | + this.res.push({ |
| 86 | + id: file, |
| 87 | + source, |
| 88 | + deps, |
| 89 | + file, |
| 90 | + sortKey: sortKey + '!' + file |
| 91 | + }); |
| 92 | + return file; |
| 93 | + } |
| 94 | + |
| 95 | + parseDeps(file, src) { |
| 96 | + try { |
| 97 | + const ast = parseToAst(src, file); |
| 98 | + return konan(ast, { dynamicImport: false }).strings; |
| 99 | + } catch (ex) { |
| 100 | + console.error(`Parsing file ${file}: ${ex}`); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +export default async function (input = [], opts = {}) { |
| 106 | + const dep = new Deps(opts); |
| 107 | + return dep.flush(Array.from(new Set(input))); |
| 108 | +} |
0 commit comments