Skip to content

Also rewrite requires in page templates with Webpack - #95446

Merged
mischnic merged 2 commits into
canaryfrom
mischnic/webpack-tempalte-require-rerwite
Jul 6, 2026
Merged

mischnic merged 2 commits into
canaryfrom
mischnic/webpack-tempalte-require-rerwite

Conversation

@mischnic

@mischnic mischnic commented Jul 3, 2026 •

Copy link
Copy Markdown
Member

Thus far, it wasn't possible to use require() in a template file with Webpack.
But I needed to add a if(NEXT_RUNTIME !== "edge") { const x = require("../../something"); }

Turbopack doesn't do any regex rewrite hacks, so this just worked

mischnic commented Jul 3, 2026

Copy link
Copy Markdown
Member Author

@github-actions

github-actions Bot commented Jul 3, 2026 •

Copy link
Copy Markdown
Contributor

Tests Passed

Commit: 3a82587

@github-actions

github-actions Bot commented Jul 3, 2026 •

Copy link
Copy Markdown
Contributor

Stats cancelled

Commit: 3a82587
View workflow run

@mischnic
mischnic force-pushed the mischnic/webpack-tempalte-require-rerwite branch from 4f31060 to 3a82587 Compare July 3, 2026 07:57

@Ziiyodullayevv Ziiyodullayevv left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice improvement to the import path regex. The changes:

  1. Adding require() support alongside from and import
  2. Using LazyLock for regex compilation is efficient
  3. The capture group handling with or_else is clean

One observation: The regex now handles three cases (from, import, require). Consider adding a comment explaining why require support was added - was there a specific use case?

The code is well-structured and the refactoring makes it more maintainable.

@mischnic
mischnic marked this pull request as ready for review July 3, 2026 09:33
@mischnic
mischnic requested a review from a team July 3, 2026 09:34
@mischnic mischnic changed the title Also rewrite requires in Webpack template Also rewrite requires in page templates with Webpack Jul 3, 2026
@mischnic
mischnic merged commit e7c04f6 into canary Jul 6, 2026
130 of 131 checks passed
@mischnic
mischnic deleted the mischnic/webpack-tempalte-require-rerwite branch July 6, 2026 10:01
m-kawafuji pushed a commit to m-kawafuji/next.js that referenced this pull request Aug 8, 2026
Thus far, it wasn't possible to use `require()` in a template file with
Webpack.
But I needed to add a `if(NEXT_RUNTIME !== "edge") { const x =
require("../../something"); }`

Turbopack doesn't do any regex rewrite hacks, so this just worked
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Sep 15, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants