Skip to content

Commit

Permalink
refactor!: use environment.htmlPaths instead of api.getHtmlPath (#2771)
Browse files Browse the repository at this point in the history
  • Loading branch information
9aoy authored Jul 3, 2024
1 parent bd45531 commit 310ced6
Show file tree
Hide file tree
Showing 16 changed files with 6 additions and 114 deletions.
1 change: 0 additions & 1 deletion packages/core/src/createRsbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ export async function createRsbuild(
'onCloseDevServer',
'onDevCompileDone',
'onExit',
'getHTMLPaths',
'getRsbuildConfig',
'getNormalizedConfig',
]),
Expand Down
11 changes: 0 additions & 11 deletions packages/core/src/initPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>,
);
};

const exposed: Array<{ id: string | symbol; api: any }> = [];

const expose = (id: string | symbol, api: any) => {
Expand Down Expand Up @@ -161,7 +151,6 @@ export function getPluginAPI({
expose,
transform,
useExposed,
getHTMLPaths,
getRsbuildConfig,
getNormalizedConfig,
isPluginExists: pluginManager.isPluginExists,
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/plugins/inlineChunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/plugins/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, [
{
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/plugins/resourceHints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/plugins/sri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ export type RsbuildInstance = {
createDevServer: ProviderInstance['createDevServer'];
startDevServer: ProviderInstance['startDevServer'];

getHTMLPaths: RsbuildPluginAPI['getHTMLPaths'];
getRsbuildConfig: RsbuildPluginAPI['getRsbuildConfig'];
getNormalizedConfig: RsbuildPluginAPI['getNormalizedConfig'];

Expand Down
3 changes: 1 addition & 2 deletions packages/plugin-assets-retry/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 0 additions & 5 deletions packages/shared/src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,6 @@ export type RsbuildPluginAPI = Readonly<{
/** Only works when bundler is Webpack */
modifyWebpackConfig: PluginHook<ModifyWebpackConfigFn>;

/**
* Get the relative paths of generated HTML files.
* The key is entry name and the value is path.
*/
getHTMLPaths: (options?: { environment: string }) => Record<string, string>;
getRsbuildConfig: GetRsbuildConfig;
getNormalizedConfig: typeof getNormalizedConfig;

Expand Down
15 changes: 0 additions & 15 deletions website/docs/en/api/javascript-api/instance.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -784,18 +784,3 @@ rsbuild.onBeforeBuild(() => {
console.log(config.html.title);
});
```

## rsbuild.getHTMLPaths

import GetHTMLPaths from '@en/shared/getHtmlPaths.mdx';

<GetHTMLPaths />

- **Example:**

```ts
rsbuild.onBeforeBuild(() => {
const htmlPaths = api.getHTMLPaths();
console.log(htmlPaths); // { main: 'html/main/index.html' };
});
```
19 changes: 0 additions & 19 deletions website/docs/en/plugins/dev/core.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -161,25 +161,6 @@ export default () => ({
});
```

## api.getHTMLPaths

import GetHTMLPaths from '@en/shared/getHtmlPaths.mdx';

<GetHTMLPaths />

- **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.
Expand Down
9 changes: 0 additions & 9 deletions website/docs/en/shared/getHtmlPaths.mdx

This file was deleted.

15 changes: 0 additions & 15 deletions website/docs/zh/api/javascript-api/instance.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -776,18 +776,3 @@ rsbuild.onBeforeBuild(() => {
console.log(config.html.title);
});
```

## rsbuild.getHTMLPaths

import GetHTMLPaths from '@zh/shared/getHtmlPaths.mdx';

<GetHTMLPaths />

- **示例:**

```ts
rsbuild.onBeforeBuild(() => {
const htmlPaths = api.getHTMLPaths();
console.log(htmlPaths); // { main: 'html/main/index.html' };
});
```
19 changes: 0 additions & 19 deletions website/docs/zh/plugins/dev/core.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -159,25 +159,6 @@ export default () => ({
});
```

## api.getHTMLPaths

import GetHTMLPaths from '@zh/shared/getHtmlPaths.mdx';

<GetHTMLPaths />

- **示例:**

```ts
const pluginFoo = () => ({
setup(api) {
api.modifyRspackConfig(() => {
const htmlPaths = api.getHTMLPaths();
console.log(htmlPaths); // { index: 'index.html' };
});
},
});
```

## api.transform

用于转换模块的代码。
Expand Down
9 changes: 0 additions & 9 deletions website/docs/zh/shared/getHtmlPaths.mdx

This file was deleted.

0 comments on commit 310ced6

Please sign in to comment.