Skip to content

feat: add nodeProtocol option#336

Merged
sxzz merged 14 commits intorolldown:mainfrom
ryoppippi:node-module
Jun 22, 2025
Merged

feat: add nodeProtocol option#336
sxzz merged 14 commits intorolldown:mainfrom
ryoppippi:node-module

Conversation

@ryoppippi
Copy link
Contributor

@ryoppippi ryoppippi commented Jun 20, 2025

Description

This PR introduces a new nodeProtocol option that provides more flexible control over Node.js built-in module imports. It consolidates and enhances
the functionality of the existing removeNodeProtocol option.
fixes: #331

What this PR adds:

  • New nodeProtocol option with three modes:
    • true: Adds node: prefix to built-in modules (e.g., fsnode:fs)
    • 'strip': Removes node: prefix from imports (e.g., node:fsfs)
    • false: Keeps imports unchanged (default)
  • Deprecates removeNodeProtocol in favor of nodeProtocol: 'strip'
  • Maintains backward compatibility with existing removeNodeProtocol option

Implementation details:

  • Refactored NodeProtocolPlugin to handle both adding and stripping node: prefixes
  • Plugin now uses builtinModules from Node.js to accurately identify built-in modules
  • Early return optimization when the plugin is not needed

Linked Issues

N/A

Additional context

The implementation prioritizes the new nodeProtocol option when both nodeProtocol and removeNodeProtocol are specified. This ensures a smooth
migration path for users while maintaining backward compatibility.

All tests pass successfully, including comprehensive new tests covering various import scenarios (static imports, dynamic imports, subpath imports,
mixed imports with external packages).

@netlify
Copy link

netlify bot commented Jun 20, 2025

Deploy Preview for tsdown ready!

Name Link
🔨 Latest commit f9b79e0
🔍 Latest deploy log https://app.netlify.com/projects/tsdown/deploys/68577e107038dd00088c5e13
😎 Deploy Preview https://deploy-preview-336--tsdown.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Jun 20, 2025

Open in StackBlitz

npm i https://pkg.pr.new/tsdown@336

commit: f9b79e0

Comment on lines +12 to +16
const nodeProtocolOption =
options.nodeProtocol ??
// `removeNodeProtocol: true` means stripping the `node:` protocol which equales to `nodeProtocol: 'strip'`
// `removeNodeProtocol: false` means keeping the `node:` protocol which equales to `nodeProtocol: false` (ignore it)
(options.removeNodeProtocol ? 'strip' : false)
Copy link
Member

Choose a reason for hiding this comment

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

Move it to resolveOptions, and remove removeNodeProtocol from ResolvedOptions

*/
export function NodeProtocolPlugin(): Plugin {
export function NodeProtocolPlugin(
options: Pick<ResolvedOptions, 'nodeProtocol' | 'removeNodeProtocol'>,
Copy link
Member

Choose a reason for hiding this comment

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

Simplify it

src/index.ts Outdated

if (removeNodeProtocol) {
plugins.push(NodeProtocolPlugin())
if (removeNodeProtocol || nodeProtocol) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (removeNodeProtocol || nodeProtocol) {
if (nodeProtocol) {

src/index.ts Outdated
@@ -211,15 +211,16 @@ async function getBuildOptions(
report,
env,
removeNodeProtocol,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
removeNodeProtocol,

@@ -366,6 +385,7 @@ export type ResolvedOptions = Omit<
| 'outExtensions'
| 'hooks'
| 'removeNodeProtocol'
Copy link
Member

Choose a reason for hiding this comment

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

Remove it

@@ -0,0 +1,200 @@
import { describe, expect, test } from 'vitest'
Copy link
Member

Choose a reason for hiding this comment

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

👍 Great unit tests!

@ryoppippi
Copy link
Contributor Author

ryoppippi commented Jun 20, 2025

Okay I'll remove it

Move backward compatibility logic from NodeProtocolPlugin to resolveOptions function

This centralizes option resolution and simplifies plugin interface
Remove removeNodeProtocol from excluded fields

Add nodeProtocol to overwrite section with proper type
Remove complex options parameter and backward compatibility logic

Plugin now accepts single nodeProtocolOption parameter directly
Remove removeNodeProtocol from destructuring

Update condition to only check nodeProtocol

Pass resolved nodeProtocol directly to plugin
@ryoppippi ryoppippi requested a review from sxzz June 21, 2025 23:08
ryoppippi and others added 3 commits June 22, 2025 00:13
Remove references to the deprecated removeNodeProtocol option from both English and Chinese configuration documentation, as the function has been removed from the codebase in favor of nodeProtocol: "strip"
@sxzz sxzz merged commit b3f671a into rolldown:main Jun 22, 2025
15 checks passed
@ryoppippi ryoppippi deleted the node-module branch June 22, 2025 05:49
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.

addNodeProtocol: reverse option towards removeNodeProtocol

2 participants