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

Try to serve static before rewrites when using serve option #236

Merged
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
30 changes: 18 additions & 12 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ function createWatchCompiler(config: webpack.Configuration) {
return compiler;
}

function serveStatic(app: express.Application, outputDir: string, mode: string, compression?: string[]) {
if (mode === 'dist' && Array.isArray(compression)) {
const useBrotli = compression.indexOf('brotli') > -1;
app.use(
expressStaticGzip(outputDir, {
enableBrotli: useBrotli,
orderPreference: useBrotli ? ['br'] : undefined
})
);
} else {
app.use(express.static(outputDir));
}
}

function build(config: webpack.Configuration, args: any) {
const compiler = createCompiler(config);
const spinner = ora('building').start();
Expand Down Expand Up @@ -137,6 +151,9 @@ function serve(config: webpack.Configuration, args: any): Promise<void> {
})
);

const outputDir = (config.output && config.output.path) || process.cwd();
serveStatic(app, outputDir, args.mode, args.compression);

app.use(
history({
rewrites: [
Expand All @@ -163,18 +180,7 @@ function serve(config: webpack.Configuration, args: any): Promise<void> {
})
);

const outputDir = (config.output && config.output.path) || process.cwd();
if (args.mode === 'dist' && Array.isArray(args.compression)) {
const useBrotli = args.compression.includes('brotli');
app.use(
expressStaticGzip(outputDir, {
enableBrotli: useBrotli,
orderPreference: useBrotli ? ['br'] : undefined
})
);
} else {
app.use(express.static(outputDir));
}
serveStatic(app, outputDir, args.mode, args.compression);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is the second time serveStatic is called within serve, without any return or conditional in between. Is this deliberate?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's is, the first time is before the history middleware and the middleware doesn't call next if it successfully serve a resource. If it doesn't find a resource then it will carry on with the middleware and run the history re-write middleware and then try and serve the re-written resource.

Copy link
Member Author

Choose a reason for hiding this comment

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

I should probably rename the function to either 'use....' or 'register....'

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, that makes sense. Thanks for the explanation.


if (args.proxy) {
Object.keys(args.proxy).forEach((context) => {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ describe('command', () => {
watch: true
})
.then(() => {
assert.strictEqual(useStub.callCount, 4);
assert.strictEqual(useStub.callCount, 5);
assert.isTrue(
hotMiddleware.calledWith(compiler, {
heartbeat: 10000
Expand Down