generated from rspack-contrib/rsbuild-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e4bf5d
commit 035f692
Showing
3 changed files
with
163 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,100 @@ | ||
import type { RsbuildPlugin } from '@rsbuild/core'; | ||
import type { | ||
MergedEnvironmentConfig, | ||
RsbuildConfig, | ||
RsbuildPlugin, | ||
RsbuildTarget, | ||
} from '@rsbuild/core'; | ||
import { type ConfigChain, reduceConfigs } from 'reduce-configs'; | ||
|
||
export type PluginExampleOptions = { | ||
foo?: string; | ||
bar?: boolean; | ||
/** | ||
* The options of [@swc/plugin-styled-components](https://www.npmjs.com/package/@swc/plugin-styled-components). | ||
*/ | ||
export type PluginStyledComponentsOptions = { | ||
displayName?: boolean; | ||
ssr?: boolean; | ||
fileName?: boolean; | ||
meaninglessFileNames?: string[]; | ||
namespace?: string; | ||
topLevelImportPaths?: string[]; | ||
transpileTemplateLiterals?: boolean; | ||
minify?: boolean; | ||
pure?: boolean; | ||
cssProps?: boolean; | ||
}; | ||
|
||
export const pluginExample = ( | ||
options: PluginExampleOptions = {}, | ||
function isServerTarget(target: RsbuildTarget[]) { | ||
return Array.isArray(target) ? target.includes('node') : target === 'node'; | ||
} | ||
|
||
const getDefaultStyledComponentsConfig = (isProd: boolean, ssr: boolean) => { | ||
return { | ||
ssr, | ||
// "pure" is used to improve dead code elimination in production. | ||
// we don't need to enable it in development because it will slow down the build process. | ||
pure: isProd, | ||
displayName: true, | ||
transpileTemplateLiterals: true, | ||
}; | ||
}; | ||
|
||
export const PLUGIN_STYLED_COMPONENTS_NAME = 'rsbuild:styled-components'; | ||
|
||
export const pluginStyledComponents = ( | ||
pluginOptions: ConfigChain<PluginStyledComponentsOptions> = {}, | ||
): RsbuildPlugin => ({ | ||
name: 'plugin-example', | ||
name: PLUGIN_STYLED_COMPONENTS_NAME, | ||
|
||
setup(api) { | ||
if (api.context.bundlerType === 'webpack') { | ||
return; | ||
} | ||
|
||
const getMergedOptions = ( | ||
useSSR: boolean, | ||
config: MergedEnvironmentConfig, | ||
) => { | ||
const isProd = config.mode === 'production'; | ||
|
||
return reduceConfigs({ | ||
initial: getDefaultStyledComponentsConfig(isProd, useSSR), | ||
config: pluginOptions, | ||
}); | ||
}; | ||
|
||
api.modifyEnvironmentConfig((userConfig, { mergeEnvironmentConfig }) => { | ||
const rsbuildConfig = api.getRsbuildConfig(); | ||
|
||
const targets = rsbuildConfig.environments | ||
? Object.values(rsbuildConfig.environments).map( | ||
(e) => e.output?.target || userConfig.output?.target || 'web', | ||
) | ||
: [userConfig.output?.target || 'web']; | ||
|
||
const useSSR = isServerTarget(targets); | ||
|
||
const mergedOptions = getMergedOptions(useSSR, userConfig); | ||
if (!mergedOptions) { | ||
return userConfig; | ||
} | ||
|
||
const extraConfig: RsbuildConfig = { | ||
tools: { | ||
swc: { | ||
jsc: { | ||
experimental: { | ||
plugins: [ | ||
[ | ||
require.resolve('@swc/plugin-styled-components'), | ||
mergedOptions, | ||
], | ||
], | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
setup() { | ||
console.log('Hello Rsbuild!', options); | ||
return mergeEnvironmentConfig(extraConfig, userConfig); | ||
}); | ||
}, | ||
}); |