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

[Bug]: failed to render template from string: ReferenceError: process is not defined #7926

Closed
cbbfcd opened this issue Sep 19, 2024 · 4 comments
Labels
bug Something isn't working pending triage The issue/PR is currently untouched.

Comments

@cbbfcd
Copy link

cbbfcd commented Sep 19, 2024

System Info

System:
OS: macOS 14.4.1
CPU: (8) arm64 Apple M1
Memory: 569.81 MB / 16.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 20.10.0 - ~/.nvm/versions/node/v20.10.0/bin/node
Yarn: 1.22.21 - ~/.nvm/versions/node/v20.10.0/bin/yarn
npm: 10.2.3 - ~/.nvm/versions/node/v20.10.0/bin/npm
pnpm: 9.4.0 - ~/.nvm/versions/node/v20.10.0/bin/pnpm
bun: 1.1.5 - ~/.bun/bin/bun
Browsers:
Chrome: 128.0.6613.138
Safari: 17.4.1

Details

I'm not sure if it's a bug or by design. In my code, I used rspack.HtmlRspackPlugin, and then my html has a comment:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Example</title>
    <!-- <script src="/<%= process.env.REACT_APP_RESOURCE_DOMAIN %>/test.js"></script> -->
  </head>
  <body>
    <div id="root"></div>
  </body>
</html>

Then an error occurred with the message: HtmlRspackPlugin: failed to render template from string: ReferenceError: process is not defined;

So it goes into the logic here:

TemplateRender::Template(content) => {
        // process with template parameters
        let mut dj = Dojang::new();
        // align escape | unescape with lodash.template syntax https://lodash.com/docs/4.17.15#template which is html-webpack-plugin's default behavior
        dj.with_options(DojangOptions {
          escape: "-".to_string(),
          unescape: "=".to_string(),
        });

        dj.add_function_1("toHtml".into(), render_tag)
          .expect("failed to add template function `renderTag`");

        dj.add_with_option(self.url.clone(), content.clone())
          .expect("failed to add template");

        dj.render(&self.url, parameters).map_err(|err| {
          miette::Error::msg(format!(
            "HtmlRspackPlugin: failed to render template from string: {err}"
          ))
        })
      }

But since it's commented out, it should be ignored, right? So should the module in https://docs.rs/crate/rspack_dojang/latest/source/src/dojang.rs consider ignoring template code within comments?

Reproduce link

No response

Reproduce Steps

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Example</title>
    <!-- <script src="/<%= process.env.REACT_APP_RESOURCE_DOMAIN %>/test.js"></script> -->
  </head>
  <body>
    <div id="root"></div>
  </body>
</html>

use this html to build(with rspack.HtmlRspackPlugin).

@cbbfcd cbbfcd added bug Something isn't working pending triage The issue/PR is currently untouched. labels Sep 19, 2024
@cbbfcd cbbfcd changed the title [Bug]: [Bug]: failed to render template from string: ReferenceError: process is not defined Sep 19, 2024
@hardfist
Copy link
Contributor

seems a bug of dojang, since you spot the problem are you willing to fix it

@cbbfcd
Copy link
Author

cbbfcd commented Sep 19, 2024

@hardfist Certainly, I need to confirm that the expectation is to add a configuration that by default does not process comments, but also allows this configuration to be turned off (maintaining the current logic of processing all comments).

@cbbfcd
Copy link
Author

cbbfcd commented Sep 21, 2024

I tried to understand the implementation mechanism of the html-webpack-plugin and found that it has similar issues. The template engine might not handle comments and directly process variables. In this case, you can solve the problem through templateParams. Therefore, in terms of aligning with the capabilities of the html-webpack-plugin, the current html-plugin of Rspack seems reasonable and not a bug.

If modifications are needed, it would also be reasonable because the commented-out code should not have any actual code significance and does not need to process variables. This would require changes to the dojang crate, but it seems I didn't find where to modify this crate.

In conclusion, this seems to be a non-essential modification.

@hardfist
Copy link
Contributor

thanks for investigation, then we keep it as it is

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pending triage The issue/PR is currently untouched.
Projects
None yet
Development

No branches or pull requests

2 participants