Skip to content

Commit

Permalink
chore: adapted to rspack 0.7 (#2352)
Browse files Browse the repository at this point in the history
  • Loading branch information
9aoy authored May 15, 2024
1 parent dfcf7bc commit 696cec3
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 103 deletions.
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;
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

0 comments on commit 696cec3

Please sign in to comment.