web-infra-dev / rspack

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

[Bug]: Incorrect parsing of `webpackInclude` #7356

Open elsassph opened 1 month ago

elsassph commented 1 month ago

System Info

  System:
    OS: macOS 14.5
    CPU: (10) arm64 Apple M1 Pro
    Memory: 1.06 GB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.17.1 - ~/.nvm/versions/node/v18.17.1/bin/node
    npm: 9.6.7 - ~/.nvm/versions/node/v18.17.1/bin/npm
    pnpm: 9.4.0 - ~/.nvm/versions/node/v18.17.1/bin/pnpm
    bun: 1.0.15 - ~/.bun/bin/bun
  Browsers:
    Chrome: 126.0.6478.183
    Safari: 17.5
  npmPackages:
    @rspack/cli: 1.0.0-beta.0 => 1.0.0-beta.0 
    @rspack/core: 1.0.0-beta.0 => 1.0.0-beta.0 

Details

rspack

PR #7055 added support for some Webpack "magic comments", but but webpackInclude is incorrect:

/* webpackInclude: /^.{2,}$/ */

Results in an error which seem to suggest the regex has been incorrectly extracted:

Magic comments parse failed: `webpackInclude` expected a regular expression, but received: /^.{2.

Looking at the PR it seems the extraction logic stops at ,: https://github.com/web-infra-dev/rspack/pull/7055/files#diff-d53d879b548a6442e88215394f482df179f48e7f98e20f401f84051fef5bd63cR132

Reproduce link

N/A

Reproduce Steps

N/A

LingyuCoder commented 1 month ago

Looking at the PR it seems the extraction logic stops at ,

Yes, this is an known issue. Webpack uses vm to eval the magic comments. But Rspack parses magic comments in rust, so we can only try to extract them with regular expressions. And also the rust std::regexp does not support (?!) so the extraction logic stops at , to get the regular expression of webpackInclude and webpackExclude.

You can just avoid to use ,. We are trying to find a better way to parse the magic comments.

elsassph commented 1 month ago

Good that you're aware, we'll work around the limitation.

FWIW it seems to me that Webpack parses those comments like JSON /* webpackInclude: /^.{2,}$/ */ would be parsed as { webpackInclude: /^.{2,}$/ }.