Last active
January 22, 2021 22:53
-
-
Save JakobJingleheimer/837e7692dc14d2512330f2f2816fad8f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import path from 'path'; | |
import { pathToFileURL } from 'url'; | |
const npmPackage = await import( | |
pathToFileURL(`${process.cwd()}/package.json`).href | |
).then(({ default: json }) => json); | |
const aliases = (() => { | |
const base = process.cwd(); | |
const { aliases } = npmPackage || {}; | |
const absoluteAliases = Object.create(null); | |
for ( | |
let aliasKeys = Object.keys(aliases), | |
i = 0, | |
end = aliasKeys.length; | |
i < end; | |
i++ | |
) { | |
const aliasKey = aliasKeys[i]; | |
const aliasPath = aliases[aliasKey]; | |
if (aliasPath[0] !== '/') { | |
absoluteAliases[aliasKey] = path.join(base, aliasPath); | |
} | |
} | |
return absoluteAliases; | |
})(); | |
const aliasKeys = Object.keys(aliases); | |
const isAliasInSpecifier = (path, alias) => { | |
const aliasLength = alias.length; | |
return ( | |
path.indexOf(alias) === 0 | |
&& ( | |
path.length === aliasLength | |
|| path[aliasLength] === '/' | |
) | |
); | |
}; | |
export const resolve = ( | |
specifier, | |
parentModuleURL, | |
defaultResolve, | |
) => { | |
const aliasKey = aliasKeys.find((key) => isAliasInSpecifier(specifier, key)); | |
if (aliasKey) { | |
const url = path.join(aliases[aliasKey], specifier.substr(aliasKey.length)); | |
return { url }; | |
} | |
return defaultResolve(specifier, parentModuleURL, defaultResolve); | |
}; |
Author
JakobJingleheimer
commented
Jan 9, 2021
SyntaxError: Setter must have exactly one formal parameter.
at Loader.moduleStrategy (node:internal/modules/esm/translators:147:18)
however, there is no setter on translators.js:147
The above was due to an error in my own source-code, which node ESM erroneously reported as coming from the alias loader
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment