Skip to content

Commit

Permalink
feat: add sources param to api.processAssets
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Jul 22, 2024
1 parent ae55a5d commit 194677a
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 10 deletions.
3 changes: 3 additions & 0 deletions packages/core/src/initPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ export function getPluginAPI({
});

compiler.hooks.compilation.tap(pluginName, (compilation) => {
const { sources } = compiler.webpack;

for (const { descriptor, handler } of processAssetsFns) {
// filter by targets
if (descriptor.targets && !descriptor.targets.includes(target)) {
Expand All @@ -173,6 +175,7 @@ export function getPluginAPI({
compiler,
compilation,
environment,
sources,
}),
);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/plugins/appIcon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const pluginAppIcon = (): RsbuildPlugin => ({

api.processAssets(
{ stage: 'additional' },
async ({ compiler, compilation, environment }) => {
async ({ compilation, environment, sources }) => {
const iconPath = getIconPath(environment);
if (!iconPath) {
return;
Expand All @@ -60,7 +60,7 @@ export const pluginAppIcon = (): RsbuildPlugin => ({

compilation.emitAsset(
iconPath.relativePath,
new compiler.webpack.sources.RawSource(source, false),
new sources.RawSource(source),
);
},
);
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/plugins/sri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export const pluginSri = (): RsbuildPlugin => ({
// use to final stage to get the final asset content
stage: 'report',
},
({ assets, compiler, environment }) => {
({ assets, sources, environment }) => {
const { htmlPaths } = environment;

if (Object.keys(htmlPaths).length === 0) {
Expand All @@ -174,7 +174,7 @@ export const pluginSri = (): RsbuildPlugin => ({
continue;
}

assets[asset] = new compiler.webpack.sources.RawSource(
assets[asset] = new sources.RawSource(
replaceIntegrity(htmlContent, assets, algorithm, integrityCache),
);
}
Expand Down
18 changes: 18 additions & 0 deletions packages/core/src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,20 @@ export type ProcessAssetsDescriptor = {
targets?: RsbuildTarget[];
};

export type RspackSources = Pick<
typeof Rspack.sources,
| 'Source'
| 'RawSource'
| 'OriginalSource'
| 'SourceMapSource'
| 'CachedSource'
| 'ConcatSource'
| 'ReplaceSource'
| 'PrefixSource'
| 'SizeOnlySource'
| 'CompatSource'
>;

export type ProcessAssetsHandler = (context: {
assets: Record<string, Rspack.sources.Source>;
compiler: Rspack.Compiler;
Expand All @@ -281,6 +295,10 @@ export type ProcessAssetsHandler = (context: {
* The environment context for current build.
*/
environment: EnvironmentContext;
/**
* Contains multiple classes which represent an Rspack `Source`.
*/
sources: RspackSources;
}) => Promise<void> | void;

export type ProcessAssetsFn = (
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-assets-retry/src/AssetsRetryPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class AssetsRetryPlugin implements Rspack.RspackPluginInstance {
const code = await this.getRetryCode();
compilation.emitAsset(
scriptPath,
new compiler.webpack.sources.RawSource(code, false),
new compiler.webpack.sources.RawSource(code),
);
},
);
Expand Down
7 changes: 2 additions & 5 deletions packages/plugin-rem/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const pluginRem = (

api.processAssets(
{ stage: 'additional' },
async ({ compiler, compilation, environment }) => {
async ({ compilation, environment, sources }) => {
const { config } = environment;

if (
Expand All @@ -87,10 +87,7 @@ export const pluginRem = (

const code = await getRuntimeCode();
const scriptPath = getScriptPath(config.output.distPath.js);
compilation.emitAsset(
scriptPath,
new compiler.webpack.sources.RawSource(code, false),
);
compilation.emitAsset(scriptPath, new sources.RawSource(code));
},
);

Expand Down

0 comments on commit 194677a

Please sign in to comment.