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

fix(bundling): resolve index files from ts paths when running esbuild without bundling #23098

Merged
merged 7 commits into from May 8, 2024

Conversation

andriizavoiko
Copy link
Contributor

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

{
  ...
  "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:

// apps/app1/src/main.ts
import lib1 from '@lib/lib1';

Hovewer, when trying to use paths with wildcards, e.g.:

// 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:

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

andriizavoiko and others added 2 commits April 30, 2024 20:54
… without bundling

Replace isFile implementation to use Node require.resolve to handle cases when name of file (index)
is ommited in code
@andriizavoiko andriizavoiko requested a review from a team as a code owner April 30, 2024 18:06
Copy link

vercel bot commented Apr 30, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
nx-dev ✅ Ready (Inspect) Visit Preview May 7, 2024 8:38am

@andriizavoiko
Copy link
Contributor Author

Just found that this issue already has a PR #22054 - but with a different approach and still in draft, maybe we can close it once this once is merged (or that one)

@Coly010 Coly010 merged commit 46336df into nrwl:master May 8, 2024
6 checks passed
FrozenPandaz pushed a commit that referenced this pull request May 9, 2024
… 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`

(cherry picked from commit 46336df)
Copy link

This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants