-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose resolver logic to plugin context #641
Comments
Right now I'm thinking of eventually having something similar to Rollup's |
It would be very useful if the resolved logic is exposed and callable from the service itself. Would be amazing if it was possible to do await service.resolve(id: string, baseurl: string) [JS apis] |
I feel like it would be tricky at the service level because much of the
resolving logic is based on build-specific config. If that could also be
passed in it would certainly be nifty!
…On Tue., Jan. 12, 2021, 12:37 p.m. Salvatore Previti, < ***@***.***> wrote:
It would be very useful if the resolved logic is exposed and callable from
the service itself. Would be amazing if it was possible to do await
service.resolve(id: string, baseurl: string) [JS apis]
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#641 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAMG3TEPMLS4GFX6BQZEA3SZSCFVANCNFSM4VTB4CFQ>
.
|
One temporary way I am using, before better alternatives are available, is to use esbuild.build() and a plugin to extract the resolved path. const esbuild = require('esbuild')
/**
* Resolves a module using esbuild module resolution
*
* @param {string} id Module to resolve
* @param {string} [resolveDir] The directory to resolve from
* @returns {string} The resolved module
*/
async function esbuildResolve(id, resolveDir = process.cwd()) {
let _resolve
const resolvedPromise = new Promise((resolve) => (_resolve = resolve))
return Promise.race([
resolvedPromise,
esbuild
.build({
sourcemap: false,
write: false,
bundle: true,
format: 'esm',
logLevel: 'silent',
platform: 'node',
stdin: {
contents: `import ${JSON.stringify(id)}`,
loader: 'js',
resolveDir,
sourcefile: __filename
},
plugins: [
{
name: 'esbuildResolve',
setup(build) {
build.onLoad({ filter: /.*/ }, ({ path }) => {
id = path
_resolve(id)
return { contents: '' }
})
}
}
]
})
.then(() => id)
])
} |
This is just awesome that we can actually afford to call esbuild again to do stuff like this with only a 1ms overhead! 😃 I have a need to be able to call esbuild's internal resolution logic. So it would be better - and faster - to have this built in. |
Are there any plans to expose the native resolver logic? I've hacked around it just like @SalvatorePreviti, but the 1ms cost for every resolve feels pretty bad. I've also tried to use a long-running builder that keeps a single build running and resolves waiting files with promises etc, but it fails with names that have already been resolved to paths, since onLoad is only ever called once per resolved file. A simple idea I think could work would be to allow returning |
When authoring plugins, I have found myself wishing I could benefit from the
esbuild
resolver logic. This logic is quite sophisticated and would be difficult to accurately reproduce and maintain. I think it would be helpful for bothonResolve
andonLoad
plugins to be able to invoke a contextualresolve
function that would invokeesbuild
's internal resolution logic. Of course, such logic would need to avoid calling back into an invokingonResolve
hook or we risk turning the the node <--> native bus into an unintentional heat source.The text was updated successfully, but these errors were encountered: