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

Rspack alias not working as expected #2914

Closed
dgmachado opened this issue Apr 24, 2023 · 12 comments
Closed

Rspack alias not working as expected #2914

dgmachado opened this issue Apr 24, 2023 · 12 comments
Assignees
Labels
bug Something isn't working

Comments

@dgmachado
Copy link
Contributor

System Info

System:
OS: macOS 13.1
CPU: (10) arm64 Apple M1 Pro
Memory 32GB
Shell: /bin/zsh

Binaries:
node: 16.13.2
npm: 9.5.0
yarn: 1.22.19

Browsers:
Chrome: 111.0.5563.146

npmPackages:
@nrwl/rspack: 15.9.5
@rspack/core: ~0.1.0
@rspack/dev-server: ~0.1.0
@rspack/plugin-minify: ~0.1.0

Details

The Rspack alias isn't working as expected. As you can see below only scenario 1 works.

resolve: {
    alias: {
      // # Scenario 1
      //  It works
      './environments/environment': '/..{absolutePathToTheApp}/nestapp/src/environments/environment.prod.ts',
      //
      // # Scenario 2 
      // it should work also but it doesn't 
      './environments/environment': './environments/environment.prod.ts',
      //
      // # Scenario 3 (relative path)
      // it should work also but it doesn't 
      './environments/environment.ts': './environments/environment.prod.ts',
    }
  }

Reproduce link

No response

Reproduce Steps

I've created a sample app you can use the branch "build" and to test you can execute the below commands:

npm run build-rspack
npm run serve-rspack

What is expected:

Environment.production:true

What is appearing:

Environment.production:false
@dgmachado dgmachado added bug Something isn't working pending triage The issue/PR is currently untouched. labels Apr 24, 2023
@hyf0
Copy link
Collaborator

hyf0 commented Apr 25, 2023

I would suggest using https://github.com/web-infra-dev/rspack-repro to confirm whether Webpack works as expected.

@hyf0 hyf0 added need more info and removed pending triage The issue/PR is currently untouched. labels Apr 25, 2023
@dgmachado
Copy link
Contributor Author

@hyf0 I just created two alias scenarios not working in Rspack as expected. I just invited you as a contributor on the repo. In summary, the below scenarios are working in Webpack and not in Rspack.

const questionAbsolutePath = path.resolve(__dirname, "./src/question.js")

...
  resolve: {
    alias: {
     "./answer": path.resolve(__dirname, "./src/answer.prod.js?raw"),
     questionAbsolutePath: path.resolve(__dirname, "./src/question.prod.js?raw"),
    },
  },

@hyf0
Copy link
Collaborator

hyf0 commented Apr 25, 2023

Thank you for taking time to create the repo. It's a bug of Rspack and we plan to fix it.

@Boshen
Copy link
Contributor

Boshen commented May 12, 2023

@dgmachado Excuse me for the late reply, can you also add me as a contributor? I'll try and unblock this for you during the next week.

@dgmachado
Copy link
Contributor Author

@dgmachado Excuse me for the late reply, can you also add me as a contributor? I'll try and unblock this for you during the next week.

@Boshen sure. I just invited you.

@Boshen
Copy link
Contributor

Boshen commented May 16, 2023

@dgmachado Your repros are some what confusing for me, you are mixing a few scenarios in different contexts.

For your real app, which one is broken?

If it's scenario 3 from the first nest repo, I can't replicate it with Webpack. This is done by changing everything to .ts in the second repo:

  resolve: {
    extensions: [".ts", ".js"],
    alias: {
     "./question.ts": path.resolve(__dirname, "./src/question.prod.ts?raw"),
    },
  },
> WEBPACK=1 pnpm webpack -c ./config.mjs

asset main.js 3.96 KiB [emitted] (name: main)
runtime modules 670 bytes 3 modules
cacheable modules 293 bytes
  ./src/index.ts 216 bytes [built] [code generated]
  ./src/answer.prod.ts?raw 37 bytes [built] [code generated]
  ./src/question.ts 40 bytes [built] [code generated]
webpack 5.79.0 compiled successfully in 86 ms

Notice it's resolved to ./src/question.ts, not the prod one.


If it's this example:

   alias: {
     questionAbsolutePath: path.resolve(__dirname, "./src/question.prod.js?raw"),
    },

It's not going to work because it's not evaluated, did you mean

   alias: {
     [questionAbsolutePath]: path.resolve(__dirname, "./src/question.prod.js?raw"),
    },

But this is the not same as scenario as the previous one?

I'm getting super confused here.

@dgmachado
Copy link
Contributor Author

@Boshen thanks for the feedback. I just pushed a new commit into the repo repo and branch bug/rspack-alias.

In my scenario, I need the below config working as webpack:

[questionAbsolutePath]: path.resolve(__dirname, "./src/question.prod.js"),

You can compare the results by executing the below commands:

npm run build:rspack
npm run build:webpack

@Boshen
Copy link
Contributor

Boshen commented May 17, 2023

Thank you for the confirmation, linking to

@bvanjoi
Copy link
Collaborator

bvanjoi commented May 26, 2023

Thanks for the report. The issue was caused by a lack of re-doResolve process after normalizing the request. Although it is a bug and needs to be fixed, it can be avoided by using a relative alias key. Therefore, it has been given a low priority.

@stale
Copy link

stale bot commented Jul 25, 2023

This issue has been automatically marked as stale because it has not had recent activity. If this issue is still affecting you, please leave any comment (for example, "bump"). We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

@stale stale bot added the stale label Jul 25, 2023
@dgmachado
Copy link
Contributor Author

Thanks for the report. The issue was caused by a lack of re-doResolve process after normalizing the request. Although it is a bug and needs to be fixed, it can be avoided by using a relative alias key. Therefore, it has been given a low priority.

@bvanjoi thank you for the heads up.

@hardfist
Copy link
Contributor

hardfist commented Sep 16, 2023

@dgmachado this bug is fixed in new resolver implmentation, you can test by enable new resolver

// rspack.config.js
module.exports = {
  experiments:  {
   rspackFuture: {
      newResolver: true
     }
  }
}

and you can see a example here https://github.com/hardfist/rspack-absolute-alias/blob/fix-alias/config.mjs#L39

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants