Skip to content

Commit 1e71a4c

Browse files
committed
refactor: bundle serve-static
1 parent b5dcb24 commit 1e71a4c

File tree

4 files changed

+9
-30
lines changed

4 files changed

+9
-30
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
"connect-next": "^4.0.0",
5353
"http-proxy-middleware": "^3.0.5",
5454
"ipaddr.js": "^2.3.0",
55-
"serve-static": "^2.2.1",
5655
"ws": "^8.19.0"
5756
},
5857
"devDependencies": {
@@ -82,6 +81,7 @@
8281
"react-refresh": "0.18.0",
8382
"require-from-string": "^2.0.2",
8483
"selfsigned": "^5.5.0",
84+
"serve-static": "^2.2.1",
8585
"simple-git-hooks": "^2.13.1",
8686
"style-loader": "^4.0.0",
8787
"typescript": "^5.9.3"

pnpm-lock.yaml

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rslib.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export default defineConfig({
1111
'connect-history-api-fallback':
1212
'commonjs connect-history-api-fallback',
1313
'http-proxy-middleware': 'commonjs http-proxy-middleware',
14-
'serve-static': 'commonjs serve-static',
1514
selfsigned: 'commonjs selfsigned',
1615
},
1716
},

src/server.ts

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -129,31 +129,11 @@ if (!process.env.WEBPACK_SERVE) {
129129
process.env.WEBPACK_SERVE = 'true';
130130
}
131131

132-
type FunctionReturning<T> = () => T;
133-
134-
const memoize = <T>(fn: FunctionReturning<T>): FunctionReturning<T> => {
135-
let cache = false;
136-
let result: T | undefined;
137-
let fnRef = fn;
138-
return () => {
139-
if (cache) {
140-
return result as T;
141-
}
142-
143-
result = fnRef();
144-
cache = true;
145-
// Allow to clean up memory for fn and all dependent resources
146-
fnRef = undefined as unknown as FunctionReturning<T>;
147-
return result as T;
148-
};
149-
};
150-
151132
const getConnect = async () => {
152133
const { connect } = await import('connect-next');
153134
return connect;
154135
};
155-
const getChokidar = memoize(() => import('chokidar'));
156-
const getServeStatic = memoize(() => require('serve-static'));
136+
const getChokidar = () => import('chokidar');
157137

158138
const encodeOverlaySettings = (
159139
setting?: OverlayMessageOptions,
@@ -1892,10 +1872,10 @@ class Server<
18921872
middlewares.push({
18931873
name: 'serve-static',
18941874
path: publicPath,
1895-
middleware: getServeStatic()(
1875+
middleware: (await import('serve-static')).default(
18961876
staticOption.directory,
18971877
staticOption.staticOptions,
1898-
),
1878+
) as DevServerMiddlewareHandler,
18991879
});
19001880
}
19011881
}
@@ -1938,10 +1918,10 @@ class Server<
19381918
middlewares.push({
19391919
name: 'serve-static',
19401920
path: publicPath,
1941-
middleware: getServeStatic()(
1921+
middleware: (await import('serve-static')).default(
19421922
staticOption.directory,
19431923
staticOption.staticOptions,
1944-
),
1924+
) as DevServerMiddlewareHandler,
19451925
});
19461926
}
19471927
}

0 commit comments

Comments
 (0)