Skip to content

Commit

Permalink
refactor(plugin-preact): modify rsbuild config directly (#1743)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Mar 5, 2024
1 parent ace57ae commit 963ca24
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 48 deletions.
1 change: 0 additions & 1 deletion packages/plugin-preact/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
},
"devDependencies": {
"@rsbuild/core": "workspace:*",
"@scripts/test-helper": "workspace:*",
"@types/node": "16.x",
"typescript": "^5.3.0"
},
Expand Down
50 changes: 27 additions & 23 deletions packages/plugin-preact/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { RsbuildPlugin } from '@rsbuild/core';
import { modifySwcLoaderOptions, type SwcReactConfig } from '@rsbuild/shared';
import type { RsbuildConfig, RsbuildPlugin } from '@rsbuild/core';
import { getNodeEnv, type SwcReactConfig } from '@rsbuild/shared';

export type PluginPreactOptions = {
/**
Expand All @@ -17,34 +17,38 @@ export const pluginPreact = (
setup(api) {
const { reactAliasesEnabled = true } = options;

api.modifyBundlerChain(async (chain, { isDev }) => {
api.modifyRsbuildConfig((userConfig, { mergeRsbuildConfig }) => {
const reactOptions: SwcReactConfig = {
development: getNodeEnv() === 'development',
runtime: 'automatic',
importSource: 'preact',
};

const extraConfig: RsbuildConfig = {
tools: {
swc(opts) {
opts.jsc ??= {};
opts.jsc.transform ??= {};
opts.jsc.transform.react = {
...opts.jsc.transform.react,
...reactOptions,
};
return opts;
},
},
};

if (reactAliasesEnabled) {
chain.resolve.alias.merge({
extraConfig.source ||= {};
extraConfig.source.alias = {
react: 'preact/compat',
'react-dom/test-utils': 'preact/test-utils',
'react-dom': 'preact/compat',
'react/jsx-runtime': 'preact/jsx-runtime',
});
};
}

const reactOptions: SwcReactConfig = {
development: isDev,
runtime: 'automatic',
importSource: 'preact',
};

modifySwcLoaderOptions({
chain,
modifier: (opts) => {
opts.jsc ??= {};
opts.jsc.transform ??= {};
opts.jsc.transform.react = {
...opts.jsc.transform.react,
...reactOptions,
};
return opts;
},
});
return mergeRsbuildConfig(extraConfig, userConfig);
});
},
});
46 changes: 25 additions & 21 deletions packages/plugin-preact/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
import { expect, describe, it, vi } from 'vitest';
import { expect, describe, it } from 'vitest';
import { pluginPreact } from '../src';
import { createStubRsbuild } from '@scripts/test-helper';
import { createRsbuild } from '@rsbuild/core';

describe('plugins/preact', () => {
const preactAlias = {
react: 'preact/compat',
'react-dom': 'preact/compat',
'react-dom/test-utils': 'preact/test-utils',
'react/jsx-runtime': 'preact/jsx-runtime',
};

it('should apply react aliases by default', async () => {
const rsbuild = await createStubRsbuild({
plugins: [pluginPreact()],
const rsbuild = await createRsbuild({
rsbuildConfig: {
plugins: [pluginPreact()],
},
});

const config = await rsbuild.unwrapConfig();
expect(config.resolve.alias).toMatchInlineSnapshot(`
{
"react": "preact/compat",
"react-dom": "preact/compat",
"react-dom/test-utils": "preact/test-utils",
"react/jsx-runtime": "preact/jsx-runtime",
}
`);
const configs = await rsbuild.initConfigs();
expect(configs[0].resolve?.alias).toMatchObject(preactAlias);
});

it('should not apply react aliases if reactAliasesEnabled is false', async () => {
const rsbuild = await createStubRsbuild({
plugins: [
pluginPreact({
reactAliasesEnabled: false,
}),
],
const rsbuild = await createRsbuild({
rsbuildConfig: {
plugins: [
pluginPreact({
reactAliasesEnabled: false,
}),
],
},
});

const config = await rsbuild.unwrapConfig();
expect(config.resolve?.alias).toBeFalsy();
const configs = await rsbuild.initConfigs();
expect(configs[0].resolve?.alias).not.toMatchObject(preactAlias);
});
});
3 changes: 0 additions & 3 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 963ca24

Please sign in to comment.