Esbuild: Allow to set module alias via Node.js API

Created on 17 Jul 2020  路  3Comments  路  Source: evanw/esbuild

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

Most helpful comment

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.

All 3 comments

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.

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?

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ojanvafai picture ojanvafai  路  3Comments

lolychee picture lolychee  路  4Comments

mohsen1 picture mohsen1  路  3Comments

fannheyward picture fannheyward  路  4Comments

tonyhb picture tonyhb  路  3Comments