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

fix: adjusted experimental rollup plugins config #52

Merged
merged 2 commits into from
Nov 4, 2024
Merged

Conversation

prisis
Copy link
Contributor

@prisis prisis commented Nov 4, 2024

Refactored the plugin api away from before/after plugin name, to enforce pre/post and type build/dts where build is default.

Summary by CodeRabbit

Release Notes

  • New Features

    • Added comprehensive documentation sections on Plugins, Aliases, ESM ⇄ CJS interoperability, Environment variables, and Validating in the README.md for improved user guidance.
  • Improvements

    • Enhanced plugin configuration and management for the Visulima Packem bundler, allowing for better organization and application order of plugins.
    • Updated test suite to reflect changes in plugin handling and enforcement logic.
  • Bug Fixes

    • Clarified documentation regarding alias configurations and environment variable handling.

These updates aim to enhance the user experience by providing clearer instructions and more flexible plugin management.

BREAKING CHANGE: Refactored the plugin api away from before/after plugin name, to enforce pre/post and type build/dts where build is default.
Copy link
Contributor

coderabbitai bot commented Nov 4, 2024

Caution

Review failed

The pull request is closed.

Walkthrough

The pull request introduces significant enhancements to the Visulima Packem bundler documentation and its plugin management system. Key additions to the README.md include sections on Plugins, Aliases, ESM ⇄ CJS interoperability, Environment variables, and Validating. The get-rollup-options.ts file is updated to improve plugin handling with a new sorting function, while the RollupPlugins type is redefined for better clarity. Additionally, the utility functions in append-plugins.ts are removed, streamlining the plugin management approach.

Changes

File Path Change Summary
packages/packem/README.md - Added sections: Plugins, Aliases, ESM ⇄ CJS interoperability, Environment variables, Validating.
packages/packem/src/rollup/get-rollup-options.ts - Added type: RollupPlugins.
- Added function: sortUserPlugins.
- Updated signatures for baseRollupOptions, getRollupOptions, and getRollupDtsOptions.
packages/packem/src/rollup/utils/append-plugins.ts - Removed file and its functions related to plugin management.
packages/packem/src/types.ts - Updated RollupPlugins type definition to include an optional enforce property.
- Removed RollupPluginName type.
packages/packem/tests/intigration/packem-config.test.ts - Updated test cases to reflect changes in plugin handling and configuration logic, including assertions for enforce property.

Possibly related PRs

🐰 In the meadow, plugins align,
With order and clarity, oh how they shine!
From aliases to envs, all laid out clear,
Packem's new guide brings us cheer!
Hop along, dear friends, let’s bundle with glee,
For every new feature, there's joy to see! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

github-actions bot commented Nov 4, 2024

Thank you for following the naming conventions! 🙏

Copy link

pkg-pr-new bot commented Nov 4, 2024

Open in Stackblitz

pnpm add https://pkg.pr.new/visulima/packem/@visulima/packem@52

commit: e2a3dfe

@prisis prisis changed the title feat: adding a new plugin api for packem fix: adding a new plugin api for packem Nov 4, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (7)
packages/packem/README.md (3)

402-419: Plugin API documentation looks good, but could be more comprehensive.

The new plugin API documentation clearly explains the enforce property and type options. The plugin resolution order is well documented.

Consider adding:

  • Examples for "dts" type plugins
  • Common use cases for "pre" vs "post" enforcement

420-435: Configuration example is clear and practical.

The example effectively demonstrates the new plugin API usage with a real-world plugin.

Consider adding an example that shows:

 {
     enforce: "pre",
     plugin: optimizeLodashImports(),
-    // type: "build" -> default value
+    type: "dts",
 }

402-435: Add migration guide for breaking changes.

Since this is a breaking change from the previous "before"/"after" naming convention to "pre"/"post", it would be helpful to include a migration guide section.

Consider adding:

  1. A section titled "Migrating from previous versions"
  2. A table showing the mapping between old and new plugin configurations
  3. Examples of how to update existing plugins to the new format
packages/packem/src/types.ts (1)

68-72: Consider documenting the new RollupPlugins structure

Adding inline documentation or comments explaining the new RollupPlugins structure can improve maintainability and help other developers understand the purpose of the enforce and type properties.

Apply this suggestion to add comments:

 export type RollupPlugins = {
+    /**
+     * Specifies the plugin's enforcement phase.
+     * - "pre": Plugin will be applied before regular plugins.
+     * - "post": Plugin will be applied after regular plugins.
+     */
     enforce?: "pre" | "post";
     plugin: Plugin;
+    /**
+     * Specifies the type of the plugin.
+     * - "build": Plugin is applied during the build phase.
+     * - "dts": Plugin is applied during the declaration type script phase.
+     * @default "build"
+     */
     type?: "build" | "dts";
 }[];
packages/packem/src/rollup/get-rollup-options.ts (3)

498-499: Simplify conditional checks using optional chaining

To improve readability, you can replace the chained logical conditions with optional chaining:

context.options.rollup.css?.loaders?.length > 0 &&

This refactor makes the condition more concise without altering its logic.

🧰 Tools
🪛 Biome

[error] 498-499: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)


518-519: Use optional chaining for cleaner conditional expressions

Consider simplifying the condition using optional chaining:

context.options.rollup.css?.loaders?.length > 0 &&

This approach enhances code clarity by reducing redundancy.

🧰 Tools
🪛 Biome

[error] 518-519: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)


795-796: Simplify conditional check using optional chaining

You can refactor the condition to use optional chaining for clarity:

context.options.rollup.license?.path &&

This simplifies the check while maintaining the intended logic.

🧰 Tools
🪛 Biome

[error] 795-796: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 46b9b50 and 066e296.

📒 Files selected for processing (4)
  • packages/packem/README.md (1 hunks)
  • packages/packem/src/rollup/get-rollup-options.ts (7 hunks)
  • packages/packem/src/rollup/utils/append-plugins.ts (0 hunks)
  • packages/packem/src/types.ts (1 hunks)
💤 Files with no reviewable changes (1)
  • packages/packem/src/rollup/utils/append-plugins.ts
🧰 Additional context used
🪛 Biome
packages/packem/src/rollup/get-rollup-options.ts

[error] 498-499: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)


[error] 518-519: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)


[error] 597-598: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)


[error] 795-796: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

🔇 Additional comments (39)
packages/packem/src/types.ts (2)

68-72: Verify that all references to RollupPluginName are removed

The type RollupPluginName has been removed from the codebase. Ensure that all references to this type have been updated or removed to prevent type errors.

Run the following script to check for any remaining references to RollupPluginName:

✅ Verification successful

Since the rg command returned no results, indicating that there are no occurrences of RollupPluginName in the codebase, I can generate the final response.

All references to RollupPluginName have been successfully removed

The verification confirms that there are no remaining references to RollupPluginName in the codebase, indicating that the type removal was properly handled.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for any remaining uses of 'RollupPluginName' in the codebase.

# Test: Find occurrences of 'RollupPluginName'.
rg 'RollupPluginName'

Length of output: 21


68-72: ⚠️ Potential issue

Ensure all plugin configurations use the updated RollupPlugins structure

The RollupPlugins type has been updated to use an optional enforce property replacing the previous before and after properties. Please verify that all plugin configurations throughout the codebase have been updated to match this new structure, and there are no remaining references to before or after.

Run the following script to identify any remaining references to before or after in plugin configurations:

✅ Verification successful

Based on the search results, I can now provide a final response:

The codebase has been updated to use the new enforce property correctly

The search results confirm that:

  1. The new RollupPlugins type with enforce property is properly defined in packages/packem/src/types.ts
  2. The README.md documents the new plugin ordering using enforce: 'pre' and enforce: 'post'
  3. The only reference to old before/after properties is in a test file (packem-build-jit.test.ts) which appears to be testing legacy behavior

The test file should be updated to use the new enforce property, but this is not a critical issue as it's just test code and doesn't affect runtime behavior.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for any remaining uses of 'before' or 'after' in plugin configurations.

# Test: Find occurrences of 'before' or 'after' properties in Rollup plugin definitions.
rg -A 3 -B 3 $'plugin:\\s*{[^}]*\\b(before|after)\\b[^}]*}'

Length of output: 191


Script:

#!/bin/bash
# Let's try a different approach to search for plugin configurations

# Search for files containing 'RollupPlugins' type references
echo "=== Files referencing RollupPlugins type ==="
rg -l "RollupPlugins"

# Search for plugin configurations with 'before' or 'after'
echo -e "\n=== Searching for 'before' or 'after' in plugin configurations ==="
rg -A 5 -B 5 "(?:before|after).*plugin.*:|plugin.*(?:before|after)"

# Search for actual plugin usage patterns
echo -e "\n=== Plugin configuration patterns ==="
rg -A 3 "enforce.*:|plugin.*:"

Length of output: 97317

packages/packem/src/rollup/get-rollup-options.ts (37)

21-21: Import of 'RollupPlugins' type approved

The addition of RollupPlugins to the imports ensures that the new type definitions are available for use in the file.


59-88: Function 'sortUserPlugins' correctly categorizes plugins

The new sortUserPlugins function effectively sorts plugins into pre, normal, and post categories based on their enforce property and the specified type. The logic accounts for undefined plugins and handles type-specific filtering appropriately.


375-376: Plugins sorted using 'sortUserPlugins' for 'build' type

Plugins are now sorted using the sortUserPlugins function for the "build" type, ensuring they are applied in the correct order during the build process.


378-378: 'baseRollupOptions' called with 'build' type

The baseRollupOptions function is correctly called with "build" as the type parameter, aligning with the updated function signature.


446-450: Adding essential plugins with caching for performance

Plugins such as resolveFileUrlPlugin and resolveTypescriptMjsCtsPlugin are added with caching mechanisms, which enhances build performance by avoiding redundant computations.


453-465: Conditional inclusion of 'replacePlugin' and 'aliasPlugin'

The code conditionally includes replacePlugin and aliasPlugin based on the Rollup configuration options. This allows for flexible plugin management tailored to the user's settings.


467-467: Including pre-plugins in the correct order

Pre-plugins are inserted into the plugins array, ensuring they are applied before other plugins during the build process.


469-469: Including 'nodeResolver' in plugins array

The nodeResolver plugin is appropriately added to the plugins array, facilitating module resolution.


471-475: Conditional addition of 'polyfillPlugin'

The polyfillPlugin is conditionally included when polyfillNode is enabled, with correct handling of source maps and user-defined options.


477-481: Inclusion of 'JSONPlugin' based on configuration

The JSONPlugin is included when configured in the Rollup options, enabling JSON file handling within the build.


482-484: Adding 'chunkSplitter' and optional 'wasmPlugin'

The chunkSplitter plugin is added to manage code splitting, and the wasmPlugin is conditionally included to support WebAssembly modules when enabled.


486-496: Including 'isolatedDeclarationsPlugin' for declaration files

The isolatedDeclarationsPlugin is conditionally included based on the declaration settings, ensuring proper generation of isolated TypeScript declaration files.


524-524: Inclusion of 'rawPlugin' with caching for performance

The rawPlugin is added with caching, improving performance when processing raw asset files.


526-528: Adding transformer plugin with proper configuration

The transformer plugin is included by invoking context.options.transformer with the appropriate configuration obtained from getTransformerConfig. Ensure the transformer is correctly set up in the context options.


530-537: Including 'preserveDirectivesPlugin' with caching

The preserveDirectivesPlugin is added with caching to maintain directive comments in the output, which is important for preserving certain JavaScript behaviors.


539-546: Conditional inclusion of 'shebangPlugin'

The shebangPlugin is added when the build entries are executable and shebang handling is enabled. This ensures that scripts retain their executable status after the build.


548-554: Including 'cjsInteropPlugin' for CommonJS interop

The cjsInteropPlugin is conditionally included to enhance interoperability between CommonJS and ES modules, based on the configuration settings.


556-557: Adding dynamic import handling plugins

The fixDynamicImportExtension and dynamicImportVarsPlugin are added when dynamic variable imports are enabled, ensuring proper handling of dynamic imports in the build.


568-575: Preserving dynamic imports when configured

A custom plugin is added to preserve dynamic imports when preserveDynamicImports is enabled, which may be necessary for certain runtime behaviors.


576-576: Inclusion of 'esmShimCjsSyntaxPlugin' for ES modules

The esmShimCjsSyntaxPlugin is conditionally added to shim CommonJS syntax in ES modules, aiding compatibility when shimming is enabled.


578-585: Adding 'jsxRemoveAttributes' plugin with caching

The jsxRemoveAttributes plugin is included with caching to remove specified attributes from JSX elements, which can be useful for optimizing the output.


587-587: Including post-plugins in the correct sequence

Post-plugins are appropriately added at the end of the plugins array, ensuring they are applied after other plugins.


589-636: Adding analysis and utility plugins conditionally

Plugins like metafilePlugin, copyPlugin, licensePlugin, prependDirectivePlugin, and node10CompatibilityPlugin are conditionally added based on the configuration, providing functionalities like bundle analysis, file copying, license management, directive prepending, and compatibility support.

🧰 Tools
🪛 Biome

[error] 597-598: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)


685-686: Sorting plugins for 'dts' type using 'sortUserPlugins'

In the DTS (declaration types) Rollup options, plugins are sorted using sortUserPlugins for the "dts" type, ensuring correct plugin application order.


736-738: Including essential plugins in DTS build

Essential plugins like resolveFileUrlPlugin and resolveTypescriptMjsCtsPlugin are included with caching in the DTS Rollup configuration.


740-743: Inclusion of 'JSONPlugin' in DTS build

The JSONPlugin is conditionally included in the DTS build based on the configuration, ensuring JSON files are handled correctly during type declaration generation.


745-754: Ignoring non-supported file types in DTS build

A custom plugin is added to ignore non-supported file types in the DTS build, preventing potential build errors from unsupported imports.


756-757: Resolving TypeScript paths and root directories

Plugins for resolving TypeScript configuration paths and root directories are conditionally included, aiding in accurate module resolution during the DTS build.


759-763: Conditional inclusion of 'replacePlugin' in DTS build

The replacePlugin is conditionally included in the DTS build to handle string replacements as configured.


765-771: Including 'aliasPlugin' with custom resolver in DTS build

The aliasPlugin is included with a custom resolver, ensuring that module aliases are correctly resolved during the DTS build.


773-777: Including pre-plugins and node resolver in DTS plugins

Pre-plugins and the nodeResolver are added to the DTS build's plugins array, maintaining consistency with the build configuration.


779-779: Including normal plugins in DTS build

Normal plugins are appropriately included in the DTS build, ensuring that plugins without 'pre' or 'post' enforcement are applied correctly.


781-788: Adding DTS plugin and 'cjsInteropPlugin' conditionally

The DTS plugin is included using memoization to optimize performance. The cjsInteropPlugin is conditionally added based on the configuration, similar to the build process.


789-790: Including 'patchTypescriptTypesPlugin' with caching

The patchTypescriptTypesPlugin is added with caching when type patching is enabled, allowing for modifications to generated type declarations.


791-792: Removing shebangs in DTS build

The removeShebangPlugin is included to strip shebang lines from type declaration files, preventing syntax errors.


793-794: Including post-plugins in DTS build

Post-plugins are added at the end of the DTS build's plugins array, ensuring proper plugin sequencing.


227-227: Updated 'baseRollupOptions' function signature

The type parameter in baseRollupOptions has been updated to accept "build" or "dts" instead of "dependencies" or "dts". Ensure that all invocations of this function are updated to reflect the new parameter values and that any internal logic dependent on type is consistent with this change.

To confirm that all calls have been updated, you can run the following script:

✅ Verification successful

Function signature change verified successfully

All calls to baseRollupOptions have been updated correctly to use the new type parameter values:

  • Using "build" type in one call
  • Using "dts" type in another call

No instances of the old "dependencies" type parameter were found in the codebase.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that 'baseRollupOptions' calls use the new 'type' values

# Test: Search for calls to 'baseRollupOptions' with the old 'dependencies' type
# Expected: No occurrences found
rg 'baseRollupOptions\([^\)]*,"dependencies"\)'

Length of output: 49


Script:

#!/bin/bash
# Let's search for all calls to baseRollupOptions to verify the type parameter
rg 'baseRollupOptions\(' -A 2

# Also search for the function definition and its usage context
ast-grep --pattern 'baseRollupOptions = $_'

Length of output: 560

@prisis prisis changed the title fix: adding a new plugin api for packem fix: adjusted experimental rollup plugins config Nov 4, 2024
@prisis prisis merged commit 1573157 into main Nov 4, 2024
19 of 22 checks passed
@prisis prisis deleted the plugins-rework branch November 4, 2024 23:41
prisis pushed a commit that referenced this pull request Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant