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

Single warning per webpack --watch #3246

Open
wants to merge 1 commit into
base: v7
Choose a base branch
from
Open
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
37 changes: 21 additions & 16 deletions packages/workbox-webpack-plugin/src/generate-sw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,15 @@ export interface GenerateSWConfig extends WebpackGenerateSWOptions {
class GenerateSW {
protected config: GenerateSWConfig;
private alreadyCalled: boolean;
private alreadyWarned: boolean;

/**
* Creates an instance of GenerateSW.
*/
constructor(config: GenerateSWConfig = {}) {
this.config = config;
this.alreadyCalled = false;
this.alreadyWarned = false;
}

/**
Expand Down Expand Up @@ -147,22 +149,25 @@ class GenerateSW {
async addAssets(compilation: webpack.Compilation): Promise<void> {
// See https://github.com/GoogleChrome/workbox/issues/1790
if (this.alreadyCalled) {
const warningMessage =
`${this.constructor.name} has been called ` +
`multiple times, perhaps due to running webpack in --watch mode. The ` +
`precache manifest generated after the first call may be inaccurate! ` +
`Please see https://github.com/GoogleChrome/workbox/issues/1790 for ` +
`more information.`;

if (
!compilation.warnings.some(
(warning) =>
warning instanceof Error && warning.message === warningMessage,
)
) {
compilation.warnings.push(
Error(warningMessage) as webpack.WebpackError,
);
if(!this.alreadyWarned) {
const warningMessage =
`${this.constructor.name} has been called ` +
`multiple times, perhaps due to running webpack in --watch mode. The ` +
`precache manifest generated after the first call may be inaccurate! ` +
`Please see https://github.com/GoogleChrome/workbox/issues/1790 for ` +
`more information.`;

if (
!compilation.warnings.some(
(warning) =>
warning instanceof Error && warning.message === warningMessage,
)
) {
compilation.warnings.push(
Error(warningMessage) as webpack.WebpackError,
);
this.alreadyWarned = true;
}
}
} else {
this.alreadyCalled = true;
Expand Down
29 changes: 17 additions & 12 deletions packages/workbox-webpack-plugin/src/inject-manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ const {RawSource} = webpack.sources || require('webpack-sources');
class InjectManifest {
protected config: WebpackInjectManifestOptions;
private alreadyCalled: boolean;
private alreadyWarned: boolean;

/**
* Creates an instance of InjectManifest.
*/
constructor(config: WebpackInjectManifestOptions) {
this.config = config;
this.alreadyCalled = false;
this.alreadyWarned = false;
}

/**
Expand Down Expand Up @@ -260,23 +262,26 @@ class InjectManifest {
async addAssets(compilation: webpack.Compilation): Promise<void> {
// See https://github.com/GoogleChrome/workbox/issues/1790
if (this.alreadyCalled) {
const warningMessage =
if(!this.alreadyWarned) {
const warningMessage =
`${this.constructor.name} has been called ` +
`multiple times, perhaps due to running webpack in --watch mode. The ` +
`precache manifest generated after the first call may be inaccurate! ` +
`Please see https://github.com/GoogleChrome/workbox/issues/1790 for ` +
`more information.`;

if (
!compilation.warnings.some(
(warning) =>
warning instanceof Error && warning.message === warningMessage,
)
) {
compilation.warnings.push(
new Error(warningMessage) as webpack.WebpackError,
);
}
if (
!compilation.warnings.some(
(warning) =>
warning instanceof Error && warning.message === warningMessage,
)
) {
compilation.warnings.push(
new Error(warningMessage) as webpack.WebpackError,
);
this.alreadyWarned = true;
}
}
} else {
this.alreadyCalled = true;
}
Expand Down Expand Up @@ -368,4 +373,4 @@ class InjectManifest {
}
}

export {InjectManifest};
export { InjectManifest };