Skip to content

Commit bfd211b

Browse files
committed
Support globalPrefix
1 parent 2e062a2 commit bfd211b

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

lib/esbuild-plugin-react18-css-example/tsup.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ export default defineConfig(options => ({
99
clean: true,
1010
bundle: true,
1111
minify: !options.watch,
12-
esbuildPlugins: [react18Plugin(), cssModulePlugin()],
12+
esbuildPlugins: [react18Plugin(), cssModulePlugin({ globalPrefix: "hk" })],
1313
legacyOutput: true,
1414
}));

lib/esbuild-plugin-react18-css/src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ import { compile } from "sass";
99
const uuid = () => (Date.now() * Math.random()).toString(36).slice(0, 8);
1010

1111
interface CSSModulePluginOptions {
12+
/** by default name is generated without hash */
1213
generateScopedName?: string | ((name: string, filename: string, css: string) => string);
14+
/** set skipAutoPrefixer to true to disable autoprefixer */
1315
skipAutoPrefixer?: boolean;
16+
/** global CSS class prefix. @defaultValue "" */
17+
globalPrefix?: string;
1418
}
1519

1620
function applyAutoPrefixer(build: PluginBuild, options: CSSModulePluginOptions, write?: boolean) {
@@ -89,12 +93,14 @@ function handleModules(
8993
}));
9094
}
9195

92-
const cssPlugin: (options: CSSModulePluginOptions) => Plugin = (options = {}) => ({
96+
const cssPlugin: (options?: CSSModulePluginOptions) => Plugin = (options = {}) => ({
9397
name: "esbuild-plugin-react18-css-" + uuid(),
9498
setup(build): void {
9599
const write = build.initialOptions.write;
96100
if (!options.generateScopedName) {
101+
const globalPrefix = options.globalPrefix ?? "";
97102
options.generateScopedName = (name, filename) =>
103+
(globalPrefix ? `${globalPrefix}__` : "") +
98104
`${path.basename(filename).split(".")[0]}__${name}`;
99105
}
100106
handleModules(build, options);

0 commit comments

Comments
 (0)