Skip to content
This repository was archived by the owner on Sep 25, 2024. It is now read-only.

Commit c472ce7

Browse files
committed
feat: Add options for cssModules preprocessor
This commit introduces new options for the cssModules preprocessor. These options include 'moduleNameingPattern' for module naming and 'includeOriginalPath' to decide whether to include the original path in the css module. The default value for 'includeOriginalPath' is true.
1 parent 4940f8f commit c472ce7

File tree

5 files changed

+41
-5
lines changed

5 files changed

+41
-5
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"mlly": "^1.7.1",
2424
"svelte": "5.0.0-next.210",
2525
"svelte-parse-markup": "^0.1.5",
26+
"type-fest": "^4.23.0",
2627
"uint8array-extras": "^1.4.0",
2728
"unconfig": "^0.5.5",
2829
"zimmerframe": "^1.1.2"

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import { genObjectFromValues } from 'knitwork';
66
import { loadAliases } from './utils/alias';
77
import type { CssModule } from './utils/css-module';
88
import { getCssModule, getCssModuleImports } from './utils/css-module';
9+
import { type Options, resolveOptions } from './options';
910

1011
// TODO: improve tree-shaking for production build
1112

12-
export function cssModules(): PreprocessorGroup {
13+
export function cssModules(_options: Options = {}): PreprocessorGroup {
14+
const options = resolveOptions(_options);
15+
1316
const cssModuleCache = new Map<string, CssModule[]>();
1417
return {
1518
markup({ content, filename }) {
@@ -48,7 +51,7 @@ export function cssModules(): PreprocessorGroup {
4851
/* transform css/scss modules */
4952
const cssModules = [];
5053
for (const cmi of cssModuleImports) {
51-
const cssModule = await getCssModule(cmi);
54+
const cssModule = await getCssModule(cmi, options);
5255
cssModules.push(cssModule);
5356

5457
/* generate css module exports */
@@ -81,7 +84,7 @@ export function cssModules(): PreprocessorGroup {
8184
/* append css module styles */
8285
for (const cssModule of cssModules) {
8386
const code = `
84-
/* ${cssModule.path} */
87+
${options.includeOriginalPath === false ? '' : `/*${cssModule.path}*/`}
8588
${cssModule.css}
8689
`;
8790
s.append(code);

src/options.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { SetRequired } from 'type-fest';
2+
3+
/**
4+
* Options for the cssModules preprocessor
5+
*/
6+
export type Options = {
7+
/*
8+
* The pattern to use for module naming
9+
* @see https://lightningcss.dev/css-modules.html
10+
*/
11+
moduleNameingPattern?: string;
12+
13+
/*
14+
* Whether to include the original path in the css module
15+
* @default true
16+
*/
17+
includeOriginalPath?: boolean;
18+
};
19+
20+
export type ResolvedOptions = SetRequired<Options, 'includeOriginalPath'>;
21+
22+
export function resolveOptions(options: Options): ResolvedOptions {
23+
return {
24+
moduleNameingPattern: options.moduleNameingPattern,
25+
includeOriginalPath: options.includeOriginalPath ?? true,
26+
};
27+
}

src/utils/css-module.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import MagicString from 'magic-string';
66
import type { StaticImport } from 'mlly';
77
import { parseStaticImport, pathToFileURL, resolvePath } from 'mlly';
88
import { stringToUint8Array, uint8ArrayToString } from 'uint8array-extras';
9+
import type { ResolvedOptions } from '../options';
910

1011
type getCssModuleImportsProps = {
1112
imports: StaticImport[];
@@ -63,7 +64,8 @@ export type CssModule = {
6364
exports: Record<string, string>;
6465
} & ResolvedModuleImport;
6566

66-
export async function getCssModule({ path, ...rest }: ResolvedModuleImport): Promise<CssModule> {
67+
export async function getCssModule({ path, ...rest }: ResolvedModuleImport, options: ResolvedOptions): Promise<CssModule> {
68+
const { moduleNameingPattern: pattern } = options;
6769
const [err, code] = await to(readFile(path, { encoding: 'utf-8' }));
6870

6971
if (err != null) {
@@ -73,7 +75,7 @@ export async function getCssModule({ path, ...rest }: ResolvedModuleImport): Pro
7375

7476
const { code: _css, exports: _exports } = transform({
7577
code: stringToUint8Array(code),
76-
cssModules: true,
78+
cssModules: pattern != null ? { pattern } : true,
7779
minify: false,
7880
sourceMap: false,
7981
filename: path,

0 commit comments

Comments
 (0)