Skip to content

fix(pack): add createRequire as global API in loader worker and fix hmr#2762

Merged
xusd320 merged 2 commits into
nextfrom
feat/create-require-global-api
Apr 7, 2026
Merged

fix(pack): add createRequire as global API in loader worker and fix hmr#2762
xusd320 merged 2 commits into
nextfrom
feat/create-require-global-api

Conversation

@xusd320
Copy link
Copy Markdown
Contributor

@xusd320 xusd320 commented Apr 7, 2026

Changes

packages/utoo-web/src/webpackLoaders/cjs.ts

  • Expose createRequire on self (global) inside the cjs() function, so webpack loaders can use createRequire(filename) to get a require function that resolves modules relative to path.dirname(filename)

packages/utoo-web/src/webpackLoaders/polyfills/nodePolyFills.ts

  • Update module / node:module polyfill getters to return { createRequire } from the global, so require('module').createRequire and import { createRequire } from 'module' both work

next.js submodule

crates/pack-api/src/project.rs — Fix HMR updates always returning Update::None

Two bugs were fixed:

  1. hmr_version_state was re-creating VersionState on every change — The old implementation used version.into_trait_ref().await? which is tracked, causing turbo-tasks to re-execute hmr_version_state whenever content changed. This created a new VersionState initialized with the new version, so content.update(from) always compared identical versions → Update::None. Fixed by using an inner #[turbo_tasks::function(operation)] with .read_trait_strongly_consistent().untracked(), matching the Next.js implementation.

  2. hmr_identifiers used dist_root() instead of client_root() — Output chunk assets are stored on the virtual client-fs filesystem, but keys_in_path(dist_root) queries the output-fs, which only contained stats.json. Changed to client_root() to match how hmr_content() and emit_all_output_assets() use the same root.

- Expose createRequire on self (global) in the CJS loader worker,
  enabling loaders to use createRequire(filename) to get a require
  function anchored at a given path
- Update module/node:module polyfill to return { createRequire }
  so require('module').createRequire also works
- Update next.js submodule with wasm file write and
  available_parallelism improvements
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request updates the next.js subproject and implements a custom createRequire function within the CJS loader. The node:module and module polyfills are updated to utilize this new function. Feedback suggests implementing a singleton pattern for these polyfills to ensure module identity consistency and improve performance.

Comment thread packages/utoo-web/src/webpackLoaders/polyfills/nodePolyFills.ts
…r update diffing

- Fix hmr_version_state to use an operation function with
  .read_trait_strongly_consistent().untracked() matching the Next.js
  pattern. Without .untracked(), the VersionState was re-created on
  every content change with the new version, causing content.update(from)
  to always compare identical versions and return Update::None.

- Fix hmr_identifiers to use client_root() instead of dist_root().
  Output chunk assets are stored on the virtual client-fs filesystem,
  so keys_in_path(dist_root) on the output-fs could only find stats.json.
@xusd320 xusd320 changed the title feat(wasm): add createRequire as global API in loader worker fix(pack): add createRequire as global API in loader worker and fix hmr Apr 7, 2026
@xusd320 xusd320 enabled auto-merge (squash) April 7, 2026 08:58
@xusd320 xusd320 merged commit 6450173 into next Apr 7, 2026
37 of 40 checks passed
@xusd320 xusd320 deleted the feat/create-require-global-api branch April 7, 2026 09:14
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.

2 participants