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

Commit a699a48

Browse files
authored
Merge pull request #3 from ryoppippi/fix-test
feat: Add options for cssModules preprocessor
2 parents 4940f8f + 9af00a0 commit a699a48

File tree

9 files changed

+53
-13
lines changed

9 files changed

+53
-13
lines changed

eslint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ryoppippi } from '@ryoppippi/eslint-config';
22

33
export default ryoppippi({
44
svelte: true,
5+
ignores: ['tests/**/*.svelte'],
56
typescript: {
67
tsconfigPath: './tsconfig.json',
78
},

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,

tests/class/Output.svelte

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<script>
22
const s = {
3-
error: 's-Lwza_error',
4-
success: 's-Lwza_success',
5-
};
3+
error: "error_class-module-css-module-test",
4+
success: "success_class-module-css-module-test"
5+
};
66
</script>
77

88
<div class={s.error}>
@@ -15,12 +15,12 @@
1515

1616
<style>
1717
18-
/* /Users/ryoppippi/ghq/github.com/ryoppippi/svelte-preprocess-css-modules/tests/assets/class.module.css */
19-
.s-Lwza_error {
18+
19+
.error_class-module-css-module-test {
2020
color: red;
2121
}
2222
23-
.s-Lwza_success {
23+
.success_class-module-css-module-test {
2424
color: green;
2525
}
2626

tests/class/class.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function resolve(file: string) {
77
return path.resolve(__dirname, file);
88
}
99

10-
it('load css module for class', async () => {
10+
it('load css module for class', { retry: 5 }, async () => {
1111
const filename = resolve('Input.svelte');
1212

1313
const source = await fs.readFile(filename, 'utf-8');

tests/compiler.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ export async function compiler({
1212
}: Params) {
1313
const { code } = await preprocess(
1414
source,
15-
[cssModules()],
15+
[cssModules({
16+
moduleNameingPattern: '[local]_[name]-css-module-test',
17+
includeOriginalPath: false,
18+
})],
1619
preprocessOptions,
1720
);
1821

0 commit comments

Comments
 (0)