Skip to content

Commit

Permalink
passing tests for workbox webpack plugin
Browse files Browse the repository at this point in the history
passing tests for wb wpp

cleaning imports
  • Loading branch information
tropicadri committed Jan 3, 2022
1 parent 8a5ee86 commit dfcc568
Show file tree
Hide file tree
Showing 8 changed files with 27,040 additions and 286 deletions.
27,203 changes: 26,980 additions & 223 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -22,6 +22,7 @@
"@types/fs-extra": "^9.0.5",
"@types/lodash": "^4.14.165",
"@types/stringify-object": "^3.3.0",
"@types/webpack": "^5.28.0",
"@typescript-eslint/eslint-plugin": "^4.32.0",
"@typescript-eslint/parser": "^4.32.0",
"acorn": "^8.0.1",
Expand Down Expand Up @@ -123,6 +124,5 @@
"test_node": "gulp test_node",
"test_server": "gulp test_server",
"version": "gulp build && git add -A packages"
},
"version": "0.0.0"
}
}
16 changes: 7 additions & 9 deletions packages/workbox-webpack-plugin/src/generate-sw.ts
Expand Up @@ -9,12 +9,9 @@
import {validateWebpackGenerateSWOptions} from 'workbox-build/build/lib/validate-options';
import {bundle} from 'workbox-build/build/lib/bundle';
import {populateSWTemplate} from 'workbox-build/build/lib/populate-sw-template';
//import {GoogleAnalyticsInitializeOptions} from 'workbox-google-analytics/initialize';
import prettyBytes from 'pretty-bytes';
import webpack from 'webpack';
//import {RuntimeCachingEntry} from 'workbox-build';
import {ManifestEntry, WebpackGenerateSWOptions} from 'workbox-build';
//import {CommonConfig} from './types';
import {getScriptFilesForChunks} from './lib/get-script-files-for-chunks';
import {getManifestEntriesFromCompilation} from './lib/get-manifest-entries-from-compilation';
import {relativeToOutputPath} from './lib/relative-to-output-path';
Expand Down Expand Up @@ -230,7 +227,7 @@ class GenerateSW {
// https://github.com/webpack/webpack/issues/11425#issuecomment-690387207
if (webpack.version.startsWith('4.')) {
compiler.hooks.emit.tapPromise(this.constructor.name, (compilation) =>
this.addAssets(compilation).catch((error: webpack.WebpackError) => {
this.addAssets(compilation).catch((error) => {
compilation.errors.push(error);
}),
);
Expand Down Expand Up @@ -281,7 +278,9 @@ class GenerateSW {
warning instanceof Error && warning.message === warningMessage,
)
) {
compilation.warnings.push(new webpack.WebpackError(warningMessage));
compilation.warnings.push(
Error(warningMessage) as webpack.WebpackError,
);
}
} else {
this.alreadyCalled = true;
Expand All @@ -297,15 +296,14 @@ class GenerateSW {
if (error instanceof Error) {
throw new Error(
`Please check your ${this.constructor.name} plugin ` +
`configuration:\n${error.message}`,
`configuration:\n${error.message}`,
);
}
}

// Ensure that we don't precache any of the assets generated by *any*
// instance of this plugin.
// eslint-disable-next-line
//config.exclude!.push(({asset}) => _generatedAssetNames.has(asset.name));
config.exclude!.push((asset: string) => _generatedAssetNames.has(asset));

if (config.importScriptsViaChunks) {
// Anything loaded via importScripts() is implicitly cached by the service
Expand Down Expand Up @@ -359,4 +357,4 @@ class GenerateSW {
}
}

export {GenerateSW}
export {GenerateSW};
42 changes: 21 additions & 21 deletions packages/workbox-webpack-plugin/src/inject-manifest.ts
Expand Up @@ -31,13 +31,6 @@ const SingleEntryPlugin = webpack.EntryPlugin || webpack.SingleEntryPlugin;
// https://github.com/webpack/webpack/issues/11425#issuecomment-686607633
const {RawSource} = webpack.sources || require('webpack-sources');

// interface InjectManifestConfig extends CommonConfig {
// swSrc?: string;
// compileSrc?: boolean;
// injectionPoint?: string;
// webpackCompilationPlugins?: Array<webpack.WebpackPluginInstance>;
// }

/**
* This class supports compiling a service worker file provided via `swSrc`,
* and injecting into that service worker a list of URLs and revision
Expand Down Expand Up @@ -219,7 +212,8 @@ class InjectManifest {

const childCompiler = compilation.createChildCompiler(
this.constructor.name,
outputOptions, [],
outputOptions,
[],
);

childCompiler.context = parentCompiler.context;
Expand Down Expand Up @@ -267,7 +261,9 @@ class InjectManifest {
parentCompiler: webpack.Compiler,
): void {
// eslint-disable-next-line
const source = (parentCompiler.inputFileSystem as any).readFileSync(this.config.swSrc)
const source = (parentCompiler.inputFileSystem as any).readFileSync(
this.config.swSrc,
);
compilation.emitAsset(this.config.swDest!, new RawSource(source));
}

Expand All @@ -285,11 +281,13 @@ class InjectManifest {
this.config = validateWebpackInjectManifestOptions(this.config);
} catch (error) {
// eslint-disable-next-line
throw new Error(
`Please check your ${this.constructor.name} plugin ` +
// eslint-disable-next-line
`configuration:\n${error.message}`,
);
if (error instanceof Error) {
throw new Error(
`Please check your ${this.constructor.name} plugin ` +
// eslint-disable-next-line
`configuration:\n${error.message}`,
);
}
}

this.config.swDest = relativeToOutputPath(compilation, this.config.swDest!);
Expand All @@ -306,10 +304,10 @@ class InjectManifest {
this.config.webpackCompilationPlugins.length > 0
) {
compilation.warnings.push(
new webpack.WebpackError(
new Error(
'compileSrc is false, so the ' +
'webpackCompilationPlugins option will be ignored.',
),
'webpackCompilationPlugins option will be ignored.',
) as webpack.WebpackError,
);
}
}
Expand All @@ -336,7 +334,9 @@ class InjectManifest {
warning instanceof Error && warning.message === warningMessage,
)
) {
compilation.warnings.push(new webpack.WebpackError(warningMessage));
compilation.warnings.push(
new Error(warningMessage) as webpack.WebpackError,
);
}
} else {
this.alreadyCalled = true;
Expand All @@ -346,7 +346,7 @@ class InjectManifest {

// Ensure that we don't precache any of the assets generated by *any*
// instance of this plugin.
config.exclude!.push((name) => _generatedAssetNames.has(name));
config.exclude!.push((name: string) => _generatedAssetNames.has(name));

// See https://webpack.js.org/contribute/plugin-patterns/#monitoring-the-watch-graph
const absoluteSwSrc = upath.resolve(this.config.swSrc);
Expand All @@ -367,7 +367,7 @@ class InjectManifest {
throw new Error(
`Multiple instances of ${config.injectionPoint ?? ''} were ` +
`found in your SW source. Include it only once. For more info, see ` +
`https://github.com/GoogleChrome/workbox/issues/2681`,
`https://github.com/GoogleChrome/workbox/issues/2681`,
);
}

Expand Down Expand Up @@ -432,4 +432,4 @@ class InjectManifest {
}
}

export {InjectManifest}
export {InjectManifest};
Expand Up @@ -36,7 +36,7 @@ function checkConditions(
): boolean {
for (const condition of conditions) {
if (typeof condition === 'function') {
return condition(asset.name)
return condition(asset.name);
} else {
if (ModuleFilenameHelpers.matchPart(asset.name, condition)) {
return true;
Expand Down Expand Up @@ -137,11 +137,11 @@ function filterAssets(
}
} else {
compilation.warnings.push(
new WebpackError(
new Error(
`The chunk '${name}' was ` +
`provided in your Workbox chunks config, but was not found in the ` +
`compilation.`,
),
`provided in your Workbox chunks config, but was not found in the ` +
`compilation.`,
) as WebpackError,
);
}
}
Expand Down Expand Up @@ -188,8 +188,7 @@ function filterAssets(

// Treat an empty config.includes as an implicit inclusion.
const isIncluded =
!Array.isArray(config.include) ||
checkConditions(asset, config.include);
!Array.isArray(config.include) || checkConditions(asset, config.include);
if (!isIncluded) {
continue;
}
Expand Down Expand Up @@ -229,7 +228,7 @@ export async function getManifestEntriesFromCompilation(

// See https://github.com/GoogleChrome/workbox/issues/2790
for (const warning of warnings) {
compilation.warnings.push(new WebpackError(warning));
compilation.warnings.push(new Error(warning) as WebpackError);
}

// Ensure that the entries are properly sorted by URL.
Expand Down
Expand Up @@ -30,20 +30,20 @@ export function getScriptFilesForChunks(
}
} else {
compilation.warnings.push(
new WebpackError(
new Error(
`${chunkName} was provided to ` +
`importScriptsViaChunks, but didn't match any named chunks.`,
),
`importScriptsViaChunks, but didn't match any named chunks.`,
) as WebpackError,
);
}
}

if (scriptFiles.size === 0) {
compilation.warnings.push(
new WebpackError(
new Error(
`There were no assets matching ` +
`importScriptsViaChunks: [${chunkNames.join(' ')}].`,
),
`importScriptsViaChunks: [${chunkNames.join(' ')}].`,
) as WebpackError,
);
}

Expand Down
Expand Up @@ -22,13 +22,17 @@ import type {Compilation} from 'webpack';
* @param {string} swContents The contents of the swSrc file, which may or
* may not include a valid sourcemap comment.
* @param {string} swDest The configured swDest value.
* @return {string|undefined} If the swContents contains a valid soucemap
* @return {string|undefined} If the swContents contains a valid sourcemap
* comment pointing to an asset present in the compilation, this will return the
* name of that asset. Otherwise, it will return undefined.
*
* @private
*/
export function getSourcemapAssetName(compilation: Compilation, swContents: string, swDest: string): string | undefined {
export function getSourcemapAssetName(
compilation: Compilation,
swContents: string,
swDest: string,
): string | undefined {
// eslint-disable-next-line
const url = sourceMapURL.getFrom(swContents);
if (url) {
Expand All @@ -39,7 +43,8 @@ export function getSourcemapAssetName(compilation: Compilation, swContents: stri
// See https://github.com/GoogleChrome/workbox/issues/2250
const swAssetDirname = upath.dirname(swDest);
const sourcemapURLAssetName = upath.normalize(
upath.join(swAssetDirname, url));
upath.join(swAssetDirname, url),
);

// Not sure if there's a better way to check for asset existence?
if (compilation.getAsset(sourcemapURLAssetName)) {
Expand Down
23 changes: 9 additions & 14 deletions packages/workbox-webpack-plugin/tsconfig.json
@@ -1,19 +1,14 @@
{
"extends": "../../tsconfig",
"compilerOptions": {
"esModuleInterop": true,
"module": "CommonJS",
"outDir": "./build",
"resolveJsonModule": true,
"rootDir": "./src",
"target": "ES2018",
"tsBuildInfoFile": "./tsconfig.tsbuildinfo"
"esModuleInterop": true,
"module": "CommonJS",
"outDir": "./build",
"resolveJsonModule": true,
"rootDir": "./src",
"target": "ES2018",
"tsBuildInfoFile": "./tsconfig.tsbuildinfo"
},
"include": [
"src/**/*.ts"
],
"exclude": ["../workbox-cli/src/index.d.ts"],
"references": [
{"path": "../workbox-build/"}
]
"include": ["src/**/*.ts"],
"references": [{"path": "../workbox-build/"}]
}

0 comments on commit dfcc568

Please sign in to comment.