-
Notifications
You must be signed in to change notification settings - Fork 482
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replace module-deps-sortable on own implementation to contr…
…ol how parse files BREAKING CHANGE: all Extensions whould contains '.' so that mean if you have just 'ts' then need to convert to '.ts'
- Loading branch information
1 parent
e3c59d7
commit abb781a
Showing
8 changed files
with
331 additions
and
353 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import path from 'path'; | ||
import util from 'util'; | ||
import { readFile } from 'fs/promises'; | ||
import r from 'resolve'; | ||
import detective from 'detective'; | ||
import konan from 'konan'; | ||
|
||
// const parseExst = ['.js', '.mjs', '.jsx', '.vue', '.ts', '.tsx']; | ||
const resolveExst = ['.json', '.css', '.less', '.sass']; | ||
const resolve = util.promisify(r); | ||
|
||
class Deps { | ||
constructor(opts = {}) { | ||
this.fileCache = opts.fileCache || {}; | ||
this.visited = {}; | ||
this.res = []; | ||
|
||
this.options = { ...opts }; | ||
} | ||
|
||
async flush(input) { | ||
const promises = input.map(file => { | ||
const dir = path.dirname(file); | ||
return this.walk(file, { | ||
basedir: dir, | ||
filename: 'root' | ||
}); | ||
}); | ||
await Promise.all(promises); | ||
|
||
return this.res; | ||
} | ||
|
||
async readFile(file) { | ||
if (this.fileCache[file]) { | ||
return this.fileCache[file]; | ||
} | ||
return readFile(file, { | ||
encoding: 'utf8' | ||
}); | ||
} | ||
|
||
async walk(id, parent) { | ||
const extensions = this.options.extensions; | ||
const sortKey = parent.sortKey || ''; | ||
let file = null; | ||
|
||
try { | ||
file = await resolve(id, { ...parent, extensions }); | ||
} catch (err) { | ||
if (err.code === 'MODULE_NOT_FOUND') { | ||
console.warn(`module not found: "${id}" from file ${parent.filename}`); | ||
return; | ||
} | ||
throw err; | ||
} | ||
|
||
if (this.visited[file] || resolveExst.includes(path.extname(file))) { | ||
return file; | ||
} | ||
this.visited[file] = true; | ||
|
||
const source = await this.readFile(file); | ||
const depsArray = this.parseDeps(file, source); | ||
if (!depsArray) { | ||
return file; | ||
} | ||
|
||
const deps = {}; | ||
const promises = depsArray.map(async (id, i) => { | ||
const filter = this.options.filter; | ||
if (filter && !filter(id)) { | ||
deps[id] = false; | ||
return; | ||
} | ||
const number = i.toString().padStart(8, '0'); | ||
deps[id] = await this.walk(id, { | ||
filename: file, | ||
basedir: path.dirname(file), | ||
sortKey: sortKey + '!' + file + ':' + number | ||
}); | ||
}); | ||
|
||
await Promise.all(promises); | ||
|
||
this.res.push({ | ||
id: file, | ||
source, | ||
deps, | ||
file, | ||
sortKey: sortKey + '!' + file | ||
}); | ||
return file; | ||
} | ||
|
||
parseDeps(file, src) { | ||
try { | ||
try { | ||
return konan(src).strings; | ||
} catch (ex) { | ||
// konan does not support Vue (component) file, try to parse using detective (as a fallback) | ||
return detective(src); | ||
} | ||
} catch (ex) { | ||
console.error(`Parsing file ${file}: ${ex}`); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
export default async function (input = [], opts = {}) { | ||
const dep = new Deps(opts); | ||
return dep.flush(Array.from(new Set(input))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abb781a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@anthony-redFox after this PR, if I have
it will get parsed, before this PR this was not the case, is this intended?
abb781a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I'll take a look
abb781a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mmomtchev Did you use external option? if yes then it is expected because just to support external resources is difficult because now days package.json can contains many entry points which valid for cjs, ES Modules, browser entry point.
So as BREKING CHANGES you can just add you external library as input and you will get the same result if the code just parse package.json and try to guess which file need to parse.