Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@serwist/webpack-plugin

Package Overview
Dependencies
Maintainers
1
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@serwist/webpack-plugin - npm Package Compare versions

Comparing version 9.0.6 to 9.0.7

dist/chunks/perform-child-compilation.js

40

dist/index.internal.js

@@ -1,3 +0,3 @@

import { r as relativeToOutputPath } from './chunks/relative-to-output-path.js';
import 'upath';
import { p as performChildCompilation, r as relativeToOutputPath } from './chunks/perform-child-compilation.js';
import 'node:path';

@@ -8,3 +8,2 @@ class ChildCompilationPlugin {

plugins;
webpack;
constructor({ src, dest, plugins }){

@@ -14,43 +13,10 @@ this.src = src;

this.plugins = plugins;
this.webpack = null;
}
propagateWebpackConfig(compiler) {
this.webpack = compiler.webpack;
}
apply(compiler) {
this.propagateWebpackConfig(compiler);
compiler.hooks.make.tapPromise(this.constructor.name, (compilation)=>this.performChildCompilation(compilation, compiler).catch((error)=>{
compiler.hooks.make.tapPromise(this.constructor.name, (compilation)=>performChildCompilation(compiler, compilation, this.constructor.name, this.src, relativeToOutputPath(compilation, this.dest), this.plugins).catch((error)=>{
compilation.errors.push(error);
}));
}
async performChildCompilation(compilation, parentCompiler) {
const resolvedDest = relativeToOutputPath(compilation, this.dest);
const outputOptions = {
filename: resolvedDest
};
const childCompiler = compilation.createChildCompiler(this.constructor.name, outputOptions, []);
childCompiler.options.target = "webworker";
childCompiler.context = parentCompiler.context;
childCompiler.inputFileSystem = parentCompiler.inputFileSystem;
childCompiler.outputFileSystem = parentCompiler.outputFileSystem;
if (this.plugins !== undefined) {
for (const plugin of this.plugins){
plugin?.apply(childCompiler);
}
}
new this.webpack.EntryPlugin(parentCompiler.context, this.src, this.constructor.name).apply(childCompiler);
await new Promise((resolve, reject)=>{
childCompiler.runAsChild((error, _entries, childCompilation)=>{
if (error) {
reject(error);
} else {
compilation.warnings = compilation.warnings.concat(childCompilation?.warnings ?? []);
compilation.errors = compilation.errors.concat(childCompilation?.errors ?? []);
resolve();
}
});
});
}
}
export { ChildCompilationPlugin, relativeToOutputPath };

@@ -0,8 +1,12 @@

import path from 'node:path';
import { transformManifest, getSourceMapURL, stringify, escapeRegExp, replaceAndUpdateSourceMap } from '@serwist/build';
import prettyBytes from 'pretty-bytes';
import upath from 'upath';
import { validationErrorMap, SerwistConfigError } from '@serwist/build/schema';
import crypto from 'node:crypto';
import { r as relativeToOutputPath } from './chunks/relative-to-output-path.js';
import { r as relativeToOutputPath, p as performChildCompilation } from './chunks/perform-child-compilation.js';
const toUnix = (p)=>{
return p.replace(/\\/g, "/").replace(/(?<!^)\/+/g, "/");
};
const validateInjectManifestOptions = async (input)=>{

@@ -154,4 +158,4 @@ const result = await (await import('./chunks/schema.js')).injectManifestOptions.spa(input, {

if (url) {
const swAssetDirname = upath.dirname(swDest);
const sourcemapURLAssetName = upath.normalize(upath.join(swAssetDirname, url));
const swAssetDirname = path.dirname(swDest);
const sourcemapURLAssetName = path.normalize(path.join(swAssetDirname, url));
if (compilation.getAsset(sourcemapURLAssetName)) {

@@ -176,3 +180,3 @@ return sourcemapURLAssetName;

this.webpack = compiler.webpack;
const parsedSwSrc = upath.parse(this.config.swSrc);
const parsedSwSrc = path.parse(this.config.swSrc);
this.config = {

@@ -213,3 +217,3 @@ swDest: `${parsedSwSrc.name}.js`,

this.propagateWebpackConfig(compiler);
compiler.hooks.make.tapPromise(this.constructor.name, (compilation)=>this.handleMake(compilation, compiler).catch((error)=>{
compiler.hooks.make.tapPromise(this.constructor.name, (compilation)=>this.handleMake(compiler, compilation).catch((error)=>{
compilation.errors.push(error);

@@ -227,29 +231,7 @@ }));

}
async performChildCompilation(compilation, parentCompiler) {
const childCompiler = compilation.createChildCompiler(this.constructor.name, {
filename: this.config.swDest
}, this.config.webpackCompilationPlugins);
new this.webpack.webworker.WebWorkerTemplatePlugin().apply(childCompiler);
new this.webpack.EntryPlugin(parentCompiler.context, this.config.swSrc, this.constructor.name).apply(childCompiler);
await new Promise((resolve, reject)=>{
childCompiler.runAsChild((error, _entries, childCompilation)=>{
if (error) {
reject(error);
} else {
if (childCompilation?.warnings) {
compilation.warnings.push(...childCompilation.warnings);
}
if (childCompilation?.errors) {
compilation.errors.push(...childCompilation.errors);
}
resolve();
}
});
});
}
addSrcToAssets(compilation, parentCompiler) {
const source = parentCompiler.inputFileSystem.readFileSync(this.config.swSrc);
addSrcToAssets(compiler, compilation) {
const source = compiler.inputFileSystem.readFileSync(this.config.swSrc);
compilation.emitAsset(this.config.swDest, new this.webpack.sources.RawSource(source));
}
async handleMake(compilation, parentCompiler) {
async handleMake(compiler, compilation) {
this.config = await validateInjectManifestOptions(this.config);

@@ -259,5 +241,5 @@ this.config.swDest = relativeToOutputPath(compilation, this.config.swDest);

if (this.config.compileSrc) {
await this.performChildCompilation(compilation, parentCompiler);
await performChildCompilation(compiler, compilation, this.constructor.name, this.config.swSrc, this.config.swDest, this.config.webpackCompilationPlugins);
} else {
this.addSrcToAssets(compilation, parentCompiler);
this.addSrcToAssets(compiler, compilation);
if (Array.isArray(this.config.webpackCompilationPlugins) && this.config.webpackCompilationPlugins.length > 0) {

@@ -271,4 +253,3 @@ compilation.warnings.push(new Error("'compileSrc' is 'false', so the 'webpackCompilationPlugins' option will be ignored."));

const { size, sortedEntries, manifestString } = await this.getManifestEntries(compilation, config);
const absoluteSwSrc = upath.resolve(config.swSrc);
compilation.fileDependencies.add(absoluteSwSrc);
compilation.fileDependencies.add(path.resolve(config.swSrc));
const swAsset = compilation.getAsset(config.swDest);

@@ -289,3 +270,3 @@ const swAssetString = swAsset.source.source().toString();

const { source, map } = await replaceAndUpdateSourceMap({
jsFilename: config.swDest,
jsFilename: toUnix(config.swDest),
originalMap: JSON.parse(sourcemapAsset.source.source().toString()),

@@ -292,0 +273,0 @@ originalSource: swAssetString,

@@ -52,18 +52,11 @@ import type { Compiler } from "webpack";

/**
* @param compiler The webpack parent compiler.
* @param compilation The webpack compilation.
* @param parentCompiler The webpack parent compiler.
*
* @private
*/
private performChildCompilation;
/**
* @param compilation The webpack compilation.
* @param parentCompiler The webpack parent compiler.
*
* @private
*/
private addSrcToAssets;
/**
* @param compiler The webpack parent compiler.
* @param compilation The webpack compilation.
* @param parentCompiler The webpack parent compiler.
*

@@ -70,0 +63,0 @@ * @private

@@ -1,2 +0,2 @@

import type { Compiler, WebpackPluginInstance, default as Webpack } from "webpack";
import type { Compiler, WebpackPluginInstance } from "webpack";
export interface ChildCompilationPluginOptions {

@@ -16,3 +16,2 @@ src: string;

plugins: WebpackPluginInstance[] | undefined;
webpack: typeof Webpack;
constructor({ src, dest, plugins }: ChildCompilationPluginOptions);

@@ -24,17 +23,4 @@ /**

*/
private propagateWebpackConfig;
/**
* @param compiler default compiler object passed from webpack
*
* @private
*/
apply(compiler: Compiler): void;
/**
* @param compilation The webpack compilation.
* @param parentCompiler The webpack parent compiler.
*
* @private
*/
private performChildCompilation;
}
//# sourceMappingURL=child-compilation-plugin.d.ts.map
import type { Compilation } from "webpack";
/**
* @param compilation The webpack compilation.
* @param path The original path value.
* @param originalPath The original path value.
*

@@ -11,3 +11,3 @@ * @returns If path was not absolute, the returns path as-is.

*/
export declare const relativeToOutputPath: (compilation: Compilation, path: string) => string;
export declare const relativeToOutputPath: (compilation: Compilation, originalPath: string) => string;
//# sourceMappingURL=relative-to-output-path.d.ts.map

@@ -13,4 +13,4 @@ import { z } from "zod";

}, {
exclude?: (string | RegExp | ((args_0: any) => boolean))[] | undefined;
chunks?: string[] | undefined;
exclude?: (string | RegExp | ((args_0: any) => boolean))[] | undefined;
excludeChunks?: string[] | undefined;

@@ -25,8 +25,8 @@ include?: (string | RegExp | ((args_0: any) => boolean))[] | undefined;

compileSrc: boolean;
webpackCompilationPlugins?: any[] | undefined;
swDest?: string | undefined;
webpackCompilationPlugins?: any[] | undefined;
}, {
compileSrc?: boolean | undefined;
webpackCompilationPlugins?: any[] | undefined;
swDest?: string | undefined;
webpackCompilationPlugins?: any[] | undefined;
}>;

@@ -162,3 +162,2 @@ export declare const injectManifestOptions: z.ZodObject<z.objectUtil.extendShape<z.objectUtil.extendShape<z.objectUtil.extendShape<z.objectUtil.extendShape<{

include?: (string | RegExp | ((args_0: any) => boolean))[] | undefined;
swDest?: string | undefined;
webpackCompilationPlugins?: any[] | undefined;

@@ -194,10 +193,10 @@ additionalPrecacheEntries?: (string | {

modifyURLPrefix?: Record<string, string> | undefined;
swDest?: string | undefined;
}, {
swSrc: string;
exclude?: (string | RegExp | ((args_0: any) => boolean))[] | undefined;
chunks?: string[] | undefined;
exclude?: (string | RegExp | ((args_0: any) => boolean))[] | undefined;
excludeChunks?: string[] | undefined;
include?: (string | RegExp | ((args_0: any) => boolean))[] | undefined;
compileSrc?: boolean | undefined;
swDest?: string | undefined;
webpackCompilationPlugins?: any[] | undefined;

@@ -236,3 +235,4 @@ additionalPrecacheEntries?: (string | {

injectionPoint?: string | undefined;
swDest?: string | undefined;
}>;
//# sourceMappingURL=schema.d.ts.map

@@ -48,3 +48,3 @@ import type { BasePartial, BaseResolved, InjectPartial as BaseInjectPartial, InjectResolved as BaseInjectResolved, OptionalSwDestPartial, OptionalSwDestResolved } from "@serwist/build";

*/
webpackCompilationPlugins?: (WebpackPluginFunction | WebpackPluginInstance)[];
webpackCompilationPlugins?: WebpackPlugin[];
}

@@ -56,2 +56,3 @@ export type InjectResolved = Require<InjectPartial, "compileSrc">;

}
export type WebpackPlugin = WebpackPluginFunction | WebpackPluginInstance;
//# sourceMappingURL=types.d.ts.map
{
"name": "@serwist/webpack-plugin",
"version": "9.0.6",
"version": "9.0.7",
"type": "module",

@@ -62,5 +62,4 @@ "description": "A plugin for your webpack build process, helping you generate a manifest of local files that should be precached.",

"pretty-bytes": "6.1.1",
"upath": "2.0.1",
"zod": "3.23.8",
"@serwist/build": "9.0.6"
"@serwist/build": "9.0.7"
},

@@ -73,4 +72,4 @@ "devDependencies": {

"webpack": "5.93.0",
"@serwist/utils": "9.0.6",
"@serwist/configs": "9.0.6"
"@serwist/configs": "9.0.7",
"@serwist/utils": "9.0.7"
},

@@ -77,0 +76,0 @@ "peerDependencies": {

@@ -0,11 +1,12 @@

import path from "node:path";
import { escapeRegExp, replaceAndUpdateSourceMap, stringify } from "@serwist/build";
import { toUnix } from "@serwist/utils";
import prettyBytes from "pretty-bytes";
import upath from "upath";
import type { Compilation, Compiler, WebpackError, default as Webpack } from "webpack";
import type { InjectManifestOptions, InjectManifestOptionsComplete } from "./lib/types.js";
import { validateInjectManifestOptions } from "./lib/validator.js";
import { getManifestEntriesFromCompilation } from "./lib/get-manifest-entries-from-compilation.js";
import { getSourcemapAssetName } from "./lib/get-sourcemap-asset-name.js";
import { relativeToOutputPath } from "./lib/relative-to-output-path.js";
import { performChildCompilation } from "./lib/perform-child-compilation.js";

@@ -47,3 +48,4 @@ // Used to keep track of swDest files written by *any* instance of this plugin.

constructor(config: InjectManifestOptions) {
// We are essentially lying to TypeScript.
// We are essentially lying to TypeScript. When `handleMake`
// is called, `this.config` will be replaced by a validated config.
this.config = config as InjectManifestOptionsComplete;

@@ -62,3 +64,3 @@ this.alreadyCalled = false;

const parsedSwSrc = upath.parse(this.config.swSrc);
const parsedSwSrc = path.parse(this.config.swSrc);
// Because this.config is listed last, properties that are already set

@@ -100,4 +102,3 @@ // there take precedence over derived properties from the compiler.

// instance of this plugin.
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
config.exclude!.push(({ asset }) => _generatedAssetNames.has(asset.name));
config.exclude.push(({ asset }) => _generatedAssetNames.has(asset.name));

@@ -128,3 +129,3 @@ const { size, sortedEntries } = await getManifestEntriesFromCompilation(compilation, config);

compiler.hooks.make.tapPromise(this.constructor.name, (compilation) =>
this.handleMake(compilation, compiler).catch((error: WebpackError) => {
this.handleMake(compiler, compilation).catch((error: WebpackError) => {
compilation.errors.push(error);

@@ -155,45 +156,9 @@ }),

/**
* @param compiler The webpack parent compiler.
* @param compilation The webpack compilation.
* @param parentCompiler The webpack parent compiler.
*
* @private
*/
private async performChildCompilation(compilation: Compilation, parentCompiler: Compiler): Promise<void> {
const childCompiler = compilation.createChildCompiler(
this.constructor.name,
{
filename: this.config.swDest,
},
this.config.webpackCompilationPlugins,
);
new this.webpack.webworker.WebWorkerTemplatePlugin().apply(childCompiler);
new this.webpack.EntryPlugin(parentCompiler.context, this.config.swSrc, this.constructor.name).apply(childCompiler);
await new Promise<void>((resolve, reject) => {
childCompiler.runAsChild((error, _entries, childCompilation) => {
if (error) {
reject(error);
} else {
if (childCompilation?.warnings) {
compilation.warnings.push(...childCompilation.warnings);
}
if (childCompilation?.errors) {
compilation.errors.push(...childCompilation.errors);
}
resolve();
}
});
});
}
/**
* @param compilation The webpack compilation.
* @param parentCompiler The webpack parent compiler.
*
* @private
*/
private addSrcToAssets(compilation: Compilation, parentCompiler: Compiler): void {
const source = parentCompiler.inputFileSystem!.readFileSync!(this.config.swSrc);
private addSrcToAssets(compiler: Compiler, compilation: Compilation): void {
const source = compiler.inputFileSystem!.readFileSync!(this.config.swSrc);
compilation.emitAsset(this.config.swDest!, new this.webpack.sources.RawSource(source));

@@ -203,8 +168,8 @@ }

/**
* @param compiler The webpack parent compiler.
* @param compilation The webpack compilation.
* @param parentCompiler The webpack parent compiler.
*
* @private
*/
private async handleMake(compilation: Compilation, parentCompiler: Compiler): Promise<void> {
private async handleMake(compiler: Compiler, compilation: Compilation): Promise<void> {
this.config = await validateInjectManifestOptions(this.config);

@@ -215,5 +180,12 @@ this.config.swDest = relativeToOutputPath(compilation, this.config.swDest!);

if (this.config.compileSrc) {
await this.performChildCompilation(compilation, parentCompiler);
await performChildCompilation(
compiler,
compilation,
this.constructor.name,
this.config.swSrc,
this.config.swDest,
this.config.webpackCompilationPlugins,
);
} else {
this.addSrcToAssets(compilation, parentCompiler);
this.addSrcToAssets(compiler, compilation);
// This used to be a fatal error, but just warn at runtime because we

@@ -238,6 +210,6 @@ // can't validate it easily.

// See https://webpack.js.org/contribute/plugin-patterns/#monitoring-the-watch-graph
const absoluteSwSrc = upath.resolve(config.swSrc);
compilation.fileDependencies.add(absoluteSwSrc);
compilation.fileDependencies.add(path.resolve(config.swSrc));
const swAsset = compilation.getAsset(config.swDest!);
const swAssetString = swAsset!.source.source().toString();

@@ -263,10 +235,8 @@

const { source, map } = await replaceAndUpdateSourceMap({
jsFilename: config.swDest!,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
jsFilename: toUnix(config.swDest!),
originalMap: JSON.parse(sourcemapAsset!.source.source().toString()),
originalSource: swAssetString,
replaceString: manifestString,
searchString: config.injectionPoint!,
searchString: config.injectionPoint,
});
compilation.updateAsset(sourcemapAssetName, new this.webpack.sources.RawSource(map));

@@ -277,3 +247,3 @@ compilation.updateAsset(config.swDest!, new this.webpack.sources.RawSource(source));

// replacement will suffice.
compilation.updateAsset(config.swDest!, new this.webpack.sources.RawSource(swAssetString.replace(config.injectionPoint!, manifestString)));
compilation.updateAsset(config.swDest!, new this.webpack.sources.RawSource(swAssetString.replace(config.injectionPoint, manifestString)));
}

@@ -280,0 +250,0 @@

@@ -1,4 +0,5 @@

import type { Compilation, Compiler, WebpackError, WebpackPluginInstance, default as Webpack } from "webpack";
import type { Compiler, WebpackError, WebpackPluginInstance } from "webpack";
import { relativeToOutputPath } from "./relative-to-output-path.js";
import { performChildCompilation } from "./perform-child-compilation.js";

@@ -20,3 +21,2 @@ export interface ChildCompilationPluginOptions {

plugins: WebpackPluginInstance[] | undefined;
webpack: typeof Webpack;
constructor({ src, dest, plugins }: ChildCompilationPluginOptions) {

@@ -26,3 +26,2 @@ this.src = src;

this.plugins = plugins;
this.webpack = null!;
}

@@ -34,15 +33,12 @@ /**

*/
private propagateWebpackConfig(compiler: Compiler): void {
this.webpack = compiler.webpack;
}
/**
* @param compiler default compiler object passed from webpack
*
* @private
*/
apply(compiler: Compiler) {
this.propagateWebpackConfig(compiler);
compiler.hooks.make.tapPromise(this.constructor.name, (compilation) =>
this.performChildCompilation(compilation, compiler).catch((error: WebpackError) => {
performChildCompilation(
compiler,
compilation,
this.constructor.name,
this.src,
relativeToOutputPath(compilation, this.dest),
this.plugins,
).catch((error: WebpackError) => {
compilation.errors.push(error);

@@ -52,41 +48,2 @@ }),

}
/**
* @param compilation The webpack compilation.
* @param parentCompiler The webpack parent compiler.
*
* @private
*/
private async performChildCompilation(compilation: Compilation, parentCompiler: Compiler): Promise<void> {
const resolvedDest = relativeToOutputPath(compilation, this.dest);
const outputOptions: Parameters<Compilation["createChildCompiler"]>["1"] = {
filename: resolvedDest,
};
const childCompiler = compilation.createChildCompiler(this.constructor.name, outputOptions, []);
childCompiler.options.target = "webworker";
childCompiler.context = parentCompiler.context;
childCompiler.inputFileSystem = parentCompiler.inputFileSystem;
childCompiler.outputFileSystem = parentCompiler.outputFileSystem;
if (this.plugins !== undefined) {
for (const plugin of this.plugins) {
plugin?.apply(childCompiler);
}
}
new this.webpack.EntryPlugin(parentCompiler.context, this.src, this.constructor.name).apply(childCompiler);
await new Promise<void>((resolve, reject) => {
childCompiler.runAsChild((error, _entries, childCompilation) => {
if (error) {
reject(error);
} else {
compilation.warnings = compilation.warnings.concat(childCompilation?.warnings ?? []);
compilation.errors = compilation.errors.concat(childCompilation?.errors ?? []);
resolve();
}
});
});
}
}

@@ -8,4 +8,3 @@ /*

*/
import upath from "upath";
import path from "node:path";
import type { Compilation, WebpackError } from "webpack";

@@ -25,3 +24,3 @@

// See https://github.com/GoogleChrome/workbox/issues/2161
if (upath.extname(file) === ".js") {
if (path.extname(file) === ".js") {
scriptFiles.add(resolveWebpackURL(publicPath as string, file));

@@ -28,0 +27,0 @@ }

@@ -8,5 +8,4 @@ /*

*/
import path from "node:path";
import { getSourceMapURL } from "@serwist/build";
import upath from "upath";
import type { Compilation } from "webpack";

@@ -39,5 +38,4 @@

// See https://github.com/GoogleChrome/workbox/issues/2250
const swAssetDirname = upath.dirname(swDest);
const sourcemapURLAssetName = upath.normalize(upath.join(swAssetDirname, url));
const swAssetDirname = path.dirname(swDest);
const sourcemapURLAssetName = path.normalize(path.join(swAssetDirname, url));
// Not sure if there's a better way to check for asset existence?

@@ -44,0 +42,0 @@ if (compilation.getAsset(sourcemapURLAssetName)) {

@@ -8,4 +8,3 @@ /*

*/
import upath from "upath";
import path from "node:path";
import type { Compilation } from "webpack";

@@ -15,3 +14,3 @@

* @param compilation The webpack compilation.
* @param path The original path value.
* @param originalPath The original path value.
*

@@ -23,10 +22,10 @@ * @returns If path was not absolute, the returns path as-is.

*/
export const relativeToOutputPath = (compilation: Compilation, path: string): string => {
export const relativeToOutputPath = (compilation: Compilation, originalPath: string): string => {
// See https://github.com/jantimon/html-webpack-plugin/pull/266/files#diff-168726dbe96b3ce427e7fedce31bb0bcR38
if (upath.resolve(path) === upath.normalize(path)) {
return upath.relative(compilation.options.output.path!, path);
if (path.resolve(originalPath) === path.normalize(originalPath)) {
return path.relative(compilation.options.output.path!, originalPath);
}
// Otherwise, return swDest as-is.
return path;
return originalPath;
};

@@ -61,3 +61,3 @@ import type {

*/
webpackCompilationPlugins?: (WebpackPluginFunction | WebpackPluginInstance)[];
webpackCompilationPlugins?: WebpackPlugin[];
}

@@ -70,1 +70,3 @@

export interface InjectManifestOptionsComplete extends BaseResolved, WebpackResolved, BaseInjectResolved, OptionalSwDestResolved, InjectResolved {}
export type WebpackPlugin = WebpackPluginFunction | WebpackPluginInstance;

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc