Skip to content

Commit

Permalink
fix(bundling): resolve index files from ts paths when running esbuild…
Browse files Browse the repository at this point in the history
… without bundling (#23098)

## Current Behavior
During build process a file `tmp/<path>/main-with-require-overrides.js`
is generated to handle `paths` resolutions from `tsconfig` when
`esbuild` is not running in `bundle` mode.

Having following `tsconfig.json` as example
```json
{
  ...
  "compilerOptions": {
    ...
    "paths": {
      "@lib/lib1": ["libs/lib1/src/index.ts"],
      "@app1/*": ["apps/app1/src/*"],
    }
  }
}
```

We can use `lib1` in code as follows and during runtime it will
automatically be resolved to `dist/apps/app1/libs/lib1/src/index.js`:
```js
// apps/app1/src/main.ts
import lib1 from '@lib/lib1';
```

Hovewer, when trying to use paths with wildcards, e.g.:
```js
// apps/app1/src/config/index.ts
const config = {};
export default config;

// apps/app1/src/main.ts
import config from '@app1/config';
```

It gets resolved to `dist/apps/app1/apps/app1/src/config.js` and
`isFile` condition fails as such module doesn't exist, thus path is not
replaced and runtime error is produced.

## Expected Behavior
During resolution of following code:
```js
import config from '@app1/config';
```

`_resolveFilename` should consider all possible combinations of module
path - `dist/apps/app1/apps/app1/src/config.js` and
`dist/apps/app1/apps/app1/src/config/index.js`
  • Loading branch information
andriizavoiko authored May 8, 2024
1 parent 7dd7c2b commit 46336df
Showing 1 changed file with 3 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ Module._resolveFilename = function(request, parent) {
const match = request.match(re);
if (match?.groups) {
const candidate = path.join(distPath, entry.pattern.replace("*", ""), match.groups.rest + ".js");
const candidate = path.join(distPath, entry.pattern.replace("*", ""), match.groups.rest);
if (isFile(candidate)) {
found = candidate;
}
Expand All @@ -278,7 +278,8 @@ Module._resolveFilename = function(request, parent) {
function isFile(s) {
try {
return fs.statSync(s).isFile();
require.resolve(s);
return true;
} catch (_e) {
return false;
}
Expand Down

0 comments on commit 46336df

Please sign in to comment.