Skip to content
Open
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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
"connect-next": "^4.0.0",
"http-proxy-middleware": "^3.0.5",
"ipaddr.js": "^2.3.0",
"serve-static": "^2.2.1",
"ws": "^8.19.0"
},
"devDependencies": {
Expand Down Expand Up @@ -82,6 +81,7 @@
"react-refresh": "0.18.0",
"require-from-string": "^2.0.2",
"selfsigned": "^5.5.0",
"serve-static": "^2.2.1",
"simple-git-hooks": "^2.13.1",
"style-loader": "^4.0.0",
"typescript": "^5.9.3"
Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

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

1 change: 0 additions & 1 deletion rslib.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export default defineConfig({
'connect-history-api-fallback':
'commonjs connect-history-api-fallback',
'http-proxy-middleware': 'commonjs http-proxy-middleware',
'serve-static': 'commonjs serve-static',
selfsigned: 'commonjs selfsigned',
},
},
Expand Down
30 changes: 5 additions & 25 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,31 +129,11 @@ if (!process.env.WEBPACK_SERVE) {
process.env.WEBPACK_SERVE = 'true';
}

type FunctionReturning<T> = () => T;

const memoize = <T>(fn: FunctionReturning<T>): FunctionReturning<T> => {
let cache = false;
let result: T | undefined;
let fnRef = fn;
return () => {
if (cache) {
return result as T;
}

result = fnRef();
cache = true;
// Allow to clean up memory for fn and all dependent resources
fnRef = undefined as unknown as FunctionReturning<T>;
return result as T;
};
};

const getConnect = async () => {
const { connect } = await import('connect-next');
return connect;
};
const getChokidar = memoize(() => import('chokidar'));
const getServeStatic = memoize(() => require('serve-static'));
const getChokidar = () => import('chokidar');

const encodeOverlaySettings = (
setting?: OverlayMessageOptions,
Expand Down Expand Up @@ -1892,10 +1872,10 @@ class Server<
middlewares.push({
name: 'serve-static',
path: publicPath,
middleware: getServeStatic()(
middleware: (await import('serve-static')).default(
staticOption.directory,
staticOption.staticOptions,
),
) as DevServerMiddlewareHandler,
Comment on lines +1875 to +1878
});
}
}
Expand Down Expand Up @@ -1938,10 +1918,10 @@ class Server<
middlewares.push({
name: 'serve-static',
path: publicPath,
middleware: getServeStatic()(
middleware: (await import('serve-static')).default(
staticOption.directory,
staticOption.staticOptions,
),
) as DevServerMiddlewareHandler,
Comment on lines +1921 to +1924
});
Comment on lines 1918 to 1925
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This block for adding the serve-static middleware is a duplicate of the code at lines 1872-1879. To improve maintainability and reduce redundancy, consider extracting this logic into a private helper method. This would also allow you to import('serve-static') just once inside that helper.

}
}
Expand Down
Loading