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
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.20.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": "^6.0.2"
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
38 changes: 11 additions & 27 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 @@ -1860,17 +1840,21 @@ class Server<
}

const staticOptions = this.options.static as NormalizedStatic[];
const useStatic = staticOptions.length > 0;
const serveStatic = useStatic
? (await import('serve-static')).default
: null;

if (staticOptions.length > 0) {
if (useStatic) {
for (const staticOption of staticOptions) {
for (const publicPath of staticOption.publicPath) {
middlewares.push({
name: 'serve-static',
path: publicPath,
middleware: getServeStatic()(
middleware: serveStatic!(
staticOption.directory,
staticOption.staticOptions,
),
) as DevServerMiddlewareHandler,
});
}
}
Expand Down Expand Up @@ -1907,16 +1891,16 @@ class Server<
middleware: this.middleware as DevServerMiddlewareHandler,
});

if (staticOptions.length > 0) {
if (useStatic) {
for (const staticOption of staticOptions) {
for (const publicPath of staticOption.publicPath) {
middlewares.push({
name: 'serve-static',
path: publicPath,
middleware: getServeStatic()(
middleware: serveStatic!(
staticOption.directory,
staticOption.staticOptions,
),
) as DevServerMiddlewareHandler,
});
Comment thread
chenjiahan marked this conversation as resolved.
}
}
Expand Down
9 changes: 0 additions & 9 deletions tests/e2e/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ describe('API', () => {
let consoleMessages;

beforeEach(async () => {
// this is important - it clears the cache
rs.resetModules();

process.env = { ...OLD_ENV };

process.env.WEBPACK_SERVE = undefined;

({ page, browser } = await runBrowser());

pageErrors = [];
Expand All @@ -36,8 +29,6 @@ describe('API', () => {
});

it('should be present', async () => {
expect(process.env.WEBPACK_SERVE).toBeUndefined();

page
.on('console', (message) => {
consoleMessages.push(message);
Expand Down
5 changes: 1 addition & 4 deletions tests/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
"compilerOptions": {
"allowJs": true,
"checkJs": false,
"rootDir": "../",
"paths": {
"@rspack/dev-server": ["../"]
}
"rootDir": "../"
},
"include": ["../src", "../tests", "../client-src"],
"references": []
Expand Down