Esbuild: feature request: module alias

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

Hello, I'm slowly moving a big react project to preact and as a workaround I have

alias: {
    "react": "preact/compat",
    "react-dom": "preact/compat",
},

in my webpack config, is there a way to do that with esbuild?

Most helpful comment

You should be able to do this with esbuild by creating a tsconfig.json or jsconfig.json file that looks something like this:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "react": ["./node_modules/preact/compat"],
      "react-dom": ["./node_modules/preact/compat"]
    }
  }
}

All 3 comments

You should be able to do this with esbuild by creating a tsconfig.json or jsconfig.json file that looks something like this:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "react": ["./node_modules/preact/compat"],
      "react-dom": ["./node_modules/preact/compat"]
    }
  }
}

@evanw, @OneOfOne, please tell me if it is possible to specify the path to the alias outside the project directory? These options give an error: "Could not resolve "react"":

"baseUrl": "/home/user/node_modules",
"paths": {
  "react": [
    "./preact/compat"
  ],
}
"baseUrl": "."
"paths": {
  "react": [
    "/home/user/node_modules/preact/compat"
  ],
}
"baseUrl": "."
"paths": {
  "react": [
    "../../node_modules/preact/compat"
  ],
}
"baseUrl": "."
"paths": {
  "react": [
    "preact/compat"
  ],
}

Not sure this use case is supported, but, I'm seeing this error in this setup:

  1. Parent project with dependency on Preact,
  2. 鈥sing an npm module with a peer dependency on React which is symlinked into the parent's node_modules with npm link.

The parent project has a tsconfig.json like this:

"baseUrl": ".",
    "paths": {
       "react": ["node_modules/preact/compat"],
       "react-dom": ["node_modules/preact/compat"],
    },

I then run this inside of the parent project:

npm link ../child

Then parent uses the child dependency with a symlink. At this point, esbuild prints:

../child/build/Component.js:1:23: error: Could not resolve "react"
import * as React from 'react';

Again, not sure this is a bug or not, but I thought I'd file this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lolychee picture lolychee  路  4Comments

vforsh picture vforsh  路  3Comments

elektronik2k5 picture elektronik2k5  路  3Comments

egoist picture egoist  路  3Comments

fannheyward picture fannheyward  路  4Comments