Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"singleQuote": true
"singleQuote": true,
"plugins": ["@prettier/plugin-oxc"]
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"e2e:rspack": "cd ./e2e && pnpm e2e:rspack",
"e2e:webpack": "cd ./e2e && pnpm e2e:webpack",
"format": "prettier --experimental-cli --write . && heading-case --write",
"lint": "biome check && pnpm lint:type",
"lint": "biome check",
"lint:type": "rslint",
"prebundle": "nx run-many -t prebundle",
"prepare": "simple-git-hooks && pnpm build",
Expand All @@ -35,6 +35,7 @@
},
"devDependencies": {
"@biomejs/biome": "^2.3.2",
"@prettier/plugin-oxc": "^0.0.4",
"@rsbuild/config": "workspace:*",
"@rslint/core": "^0.1.13",
"@rstest/core": "^0.6.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/compat/plugin-webpack-swc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"devDependencies": {
"@rsbuild/core": "workspace:*",
"@rsbuild/webpack": "workspace:*",
"@rslib/core": "0.17.0",
"@rslib/core": "0.17.1",
"@types/lodash": "^4.17.20",
"@types/semver": "^7.7.1",
"typescript": "^5.9.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/compat/webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"devDependencies": {
"@rsbuild/core": "workspace:*",
"@rslib/core": "0.17.0",
"@rslib/core": "0.17.1",
"@scripts/test-helper": "workspace:*",
"@types/node": "^24.9.2",
"ansi-escapes": "4.3.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"devDependencies": {
"@jridgewell/remapping": "^2.3.5",
"@jridgewell/trace-mapping": "^0.3.31",
"@rslib/core": "0.17.0",
"@rslib/core": "0.17.1",
"@types/connect": "3.4.38",
"@types/cors": "^2.8.19",
"@types/node": "^24.9.2",
Expand Down
7 changes: 7 additions & 0 deletions packages/core/rslib.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,11 @@ export default defineConfig({
},
},
],
tools: {
rspack: {
experiments: {
deferImport: true,
},
},
},
});
2 changes: 1 addition & 1 deletion packages/core/src/createContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export async function updateEnvironmentContext(
const { entry = {}, tsconfigPath } = config.source;
const htmlPaths = getEnvironmentHTMLPaths(entry, config);
const webSocketToken =
context.action === 'dev' ? await hash(context.rootPath + name) : '';
context.action === 'dev' ? hash(context.rootPath + name) : '';

const environmentContext: EnvironmentContext = {
index,
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/helpers/exitHook.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { constants } from 'node:os';
import process from 'node:process';
import defer * as os from 'node:os';
import defer * as process from 'node:process';

type Callback = (exitCode: number) => void;

Expand Down Expand Up @@ -37,11 +37,11 @@ export function exitHook(onExit: Callback): () => void {
// Use `process.on` instead of `process.once` to disable
// Node.js's default exit behavior
process.on('SIGINT', () => {
exit(constants.signals.SIGINT + 128, 'SIGINT');
exit(os.constants.signals.SIGINT + 128, 'SIGINT');
});
// `kill` command
process.once('SIGTERM', () => {
exit(constants.signals.SIGTERM + 128, 'SIGTERM');
exit(os.constants.signals.SIGTERM + 128, 'SIGTERM');
});
// process.exit or others
process.once('exit', (exitCode) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import defer * as crypto from 'node:crypto';
import deepmerge from 'deepmerge';
import RspackChain from '../../compiled/rspack-chain';
import type {
Expand Down Expand Up @@ -209,8 +210,7 @@ export const isTTY = (type: 'stdin' | 'stdout' = 'stdout'): boolean => {
);
};

export async function hash(data: string): Promise<string> {
const crypto = await import('node:crypto');
export function hash(data: string): string {
// Available in Node.js v20.12.0
// faster than `crypto.createHash()` when hashing a smaller amount of data (<= 5MB)
if (crypto.hash) {
Expand Down
13 changes: 8 additions & 5 deletions packages/core/src/loadConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'node:fs';
import { isAbsolute, join } from 'node:path';
import { pathToFileURL } from 'node:url';
import defer * as jiti from 'jiti';
import { __filename } from './constants';
import { color, getNodeEnv, isObject } from './helpers';
import { logger } from './logger';
Expand Down Expand Up @@ -167,8 +168,7 @@ export async function loadConfig({

if (configExport === undefined) {
try {
const { createJiti } = await import('jiti');
const jiti = createJiti(__filename, {
const instance = jiti.createJiti(__filename, {
// disable require cache to support restart CLI and read the new config
moduleCache: false,
interopDefault: true,
Expand All @@ -177,9 +177,12 @@ export async function loadConfig({
nativeModules: ['@rspack/core', 'typescript'],
});

configExport = await jiti.import<RsbuildConfigExport>(configFilePath, {
default: true,
});
configExport = await instance.import<RsbuildConfigExport>(
configFilePath,
{
default: true,
},
);
} catch (err) {
logger.error(
`Failed to load file with jiti: ${color.dim(configFilePath)}`,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/plugins/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export const pluginCache = (): RsbuildPlugin => ({

// set cache name to avoid cache conflicts between different environments
const cacheVersion = useDigest
? `${environment.name}-${env}-${await hash(JSON.stringify(cacheConfig.cacheDigest))}`
? `${environment.name}-${env}-${hash(JSON.stringify(cacheConfig.cacheDigest))}`
: `${environment.name}-${env}`;

if (bundlerType === 'rspack') {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/plugins/fileSize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import fs from 'node:fs';
import path from 'node:path';
import { promisify } from 'node:util';
import zlib from 'node:zlib';
import defer * as zlib from 'node:zlib';
import { JS_REGEX } from '../constants';
import { color } from '../helpers';
import { getAssetsFromStats, type RsbuildAsset } from '../helpers/stats';
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/server/cliShortcuts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import defer * as readline from 'node:readline';
import { color, isTTY } from '../helpers';
import { logger } from '../logger';
import type { CliShortcut, NormalizedConfig } from '../types/config';
Expand Down Expand Up @@ -78,8 +79,7 @@ export async function setupCliShortcuts({
);
}

const { createInterface } = await import('node:readline');
const rl = createInterface({
const rl = readline.createInterface({
input: process.stdin,
});

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/server/devServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ export async function createDevServer<

const httpServer = middlewareMode
? null
: await createHttpServer({
: createHttpServer({
serverConfig: config.server,
middlewares,
});
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/server/gracefulShutdown.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { constants } from 'node:os';
import defer * as os from 'node:os';

/**
* A set to store all cleanup callbacks that should be executed before process termination
Expand Down Expand Up @@ -41,7 +41,7 @@ export const setupGracefulShutdown = (): (() => void) => {
// or manually via 'kill -15 <pid>' or 'kill -TERM <pid>' command.
// Add 128 to signal number as per POSIX convention for signal-terminated processes.
const onSigterm = () => {
handleTermination(constants.signals.SIGTERM + 128);
handleTermination(os.constants.signals.SIGTERM + 128);
};
process.once('SIGTERM', onSigterm);

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/server/gzipMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ServerResponse } from 'node:http';
import zlib from 'node:zlib';
import defer * as zlib from 'node:zlib';
import type { CompressOptions, RequestHandler } from '../types';

const ENCODING_REGEX = /\bgzip\b/;
Expand Down
11 changes: 5 additions & 6 deletions packages/core/src/server/helper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { IncomingMessage, Server } from 'node:http';
import type { Http2SecureServer } from 'node:http2';
import type { Socket } from 'node:net';
import defer * as net from 'node:net';
import os from 'node:os';
import { posix, relative, sep } from 'node:path';
import { DEFAULT_DEV_HOST } from '../constants';
Expand Down Expand Up @@ -270,7 +271,6 @@ export const getPort = async ({
tryLimits = 1;
}

const { createServer } = await import('node:net');
const original = port;

let found = false;
Expand All @@ -279,7 +279,7 @@ export const getPort = async ({
while (!found && attempts <= tryLimits) {
try {
await new Promise((resolve, reject) => {
const server = createServer();
const server = net.createServer();
server.unref();
server.on('error', reject);
server.listen({ port, host }, () => {
Expand Down Expand Up @@ -379,13 +379,12 @@ const isLoopbackHost = (host: string) => {
return loopbackHosts.has(host);
};

export const getHostInUrl = async (host: string): Promise<string> => {
export const getHostInUrl = (host: string): string => {
if (host === DEFAULT_DEV_HOST) {
return 'localhost';
}

const { isIPv6 } = await import('node:net');
if (isIPv6(host)) {
if (net.isIPv6(host)) {
return host === '::' ? '[::1]' : `[${host}]`;
}
return host;
Expand Down Expand Up @@ -427,7 +426,7 @@ export const getAddressUrls = async ({
if (host && host !== DEFAULT_DEV_HOST) {
const url = concatUrl({
port,
host: await getHostInUrl(host),
host: getHostInUrl(host),
protocol,
});
return [
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/server/hmrFallback.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import defer * as dns from 'node:dns';
import { isWildcardHost } from './helper';

/**
Expand All @@ -8,10 +9,9 @@ import { isWildcardHost } from './helper';
* This helps detect cases where IPv4/IPv6 resolution might vary.
*/
async function getLocalhostResolvedAddress(): Promise<string | undefined> {
const { promises: dns } = await import('node:dns');
const [defaultLookup, explicitLookup] = await Promise.all([
dns.lookup('localhost'),
dns.lookup('localhost', { verbatim: true }),
dns.promises.lookup('localhost'),
dns.promises.lookup('localhost', { verbatim: true }),
]);
const match =
defaultLookup.family === explicitLookup.family &&
Expand Down
16 changes: 8 additions & 8 deletions packages/core/src/server/httpServer.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import type { Server } from 'node:http';
import defer * as http from 'node:http';
import type { Http2SecureServer } from 'node:http2';
import defer * as http2 from 'node:http2';
import defer * as https from 'node:https';
import type { Connect, ServerConfig } from '../types';

export const createHttpServer = async ({
export const createHttpServer = ({
serverConfig,
middlewares,
}: {
serverConfig: ServerConfig;
middlewares: Connect.Server;
}): Promise<Http2SecureServer | Server> => {
}): Http2SecureServer | Server => {
if (serverConfig.https) {
// http-proxy does not supports http2
if (serverConfig.proxy) {
const { createServer } = await import('node:https');
return createServer(serverConfig.https, middlewares);
return https.createServer(serverConfig.https, middlewares);
}

const { createSecureServer } = await import('node:http2');
return createSecureServer(
return http2.createSecureServer(
{
allowHTTP1: true,
// increase the maximum memory (MiB)
Expand All @@ -29,6 +30,5 @@ export const createHttpServer = async ({
);
}

const { createServer } = await import('node:http');
return createServer(middlewares);
return http.createServer(middlewares);
};
8 changes: 4 additions & 4 deletions packages/core/src/server/open.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import defer * as childProcess from 'node:child_process';
import defer * as nodeUtil from 'node:util';
import { STATIC_PATH } from '../constants';
import { castArray, color } from '../helpers';
import { canParse } from '../helpers/url';
Expand Down Expand Up @@ -64,9 +66,7 @@ async function openBrowser(url: string): Promise<boolean> {
// a Chromium browser with AppleScript. This lets us reuse an
// existing tab when possible instead of creating a new one.
if (shouldTryAppleScript(browser, browserArgs)) {
const { exec } = await import('node:child_process');
const { promisify } = await import('node:util');
const execAsync = promisify(exec);
const execAsync = nodeUtil.promisify(childProcess.exec);

/**
* Find the browser that is currently running
Expand Down Expand Up @@ -203,7 +203,7 @@ export async function open({

const urls: string[] = [];
const protocol = https ? 'https' : 'http';
const host = await getHostInUrl(config.server.host);
const host = getHostInUrl(config.server.host);
const baseUrl = `${protocol}://${host}:${port}`;

if (!targets.length) {
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/server/prodServer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Server } from 'node:http';
import type { Http2SecureServer } from 'node:http2';
import defer * as zlib from 'node:zlib';
import { getPathnameFromUrl } from '../helpers/path';
import { requireCompiledPackage } from '../helpers/vendors';
import { isVerbose, logger } from '../logger';
Expand Down Expand Up @@ -104,11 +105,10 @@ export class RsbuildProdServer {
// compression is placed after proxy middleware to avoid breaking SSE (Server-Sent Events),
// but before other middlewares to ensure responses are properly compressed
if (compress) {
const { constants } = await import('node:zlib');
this.middlewares.use(
gzipMiddleware({
// simulates the common gzip compression rates
level: constants.Z_DEFAULT_COMPRESSION,
level: zlib.constants.Z_DEFAULT_COMPRESSION,
...(typeof compress === 'object' ? compress : undefined),
}),
);
Expand Down Expand Up @@ -201,7 +201,7 @@ export async function startProdServer(

await context.hooks.onBeforeStartProdServer.callBatch();

const httpServer = await createHttpServer({
const httpServer = createHttpServer({
serverConfig,
middlewares: server.middlewares,
});
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/server/runner/asModule.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { Module, ModuleLinker, SyntheticModule } from 'node:vm';
import defer * as vm from 'node:vm';

export const asModule = async (
something: Record<string, any>,
context: Record<string, any>,
unlinked?: boolean,
): Promise<Module | SyntheticModule> => {
const { Module, SyntheticModule } = await import('node:vm');
const { Module, SyntheticModule } = vm;

if (something instanceof Module) {
return something;
Expand Down
2 changes: 1 addition & 1 deletion packages/create-rsbuild/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"@rsbuild/plugin-solid": "workspace:*",
"@rsbuild/plugin-svelte": "workspace:*",
"@rsbuild/plugin-vue": "workspace:*",
"@rslib/core": "0.17.0",
"@rslib/core": "0.17.1",
"@types/node": "^24.9.2",
"typescript": "^5.9.3"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-babel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
},
"devDependencies": {
"@rsbuild/core": "workspace:*",
"@rslib/core": "0.17.0",
"@rslib/core": "0.17.1",
"@scripts/test-helper": "workspace:*",
"@types/node": "^24.9.2",
"babel-loader": "10.0.0",
Expand Down
Loading
Loading