Skip to content

Commit

Permalink
try to serve static before rewrites and also serve after rewrites (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
agubler authored Jan 24, 2019
1 parent 3282b45 commit 648b10e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
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);

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

0 comments on commit 648b10e

Please sign in to comment.