Skip to content
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

Allow to set module alias via Node.js API #266

Closed
egoist opened this issue Jul 17, 2020 · 7 comments
Closed

Allow to set module alias via Node.js API #266

egoist opened this issue Jul 17, 2020 · 7 comments

Comments

@egoist
Copy link

egoist commented Jul 17, 2020

Use Case

I made a library that uses esbuild as the bundler and I need to set alias to some generated files (whose output path is based on user configuration), that would make it easier to access in the library itself:

// Instead of:
import { routes } from './some/path/to/.cache/generated-routes.js'
// I can do:
import { routes } from '@cache/generated-routes.js'

Frameworks can also use this option to define some built-in aliases like @ to current directory so users won't have to do that in tsconfig.json or jsconfig.json

@evanw
Copy link
Owner

evanw commented Jul 25, 2020

This sounds like a good use case for plugins. I believe the work-in-progress plugin API described in #111 should be able to address this need.

@imedadel
Copy link

Hey @evanw, I hope you're doing well :) Can --define:K=V be used to substitute react with preact/compat during parsing? (and therefore it can be used to substitute ./some/path/to/.cache with @cache too) I tried doing that but it didn't work. I guess it's only for boolean values then?

@evanw
Copy link
Owner

evanw commented Nov 11, 2020

Closing this because you should be able to do this with the plugin API. The API is documented here: https://esbuild.github.io/plugins/.

@evanw evanw closed this as completed Nov 11, 2020
@PaulBGD
Copy link

PaulBGD commented Feb 28, 2021

Hello! This Issue comes up when Googling for making preact compat work, I wrote a little plugin if anyone wants an idea of how to make this work:

const preactCompatPlugin = {
    name: "preact-compat",
    setup(build) {
        const path = require("path");
        const preact = path.join(process.cwd(), "node_modules", "preact", "compat", "dist", "compat.module.js");

        build.onResolve({filter: /^(react-dom|react)$/}, args => {
            return {path: preact};
        });
    }
}

If there was a way to just replace the dependency name I couldn't find one, but these seems to work fine for now.

@osdevisnot
Copy link
Contributor

I wonder if you can use NPM alias technique like below with esbuild:

https://github.com/developit/nextjs-preact-demo/blob/master/package.json#L18

cc: @PaulBGD

@iamtekeste
Copy link

iamtekeste commented Jul 12, 2021

Thank you for your help @PaulBGD and you are right that is exactly how I ended up finding your solution 😄

@DTrejo
Copy link

DTrejo commented Oct 20, 2021

Thanks for your help everyone :) @osdevisnot that was exactly what I needed.
I elaborated on it over here in an issue on the preact project: preactjs/preact#3287
Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants