Skip to content

Commit

Permalink
chore: declare __dirname for Node.js ES modules (#3782)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Oct 21, 2024
1 parent b9ea977 commit c1791b8
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@changesets/cli": "^2.27.9",
"@modern-js/module-tools": "^2.60.4",
"@rsbuild/config": "workspace:*",
"@rslib/core": "0.0.0-next-20241012061647",
"@rslib/core": "0.0.13",
"@scripts/test-helper": "workspace:*",
"check-dependency-version-consistency": "^4.1.0",
"cross-env": "^7.0.3",
Expand Down
3 changes: 3 additions & 0 deletions packages/compat/plugin-webpack-swc/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import type { RsbuildPlugin } from '@rsbuild/core';
import { SwcMinimizerPlugin } from './minimizer.js';
import type {
Expand All @@ -12,6 +13,8 @@ import {
removeUselessOptions,
} from './utils.js';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

/**
* In this plugin, we do:
* - Remove Babel loader if exists
Expand Down
3 changes: 3 additions & 0 deletions packages/plugin-assets-retry/src/AsyncChunkRetryPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { type Rspack, rspack } from '@rsbuild/core';
import serialize from 'serialize-javascript';
import type { PluginAssetsRetryOptions, RuntimeRetryOptions } from './types.js';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

// https://github.com/web-infra-dev/rspack/pull/5370
function appendWebpackScript(module: any, appendSource: string) {
try {
Expand Down
3 changes: 3 additions & 0 deletions packages/plugin-assets-retry/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import type {
EnvironmentContext,
NormalizedEnvironmentConfig,
Expand All @@ -10,6 +11,8 @@ import serialize from 'serialize-javascript';
import { AsyncChunkRetryPlugin } from './AsyncChunkRetryPlugin.js';
import type { PluginAssetsRetryOptions } from './types.js';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

export type { PluginAssetsRetryOptions };

export const PLUGIN_ASSETS_RETRY_NAME = 'rsbuild:assets-retry';
Expand Down
2 changes: 2 additions & 0 deletions packages/plugin-babel/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'node:fs';
import { createRequire } from 'node:module';
import path, { isAbsolute, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import type {
EnvironmentContext,
NormalizedEnvironmentConfig,
Expand All @@ -11,6 +12,7 @@ import deepmerge from 'deepmerge';
import { BABEL_JS_RULE, applyUserBabelConfig, castArray } from './helper.js';
import type { BabelLoaderOptions, PluginBabelOptions } from './types.js';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const require = createRequire(import.meta.url);

export const PLUGIN_BABEL_NAME = 'rsbuild:babel';
Expand Down
3 changes: 3 additions & 0 deletions packages/plugin-less/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import type {
ConfigChainWithContext,
RsbuildPlugin,
Expand All @@ -7,6 +8,8 @@ import type {
import deepmerge from 'deepmerge';
import { reduceConfigsWithContext } from 'reduce-configs';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

export const PLUGIN_LESS_NAME = 'rsbuild:less';

export type LessLoaderOptions = {
Expand Down
8 changes: 5 additions & 3 deletions packages/plugin-sass/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { createRequire } from 'node:module';
import { join } from 'node:path';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import type { RsbuildPlugin } from '@rsbuild/core';
import deepmerge from 'deepmerge';
import { reduceConfigsWithContext } from 'reduce-configs';
import { getResolveUrlJoinFn, patchCompilerGlobalLocation } from './helpers.js';
import type { PluginSassOptions, SassLoaderOptions } from './types.js';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const require = createRequire(import.meta.url);

export const PLUGIN_SASS_NAME = 'rsbuild:sass';
Expand Down Expand Up @@ -123,7 +125,7 @@ export const pluginSass = (

rule
.use(CHAIN_ID.USE.RESOLVE_URL)
.loader(join(__dirname, '../compiled/resolve-url-loader/index.js'))
.loader(path.join(__dirname, '../compiled/resolve-url-loader/index.js'))
.options({
join: await getResolveUrlJoinFn(),
// 'resolve-url-loader' relies on 'adjust-sourcemap-loader',
Expand All @@ -133,7 +135,7 @@ export const pluginSass = (
})
.end()
.use(CHAIN_ID.USE.SASS)
.loader(join(__dirname, '../compiled/sass-loader/index.js'))
.loader(path.join(__dirname, '../compiled/sass-loader/index.js'))
.options(options);
});
},
Expand Down
3 changes: 3 additions & 0 deletions packages/plugin-svgr/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import type { RsbuildPlugin, Rspack } from '@rsbuild/core';
import { PLUGIN_REACT_NAME } from '@rsbuild/plugin-react';
import type { Config as SvgrOptions } from '@svgr/core';
import deepmerge from 'deepmerge';
import type { Config as SvgoConfig } from 'svgo';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

type SvgoPluginConfig = NonNullable<SvgoConfig['plugins']>[0];

export type SvgDefaultExport = 'component' | 'url';
Expand Down
36 changes: 11 additions & 25 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c1791b8

Please sign in to comment.