resolve root from babel root
This commit is contained in:
+2
-1
@@ -58,7 +58,8 @@ module.exports = function (api) {
|
|||||||
*/
|
*/
|
||||||
[
|
[
|
||||||
'./plugins/babel-plugin-lexicon-leaf-imports',
|
'./plugins/babel-plugin-lexicon-leaf-imports',
|
||||||
{roots: ['./src/lexicons']},
|
// Absolute path: the plugin must not depend on the host process's cwd.
|
||||||
|
{roots: [require('path').join(__dirname, 'src/lexicons')]},
|
||||||
],
|
],
|
||||||
|
|
||||||
// cannot use `env` field because it will put them after
|
// cannot use `env` field because it will put them after
|
||||||
|
|||||||
@@ -172,9 +172,18 @@ module.exports = function lexiconLeafImports(babel, options = {}) {
|
|||||||
* Absolute paths of barrel directories (e.g. <root>/src/lexicons). Matched
|
* Absolute paths of barrel directories (e.g. <root>/src/lexicons). Matched
|
||||||
* against the import specifier after resolving it relative to the importing
|
* against the import specifier after resolving it relative to the importing
|
||||||
* file, because babel-plugin-module-resolver has already turned '#/lexicons'
|
* file, because babel-plugin-module-resolver has already turned '#/lexicons'
|
||||||
* into a relative path by the time this plugin runs.
|
* into a relative path by the time this plugin runs. Relative entries are
|
||||||
|
* resolved against Babel's root, not process.cwd(): cwd depends on how the
|
||||||
|
* host process (Metro worker, Jest, an IDE runner) was launched, and a wrong
|
||||||
|
* base would silently disable every rewrite.
|
||||||
*/
|
*/
|
||||||
const roots = new Set((options.roots ?? []).map(r => path.resolve(r)))
|
let roots = null
|
||||||
|
function resolveRoots(state) {
|
||||||
|
if (!roots) {
|
||||||
|
const base = state.file.opts.root ?? state.cwd
|
||||||
|
roots = new Set((options.roots ?? []).map(r => path.resolve(base, r)))
|
||||||
|
}
|
||||||
|
}
|
||||||
const debug = !!process.env.BSKY_LEXICON_IMPORTS_DEBUG
|
const debug = !!process.env.BSKY_LEXICON_IMPORTS_DEBUG
|
||||||
const stats = {files: 0, rewrites: 0, bails: 0}
|
const stats = {files: 0, rewrites: 0, bails: 0}
|
||||||
|
|
||||||
@@ -244,6 +253,7 @@ module.exports = function lexiconLeafImports(babel, options = {}) {
|
|||||||
exit(programPath, state) {
|
exit(programPath, state) {
|
||||||
const filename = state.filename
|
const filename = state.filename
|
||||||
if (!filename) return
|
if (!filename) return
|
||||||
|
resolveRoots(state)
|
||||||
const targets = []
|
const targets = []
|
||||||
for (const stmt of programPath.get('body')) {
|
for (const stmt of programPath.get('body')) {
|
||||||
if (!stmt.isImportDeclaration()) continue
|
if (!stmt.isImportDeclaration()) continue
|
||||||
|
|||||||
Reference in New Issue
Block a user