Skip to content

Commit c1791b8

Browse files
authored
chore: declare __dirname for Node.js ES modules (#3782)
1 parent b9ea977 commit c1791b8

File tree

9 files changed

+34
-29
lines changed

9 files changed

+34
-29
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"@changesets/cli": "^2.27.9",
4545
"@modern-js/module-tools": "^2.60.4",
4646
"@rsbuild/config": "workspace:*",
47-
"@rslib/core": "0.0.0-next-20241012061647",
47+
"@rslib/core": "0.0.13",
4848
"@scripts/test-helper": "workspace:*",
4949
"check-dependency-version-consistency": "^4.1.0",
5050
"cross-env": "^7.0.3",

packages/compat/plugin-webpack-swc/src/plugin.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from 'node:path';
2+
import { fileURLToPath } from 'node:url';
23
import type { RsbuildPlugin } from '@rsbuild/core';
34
import { SwcMinimizerPlugin } from './minimizer.js';
45
import type {
@@ -12,6 +13,8 @@ import {
1213
removeUselessOptions,
1314
} from './utils.js';
1415

16+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
17+
1518
/**
1619
* In this plugin, we do:
1720
* - Remove Babel loader if exists

packages/plugin-assets-retry/src/AsyncChunkRetryPlugin.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
3+
import { fileURLToPath } from 'node:url';
34
import { type Rspack, rspack } from '@rsbuild/core';
45
import serialize from 'serialize-javascript';
56
import type { PluginAssetsRetryOptions, RuntimeRetryOptions } from './types.js';
67

8+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
9+
710
// https://github.com/web-infra-dev/rspack/pull/5370
811
function appendWebpackScript(module: any, appendSource: string) {
912
try {

packages/plugin-assets-retry/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
3+
import { fileURLToPath } from 'node:url';
34
import type {
45
EnvironmentContext,
56
NormalizedEnvironmentConfig,
@@ -10,6 +11,8 @@ import serialize from 'serialize-javascript';
1011
import { AsyncChunkRetryPlugin } from './AsyncChunkRetryPlugin.js';
1112
import type { PluginAssetsRetryOptions } from './types.js';
1213

14+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
15+
1316
export type { PluginAssetsRetryOptions };
1417

1518
export const PLUGIN_ASSETS_RETRY_NAME = 'rsbuild:assets-retry';

packages/plugin-babel/src/plugin.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from 'node:fs';
22
import { createRequire } from 'node:module';
33
import path, { isAbsolute, join } from 'node:path';
4+
import { fileURLToPath } from 'node:url';
45
import type {
56
EnvironmentContext,
67
NormalizedEnvironmentConfig,
@@ -11,6 +12,7 @@ import deepmerge from 'deepmerge';
1112
import { BABEL_JS_RULE, applyUserBabelConfig, castArray } from './helper.js';
1213
import type { BabelLoaderOptions, PluginBabelOptions } from './types.js';
1314

15+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1416
const require = createRequire(import.meta.url);
1517

1618
export const PLUGIN_BABEL_NAME = 'rsbuild:babel';

packages/plugin-less/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from 'node:path';
2+
import { fileURLToPath } from 'node:url';
23
import type {
34
ConfigChainWithContext,
45
RsbuildPlugin,
@@ -7,6 +8,8 @@ import type {
78
import deepmerge from 'deepmerge';
89
import { reduceConfigsWithContext } from 'reduce-configs';
910

11+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
12+
1013
export const PLUGIN_LESS_NAME = 'rsbuild:less';
1114

1215
export type LessLoaderOptions = {

packages/plugin-sass/src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { createRequire } from 'node:module';
2-
import { join } from 'node:path';
2+
import path from 'node:path';
3+
import { fileURLToPath } from 'node:url';
34
import type { RsbuildPlugin } from '@rsbuild/core';
45
import deepmerge from 'deepmerge';
56
import { reduceConfigsWithContext } from 'reduce-configs';
67
import { getResolveUrlJoinFn, patchCompilerGlobalLocation } from './helpers.js';
78
import type { PluginSassOptions, SassLoaderOptions } from './types.js';
89

10+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
911
const require = createRequire(import.meta.url);
1012

1113
export const PLUGIN_SASS_NAME = 'rsbuild:sass';
@@ -123,7 +125,7 @@ export const pluginSass = (
123125

124126
rule
125127
.use(CHAIN_ID.USE.RESOLVE_URL)
126-
.loader(join(__dirname, '../compiled/resolve-url-loader/index.js'))
128+
.loader(path.join(__dirname, '../compiled/resolve-url-loader/index.js'))
127129
.options({
128130
join: await getResolveUrlJoinFn(),
129131
// 'resolve-url-loader' relies on 'adjust-sourcemap-loader',
@@ -133,7 +135,7 @@ export const pluginSass = (
133135
})
134136
.end()
135137
.use(CHAIN_ID.USE.SASS)
136-
.loader(join(__dirname, '../compiled/sass-loader/index.js'))
138+
.loader(path.join(__dirname, '../compiled/sass-loader/index.js'))
137139
.options(options);
138140
});
139141
},

packages/plugin-svgr/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import path from 'node:path';
2+
import { fileURLToPath } from 'node:url';
23
import type { RsbuildPlugin, Rspack } from '@rsbuild/core';
34
import { PLUGIN_REACT_NAME } from '@rsbuild/plugin-react';
45
import type { Config as SvgrOptions } from '@svgr/core';
56
import deepmerge from 'deepmerge';
67
import type { Config as SvgoConfig } from 'svgo';
78

9+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
10+
811
type SvgoPluginConfig = NonNullable<SvgoConfig['plugins']>[0];
912

1013
export type SvgDefaultExport = 'component' | 'url';

pnpm-lock.yaml

Lines changed: 11 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)