Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: adapted to Rspack v0.7 incompatible changes #2352

Merged
merged 2 commits into from
May 15, 2024
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
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
},
"dependencies": {
"@rsbuild/shared": "workspace:*",
"@rspack/core": "0.6.5",
"@rspack/core": "0.6.5-canary-2456d69-20240515093621",
"@swc/helpers": "0.5.3",
"core-js": "~3.36.0",
"html-webpack-plugin": "npm:html-rspack-plugin@5.7.2",
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/client/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ function formatMessage(stats: StatsError | string) {
// Stats error object
if (typeof stats === 'object') {
const fileName = resolveFileName(stats);
const mainMessage =
typeof stats.formatted === 'string' ? stats.formatted : stats.message;
const mainMessage = stats.message;
chenjiahan marked this conversation as resolved.
Show resolved Hide resolved
const details = stats.details ? `\nDetails: ${stats.details}\n` : '';
const stack = stats.stack ? `\n${stats.stack}` : '';

Expand Down
10 changes: 6 additions & 4 deletions packages/core/src/server/devServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'node:fs';
import {
type CreateDevMiddlewareReturns,
type CreateDevServerOptions,
type OutputFileSystem,
ROOT_DIST_DIR,
type RsbuildDevServer,
type StartDevServerOptions,
Expand Down Expand Up @@ -66,7 +67,7 @@ export async function createDevServer<
https,
};

let outputFileSystem = fs;
let outputFileSystem: OutputFileSystem = fs;

const startCompile: () => Promise<
RsbuildDevMiddlewareOptions['compileMiddlewareAPI']
Expand All @@ -91,9 +92,10 @@ export async function createDevServer<

compilerDevMiddleware.init();

outputFileSystem = isMultiCompiler(compiler)
? compiler.compilers[0].outputFileSystem
: compiler.outputFileSystem;
outputFileSystem =
(isMultiCompiler(compiler)
? compiler.compilers[0].outputFileSystem
: compiler.outputFileSystem) || fs;

return {
middleware: compilerDevMiddleware.middleware,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/server/getDevMiddlewares.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type fs from 'node:fs';
import { isAbsolute, join } from 'node:path';
import url from 'node:url';
import type {
CompileMiddlewareAPI,
DevConfig,
Middlewares,
OutputFileSystem,
RequestHandler,
ServerAPIs,
ServerConfig,
Expand All @@ -22,7 +22,7 @@ export type RsbuildDevMiddlewareOptions = {
dev: DevConfig;
server: ServerConfig;
compileMiddlewareAPI?: CompileMiddlewareAPI;
outputFileSystem: typeof fs;
outputFileSystem: OutputFileSystem;
output: {
distPath: string;
};
Expand Down
20 changes: 14 additions & 6 deletions packages/core/src/server/middlewares.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type fs from 'node:fs';
import path from 'node:path';
import { parse } from 'node:url';
import {
type HtmlFallback,
type RequestHandler as Middleware,
type OutputFileSystem,
color,
debug,
isDebug,
Expand Down Expand Up @@ -81,12 +81,12 @@ export const getHtmlFallbackMiddleware: (params: {
distPath: string;
callback?: Middleware;
htmlFallback?: HtmlFallback;
outputFileSystem: typeof fs;
outputFileSystem: OutputFileSystem;
}) => Middleware = ({ htmlFallback, distPath, callback, outputFileSystem }) => {
/**
* support access page without suffix and support fallback in some edge cases
*/
return (req, res, next) => {
return async (req, res, next) => {
if (
// Only accept GET or HEAD
(req.method !== 'GET' && req.method !== 'HEAD') ||
Expand Down Expand Up @@ -119,6 +119,14 @@ export const getHtmlFallbackMiddleware: (params: {
return next();
}

const isFileExists = async (filePath: string) => {
return new Promise((resolve) => {
outputFileSystem.stat(filePath, (_error, stats) => {
resolve(stats?.isFile());
});
});
};

const rewrite = (newUrl: string, isFallback = false) => {
if (isFallback && isDebug()) {
debug(
Expand All @@ -143,7 +151,7 @@ export const getHtmlFallbackMiddleware: (params: {
const newUrl = `${pathname}index.html`;
const filePath = path.join(distPath, pathname, 'index.html');

if (outputFileSystem.existsSync(filePath)) {
if (await isFileExists(filePath)) {
return rewrite(newUrl);
}
} else if (
Expand All @@ -153,13 +161,13 @@ export const getHtmlFallbackMiddleware: (params: {
const newUrl = `${pathname}.html`;
const filePath = path.join(distPath, `${pathname}.html`);

if (outputFileSystem.existsSync(filePath)) {
if (await isFileExists(filePath)) {
return rewrite(newUrl);
}
}

if (htmlFallback === 'index') {
if (outputFileSystem.existsSync(path.join(distPath, 'index.html'))) {
if (await isFileExists(path.join(distPath, 'index.html'))) {
return rewrite('/index.html', true);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"dependencies": {
"@rsbuild/shared": "workspace:*",
"@rspack/plugin-react-refresh": "0.6.5",
"@rspack/plugin-react-refresh": "0.6.5-canary-2456d69-20240515093621",
"react-refresh": "^0.14.2"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
"test:watch": "vitest dev --no-coverage"
},
"dependencies": {
"@rspack/core": "0.6.5",
"@rspack/core": "0.6.5-canary-2456d69-20240515093621",
"caniuse-lite": "^1.0.30001617",
"html-webpack-plugin": "npm:html-rspack-plugin@5.7.2",
"postcss": "^8.4.38"
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/src/types/rspack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export type RspackMultiCompiler = Rspack.MultiCompiler;
/** T[] => T */
type GetElementType<T extends any[]> = T extends (infer U)[] ? U : never;

export type OutputFileSystem = Rspack.OutputFileSystem;

export type RspackRule = GetElementType<
NonNullable<NonNullable<RspackConfig['module']>['rules']>
>;
Expand Down
Loading
Loading