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

Webpack5: Quit process after finishing a static build #15483

Merged
merged 2 commits into from Jul 7, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion app/angular/src/server/build.ts
@@ -1,4 +1,13 @@
import { buildStatic } from '@storybook/core/server';
import { logger } from '@storybook/node-logger';
import options from './options';

buildStatic(options);
async function build() {
try {
await buildStatic(options);
} catch (error) {
logger.error(error);
}
}

build();
23 changes: 19 additions & 4 deletions lib/builder-webpack5/src/index.ts
Expand Up @@ -129,14 +129,19 @@ export const build: WebpackBuilder['build'] = async ({ options, startTime }) =>
const config = await getConfig(options);

return new Promise((succeed, fail) => {
webpackInstance(config).run((error, stats) => {
const compiler = webpackInstance(config);

compiler.run((error, stats) => {
if (error || !stats || stats.hasErrors()) {
logger.error('=> Failed to build the preview');
process.exitCode = 1;

if (error) {
logger.error(error.message);
return fail(error);

compiler.close(() => fail(error));

return;
}

if (stats && (stats.hasErrors() || stats.hasWarnings())) {
Expand All @@ -145,7 +150,9 @@ export const build: WebpackBuilder['build'] = async ({ options, startTime }) =>
errors.forEach((e) => logger.error(e.message));
warnings.forEach((e) => logger.error(e.message));

return fail(stats);
compiler.close(() => fail(stats));

return;
}
}

Expand All @@ -154,7 +161,15 @@ export const build: WebpackBuilder['build'] = async ({ options, startTime }) =>
stats.toJson({ warnings: true }).warnings.forEach((e) => logger.warn(e.message));
}

return succeed(stats);
// https://webpack.js.org/api/node/#run
// #15227
compiler.close((closeErr) => {
if (closeErr) {
return fail(closeErr);
}

return succeed(stats);
});
});
});
};
Expand Down