Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
/**
* Export locals convention types matching css-loader
*/
export type ExportLocalsConvention =
| "as-is"
| "camel-case"
| "camel-case-only"
| "dashes"
| "dashes-only";

/**
* Loader options interface
*/
export interface LoaderOptions {
/** @deprecated Use exportLocalsConvention instead. Will be removed in v2.0 */
camelCase?: boolean;
Expand All @@ -24,34 +18,21 @@ export interface LoaderOptions {
banner?: string;
}

/**
* Export format marker interface
*/
export interface ExportMarker {
pattern: string;
isNamedExport: boolean;
}

/**
* CSS module pattern interface
*/
export interface CssModulePatterns {
OBJECT_EXPORT: RegExp;
NAMED_EXPORT: RegExp;
ALIASED_EXPORT: RegExp;
}

/**
* JSON Schema property definition
*/
interface SchemaProperty {
type: "boolean" | "string" | "number";
enum?: string[];
}

/**
* JSON Schema definition for validation
*/
interface SchemaDefinition {
type: "object";
properties: Record<string, SchemaProperty>;
Expand All @@ -65,7 +46,6 @@ export const STYLE_EXT_REGEX = /\.(css|postcss|pcss|scss|sass|less|styl|sss)$/;

/**
* Schema for loader options validation.
* Conforms to JSON Schema Draft-07 specification for use with schema-utils.
*/
export const SCHEMA: SchemaDefinition = {
type: "object",
Expand Down
4 changes: 0 additions & 4 deletions src/messages.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* Error and info message templates for the loader.
*/

export const ERRORS = {
FILE_NOT_FOUND: (filePath: string): string =>
`CSS DTS Loader: File "${filePath}" not found. Run build in "emit" mode to create it.`,
Expand Down
16 changes: 13 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,21 @@ export function applyExportLocalsConvention(
* @internal
*/
function detectExportFormat(source: string): { startIndex: number; isNamedExport: boolean } {
const match = EXPORT_MARKERS.find(marker => source.includes(marker.pattern));
// Find the earliest export marker in the source
let earliestIndex = -1;
let earliestMarker: typeof EXPORT_MARKERS[0] | null = null;

for (const marker of EXPORT_MARKERS) {
const index = source.indexOf(marker.pattern);
if (index !== -1 && (earliestIndex === -1 || index < earliestIndex)) {
earliestIndex = index;
earliestMarker = marker;
}
}

return {
startIndex: match ? source.indexOf(match.pattern) : -1,
isNamedExport: match?.isNamedExport || false
startIndex: earliestIndex,
isNamedExport: earliestMarker?.isNamedExport || false
};
}

Expand Down
20 changes: 20 additions & 0 deletions test/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ export { __dts_import as "import" };
"
`;

exports[`css-modules-dts-loader > JavaScript Keywords as Class Names > should handle mixed keywords and normal classes 1`] = `
"// This file is automatically generated.
// Please do not change this file!
export const container: string;
export const title: string;

declare const __dts_class: string;
export { __dts_class as "class" };
"
`;

exports[`css-modules-dts-loader > JavaScript Keywords as Class Names > should handle mixed keywords and normal classes with only keyword exported 1`] = `
"// This file is automatically generated.
// Please do not change this file!

declare const __dts_class: string;
export { __dts_class as "class" };
"
`;

exports[`css-modules-dts-loader > Options: camelCase > should convert kebab-case class names to camelCase 1`] = `
"// This file is automatically generated.
// Please do not change this file!
Expand Down
33 changes: 33 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,39 @@ describe("css-modules-dts-loader", () => {
expect(dtsContent).toContain('"export"');
expect(dtsContent).toContain('"for"');
});

it("should handle mixed keywords and normal classes", async () => {
const files = {
"index.js": "import styles from './styles.module.css';",
"styles.module.css": `
.container {
height: var(--ds-color-surface-main);
}
.title {
color: var(--ds-color-text-main);
}
.class {
background-color: var(--ds-color-background-accent);
}
`
};

const { tmpDir } = await compileProject({
files,
loaderOptions: { namedExport: true }
});

const dtsContent = readFile(tmpDir, "styles.module.css.d.ts");
expect(normalizeLineEndings(dtsContent)).toMatchSnapshot();

// Should export normal classnames as regular named exports
expect(dtsContent).toContain("export const container: string;");
expect(dtsContent).toContain("export const title: string;");

// Should export the keyword with aliased export
expect(dtsContent).toContain("declare const __dts_class: string;");
expect(dtsContent).toContain('export { __dts_class as "class" };');
});
});

describe("File Extensions", () => {
Expand Down