From 310ced655d1809fff8f813b7779581c4c58271e2 Mon Sep 17 00:00:00 2001 From: gaoyuan Date: Wed, 3 Jul 2024 16:09:27 +0800 Subject: [PATCH] refactor!: use environment.htmlPaths instead of api.getHtmlPath (#2771) --- packages/core/src/createRsbuild.ts | 1 - packages/core/src/initPlugins.ts | 11 ----------- packages/core/src/plugins/html.ts | 3 +-- packages/core/src/plugins/inlineChunk.ts | 3 +-- packages/core/src/plugins/manifest.ts | 2 +- packages/core/src/plugins/resourceHints.ts | 3 +-- packages/core/src/plugins/sri.ts | 2 +- packages/core/src/types.ts | 1 - packages/plugin-assets-retry/src/index.ts | 3 +-- packages/shared/src/types/plugin.ts | 5 ----- .../docs/en/api/javascript-api/instance.mdx | 15 --------------- website/docs/en/plugins/dev/core.mdx | 19 ------------------- website/docs/en/shared/getHtmlPaths.mdx | 9 --------- .../docs/zh/api/javascript-api/instance.mdx | 15 --------------- website/docs/zh/plugins/dev/core.mdx | 19 ------------------- website/docs/zh/shared/getHtmlPaths.mdx | 9 --------- 16 files changed, 6 insertions(+), 114 deletions(-) delete mode 100644 website/docs/en/shared/getHtmlPaths.mdx delete mode 100644 website/docs/zh/shared/getHtmlPaths.mdx diff --git a/packages/core/src/createRsbuild.ts b/packages/core/src/createRsbuild.ts index 3aa9280450..4b0326002e 100644 --- a/packages/core/src/createRsbuild.ts +++ b/packages/core/src/createRsbuild.ts @@ -158,7 +158,6 @@ export async function createRsbuild( 'onCloseDevServer', 'onDevCompileDone', 'onExit', - 'getHTMLPaths', 'getRsbuildConfig', 'getNormalizedConfig', ]), diff --git a/packages/core/src/initPlugins.ts b/packages/core/src/initPlugins.ts index dbf208f4ce..ad1be6fbe3 100644 --- a/packages/core/src/initPlugins.ts +++ b/packages/core/src/initPlugins.ts @@ -97,16 +97,6 @@ export function getPluginAPI({ throw new Error('`getRsbuildConfig` get an invalid type param.'); }) as GetRsbuildConfig; - const getHTMLPaths = (options?: { environment: string }) => { - if (options?.environment) { - return context.environments[options.environment].htmlPaths; - } - return Object.values(context.environments).reduce( - (prev, context) => Object.assign(prev, context.htmlPaths), - {} as Record, - ); - }; - const exposed: Array<{ id: string | symbol; api: any }> = []; const expose = (id: string | symbol, api: any) => { @@ -161,7 +151,6 @@ export function getPluginAPI({ expose, transform, useExposed, - getHTMLPaths, getRsbuildConfig, getNormalizedConfig, isPluginExists: pluginManager.isPluginExists, diff --git a/packages/core/src/plugins/html.ts b/packages/core/src/plugins/html.ts index a9758a45ec..752f18577d 100644 --- a/packages/core/src/plugins/html.ts +++ b/packages/core/src/plugins/html.ts @@ -278,9 +278,8 @@ export const pluginHtml = (modifyTagsFn?: ModifyHTMLTagsFn): RsbuildPlugin => ({ setup(api) { api.modifyBundlerChain( async (chain, { HtmlPlugin, isProd, CHAIN_ID, environment }) => { - const { config } = environment; + const { config, htmlPaths } = environment; - const htmlPaths = api.getHTMLPaths({ environment: environment.name }); if (Object.keys(htmlPaths).length === 0) { return; } diff --git a/packages/core/src/plugins/inlineChunk.ts b/packages/core/src/plugins/inlineChunk.ts index 4cde5539f2..e45ea5af68 100644 --- a/packages/core/src/plugins/inlineChunk.ts +++ b/packages/core/src/plugins/inlineChunk.ts @@ -8,12 +8,11 @@ export const pluginInlineChunk = (): RsbuildPlugin => ({ setup(api) { api.modifyBundlerChain(async (chain, { CHAIN_ID, isDev, environment }) => { - const htmlPaths = api.getHTMLPaths({ environment: environment.name }); + const { htmlPaths, config } = environment; if (Object.keys(htmlPaths).length === 0 || isDev) { return; } - const { config } = environment; const { InlineChunkHtmlPlugin } = await import( '../rspack/InlineChunkHtmlPlugin' ); diff --git a/packages/core/src/plugins/manifest.ts b/packages/core/src/plugins/manifest.ts index 5f029bd5e2..bddcc91054 100644 --- a/packages/core/src/plugins/manifest.ts +++ b/packages/core/src/plugins/manifest.ts @@ -156,7 +156,7 @@ export const pluginManifest = (): RsbuildPlugin => ({ typeof manifest === 'string' ? manifest : 'manifest.json'; const { RspackManifestPlugin } = await import('rspack-manifest-plugin'); - const htmlPaths = api.getHTMLPaths({ environment: environment.name }); + const { htmlPaths } = environment; chain.plugin(CHAIN_ID.PLUGIN.MANIFEST).use(RspackManifestPlugin, [ { diff --git a/packages/core/src/plugins/resourceHints.ts b/packages/core/src/plugins/resourceHints.ts index e186cc61c7..644ea6e9bf 100644 --- a/packages/core/src/plugins/resourceHints.ts +++ b/packages/core/src/plugins/resourceHints.ts @@ -45,13 +45,12 @@ export const pluginResourceHints = (): RsbuildPlugin => ({ }); api.modifyBundlerChain(async (chain, { CHAIN_ID, environment }) => { - const htmlPaths = api.getHTMLPaths({ environment: environment.name }); + const { config, htmlPaths } = environment; if (Object.keys(htmlPaths).length === 0) { return; } - const { config } = environment; const { performance: { preload, prefetch }, } = config; diff --git a/packages/core/src/plugins/sri.ts b/packages/core/src/plugins/sri.ts index 298c8a9337..73295a2ba1 100644 --- a/packages/core/src/plugins/sri.ts +++ b/packages/core/src/plugins/sri.ts @@ -192,7 +192,7 @@ export const pluginSri = (): RsbuildPlugin => ({ } api.modifyBundlerChain((chain, { environment }) => { - const htmlPaths = api.getHTMLPaths({ environment: environment.name }); + const { htmlPaths } = environment; if (Object.keys(htmlPaths).length === 0) { return; diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index b6df3c1fa6..53f3ecbb19 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -115,7 +115,6 @@ export type RsbuildInstance = { createDevServer: ProviderInstance['createDevServer']; startDevServer: ProviderInstance['startDevServer']; - getHTMLPaths: RsbuildPluginAPI['getHTMLPaths']; getRsbuildConfig: RsbuildPluginAPI['getRsbuildConfig']; getNormalizedConfig: RsbuildPluginAPI['getNormalizedConfig']; diff --git a/packages/plugin-assets-retry/src/index.ts b/packages/plugin-assets-retry/src/index.ts index 5a89e2d2a9..33b4997414 100644 --- a/packages/plugin-assets-retry/src/index.ts +++ b/packages/plugin-assets-retry/src/index.ts @@ -12,9 +12,8 @@ export const pluginAssetsRetry = ( setup(api) { api.modifyBundlerChain( async (chain, { CHAIN_ID, HtmlPlugin, isProd, environment }) => { - const { config } = environment; + const { config, htmlPaths } = environment; - const htmlPaths = api.getHTMLPaths({ environment: environment.name }); if (!options || Object.keys(htmlPaths).length === 0) { return; } diff --git a/packages/shared/src/types/plugin.ts b/packages/shared/src/types/plugin.ts index 7d062ddb8f..96d5ed3ace 100644 --- a/packages/shared/src/types/plugin.ts +++ b/packages/shared/src/types/plugin.ts @@ -259,11 +259,6 @@ export type RsbuildPluginAPI = Readonly<{ /** Only works when bundler is Webpack */ modifyWebpackConfig: PluginHook; - /** - * Get the relative paths of generated HTML files. - * The key is entry name and the value is path. - */ - getHTMLPaths: (options?: { environment: string }) => Record; getRsbuildConfig: GetRsbuildConfig; getNormalizedConfig: typeof getNormalizedConfig; diff --git a/website/docs/en/api/javascript-api/instance.mdx b/website/docs/en/api/javascript-api/instance.mdx index edb696a8ec..fd5079ec67 100644 --- a/website/docs/en/api/javascript-api/instance.mdx +++ b/website/docs/en/api/javascript-api/instance.mdx @@ -784,18 +784,3 @@ rsbuild.onBeforeBuild(() => { console.log(config.html.title); }); ``` - -## rsbuild.getHTMLPaths - -import GetHTMLPaths from '@en/shared/getHtmlPaths.mdx'; - - - -- **Example:** - -```ts -rsbuild.onBeforeBuild(() => { - const htmlPaths = api.getHTMLPaths(); - console.log(htmlPaths); // { main: 'html/main/index.html' }; -}); -``` diff --git a/website/docs/en/plugins/dev/core.mdx b/website/docs/en/plugins/dev/core.mdx index afd6df80d5..3d00c8bb36 100644 --- a/website/docs/en/plugins/dev/core.mdx +++ b/website/docs/en/plugins/dev/core.mdx @@ -161,25 +161,6 @@ export default () => ({ }); ``` -## api.getHTMLPaths - -import GetHTMLPaths from '@en/shared/getHtmlPaths.mdx'; - - - -- **Example:** - -```ts -const pluginFoo = () => ({ - setup(api) { - api.modifyRspackConfig(() => { - const htmlPaths = api.getHTMLPaths(); - console.log(htmlPaths); // { index: 'index.html' }; - }); - }, -}); -``` - ## api.transform Used to transform the code of modules. diff --git a/website/docs/en/shared/getHtmlPaths.mdx b/website/docs/en/shared/getHtmlPaths.mdx deleted file mode 100644 index 0fda294c87..0000000000 --- a/website/docs/en/shared/getHtmlPaths.mdx +++ /dev/null @@ -1,9 +0,0 @@ -Get path information for all HTML assets. - -This method will return an object, the key is the entry name and the value is the relative path of the HTML file in the dist directory. - -- **Type:** - -```ts -function GetHTMLPaths(): Record; -``` diff --git a/website/docs/zh/api/javascript-api/instance.mdx b/website/docs/zh/api/javascript-api/instance.mdx index 5227f50999..b664d79a8c 100644 --- a/website/docs/zh/api/javascript-api/instance.mdx +++ b/website/docs/zh/api/javascript-api/instance.mdx @@ -776,18 +776,3 @@ rsbuild.onBeforeBuild(() => { console.log(config.html.title); }); ``` - -## rsbuild.getHTMLPaths - -import GetHTMLPaths from '@zh/shared/getHtmlPaths.mdx'; - - - -- **示例:** - -```ts -rsbuild.onBeforeBuild(() => { - const htmlPaths = api.getHTMLPaths(); - console.log(htmlPaths); // { main: 'html/main/index.html' }; -}); -``` diff --git a/website/docs/zh/plugins/dev/core.mdx b/website/docs/zh/plugins/dev/core.mdx index 229f691e52..30a105ca5b 100644 --- a/website/docs/zh/plugins/dev/core.mdx +++ b/website/docs/zh/plugins/dev/core.mdx @@ -159,25 +159,6 @@ export default () => ({ }); ``` -## api.getHTMLPaths - -import GetHTMLPaths from '@zh/shared/getHtmlPaths.mdx'; - - - -- **示例:** - -```ts -const pluginFoo = () => ({ - setup(api) { - api.modifyRspackConfig(() => { - const htmlPaths = api.getHTMLPaths(); - console.log(htmlPaths); // { index: 'index.html' }; - }); - }, -}); -``` - ## api.transform 用于转换模块的代码。 diff --git a/website/docs/zh/shared/getHtmlPaths.mdx b/website/docs/zh/shared/getHtmlPaths.mdx deleted file mode 100644 index 6655075192..0000000000 --- a/website/docs/zh/shared/getHtmlPaths.mdx +++ /dev/null @@ -1,9 +0,0 @@ -获取所有 HTML 产物的路径信息。 - -该方法会返回一个对象,对象的 key 为 entry 名称,value 为 HTML 文件在产物目录下的相对路径。 - -- **类型:** - -```ts -function GetHTMLPaths(): Record; -```