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

Core: Fix webpack stats #14198

Merged
merged 4 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 10 additions & 7 deletions lib/builder-webpack4/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ export const executor = {
get: webpack,
};

export const makeStatsFromError: (err: string) => Stats = (err) =>
({
hasErrors: () => true,
hasWarnings: () => false,
toJson: () => ({ warnings: [] as any[], errors: [err] }),
} as any);
Comment on lines +42 to +47
Copy link
Member

Choose a reason for hiding this comment

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

Is this something we want to export?

In the future we'll want to map the builders internal stats object to something common for all builders.
Right now we're leaking webpack details from our 2 builders. This is a known issue, but we should be really careful to not make that situation worse.

Copy link
Member

Choose a reason for hiding this comment

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

I'm thinking something like SyntacticEvent in React, where the underlaying raw event is still available, but there's a layer on top to smooth out the browser differences. (except we're smoothing out the builder differences)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I think that's the plan for 6.3 (or whenever we add our next builder).

We probably shouldn't be exporting this function however, that was an oversight.


export const start: WebpackBuilder['start'] = async ({ startTime, options, router }) => {
const config = await getConfig(options);
const compiler = executor.get(config);
Expand All @@ -48,11 +55,7 @@ export const start: WebpackBuilder['start'] = async ({ startTime, options, route
return {
bail,
totalTime: process.hrtime(startTime),
stats: ({
hasErrors: () => true,
hasWarngins: () => false,
toJson: () => ({ warnings: [] as any[], errors: [err] }),
} as any) as Stats,
stats: makeStatsFromError(err),
};
}

Expand Down Expand Up @@ -117,10 +120,10 @@ export const build: WebpackBuilder['build'] = async ({ options, startTime }) =>
if (!compiler) {
const err = `${config.name}: missing webpack compiler at runtime!`;
logger.error(err);
return;
return Promise.resolve(makeStatsFromError(err));
}

await new Promise<Stats>((succeed, fail) => {
return new Promise((succeed, fail) => {
compiler.run((error, stats) => {
if (error || !stats || stats.hasErrors()) {
logger.error('=> Failed to build the preview');
Expand Down
8 changes: 4 additions & 4 deletions lib/core-server/src/core-presets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ describe('dev cli flags', () => {
await buildDevStandalone({ ...cliOptions, webpackStatsJson: '/tmp/dir' });
expect(outputStats).toHaveBeenCalledWith(
'/tmp/dir',
expect.objectContaining({}),
expect.objectContaining({})
expect.objectContaining({ toJson: expect.any(Function) }),
expect.objectContaining({ toJson: expect.any(Function) })
);
});

Expand Down Expand Up @@ -233,8 +233,8 @@ describe('build cli flags', () => {
await buildStaticStandalone({ ...cliOptions, webpackStatsJson: '/tmp/dir' });
expect(outputStats).toHaveBeenCalledWith(
'/tmp/dir',
expect.objectContaining({}),
expect.objectContaining({})
expect.objectContaining({ toJson: expect.any(Function) }),
expect.objectContaining({ toJson: expect.any(Function) })
);
});
});
20 changes: 16 additions & 4 deletions lib/core-server/src/manager/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ export const executor = {
get: webpack,
};

export const makeStatsFromError = (err: string) =>
(({
hasErrors: () => true,
hasWarnings: () => false,
toJson: () => ({ warnings: [] as any[], errors: [err] }),
} as any) as Stats);

export const start: WebpackBuilder['start'] = async ({ startTime, options, router }) => {
const prebuiltDir = await getPrebuiltDir(options);
const config = await getConfig(options);
Expand Down Expand Up @@ -47,7 +54,12 @@ export const start: WebpackBuilder['start'] = async ({ startTime, options, route
if (!compiler) {
const err = `${config.name}: missing webpack compiler at runtime!`;
logger.error(err);
return;
// eslint-disable-next-line consistent-return
return {
bail,
totalTime: process.hrtime(startTime),
stats: makeStatsFromError(err),
};
}

const { handler, modulesCount } = await useProgressReporting(router, startTime, options);
Expand Down Expand Up @@ -103,10 +115,10 @@ export const build: WebpackBuilder['build'] = async ({ options, startTime }) =>
if (!compiler) {
const err = `${config.name}: missing webpack compiler at runtime!`;
logger.error(err);
return;
return Promise.resolve(makeStatsFromError(err));
}

await new Promise<void>((succeed, fail) => {
return new Promise((succeed, fail) => {
compiler.run((error, stats) => {
if (error || !stats || stats.hasErrors()) {
logger.error('=> Failed to build the manager');
Expand All @@ -131,7 +143,7 @@ export const build: WebpackBuilder['build'] = async ({ options, startTime }) =>
);
statsData?.warnings?.forEach((e) => logger.warn(e));

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