Skip to content
Merged
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
5 changes: 2 additions & 3 deletions packages/core/src/server/assets-middleware/getFileFromUrl.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { Stats as FSStats } from 'node:fs';
import path from 'node:path';
import { getPathnameFromUrl } from '../../helpers/path';
import type { InternalContext } from '../../types';
import type { InternalContext, Rspack } from '../../types';
import { HttpCode } from '../helper';
import type { OutputFileSystem } from './index';

const UP_PATH_REGEXP = /(?:^|[\\/])\.\.(?:[\\/]|$)/;

Expand All @@ -13,7 +12,7 @@ const UP_PATH_REGEXP = /(?:^|[\\/])\.\.(?:[\\/]|$)/;
*/
export async function getFileFromUrl(
url: string,
outputFileSystem: OutputFileSystem,
outputFileSystem: Rspack.OutputFileSystem,
context: InternalContext,
): Promise<
{ filename: string; fsStats: FSStats } | { errorCode: HttpCode } | undefined
Expand Down
9 changes: 0 additions & 9 deletions packages/core/src/server/assets-middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* Copyright JS Foundation and other contributors
* https://github.com/webpack/webpack-dev-middleware/blob/master/LICENSE
*/
import type { ReadStream } from 'node:fs';
import { createRequire } from 'node:module';
import type { Compiler, MultiCompiler, Watching } from '@rspack/core';
import { pick } from '../../helpers';
Expand All @@ -30,14 +29,6 @@ const noop = () => {};

export type MultiWatching = ReturnType<MultiCompiler['watch']>;

export type OutputFileSystem = Rspack.OutputFileSystem & {
// TODO: can be removed after Rspack adding this type
createReadStream?: (
p: string,
opts: { start: number; end: number },
) => ReadStream;
};

export type AssetsMiddlewareClose = (
callback: (err?: Error | null) => void,
) => void;
Expand Down
7 changes: 3 additions & 4 deletions packages/core/src/server/assets-middleware/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import onFinished from 'on-finished';
import type { Range, Result as RangeResult, Ranges } from 'range-parser';
import { requireCompiledPackage } from '../../helpers/vendors';
import { logger } from '../../logger';
import type { InternalContext, RequestHandler } from '../../types';
import type { InternalContext, RequestHandler, Rspack } from '../../types';
import { HttpCode } from '../helper';
import { getFileFromUrl } from './getFileFromUrl';
import type { OutputFileSystem } from './index';
import { parseTokenList } from './parseTokenList';

function getEtag(stat: FSStats): string {
Expand All @@ -18,7 +17,7 @@ function getEtag(stat: FSStats): string {

function createReadStreamOrReadFileSync(
filename: string,
outputFileSystem: OutputFileSystem,
outputFileSystem: Rspack.OutputFileSystem,
start: number,
end: number,
): { bufferOrStream: Buffer | ReadStream; byteLength: number } {
Expand Down Expand Up @@ -146,7 +145,7 @@ function sendError(res: ServerResponse, code: HttpCode): void {
export function createMiddleware(
context: InternalContext,
ready: (callback: () => void) => void,
outputFileSystem: OutputFileSystem,
outputFileSystem: Rspack.OutputFileSystem,
): RequestHandler {
return async function middleware(req, res, next) {
async function goNext() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import type {
Compiler,
OutputFileSystem as RspackOutputFileSystem,
} from '@rspack/core';
import fs from 'node:fs';
import type { Compiler, OutputFileSystem } from '@rspack/core';
import { requireCompiledPackage } from '../../helpers/vendors';
import type { OutputFileSystem } from './index';
import type { ResolvedWriteToDisk } from './setupWriteToDisk';

export function setupOutputFileSystem(
Expand All @@ -12,12 +9,17 @@ export function setupOutputFileSystem(
): OutputFileSystem {
if (writeToDisk !== true) {
const { createFsFromVolume, Volume } = requireCompiledPackage('memfs');
const outputFileSystem = createFsFromVolume(new Volume());
const outputFileSystem = createFsFromVolume(
new Volume(),
) as OutputFileSystem;

for (const compiler of compilers) {
compiler.outputFileSystem = outputFileSystem as RspackOutputFileSystem;
compiler.outputFileSystem = outputFileSystem;
}
}

return compilers[0].outputFileSystem as OutputFileSystem;
const compiler = compilers.find((compiler) =>
Boolean(compiler.outputFileSystem),
);
return compiler?.outputFileSystem ?? fs;
}
7 changes: 1 addition & 6 deletions packages/core/src/server/browserLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { requireCompiledPackage } from '../helpers/vendors';
import { logger } from '../logger';
import type { BrowserLogsStackTrace, InternalContext, Rspack } from '../types';
import { getFileFromUrl } from './assets-middleware/getFileFromUrl';
import type { OutputFileSystem } from './assets-middleware/index';
import type { ClientMessageError } from './socketServer';

/**
Expand Down Expand Up @@ -62,11 +61,7 @@ const parseFrame = async (
context: InternalContext,
) => {
const { file, column, lineNumber } = frame;
const sourceMapInfo = await getFileFromUrl(
`${file}.map`,
fs as OutputFileSystem,
context,
);
const sourceMapInfo = await getFileFromUrl(`${file}.map`, fs, context);

if (!sourceMapInfo || 'errorCode' in sourceMapInfo) {
return;
Expand Down
Loading