-
-
Notifications
You must be signed in to change notification settings - Fork 735
feat(rslib): handle hashbang / react directives natively #12168
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
Conversation
✅ Deploy Preview for rspack canceled.
|
📦 Binary Size-limit
❌ Size increased by 18.00KB from 47.45MB to 47.47MB (⬆️0.04%) |
CodSpeed Performance ReportMerging #12168 will not alter performanceComparing Summary
|
0fdeffc to
5ce14bf
Compare
e3d9b07 to
4f12372
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds native support for handling hashbang (#!/usr/bin/env node) and React directives ("use client", "use server") in rslib by implementing parser plugins that extract these elements from source code and re-insert them at the correct positions in the output, along with automatic file permission setting for hashbang files.
Key Changes:
- Added
HashbangParserPluginandReactDirectivesParserPluginto extract hashbang and directives during parsing - Implemented render hooks in both
RslibPluginandEsmLibraryPluginto re-inject extracted content at the beginning of output files - Added automatic
chmod 0o755for files with hashbang via theCompilerAssetEmittedhook
Reviewed Changes
Copilot reviewed 17 out of 18 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/rspack-test/configCases/rslib/react-directives/test.js | Test suite for React directives handling across CJS and ESM formats |
| tests/rspack-test/configCases/rslib/react-directives/test.config.js | Test configuration specifying bundle selection |
| tests/rspack-test/configCases/rslib/react-directives/rspack.config.js | Build configurations for testing directives in CJS, ESM, and ESM with EsmLibraryPlugin |
| tests/rspack-test/configCases/rslib/react-directives/index.js | Source file with "use client" directive for testing |
| tests/rspack-test/configCases/rslib/hashbang-and-chmod/test.js | Test suite for hashbang extraction and executable permissions |
| tests/rspack-test/configCases/rslib/hashbang-and-chmod/test.config.js | Test configuration for hashbang tests |
| tests/rspack-test/configCases/rslib/hashbang-and-chmod/rspack.config.js | Build configurations for testing hashbang across formats |
| tests/rspack-test/configCases/rslib/hashbang-and-chmod/index.js | Source file with hashbang for testing |
| crates/rspack_plugin_rslib/src/react_directives_parser_plugin.rs | Parser plugin that extracts directives starting with "use " from source code |
| crates/rspack_plugin_rslib/src/plugin.rs | Integration of parser plugins and render hooks, plus asset_emitted hook for chmod |
| crates/rspack_plugin_rslib/src/lib.rs | Module exports for new parser plugins |
| crates/rspack_plugin_rslib/src/hashbang_parser_plugin.rs | Parser plugin that extracts hashbang from source code |
| crates/rspack_plugin_rslib/Cargo.toml | Added rspack_fs dependency for file permissions |
| crates/rspack_plugin_esm_library/src/render.rs | Added hashbang and directives handling via InitFragments for ESM output |
| crates/rspack_core/src/utils/mod.rs | Added utility functions to retrieve hashbang and directives from module build_info |
| crates/rspack_binding_api/src/fs_node/write.rs | Updated chmod call to properly wrap arguments with FnArgs |
| crates/rspack_binding_api/src/fs_node/node.rs | Added type alias for chmod function signature |
| Cargo.lock | Updated dependencies for rspack_plugin_rslib |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Summary
related Rslib PR web-infra-dev/rslib#1331.
Overview
This PR adds native support for handling hashbang (
#!/usr/bin/env node) and React directives ("use client","use server"and whatever the very first comment that starts with"use ") in bothRslibPluginandEsmLibraryPlugin.Key Features
1. Hashbang Support
0o755) for files with hashbang2. React Directives Support
"use client"and"use server""use strict"if present)Implementation Details
Parser Plugins:
implementated in RslibPlugin, so RslibPlugin is required, which means it won't work with
EsmLibraryPluginalone.HashbangParserPlugin: Extracts hashbang from source codeReactDirectivesParserPlugin: Extracts React directives from source codebuild_info.extrasRender Integration:
RslibPlugin: Direct source manipulation during render phase for non-ESM formatsEsmLibraryPlugin: UsesInitFragmentmechanism for ESM formatFile Permissions:
CompilerAssetEmittedhookrspack_fs::FilePermissionsfor cross-platform compatibilityUtility Functions:
get_module_hashbang()andget_module_directives()inrspack_coreRelated links
Checklist