tailwindcss/packages/@tailwindcss-node/src/esm-cache.loader.mts

41 lines
1,004 B
TypeScript
Raw Normal View History

fix(node): prefer Module#registerHooks over Module#register (#20028) <!-- 👋 Hey, thanks for your interest in contributing to Tailwind! **Please ask first before starting work on any significant new features.** It's never a fun experience to have your pull request declined after investing a lot of time and effort into a new feature. To avoid this from happening, we request that contributors create a discussion to first discuss any significant new features. For more info, check out the contributing guide: https://github.com/tailwindlabs/tailwindcss/blob/main/.github/CONTRIBUTING.md --> ## Summary <!-- Provide a summary of the issue and the changes you're making. How does your change solve the problem? --> Fix this warning when building apps with TailwindCSS with Node 26+: ``` (node:25346) [DEP0205] DeprecationWarning: `module.register()` is deprecated. Use `module.registerHooks()` instead. at node:internal/util:129:11 at Module.register (node:internal/modules/esm/loader:969:3) at file:///Users/antoine/Developer/my-app/node_modules/.pnpm/@tailwindcss+node@4.3.0/node_modules/@tailwindcss/node/dist/index.mjs:18:214 at ... ``` This PR: - Correctly prefers using `Module#registerHooks` instead of `Module#register` by checking its availability at runtime - Adjusts the exports of the hooks’ file by creating a synchronous version for the new API - Remove now unused exports from the `package.json`, relying on the [recommended usage in the docs](https://nodejs.org/docs/latest/api/module.html#registration-of-asynchronous-customization-hooks) for the `Module#register` calls ## Test plan <!-- Explain how you tested your changes. Include the exact commands that you used to verify the change works and include screenshots/screen recordings of the update behavior in the browser if applicable. --> I'd be happy to ensure this works correctly on my end before merging this, but it's not as trivial to test locally as a logic fix. Can you guide me through what I need to do to test this? I extensively based my change on the docs, following those guides: - [Fixing imports for `Module#register`](https://nodejs.org/docs/latest/api/module.html#registration-of-asynchronous-customization-hooks) - [Using the new `Module#registerHooks` function](https://nodejs.org/docs/latest/api/module.html#registration-of-synchronous-customization-hooks) (and other more detailed sections of the same docs page) --- Fixes #19893 Closes #19907 --------- Co-authored-by: Robin Malfait <malfait.robin@gmail.com>
2026-05-11 16:25:28 +02:00
import {
isBuiltin,
type ResolveFnOutput,
type ResolveHook,
type ResolveHookContext,
type ResolveHookSync,
} from 'node:module'
export let resolve: ResolveHook = async (specifier, context, nextResolve) => {
let result = await nextResolve(specifier, context)
fix(node): prefer Module#registerHooks over Module#register (#20028) <!-- 👋 Hey, thanks for your interest in contributing to Tailwind! **Please ask first before starting work on any significant new features.** It's never a fun experience to have your pull request declined after investing a lot of time and effort into a new feature. To avoid this from happening, we request that contributors create a discussion to first discuss any significant new features. For more info, check out the contributing guide: https://github.com/tailwindlabs/tailwindcss/blob/main/.github/CONTRIBUTING.md --> ## Summary <!-- Provide a summary of the issue and the changes you're making. How does your change solve the problem? --> Fix this warning when building apps with TailwindCSS with Node 26+: ``` (node:25346) [DEP0205] DeprecationWarning: `module.register()` is deprecated. Use `module.registerHooks()` instead. at node:internal/util:129:11 at Module.register (node:internal/modules/esm/loader:969:3) at file:///Users/antoine/Developer/my-app/node_modules/.pnpm/@tailwindcss+node@4.3.0/node_modules/@tailwindcss/node/dist/index.mjs:18:214 at ... ``` This PR: - Correctly prefers using `Module#registerHooks` instead of `Module#register` by checking its availability at runtime - Adjusts the exports of the hooks’ file by creating a synchronous version for the new API - Remove now unused exports from the `package.json`, relying on the [recommended usage in the docs](https://nodejs.org/docs/latest/api/module.html#registration-of-asynchronous-customization-hooks) for the `Module#register` calls ## Test plan <!-- Explain how you tested your changes. Include the exact commands that you used to verify the change works and include screenshots/screen recordings of the update behavior in the browser if applicable. --> I'd be happy to ensure this works correctly on my end before merging this, but it's not as trivial to test locally as a logic fix. Can you guide me through what I need to do to test this? I extensively based my change on the docs, following those guides: - [Fixing imports for `Module#register`](https://nodejs.org/docs/latest/api/module.html#registration-of-asynchronous-customization-hooks) - [Using the new `Module#registerHooks` function](https://nodejs.org/docs/latest/api/module.html#registration-of-synchronous-customization-hooks) (and other more detailed sections of the same docs page) --- Fixes #19893 Closes #19907 --------- Co-authored-by: Robin Malfait <malfait.robin@gmail.com>
2026-05-11 16:25:28 +02:00
return processResolve(context, result)
}
export let resolveSync: ResolveHookSync = (specifier, context, nextResolve) => {
let result = nextResolve(specifier, context)
return processResolve(context, result)
}
fix(node): prefer Module#registerHooks over Module#register (#20028) <!-- 👋 Hey, thanks for your interest in contributing to Tailwind! **Please ask first before starting work on any significant new features.** It's never a fun experience to have your pull request declined after investing a lot of time and effort into a new feature. To avoid this from happening, we request that contributors create a discussion to first discuss any significant new features. For more info, check out the contributing guide: https://github.com/tailwindlabs/tailwindcss/blob/main/.github/CONTRIBUTING.md --> ## Summary <!-- Provide a summary of the issue and the changes you're making. How does your change solve the problem? --> Fix this warning when building apps with TailwindCSS with Node 26+: ``` (node:25346) [DEP0205] DeprecationWarning: `module.register()` is deprecated. Use `module.registerHooks()` instead. at node:internal/util:129:11 at Module.register (node:internal/modules/esm/loader:969:3) at file:///Users/antoine/Developer/my-app/node_modules/.pnpm/@tailwindcss+node@4.3.0/node_modules/@tailwindcss/node/dist/index.mjs:18:214 at ... ``` This PR: - Correctly prefers using `Module#registerHooks` instead of `Module#register` by checking its availability at runtime - Adjusts the exports of the hooks’ file by creating a synchronous version for the new API - Remove now unused exports from the `package.json`, relying on the [recommended usage in the docs](https://nodejs.org/docs/latest/api/module.html#registration-of-asynchronous-customization-hooks) for the `Module#register` calls ## Test plan <!-- Explain how you tested your changes. Include the exact commands that you used to verify the change works and include screenshots/screen recordings of the update behavior in the browser if applicable. --> I'd be happy to ensure this works correctly on my end before merging this, but it's not as trivial to test locally as a logic fix. Can you guide me through what I need to do to test this? I extensively based my change on the docs, following those guides: - [Fixing imports for `Module#register`](https://nodejs.org/docs/latest/api/module.html#registration-of-asynchronous-customization-hooks) - [Using the new `Module#registerHooks` function](https://nodejs.org/docs/latest/api/module.html#registration-of-synchronous-customization-hooks) (and other more detailed sections of the same docs page) --- Fixes #19893 Closes #19907 --------- Co-authored-by: Robin Malfait <malfait.robin@gmail.com>
2026-05-11 16:25:28 +02:00
function processResolve(context: ResolveHookContext, result: ResolveFnOutput) {
try {
if (result.url === import.meta.url) return result
if (isBuiltin(result.url)) return result
if (!context.parentURL) return result
let parent = new URL(context.parentURL)
let id = parent.searchParams.get('id')
if (id === null) return result
let url = new URL(result.url)
url.searchParams.set('id', id)
return {
...result,
url: `${url}`,
}
} catch {
return result
}
}