Skip to content

Commit

Permalink
feat(sri): add native plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
LingyuCoder committed Feb 13, 2025
1 parent 628e89f commit 400d411
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 34 deletions.
34 changes: 17 additions & 17 deletions packages/rspack/etc/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2050,7 +2050,7 @@ interface Experiments_2 {
// (undocumented)
RsdoctorPlugin: typeof RsdoctorPlugin;
// (undocumented)
SubresourceIntegrityPlugin: typeof SRIPlugin;
SubresourceIntegrityPlugin: typeof SubresourceIntegrityPlugin;
}

// @public (undocumented)
Expand Down Expand Up @@ -4020,19 +4020,19 @@ class MultiWatching {
// @public
export type Name = string;

// @public (undocumented)
const NativeSRIPlugin: {
new (options: NativeSRIPluginOptions): {
// @internal
const NativeSubresourceIntegrityPlugin: {
new (options: NativeSubresourceIntegrityPluginOptions): {
name: BuiltinPluginName;
_args: [options: NativeSRIPluginOptions];
_args: [options: NativeSubresourceIntegrityPluginOptions];
affectedHooks: "done" | "environment" | "make" | "compile" | "emit" | "afterEmit" | "invalid" | "thisCompilation" | "afterDone" | "compilation" | "normalModuleFactory" | "contextModuleFactory" | "initialize" | "shouldEmit" | "infrastructureLog" | "beforeRun" | "run" | "assetEmitted" | "failed" | "shutdown" | "watchRun" | "watchClose" | "afterEnvironment" | "afterPlugins" | "afterResolvers" | "beforeCompile" | "afterCompile" | "finishMake" | "entryOption" | "additionalPass" | undefined;
raw(compiler: Compiler): BuiltinPlugin;
apply(compiler: Compiler): void;
};
};

// @public (undocumented)
type NativeSRIPluginOptions = Omit<RawSRIPluginOptions, "htmlPlugin"> & {
type NativeSubresourceIntegrityPluginOptions = Omit<RawSRIPluginOptions, "htmlPlugin"> & {
htmlPlugin: string | false;
};

Expand Down Expand Up @@ -10311,16 +10311,6 @@ class SplitChunksPlugin extends RspackBuiltinPlugin {
raw(compiler: Compiler): BuiltinPlugin;
}

// @public (undocumented)
type SRIHashFunction = "sha256" | "sha384" | "sha512";

// @public (undocumented)
class SRIPlugin extends NativeSRIPlugin {
constructor(options: SubresourceIntegrityPluginOptions);
// (undocumented)
apply(compiler: Compiler): void;
}

// @public
type StandardSchemaV1<Input = unknown, Output = Input> = {
readonly "~standard": StandardSchemaV1.Props<Input, Output>;
Expand Down Expand Up @@ -10637,9 +10627,19 @@ type StringValidation = "email" | "url" | "emoji" | "uuid" | "nanoid" | "regex"
// @public (undocumented)
type stripPath<T extends object> = T extends any ? util_2.OmitKeys<T, "path"> : never;

// @public (undocumented)
type SubresourceIntegrityHashFunction = "sha256" | "sha384" | "sha512";

// @public (undocumented)
class SubresourceIntegrityPlugin extends NativeSubresourceIntegrityPlugin {
constructor(options: SubresourceIntegrityPluginOptions);
// (undocumented)
apply(compiler: Compiler): void;
}

// @public (undocumented)
export type SubresourceIntegrityPluginOptions = {
hashFuncNames?: [SRIHashFunction, ...SRIHashFunction[]];
hashFuncNames?: [SubresourceIntegrityHashFunction, ...SubresourceIntegrityHashFunction[]];
htmlPlugin?: string | false;
enabled?: "auto" | boolean;
};
Expand Down
1 change: 1 addition & 0 deletions packages/rspack/scripts/check-documentation-coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ function checkPluginsDocumentationCoverage() {
"OriginEntryPlugin",
"RuntimePlugin", // This plugin only provides hooks, should not be used separately
"RsdoctorPlugin", // This plugin is not stable yet
"SubresourceIntegrityPlugin" // TODO: add document in next pr
];

const undocumentedPlugins = Array.from(implementedPlugins).filter(
Expand Down
42 changes: 27 additions & 15 deletions packages/rspack/src/builtin-plugin/SRIPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type { CrossOriginLoading } from "../config/types";
import { validate } from "../util/validate";
import { create } from "./base";

const PLUGIN_NAME = "SRIPlugin";
const PLUGIN_NAME = "SubresourceIntegrityPlugin";
const NATIVE_HTML_PLUGIN = "HtmlRspackPlugin";

type HtmlTagObject = {
Expand Down Expand Up @@ -57,9 +57,12 @@ type HtmlPluginHooks = {
alterAssetTagGroups: AsyncSeriesWaterfallHook<AlterAssetTagGroupsData>;
};

export type SRIHashFunction = "sha256" | "sha384" | "sha512";
export type SRIPluginOptions = {
hashFuncNames?: [SRIHashFunction, ...SRIHashFunction[]];
export type SubresourceIntegrityHashFunction = "sha256" | "sha384" | "sha512";
export type SubresourceIntegrityPluginOptions = {
hashFuncNames?: [
SubresourceIntegrityHashFunction,
...SubresourceIntegrityHashFunction[]
];
htmlPlugin?: string | false;
enabled?: "auto" | boolean;
};
Expand All @@ -72,17 +75,24 @@ const sriPluginOptionsSchema = z.object({
.optional(),
htmlPlugin: z.string().or(z.literal(false)).optional(),
enabled: z.literal("auto").or(z.boolean()).optional()
}) satisfies z.ZodType<SRIPluginOptions>;
}) satisfies z.ZodType<SubresourceIntegrityPluginOptions>;

export type NativeSRIPluginOptions = Omit<RawSRIPluginOptions, "htmlPlugin"> & {
export type NativeSubresourceIntegrityPluginOptions = Omit<
RawSRIPluginOptions,
"htmlPlugin"
> & {
htmlPlugin: string | false;
};

const NativeSRIPlugin = create(
/**
* Note: This is not a webpack public API, maybe removed in future.
* @internal
*/
const NativeSubresourceIntegrityPlugin = create(
BuiltinPluginName.SRIPlugin,
function (
this: Compiler,
options: NativeSRIPluginOptions
options: NativeSubresourceIntegrityPluginOptions
): RawSRIPluginOptions {
let htmlPlugin: RawSRIPluginOptions["htmlPlugin"] = "Disabled";
if (options.htmlPlugin === NATIVE_HTML_PLUGIN) {
Expand All @@ -98,17 +108,17 @@ const NativeSRIPlugin = create(
}
);

export class SRIPlugin extends NativeSRIPlugin {
export class SubresourceIntegrityPlugin extends NativeSubresourceIntegrityPlugin {
private integrities: Map<string, string> = new Map();
private options: SRIPluginOptions;
private options: SubresourceIntegrityPluginOptions;
private validateError: Error | null = null;
constructor(options: SRIPluginOptions) {
constructor(options: SubresourceIntegrityPluginOptions) {
let validateError: Error | null = null;
if (typeof options !== "object") {
throw new Error("SubResourceIntegrity: argument must be an object");
}
try {
validateSRIPluginOptions(options);
validateSubresourceIntegrityPluginOptions(options);
} catch (e) {
validateError = e as Error;
}
Expand All @@ -133,7 +143,7 @@ export class SRIPlugin extends NativeSRIPlugin {
}
});
this.validateError = validateError;
this.options = finalOptions as SRIPluginOptions;
this.options = finalOptions as SubresourceIntegrityPluginOptions;
}

private isEnabled(compiler: Compiler) {
Expand Down Expand Up @@ -308,7 +318,9 @@ export class SRIPlugin extends NativeSRIPlugin {
}
}

function validateSRIPluginOptions(options: SRIPluginOptions) {
function validateSubresourceIntegrityPluginOptions(
options: SubresourceIntegrityPluginOptions
) {
validate(options, sriPluginOptionsSchema);
}

Expand All @@ -334,7 +346,7 @@ function getTagSrc(tag: HtmlTagObject): string | undefined {
}

function computeIntegrity(
hashFuncNames: SRIHashFunction[],
hashFuncNames: SubresourceIntegrityHashFunction[],
source: string | Buffer
): string {
const result = hashFuncNames
Expand Down
4 changes: 2 additions & 2 deletions packages/rspack/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export { NormalModuleReplacementPlugin } from "./lib/NormalModuleReplacementPlug

import {
FetchCompileAsyncWasmPlugin,
SRIPlugin as SubresourceIntegrityPlugin
SubresourceIntegrityPlugin
} from "./builtin-plugin";
interface Web {
FetchCompileAsyncWasmPlugin: typeof FetchCompileAsyncWasmPlugin;
Expand Down Expand Up @@ -295,7 +295,7 @@ export type {
FeatureOptions as LightningcssFeatureOptions
} from "./builtin-loader/lightningcss/index";

export type { SRIPluginOptions as SubresourceIntegrityPluginOptions } from "./builtin-plugin";
export type { SubresourceIntegrityPluginOptions } from "./builtin-plugin";

///// Experiments Stuff /////
import { cleanupGlobalTrace, registerGlobalTrace } from "@rspack/binding";
Expand Down

0 comments on commit 400d411

Please sign in to comment.