Skip to content

Commit

Permalink
types: export HtmlRspackPlugin type (#2861)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Jul 10, 2024
1 parent c53814f commit 5e40840
Show file tree
Hide file tree
Showing 15 changed files with 37 additions and 40 deletions.
2 changes: 1 addition & 1 deletion packages/compat/webpack/tests/webpackConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ describe('webpackConfig', () => {
expect(config).toMatchSnapshot();
});

it('should export HtmlWebpackPlugin instance', async () => {
it('should expose HtmlWebpackPlugin instance via params', async () => {
await createStubRsbuild({
rsbuildConfig: {
tools: {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/configChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ export const CHAIN_ID = {
PLUGIN: {
/** HotModuleReplacementPlugin */
HMR: 'hmr',
/** CopyWebpackPlugin */
/** CopyRspackPlugin */
COPY: 'copy',
/** HtmlWebpackPlugin */
/** HtmlRspackPlugin */
HTML: 'html',
/** ESLintWebpackPlugin */
ESLINT: 'eslint',
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export type {
EnvironmentContext,
FilenameConfig,
HtmlConfig,
HtmlRspackPlugin,
HtmlTagHandler,
HtmlTagDescriptor,
InspectConfigOptions,
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/pluginHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
* This file is used to get/set the global instance for html-plugin and css-extract plugin.
*/
import rspack from '@rspack/core';
import type HtmlWebpackPlugin from 'html-rspack-plugin';
import type { HtmlRspackPlugin } from './types';

let htmlPlugin: typeof HtmlWebpackPlugin;
let htmlPlugin: typeof HtmlRspackPlugin;

/**
* This method is used to override the Rsbuild default html-plugin (html-rspack-plugin).
*/
export const setHTMLPlugin = (plugin: typeof HtmlWebpackPlugin): void => {
export const setHTMLPlugin = (plugin: typeof HtmlRspackPlugin): void => {
if (plugin) {
htmlPlugin = plugin;
}
};

export const getHTMLPlugin = (): typeof HtmlWebpackPlugin => {
export const getHTMLPlugin = (): typeof HtmlRspackPlugin => {
if (!htmlPlugin) {
htmlPlugin = require('html-rspack-plugin');
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/rspack/HtmlBasicPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Compilation, Compiler } from '@rspack/core';
import type HtmlWebpackPlugin from 'html-rspack-plugin';
import type { HtmlTagObject } from 'html-rspack-plugin';
import { ensureAssetPrefix, isFunction, partition } from '../helpers';
import { getHTMLPlugin } from '../pluginHelper';
import type { HtmlRspackPlugin } from '../types';
import type {
EnvironmentContext,
HtmlBasicTag,
Expand Down Expand Up @@ -68,7 +68,7 @@ export type AlterAssetTagGroupsData = {
bodyTags: HtmlTagObject[];
outputName: string;
publicPath: string;
plugin: HtmlWebpackPlugin;
plugin: HtmlRspackPlugin;
};

export const hasTitle = (html?: string): boolean =>
Expand Down
17 changes: 8 additions & 9 deletions packages/core/src/rspack/preload/HtmlPreloadOrPrefetchPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ import type {
Compiler,
RspackPluginInstance,
} from '@rspack/core';
import type HtmlWebpackPlugin from 'html-rspack-plugin';
import { ensureAssetPrefix, upperFirst } from '../../helpers';
import { getHTMLPlugin } from '../../pluginHelper';
import type { PreloadOrPreFetchOption } from '../../types';
import type { HtmlRspackPlugin, PreloadOrPreFetchOption } from '../../types';
import {
type As,
type BeforeAssetTagGenerationHtmlPluginData,
Expand All @@ -49,9 +48,9 @@ interface Attributes {
}

function filterResourceHints(
resourceHints: HtmlWebpackPlugin.HtmlTagObject[],
scripts: HtmlWebpackPlugin.HtmlTagObject[],
): HtmlWebpackPlugin.HtmlTagObject[] {
resourceHints: HtmlRspackPlugin.HtmlTagObject[],
scripts: HtmlRspackPlugin.HtmlTagObject[],
): HtmlRspackPlugin.HtmlTagObject[] {
return resourceHints.filter(
(resourceHint) =>
!scripts.find(
Expand All @@ -66,7 +65,7 @@ function generateLinks(
compilation: Compilation,
htmlPluginData: BeforeAssetTagGenerationHtmlPluginData,
HTMLCount: number,
): HtmlWebpackPlugin.HtmlTagObject[] {
): HtmlRspackPlugin.HtmlTagObject[] {
// get all chunks
const extractedChunks = extractChunks({
compilation,
Expand All @@ -77,7 +76,7 @@ function generateLinks(
// Handle all chunks.
options.type === 'all-assets' || HTMLCount === 1
? extractedChunks
: // Only handle chunks imported by this HtmlWebpackPlugin.
: // Only handle chunks imported by this HtmlRspackPlugin.
extractedChunks.filter((chunk) =>
doesChunkBelongToHtml({
chunk: chunk as Chunk,
Expand Down Expand Up @@ -118,7 +117,7 @@ function generateLinks(

// Sort to ensure the output is predictable.
const sortedFilteredFiles = filteredFiles.sort();
const links: HtmlWebpackPlugin.HtmlTagObject[] = [];
const links: HtmlRspackPlugin.HtmlTagObject[] = [];
const { publicPath, crossOriginLoading } = compilation.outputOptions;

for (const file of sortedFilteredFiles) {
Expand Down Expand Up @@ -168,7 +167,7 @@ function generateLinks(
export class HtmlPreloadOrPrefetchPlugin implements RspackPluginInstance {
readonly options: PreloadOrPreFetchOption;

resourceHints: HtmlWebpackPlugin.HtmlTagObject[] = [];
resourceHints: HtmlRspackPlugin.HtmlTagObject[] = [];

type: LinkType;

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/rspack/preload/helpers/type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type HtmlWebpackPlugin from 'html-rspack-plugin';
import type { HtmlRspackPlugin } from '../../../types';

export type BeforeAssetTagGenerationHtmlPluginData = {
assets: {
Expand All @@ -9,7 +9,7 @@ export type BeforeAssetTagGenerationHtmlPluginData = {
manifest?: string;
};
outputName: string;
plugin: HtmlWebpackPlugin;
plugin: HtmlRspackPlugin;
};

export type As =
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/types/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type HtmlWebpackPlugin from 'html-rspack-plugin';
import type RspackChain from 'rspack-chain';
import type { ChainIdentifier } from '..';
import type {
Expand All @@ -11,7 +10,7 @@ import type {
import type { RsbuildEntry, RsbuildTarget } from './rsbuild';
import type { Rspack } from './rspack';
import type { MultiStats, Stats } from './stats';
import type { WebpackConfig } from './thirdParty';
import type { HtmlRspackPlugin, WebpackConfig } from './thirdParty';
import type { MaybePromise, NodeEnv } from './utils';

export type OnBeforeBuildFn<B = 'rspack'> = (params: {
Expand Down Expand Up @@ -145,7 +144,7 @@ export type ModifyChainUtils = {
isWebWorker: boolean;
CHAIN_ID: ChainIdentifier;
environment: EnvironmentContext;
HtmlPlugin: typeof HtmlWebpackPlugin;
HtmlPlugin: typeof HtmlRspackPlugin;
};

interface PluginInstance {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/types/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { RuleSetCondition } from '@rspack/core';
import type HtmlWebpackPlugin from 'html-rspack-plugin';
import type RspackChain from 'rspack-chain';
import type {
RuleSetRule,
Expand Down Expand Up @@ -34,6 +33,7 @@ import type {
} from './hooks';
import type { RsbuildTarget } from './rsbuild';
import type { RspackConfig, RspackSourceMap } from './rspack';
import type { HtmlRspackPlugin } from './thirdParty';
import type { Falsy } from './utils';
import type { MaybePromise } from './utils';

Expand Down Expand Up @@ -64,7 +64,7 @@ export type ModifyWebpackChainUtils = ModifyChainUtils & {
/**
* @deprecated Use HtmlPlugin instead.
*/
HtmlWebpackPlugin: typeof HtmlWebpackPlugin;
HtmlWebpackPlugin: typeof HtmlRspackPlugin;
};

export type ModifyWebpackConfigUtils = ModifyWebpackChainUtils & {
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/types/thirdParty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import type {
CssExtractRspackPluginOptions,
} from '@rspack/core';
import type Autoprefixer from 'autoprefixer';
import type HtmlRspackPlugin from 'html-rspack-plugin';
import type { AcceptedPlugin, ProcessOptions } from 'postcss';
import type { Configuration as WebpackConfig } from 'webpack';
import type { Rspack } from './rspack';

export type { HtmlRspackPlugin };

type AutoprefixerOptions = Autoprefixer.Options;

export interface CSSExtractOptions {
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-assets-retry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"@babel/preset-typescript": "^7.24.7",
"@rsbuild/core": "workspace:*",
"@types/serialize-javascript": "^5.0.4",
"html-rspack-plugin": "6.0.0-beta.5",
"terser": "5.31.1",
"typescript": "^5.5.2"
},
Expand Down
11 changes: 7 additions & 4 deletions packages/plugin-assets-retry/src/AssetsRetryPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import fs from 'node:fs';
import path from 'node:path';
import { type Rspack, ensureAssetPrefix } from '@rsbuild/core';
import type HtmlWebpackPlugin from 'html-rspack-plugin';
import {
type HtmlRspackPlugin,
type Rspack,
ensureAssetPrefix,
} from '@rsbuild/core';
import type { PluginAssetsRetryOptions } from './types';

export class AssetsRetryPlugin implements Rspack.RspackPluginInstance {
Expand All @@ -13,7 +16,7 @@ export class AssetsRetryPlugin implements Rspack.RspackPluginInstance {

readonly minify?: boolean;

readonly HtmlPlugin: typeof HtmlWebpackPlugin;
readonly HtmlPlugin: typeof HtmlRspackPlugin;

scriptPath: string;

Expand All @@ -22,7 +25,7 @@ export class AssetsRetryPlugin implements Rspack.RspackPluginInstance {
constructor(
options: PluginAssetsRetryOptions & {
distDir: string;
HtmlPlugin: typeof HtmlWebpackPlugin;
HtmlPlugin: typeof HtmlRspackPlugin;
},
) {
const {
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-rem/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"@rsbuild/plugin-less": "workspace:*",
"@rsbuild/plugin-sass": "workspace:*",
"@scripts/test-helper": "workspace:*",
"html-rspack-plugin": "6.0.0-beta.5",
"postcss-pxtorem": "6.1.0",
"prebundle": "1.1.0",
"typescript": "^5.5.2"
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-rem/src/AutoSetRootFontSizePlugin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import path from 'node:path';
import {
type HtmlRspackPlugin,
type Rspack,
type ScriptLoading,
ensureAssetPrefix,
logger,
} from '@rsbuild/core';
import type HtmlWebpackPlugin from 'html-rspack-plugin';
import type { PluginRemOptions } from './types';

type AutoSetRootFontSizeOptions = Omit<
Expand Down Expand Up @@ -68,14 +68,14 @@ export class AutoSetRootFontSizePlugin implements Rspack.RspackPluginInstance {

scriptPath: string;

HtmlPlugin: typeof HtmlWebpackPlugin;
HtmlPlugin: typeof HtmlRspackPlugin;

scriptLoading: ScriptLoading;

constructor(
options: PluginRemOptions,
entries: Array<string>,
HtmlPlugin: typeof HtmlWebpackPlugin,
HtmlPlugin: typeof HtmlRspackPlugin,
distDir: string,
scriptLoading: ScriptLoading,
) {
Expand Down
6 changes: 0 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5e40840

Please sign in to comment.