Skip to content

Commit

Permalink
refactor: use new processAssets api to handle SRI (#2971)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Jul 21, 2024
1 parent eebe00d commit ed6d0f8
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 59 deletions.
Empty file removed examples/react/index.html
Empty file.
3 changes: 0 additions & 3 deletions examples/react/rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,4 @@ import { pluginReact } from '@rsbuild/plugin-react';

export default defineConfig({
plugins: [pluginReact()],
html: {
template: './index.html',
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly 1`] = `
"process.env.NODE_ENV": ""development"",
},
},
RsbuildProcessAssetsPlugin {},
ProgressPlugin {
"compileTime": null,
"dependenciesCount": 10000,
Expand Down Expand Up @@ -730,6 +731,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when produ
"process.env.NODE_ENV": ""production"",
},
},
RsbuildProcessAssetsPlugin {},
ProgressPlugin {
"compileTime": null,
"dependenciesCount": 10000,
Expand Down Expand Up @@ -1034,6 +1036,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
"process.env.NODE_ENV": ""production"",
},
},
RsbuildProcessAssetsPlugin {},
ProgressPlugin {
"compileTime": null,
"dependenciesCount": 10000,
Expand Down Expand Up @@ -1320,6 +1323,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
"process.env.NODE_ENV": ""production"",
},
},
RsbuildProcessAssetsPlugin {},
ProgressPlugin {
"compileTime": null,
"dependenciesCount": 10000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ exports[`webpackConfig > should allow to append and prepend plugins 1`] = `
"process.env.NODE_ENV": ""test"",
},
},
RsbuildProcessAssetsPlugin {},
ProgressPlugin {
"compileTime": null,
"dependenciesCount": 10000,
Expand Down
85 changes: 29 additions & 56 deletions packages/core/src/plugins/sri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,67 +145,40 @@ export const pluginSri = (): RsbuildPlugin => ({
return replacedHtml;
};

class SriReplaceIntegrityPlugin {
algorithm: SriAlgorithm;
api.processAssets(
{
// use to final stage to get the final asset content
stage: 'report',
},
({ assets, compiler, environment }) => {
const { htmlPaths } = environment;

constructor(algorithm: SriAlgorithm) {
this.algorithm = algorithm;
}
if (Object.keys(htmlPaths).length === 0) {
return;
}

apply(compiler: Rspack.Compiler): void {
compiler.hooks.compilation.tap(
'SriReplaceIntegrityPlugin',
(compilation) => {
compilation.hooks.processAssets.tapPromise(
{
name: 'SriReplaceIntegrityPlugin',
// use to final stage to get the final asset content
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_REPORT,
},
async (assets) => {
const integrityCache = new Map<string, string>();

for (const asset of Object.keys(assets)) {
if (!HTML_REGEX.test(asset)) {
continue;
}

const htmlContent = assets[asset].source() as string;
if (!htmlContent.includes(placeholder)) {
continue;
}

assets[asset] = new compiler.webpack.sources.RawSource(
replaceIntegrity(
htmlContent,
assets,
this.algorithm,
integrityCache,
),
);
}
},
);
},
);
}
}
const algorithm = getAlgorithm(environment);
if (!algorithm) {
return;
}

api.modifyBundlerChain((chain, { environment }) => {
const { htmlPaths } = environment;
const integrityCache = new Map<string, string>();

if (Object.keys(htmlPaths).length === 0) {
return;
}
for (const asset of Object.keys(assets)) {
if (!HTML_REGEX.test(asset)) {
continue;
}

const algorithm = getAlgorithm(environment);
if (!algorithm) {
return;
}
const htmlContent = assets[asset].source() as string;
if (!htmlContent.includes(placeholder)) {
continue;
}

chain
.plugin('rsbuild-sri-replace')
.use(SriReplaceIntegrityPlugin, [algorithm]);
});
assets[asset] = new compiler.webpack.sources.RawSource(
replaceIntegrity(htmlContent, assets, algorithm, integrityCache),
);
}
},
);
},
});
1 change: 1 addition & 0 deletions packages/core/tests/__snapshots__/builder.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ exports[`should use rspack as default bundler > apply rspack correctly 1`] = `
"affectedHooks": "compilation",
"name": "DefinePlugin",
},
RsbuildProcessAssetsPlugin {},
],
"resolve": {
"alias": {
Expand Down
4 changes: 4 additions & 0 deletions packages/core/tests/__snapshots__/default.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly 1`] = `
"affectedHooks": "compilation",
"name": "DefinePlugin",
},
RsbuildProcessAssetsPlugin {},
],
"resolve": {
"alias": {
Expand Down Expand Up @@ -834,6 +835,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when prod
"affectedHooks": undefined,
"name": "ProgressPlugin",
},
RsbuildProcessAssetsPlugin {},
],
"resolve": {
"alias": {
Expand Down Expand Up @@ -1160,6 +1162,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
"affectedHooks": "compilation",
"name": "DefinePlugin",
},
RsbuildProcessAssetsPlugin {},
],
"resolve": {
"alias": {
Expand Down Expand Up @@ -1584,6 +1587,7 @@ exports[`tools.rspack > should match snapshot 1`] = `
"affectedHooks": "compilation",
"name": "DefinePlugin",
},
RsbuildProcessAssetsPlugin {},
],
"resolve": {
"alias": {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/tests/__snapshots__/environments.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1680,6 +1680,7 @@ exports[`environment config > tools.rspack / bundlerChain can be used in environ
"affectedHooks": "compilation",
"name": "DefinePlugin",
},
RsbuildProcessAssetsPlugin {},
],
"resolve": {
"alias": {
Expand Down Expand Up @@ -1996,6 +1997,7 @@ exports[`environment config > tools.rspack / bundlerChain can be used in environ
"affectedHooks": "compilation",
"name": "DefinePlugin",
},
RsbuildProcessAssetsPlugin {},
],
"resolve": {
"alias": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ exports[`plugins/react > should work with swc-loader 1`] = `
"affectedHooks": "compilation",
"name": "DefinePlugin",
},
RsbuildProcessAssetsPlugin {},
ReactRefreshRspackPlugin {
"options": {
"exclude": /node_modules/i,
Expand Down

0 comments on commit ed6d0f8

Please sign in to comment.