web-infra-dev / rspack

The fast Rust-based web bundler with webpack-compatible API 🦀️
https://rspack.dev
MIT License
8.15k stars 484 forks source link

Unable to resolve variables defined in definePlugin in rspack mode #4926

Closed nanianlisao closed 6 months ago

nanianlisao commented 7 months ago

System Info

pnpm@8.6.0

Details

I'm encountering an issue where variables defined in definePlugin are not being resolved in rspack mode. Specifically, the variable "ddd" defined in defineConfig's define property is not being resolved when used in an ejs template.

Reproduce link

https://github.com/nanianlisao/rspack-bug-reproduction

Reproduce Steps

  1. pnpm i
  2. pnpm run build:webpack
  3. pnpm run build:rspack

Expected behavior: build successed!

Actual behavior: webpack build successed but rspack build failed!

image

Please let me know if you need any further information. Thank you!

github-actions[bot] commented 7 months ago

Hello @nanianlisao, sorry we can't investigate the problem further without reproduction demo, please provide a repro demo by forking rspack-repro, or provide a minimal GitHub repository by yourself. Issues labeled by need reproduction will be closed if no activities in 14 days.

nanianlisao commented 7 months ago

@hardfist please take another look . Thx!

hardfist commented 7 months ago

image add a filter here and it works, rspack doesn't support pass false to plugin yet, we will fix it

nanianlisao commented 7 months ago

Okay, but that doesn't seem to be the key. Even if I do it this way, there will still be this error

puppet-666 commented 7 months ago

Okay, but that doesn't seem to be the key. Even if I do it this way, there will still be this error

You need to use the DefinePlugin in rspack

nanianlisao commented 7 months ago

Okay, but that doesn't seem to be the key. Even if I do it this way, there will still be this error

You need to use the DefinePlugin in rspack

I have tried, but the results have not been any different

puppet-666 commented 7 months ago

Okay, but that doesn't seem to be the key. Even if I do it this way, there will still be this error

You need to use the DefinePlugin in rspack

I have tried, but the results have not been any different

  plugins: [
    new HtmlWebpackPlugin({
      template: "./src/document.ejs",
    }),

    isRunningWebpack &&
      new webpack.DefinePlugin({
        title: JSON.stringify("custom title"),
      }),

    isRunningRspack &&
      new rspack.DefinePlugin({
        title: "'custom title'",
      }),
  ].filter(Boolean),
puppet-666 commented 7 months ago

I see that the title is used in html in the reproduction repository, but you are defining it in DefinePlugin, perhaps htmlrspackplugin is what you need to use?

nanianlisao commented 7 months ago

I see that the title is used in html in the reproduction repository, but you are defining it in DefinePlugin, perhaps htmlrspackplugin is what you need to use?

This is just a simple example to demonstrate this phenomenon. In fact, there are a large number of variables in the template that are replaced by definePlugin. This feature works normally in webpack, but an error occurred when switching to rspack.

What I mainly hope to convey is this meaning.

Additionally, using htmlrspackplugin may not achieve the expected results.

nanianlisao commented 7 months ago

Using webpack allows for the direct use of define defined variables in the template, which cannot be achieved in rspack.

ahabhgk commented 6 months ago

I will take a look in next week

ahabhgk commented 6 months ago

It's a bug, https://github.com/web-infra-dev/rspack/pull/5093 will fix it

ahabhgk commented 6 months ago

The fix will land in 0.4.5 in next week