web-infra-dev / rspack

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

[Feature Request]: Support magical comment `import(/* rspackChunkName: "d" */ "./d")` #2688

Closed hyf0 closed 1 year ago

hyf0 commented 1 year ago

What problem does this feature solve?

Rspack has supported the same feature webpackChunkName that Webpack has in https://github.com/web-infra-dev/rspack/pull/2686.

AKA, Rspack has supported using import(/* webpackChunkName: "d" */ "./d") to specify the name of chunks.

I hope Rspack could also support using import(/* rspackChunkName: "d" */ "./d") to specify the name.

Guidance

You should improve the regexp in

https://github.com/web-infra-dev/rspack/blob/99bec3fec54dbea969c157a8236938235ad1b8c2/crates/rspack_plugin_javascript/src/visitors/dependency/scanner.rs#L101-L103

https://jex.im/regulex should be very helpful for writing the regexp.

Well, you should also change the function name to try_extract_chunk_name_from_magical_comment in :)

https://github.com/web-infra-dev/rspack/blob/99bec3fec54dbea969c157a8236938235ad1b8c2/crates/rspack_plugin_javascript/src/visitors/dependency/scanner.rs#L98

What does the proposed API of configuration look like?

.

hyf0 commented 1 year ago

I would have use a more complex Regex

\/\* (?:web|rs)packChunkName:\s+(["'`]?)((?:\.\/)?(?:\w[\w0-9\/\-]+)(?:[\w0-9\-]))\1\s+\*\/

(2nd group has to be used, 1st one is for quotes) This one is not perfect either, but it allows:


Wonderful guidance comes from @lalexdotcom in https://github.com/web-infra-dev/rspack/pull/2686#issuecomment-1503366533.

hardfist commented 1 year ago

what is the difference between webpackchunkname and rspackchunkname

hyf0 commented 1 year ago

what is the difference between webpackchunkname and rspackchunkname

The characters :)

hardfist commented 1 year ago

then i dont think it is necessary to support this, every unnecessary new api comes with a burden in the future

hyf0 commented 1 year ago

sounds reasonable.

lalexdotcom commented 1 year ago

I would say it another way: rspackChunkName is the feature, webpackChunkName is the compatibility layer with webpack... for example, this has been done for HtmlRspackPlugin

And by the way, the purpose of this updated regex is not (only) to introduce rspackChunkName: actually, I started looking at it to be able to create subfolders in chunkNames and use _ or - character...

hyf0 commented 1 year ago

started looking at it to be able to create subfolders in chunkNames and use _ or - character...

I will re-create an issue for this. Thanks for your feedback.