Skip to content

Commit 7a78ebe

Browse files
Pnlvfxaskoufis
andauthored
fix(webpack-plugin): 'require is not defined' error in Node 24 (#1602)
Co-authored-by: Adam Skoufis <[email protected]>
1 parent 62e9a3d commit 7a78ebe

File tree

5 files changed

+19
-0
lines changed

5 files changed

+19
-0
lines changed

.changeset/fresh-cars-fetch.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@vanilla-extract/webpack-plugin': patch
3+
'@vanilla-extract/next-plugin': patch
4+
---
5+
6+
Fix `require is not defined` error in ESM bundles by calling `createRequire`

packages/next-plugin/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import type {
1111
NextConfig,
1212
WebpackConfigContext,
1313
} from 'next/dist/server/config-shared';
14+
import { createRequire } from 'node:module';
15+
16+
const require = createRequire(import.meta.url);
1417

1518
type PluginOptions = ConstructorParameters<typeof VanillaExtractPlugin>[0];
1619

packages/webpack-plugin/src/loader.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import {
1212
import type { LoaderContext } from './types';
1313
import { debug, formatResourcePath } from './logger';
1414
import { ChildCompiler } from './compiler';
15+
import { createRequire } from 'node:module';
16+
17+
const require = createRequire(import.meta.url);
1518

1619
const virtualFileLoader = require.resolve(
1720
path.join(

packages/webpack-plugin/src/next.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import path from 'path';
22
import { AbstractVanillaExtractPlugin } from './plugin';
33
import type { Compiler } from 'webpack';
4+
import { createRequire } from 'node:module';
5+
6+
const require = createRequire(import.meta.url);
47

58
const virtualNextFileLoader = require.resolve(
69
path.join(

packages/webpack-plugin/src/plugin.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import type { Compiler, RuleSetRule } from 'webpack';
77
import { ChildCompiler } from './compiler';
88
import createCompat, { type WebpackCompat } from './compat';
99

10+
import { createRequire } from 'node:module';
11+
12+
const require = createRequire(import.meta.url);
13+
1014
const pluginName = 'VanillaExtractPlugin';
1115

1216
function markCSSFilesAsSideEffects(compiler: Compiler, compat: WebpackCompat) {

0 commit comments

Comments
 (0)