Skip to content

Commit 2c4dbb6

Browse files
committed
fix(theme/eject): allow treeshaking manually the theme/index file
1 parent e9a454f commit 2c4dbb6

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

packages/core/rslib.config.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { fileURLToPath } from 'node:url';
33
import { pluginReact } from '@rsbuild/plugin-react';
44
import { pluginSass } from '@rsbuild/plugin-sass';
55
import { pluginSvgr } from '@rsbuild/plugin-svgr';
6-
import { defineConfig } from '@rslib/core';
6+
import { defineConfig, type RsbuildPlugin } from '@rslib/core';
77
import { pluginPublint } from 'rsbuild-plugin-publint';
8+
import { exportStarOptimizerTransform } from './src/node/theme/exportStarOptimizerTransform';
89

910
const COMMON_EXTERNALS = [
1011
'virtual-routes',
@@ -39,6 +40,7 @@ export default defineConfig({
3940

4041
// TODO: should add entry by new URL parser in Rspack module graph
4142
'node/mdx/loader': './src/node/mdx/loader.ts',
43+
'node/theme/loader': './src/node/theme/loader.ts',
4244
'node/ssg/renderPageWorker': './src/node/ssg/renderPageWorker.ts',
4345
},
4446
},
@@ -116,6 +118,17 @@ export default defineConfig({
116118
pluginReact(),
117119
pluginSvgr({ svgrOptions: { exportType: 'default' } }),
118120
pluginSass(),
121+
{
122+
name: 'export_star_optimizer',
123+
setup(api) {
124+
api.transform(
125+
{ test: /src\/theme\/index\.ts/ },
126+
({ code, resourcePath }) => {
127+
return exportStarOptimizerTransform(code, resourcePath);
128+
},
129+
);
130+
},
131+
} satisfies RsbuildPlugin,
119132
],
120133
source: {
121134
define: {

packages/core/src/node/initRsbuild.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createRequire } from 'node:module';
2-
import path from 'node:path';
2+
import path, { join } from 'node:path';
33
import { fileURLToPath } from 'node:url';
44
import type {
55
RsbuildConfig,
@@ -367,9 +367,17 @@ async function createInternalBuildConfig(
367367
.test(/\.rspress[\\/]runtime[\\/]virtual-global-styles/)
368368
.merge({ sideEffects: true });
369369

370-
chain.module.rule('rspress-theme-index').test().merge({
370+
// Optimize the theme
371+
const themeIndexPath = join(CUSTOM_THEME_DIR, 'index');
372+
const themeIndexRule = chain.module
373+
.rule('rspress-theme-index')
374+
.test(themeIndexPath);
375+
themeIndexRule.merge({
371376
sideEffects: false,
372377
});
378+
themeIndexRule
379+
.use('EXPORT_STAR_OPTIMIZE')
380+
.loader(fileURLToPath(new URL('./theme/loader.js', import.meta.url)));
373381

374382
if (isSsg || isSsgMd) {
375383
chain.optimization.splitChunks({});

packages/core/src/node/theme/exportStarOptimizerTransform.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { readFile } from 'node:fs/promises';
2+
import { dirname } from 'node:path';
23
import { rspack } from '@rsbuild/core';
3-
import { dirname } from 'path';
44

55
/**
66
* Convert `export *` into named exports via regex for tree-shaking friendliness.
@@ -191,15 +191,6 @@ class ExportStarOptimizer {
191191

192192
return code;
193193
}
194-
195-
/**
196-
* Transform file content.
197-
*/
198-
static async transformFile(filepath: string): Promise<string> {
199-
const code = await readFile(filepath, 'utf-8');
200-
const transformer = new ExportStarOptimizer(filepath);
201-
return transformer.transform(code);
202-
}
203194
}
204195

205196
/**
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { Rspack } from '@rsbuild/core';
2+
import { exportStarOptimizerTransform } from './exportStarOptimizerTransform.js';
3+
4+
export default async function themeIndexFileOptimizerLoader(
5+
this: Rspack.LoaderContext<{}>,
6+
source: string,
7+
) {
8+
this.cacheable(true);
9+
const callback = this.async();
10+
const filepath = this.resourcePath;
11+
const content = await exportStarOptimizerTransform(source, filepath);
12+
callback(null, content);
13+
}

0 commit comments

Comments
 (0)