Skip to content

Commit

Permalink
Single warning per webpack --watch
Browse files Browse the repository at this point in the history
- Only warn one time per execution of `webpack --watch`
  • Loading branch information
jon-ressio committed Sep 14, 2023
1 parent ee62b5b commit b369807
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 28 deletions.
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 };

0 comments on commit b369807

Please sign in to comment.