|
| 1 | +import path from 'path'; |
| 2 | +import util from 'util'; |
| 3 | +import { readFile } from 'fs/promises'; |
| 4 | +import r from 'resolve'; |
| 5 | +import detective from 'detective'; |
| 6 | +import konan from 'konan'; |
| 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 | + return readFile(file, { |
| 39 | + encoding: 'utf8' |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + async walk(id, parent) { |
| 44 | + const extensions = this.options.extensions; |
| 45 | + const sortKey = parent.sortKey || ''; |
| 46 | + let file = null; |
| 47 | + |
| 48 | + try { |
| 49 | + file = await resolve(id, { ...parent, extensions }); |
| 50 | + } catch (err) { |
| 51 | + if (err.code === 'MODULE_NOT_FOUND') { |
| 52 | + console.warn(`module not found: "${id}" from file ${parent.filename}`); |
| 53 | + return; |
| 54 | + } |
| 55 | + throw err; |
| 56 | + } |
| 57 | + |
| 58 | + if (this.visited[file] || resolveExst.includes(path.extname(file))) { |
| 59 | + return file; |
| 60 | + } |
| 61 | + this.visited[file] = true; |
| 62 | + |
| 63 | + const source = await this.readFile(file); |
| 64 | + const depsArray = this.parseDeps(file, source); |
| 65 | + if (!depsArray) { |
| 66 | + return file; |
| 67 | + } |
| 68 | + |
| 69 | + const deps = {}; |
| 70 | + const promises = depsArray.map(async (id, i) => { |
| 71 | + const filter = this.options.filter; |
| 72 | + if (filter && !filter(id)) { |
| 73 | + deps[id] = false; |
| 74 | + return; |
| 75 | + } |
| 76 | + const number = i.toString().padStart(8, '0'); |
| 77 | + deps[id] = await this.walk(id, { |
| 78 | + filename: file, |
| 79 | + basedir: path.dirname(file), |
| 80 | + sortKey: sortKey + '!' + file + ':' + number |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + await Promise.all(promises); |
| 85 | + |
| 86 | + this.res.push({ |
| 87 | + id: file, |
| 88 | + source, |
| 89 | + deps, |
| 90 | + file, |
| 91 | + sortKey: sortKey + '!' + file |
| 92 | + }); |
| 93 | + return file; |
| 94 | + } |
| 95 | + |
| 96 | + parseDeps(file, src) { |
| 97 | + try { |
| 98 | + try { |
| 99 | + return konan(src).strings; |
| 100 | + } catch (ex) { |
| 101 | + // konan does not support Vue (component) file, try to parse using detective (as a fallback) |
| 102 | + return detective(src); |
| 103 | + } |
| 104 | + } catch (ex) { |
| 105 | + console.error(`Parsing file ${file}: ${ex}`); |
| 106 | + return; |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +export default async function (input = [], opts = {}) { |
| 112 | + const dep = new Deps(opts); |
| 113 | + return dep.flush(Array.from(new Set(input))); |
| 114 | +} |
0 commit comments