@css-modules-kit/codegen 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/runner.js CHANGED
@@ -4,89 +4,143 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.runCMK = runCMK;
7
+ exports.runCMKInWatchMode = runCMKInWatchMode;
7
8
  const promises_1 = require("node:fs/promises");
8
- const core_1 = require("@css-modules-kit/core");
9
- const typescript_1 = __importDefault(require("typescript"));
10
- const dts_writer_js_1 = require("./dts-writer.js");
11
- const error_js_1 = require("./error.js");
9
+ const chokidar_1 = __importDefault(require("chokidar"));
10
+ const project_js_1 = require("./project.js");
12
11
  /**
12
+ * Run css-modules-kit .d.ts generation.
13
+ * @param project The absolute path to the project directory or the path to `tsconfig.json`.
13
14
  * @throws {ReadCSSModuleFileError} When failed to read CSS Module file.
15
+ * @throws {WriteDtsFileError}
16
+ * @returns Whether the process succeeded without errors.
14
17
  */
15
- async function parseCSSModuleByFileName(fileName, config) {
16
- let text;
17
- try {
18
- text = await (0, promises_1.readFile)(fileName, 'utf-8');
18
+ async function runCMK(args, logger) {
19
+ const project = (0, project_js_1.createProject)(args);
20
+ if (args.clean) {
21
+ await (0, promises_1.rm)(project.config.dtsOutDir, { recursive: true, force: true });
19
22
  }
20
- catch (error) {
21
- throw new error_js_1.ReadCSSModuleFileError(fileName, error);
23
+ await project.emitDtsFiles();
24
+ const diagnostics = project.getDiagnostics();
25
+ if (diagnostics.length > 0) {
26
+ logger.logDiagnostics(diagnostics);
27
+ return false;
22
28
  }
23
- return (0, core_1.parseCSSModule)(text, { fileName, safe: false, keyframes: config.keyframes });
24
- }
25
- /**
26
- * @throws {WriteDtsFileError}
27
- */
28
- async function writeDtsByCSSModule(cssModule, { dtsOutDir, basePath, arbitraryExtensions, namedExports, prioritizeNamedImports }, resolver, matchesPattern) {
29
- const dts = (0, core_1.createDts)(cssModule, { resolver, matchesPattern }, { namedExports, prioritizeNamedImports, forTsPlugin: false });
30
- await (0, dts_writer_js_1.writeDtsFile)(dts.text, cssModule.fileName, {
31
- outDir: dtsOutDir,
32
- basePath,
33
- arbitraryExtensions,
34
- });
29
+ return true;
35
30
  }
36
31
  /**
37
- * Run css-modules-kit .d.ts generation.
32
+ * Run css-modules-kit .d.ts generation in watch mode.
33
+ *
34
+ * The promise resolves when the initial diagnostics report, emit, and watcher initialization are complete.
35
+ * Errors are reported through the logger.
36
+ *
37
+ * NOTE: For implementation simplicity, config file changes are not watched.
38
38
  * @param project The absolute path to the project directory or the path to `tsconfig.json`.
39
- * @throws {ReadCSSModuleFileError} When failed to read CSS Module file.
39
+ * @throws {TsConfigFileNotFoundError}
40
+ * @throws {ReadCSSModuleFileError}
40
41
  * @throws {WriteDtsFileError}
41
42
  */
42
- async function runCMK(args, logger) {
43
- const config = (0, core_1.readConfigFile)(args.project);
44
- if (config.diagnostics.length > 0) {
45
- logger.logDiagnostics(config.diagnostics);
46
- // eslint-disable-next-line n/no-process-exit
47
- process.exit(1);
43
+ async function runCMKInWatchMode(args, logger) {
44
+ const fsWatchers = [];
45
+ const project = (0, project_js_1.createProject)(args);
46
+ let emitAndReportDiagnosticsTimer = undefined;
47
+ if (args.clean) {
48
+ await (0, promises_1.rm)(project.config.dtsOutDir, { recursive: true, force: true });
48
49
  }
49
- const getCanonicalFileName = (fileName) => typescript_1.default.sys.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase();
50
- const moduleResolutionCache = typescript_1.default.createModuleResolutionCache(config.basePath, getCanonicalFileName, config.compilerOptions);
51
- const resolver = (0, core_1.createResolver)(config.compilerOptions, moduleResolutionCache);
52
- const matchesPattern = (0, core_1.createMatchesPattern)(config);
53
- const cssModuleMap = new Map();
54
- const syntacticDiagnostics = [];
55
- const fileNames = (0, core_1.getFileNamesByPattern)(config);
56
- if (fileNames.length === 0) {
57
- logger.logDiagnostics([
58
- {
59
- category: 'warning',
60
- text: `The file specified in tsconfig.json not found.`,
50
+ await emitAndReportDiagnostics();
51
+ // Watch project files and report diagnostics on changes
52
+ const readyPromises = [];
53
+ for (const wildcardDirectory of project.config.wildcardDirectories) {
54
+ const { promise, resolve } = promiseWithResolvers();
55
+ readyPromises.push(promise);
56
+ fsWatchers.push(chokidar_1.default
57
+ .watch(wildcardDirectory.fileName, {
58
+ ignored: (fileName, stats) => {
59
+ // The ignored function is called twice for the same path. The first time with stats undefined,
60
+ // and the second time with stats provided.
61
+ // In the first call, we can't determine if the path is a directory or file.
62
+ // So we include it in the watch target considering it might be a directory.
63
+ if (!stats)
64
+ return false;
65
+ // In the second call, we include directories or files that match wildcards in the watch target.
66
+ // However, `dtsOutDir` is excluded from the watch target.
67
+ if (stats.isDirectory()) {
68
+ return fileName === project.config.dtsOutDir;
69
+ }
70
+ else {
71
+ return !project.isWildcardMatchedFile(fileName);
72
+ }
61
73
  },
62
- ]);
63
- return;
64
- }
65
- const parseResults = await Promise.all(fileNames.map(async (fileName) => parseCSSModuleByFileName(fileName, config)));
66
- for (const parseResult of parseResults) {
67
- cssModuleMap.set(parseResult.cssModule.fileName, parseResult.cssModule);
68
- syntacticDiagnostics.push(...parseResult.diagnostics);
74
+ ignoreInitial: true,
75
+ ...(wildcardDirectory.recursive ? {} : { depth: 0 }),
76
+ })
77
+ .on('add', (fileName) => {
78
+ try {
79
+ project.addFile(fileName);
80
+ }
81
+ catch (e) {
82
+ logger.logError(e);
83
+ return;
84
+ }
85
+ scheduleEmitAndReportDiagnostics();
86
+ })
87
+ .on('change', (fileName) => {
88
+ try {
89
+ project.updateFile(fileName);
90
+ }
91
+ catch (e) {
92
+ logger.logError(e);
93
+ return;
94
+ }
95
+ scheduleEmitAndReportDiagnostics();
96
+ })
97
+ .on('unlink', (fileName) => {
98
+ project.removeFile(fileName);
99
+ scheduleEmitAndReportDiagnostics();
100
+ })
101
+ .on('error', (e) => logger.logError(e))
102
+ .on('ready', () => resolve()));
69
103
  }
70
- if (syntacticDiagnostics.length > 0) {
71
- logger.logDiagnostics(syntacticDiagnostics);
72
- // eslint-disable-next-line n/no-process-exit
73
- process.exit(1);
104
+ await Promise.all(readyPromises);
105
+ function scheduleEmitAndReportDiagnostics() {
106
+ // Switching between git branches results in numerous file changes occurring rapidly.
107
+ // Reporting diagnostics for each file change would overwhelm users.
108
+ // Therefore, we batch the processing.
109
+ if (emitAndReportDiagnosticsTimer !== undefined)
110
+ clearTimeout(emitAndReportDiagnosticsTimer);
111
+ emitAndReportDiagnosticsTimer = setTimeout(() => {
112
+ emitAndReportDiagnosticsTimer = undefined;
113
+ emitAndReportDiagnostics().catch(logger.logError.bind(logger));
114
+ }, 250);
74
115
  }
75
- const getCSSModule = (path) => cssModuleMap.get(path);
76
- const exportBuilder = (0, core_1.createExportBuilder)({ getCSSModule, matchesPattern, resolver });
77
- const semanticDiagnostics = [];
78
- for (const { cssModule } of parseResults) {
79
- const diagnostics = (0, core_1.checkCSSModule)(cssModule, config, exportBuilder, matchesPattern, resolver, getCSSModule);
80
- semanticDiagnostics.push(...diagnostics);
116
+ /**
117
+ * @throws {WriteDtsFileError}
118
+ */
119
+ async function emitAndReportDiagnostics() {
120
+ logger.clearScreen();
121
+ await project.emitDtsFiles();
122
+ const diagnostics = project.getDiagnostics();
123
+ if (diagnostics.length > 0) {
124
+ logger.logDiagnostics(diagnostics);
125
+ }
126
+ logger.logMessage(`Found ${diagnostics.length} error${diagnostics.length === 1 ? '' : 's'}. Watching for file changes.`, { time: true });
81
127
  }
82
- if (semanticDiagnostics.length > 0) {
83
- logger.logDiagnostics(semanticDiagnostics);
84
- // eslint-disable-next-line n/no-process-exit
85
- process.exit(1);
128
+ async function close() {
129
+ await Promise.all(fsWatchers.map(async (watcher) => watcher.close()));
86
130
  }
87
- if (args.clean) {
88
- await (0, promises_1.rm)(config.dtsOutDir, { recursive: true, force: true });
89
- }
90
- await Promise.all(parseResults.map(async (parseResult) => writeDtsByCSSModule(parseResult.cssModule, config, resolver, matchesPattern)));
131
+ return { project, close };
132
+ }
133
+ function promiseWithResolvers() {
134
+ let resolve;
135
+ let reject;
136
+ const promise = new Promise((res, rej) => {
137
+ resolve = res;
138
+ reject = rej;
139
+ });
140
+ return {
141
+ promise,
142
+ resolve: resolve,
143
+ reject: reject,
144
+ };
91
145
  }
92
146
  //# sourceMappingURL=runner.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"runner.js","sourceRoot":"","sources":["../src/runner.ts"],"names":[],"mappings":";;;;;AAkEA,wBAiEC;AAnID,+CAAgD;AAUhD,gDAS+B;AAC/B,4DAA4B;AAE5B,mDAA+C;AAC/C,yCAAoD;AAGpD;;GAEG;AACH,KAAK,UAAU,wBAAwB,CAAC,QAAgB,EAAE,MAAiB;IACzE,IAAI,IAAY,CAAC;IACjB,IAAI,CAAC;QACH,IAAI,GAAG,MAAM,IAAA,mBAAQ,EAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IAC3C,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,iCAAsB,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;IACpD,CAAC;IACD,OAAO,IAAA,qBAAc,EAAC,IAAI,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,CAAC,SAAS,EAAE,CAAC,CAAC;AACtF,CAAC;AAED;;GAEG;AACH,KAAK,UAAU,mBAAmB,CAChC,SAAoB,EACpB,EAAE,SAAS,EAAE,QAAQ,EAAE,mBAAmB,EAAE,YAAY,EAAE,sBAAsB,EAAa,EAC7F,QAAkB,EAClB,cAA8B;IAE9B,MAAM,GAAG,GAAG,IAAA,gBAAS,EACnB,SAAS,EACT,EAAE,QAAQ,EAAE,cAAc,EAAE,EAC5B,EAAE,YAAY,EAAE,sBAAsB,EAAE,WAAW,EAAE,KAAK,EAAE,CAC7D,CAAC;IACF,MAAM,IAAA,4BAAY,EAAC,GAAG,CAAC,IAAI,EAAE,SAAS,CA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ating `*.d.ts` files for `*.module.css`.",
4
- "version": "0.6.0",
4
+ "version": "0.7.0",
5
5
  "type": "commonjs",
6
6
  "sideEffects": false,
7
7
  "repository": {
@@ -38,7 +38,8 @@
38
38
  "dist"
39
39
  ],
40
40
  "dependencies": {
41
- "@css-modules-kit/core": "^0.6.0"
41
+ "@css-modules-kit/core": "^0.7.0",
42
+ "chokidar": "^4.0.3"
42
43
  },
43
44
  "peerDependencies": {
44
45
  "typescript": "^5.7.3"
package/src/cli.ts CHANGED
@@ -12,6 +12,7 @@ Options:
12
12
  --project, -p The path to its configuration file, or to a folder with a 'tsconfig.json'.
13
13
  --pretty Enable color and formatting in output to make errors easier to read.
14
14
  --clean Remove the output directory before generating files. [default: false]
15
+ --watch, -w Watch for changes and regenerate files. [default: false]
15
16
  `;
16
17
 
17
18
  export function printHelpText(): void {
@@ -30,6 +31,7 @@ export interface ParsedArgs {
30
31
  project: string;
31
32
  pretty: boolean | undefined;
32
33
  clean: boolean;
34
+ watch: boolean;
33
35
  }
34
36
 
35
37
  /**
@@ -46,6 +48,7 @@ export function parseCLIArgs(args: string[], cwd: string): ParsedArgs {
46
48
  project: { type: 'string', short: 'p', default: '.' },
47
49
  pretty: { type: 'boolean' },
48
50
  clean: { type: 'boolean', default: false },
51
+ watch: { type: 'boolean', short: 'w', default: false },
49
52
  },
50
53
  allowNegative: true,
51
54
  });
@@ -55,6 +58,7 @@ export function parseCLIArgs(args: string[], cwd: string): ParsedArgs {
55
58
  project: resolve(cwd, values.project),
56
59
  pretty: values.pretty,
57
60
  clean: values.clean,
61
+ watch: values.watch,
58
62
  };
59
63
  } catch (cause) {
60
64
  throw new ParseCLIArgsError(cause);
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { runCMK } from './runner.js';
1
+ export { runCMK, runCMKInWatchMode } from './runner.js';
2
2
  export { type Logger, createLogger } from './logger/logger.js';
3
3
  export { WriteDtsFileError, ReadCSSModuleFileError } from './error.js';
4
4
  export { parseCLIArgs, printHelpText, printVersion } from './cli.js';
@@ -1,5 +1,8 @@
1
1
  import ts from 'typescript';
2
2
 
3
+ const GRAY = '\u001b[90m';
4
+ const RESET = '\u001b[0m';
5
+
3
6
  export function formatDiagnostics(
4
7
  diagnostics: ts.Diagnostic[],
5
8
  host: ts.FormatDiagnosticsHost,
@@ -12,3 +15,12 @@ export function formatDiagnostics(
12
15
  }
13
16
  return result;
14
17
  }
18
+
19
+ export function formatTime(date: Date, pretty: boolean): string {
20
+ const text = date.toLocaleTimeString('en-US', { timeZone: 'UTC' });
21
+ if (pretty) {
22
+ return `[${GRAY}${text}${RESET}]`;
23
+ } else {
24
+ return `[${text}]`;
25
+ }
26
+ }
@@ -1,12 +1,14 @@
1
+ import { inspect } from 'node:util';
1
2
  import type { DiagnosticSourceFile } from '@css-modules-kit/core';
2
- import { convertDiagnostic, convertSystemError, type Diagnostic, type SystemError } from '@css-modules-kit/core';
3
+ import { convertDiagnostic, convertSystemError, type Diagnostic, SystemError } from '@css-modules-kit/core';
3
4
  import ts from 'typescript';
4
- import { formatDiagnostics } from './formatter.js';
5
+ import { formatDiagnostics, formatTime } from './formatter.js';
5
6
 
6
7
  export interface Logger {
7
8
  logDiagnostics(diagnostics: Diagnostic[]): void;
8
- logSystemError(error: SystemError): void;
9
- logMessage(message: string): void;
9
+ logError(error: unknown): void;
10
+ logMessage(message: string, options?: { time?: boolean }): void;
11
+ clearScreen(): void;
10
12
  }
11
13
 
12
14
  export function createLogger(cwd: string, pretty: boolean): Logger {
@@ -29,12 +31,24 @@ export function createLogger(cwd: string, pretty: boolean): Logger {
29
31
  );
30
32
  process.stderr.write(result);
31
33
  },
32
- logSystemError(error: SystemError): void {
33
- const result = formatDiagnostics([convertSystemError(error)], host, pretty);
34
- process.stderr.write(result);
34
+ logError(error: unknown): void {
35
+ // NOTE: SystemErrors are errors expected by the css-modules-kit specification and may occur within normal usage.
36
+ // These errors are formatted clearly and concisely. No stack trace is output.
37
+ //
38
+ // All other errors are unexpected errors. To assist in debugging when these errors occur, a stack trace is output.
39
+ if (error instanceof SystemError) {
40
+ const result = formatDiagnostics([convertSystemError(error)], host, pretty);
41
+ process.stderr.write(result);
42
+ } else {
43
+ process.stderr.write(`${inspect(error, { colors: pretty })}\n`);
44
+ }
45
+ },
46
+ logMessage(message: string, options?: { time?: boolean }): void {
47
+ const header = options?.time ? `${formatTime(new Date(), pretty)} ` : '';
48
+ process.stdout.write(`${header}${message}\n`);
35
49
  },
36
- logMessage(message: string): void {
37
- process.stdout.write(`${message}\n`);
50
+ clearScreen(): void {
51
+ process.stdout.write('\x1B[2J\x1B[3J\x1B[H');
38
52
  },
39
53
  };
40
54
  }
package/src/project.ts ADDED
@@ -0,0 +1,229 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import type { CSSModule, Diagnostic } from '@css-modules-kit/core';
3
+ import {
4
+ checkCSSModule,
5
+ type CMKConfig,
6
+ createExportBuilder,
7
+ createMatchesPattern,
8
+ createResolver,
9
+ generateDts,
10
+ getFileNamesByPattern,
11
+ parseCSSModule,
12
+ readConfigFile,
13
+ } from '@css-modules-kit/core';
14
+ import ts from 'typescript';
15
+ import { writeDtsFile } from './dts-writer.js';
16
+ import { ReadCSSModuleFileError } from './error.js';
17
+
18
+ interface ProjectArgs {
19
+ project: string;
20
+ }
21
+
22
+ export interface Project {
23
+ config: CMKConfig;
24
+ /** Whether the file matches the wildcard patterns in `include` / `exclude` options */
25
+ isWildcardMatchedFile(fileName: string): boolean;
26
+ /**
27
+ * Add a file to the project.
28
+ * @throws {ReadCSSModuleFileError}
29
+ */
30
+ addFile(fileName: string): void;
31
+ /**
32
+ * Update a file in the project.
33
+ * @throws {ReadCSSModuleFileError}
34
+ */
35
+ updateFile(fileName: string): void;
36
+ /** Remove a file from the project. */
37
+ removeFile(fileName: string): void;
38
+ /**
39
+ * Get all diagnostics.
40
+ * Including three types of diagnostics: project diagnostics, syntactic diagnostics, and semantic diagnostics.
41
+ * - Project diagnostics: For example, it includes configuration errors in tsconfig.json or warnings when there are no target files.
42
+ * - Syntactic diagnostics: Syntax errors in CSS Module files.
43
+ * - Semantic diagnostics: Errors related to the use of imports and exports in CSS module files.
44
+ * If there are any project diagnostics or syntactic diagnostics, semantic diagnostics will be skipped.
45
+ */
46
+ getDiagnostics(): Diagnostic[];
47
+ /**
48
+ * Emit .d.ts files for all project files.
49
+ * @throws {WriteDtsFileError}
50
+ */
51
+ emitDtsFiles(): Promise<void>;
52
+ }
53
+
54
+ /**
55
+ * Create a Project instance.
56
+ * Project is like a facade that calls core operations such as loading settings, parsing CSS Module files, and performing checks.
57
+ * The parsing and checking results are cached, and methods are also provided to clear the cache when files change.
58
+ * @throws {TsConfigFileNotFoundError}
59
+ * @throws {ReadCSSModuleFileError}
60
+ */
61
+ export function createProject(args: ProjectArgs): Project {
62
+ const config = readConfigFile(args.project);
63
+
64
+ const getCanonicalFileName = (fileName: string) =>
65
+ ts.sys.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase();
66
+ const moduleResolutionCache = ts.createModuleResolutionCache(
67
+ config.basePath,
68
+ getCanonicalFileName,
69
+ config.compilerOptions,
70
+ );
71
+ const resolver = createResolver(config.compilerOptions, moduleResolutionCache);
72
+ const matchesPattern = createMatchesPattern(config);
73
+
74
+ const cssModuleMap = new Map<string, CSSModule>();
75
+ const semanticDiagnosticsMap = new Map<string, Diagnostic[]>();
76
+ // Tracks whether .d.ts has been emitted after the last change
77
+ const emittedSet = new Set<string>();
78
+ const getCSSModule = (path: string) => cssModuleMap.get(path);
79
+ const exportBuilder = createExportBuilder({ getCSSModule, matchesPattern, resolver });
80
+
81
+ for (const fileName of getFileNamesByPattern(config)) {
82
+ // NOTE: Files may be deleted between executing `getFileNamesByPattern` and `tryParseCSSModule`.
83
+ // Therefore, `tryParseCSSModule` may return `undefined`.
84
+ const cssModule = tryParseCSSModule(fileName);
85
+ if (cssModule) cssModuleMap.set(fileName, cssModule);
86
+ }
87
+
88
+ /**
89
+ * @throws {ReadCSSModuleFileError}
90
+ */
91
+ function addFile(fileName: string) {
92
+ if (cssModuleMap.has(fileName)) return;
93
+
94
+ const cssModule = tryParseCSSModule(fileName);
95
+ if (!cssModule) return;
96
+ cssModuleMap.set(fileName, cssModule);
97
+
98
+ // TODO: Delete only the minimum amount of check stage cache
99
+ moduleResolutionCache.clear();
100
+ exportBuilder.clearCache();
101
+ semanticDiagnosticsMap.clear();
102
+ }
103
+
104
+ /**
105
+ * @throws {ReadCSSModuleFileError}
106
+ */
107
+ function updateFile(fileName: string) {
108
+ if (!cssModuleMap.has(fileName)) return;
109
+
110
+ const cssModule = tryParseCSSModule(fileName);
111
+ if (!cssModule) return;
112
+ cssModuleMap.set(fileName, cssModule);
113
+
114
+ // TODO: Delete only the minimum amount of check stage cache
115
+ exportBuilder.clearCache();
116
+ semanticDiagnosticsMap.clear();
117
+
118
+ emittedSet.delete(fileName);
119
+ }
120
+
121
+ function removeFile(fileName: string) {
122
+ if (!cssModuleMap.has(fileName)) return;
123
+
124
+ cssModuleMap.delete(fileName);
125
+
126
+ // TODO: Delete only the minimum amount of check stage cache
127
+ moduleResolutionCache.clear();
128
+ exportBuilder.clearCache();
129
+ semanticDiagnosticsMap.clear();
130
+
131
+ emittedSet.delete(fileName);
132
+ }
133
+
134
+ /**
135
+ * @throws {ReadCSSModuleFileError}
136
+ */
137
+ function tryParseCSSModule(fileName: string): CSSModule | undefined {
138
+ let text: string;
139
+ try {
140
+ // NOTE: We are not using asynchronous APIs for the following reasons:
141
+ //
142
+ // - Asynchronous APIs are slow in Node.js.
143
+ // - https://github.com/nodejs/performance/issues/151
144
+ // - Handling race conditions is cumbersome.
145
+ // - Using an asynchronous API makes `addFile` asynchronous too.
146
+ // - If `deleteFile` runs while `addFile` is executing, a race condition occurs.
147
+ // - Avoiding this requires something like a mutex. However, implementing that is cumbersome.
148
+ text = readFileSync(fileName, 'utf-8');
149
+ } catch (error) {
150
+ if (isNodeJSSystemError(error) && error.code === 'ENOENT') {
151
+ return undefined;
152
+ }
153
+ throw new ReadCSSModuleFileError(fileName, error);
154
+ }
155
+ return parseCSSModule(text, { fileName, includeSyntaxError: true, keyframes: config.keyframes });
156
+ }
157
+
158
+ function getDiagnostics(): Diagnostic[] {
159
+ const diagnostics: Diagnostic[] = [...getProjectDiagnostics(), ...getSyntacticDiagnostics()];
160
+ // If there are project or syntactic diagnostics, skip semantic diagnostics
161
+ if (diagnostics.length > 0) return diagnostics;
162
+ diagnostics.push(...getSemanticDiagnostics());
163
+ return diagnostics;
164
+ }
165
+
166
+ function getProjectDiagnostics() {
167
+ const diagnostics: Diagnostic[] = [];
168
+ diagnostics.push(...config.diagnostics);
169
+ if (cssModuleMap.size === 0) {
170
+ diagnostics.push({
171
+ category: 'error',
172
+ text: `The file specified in tsconfig.json not found.`,
173
+ });
174
+ }
175
+ return diagnostics;
176
+ }
177
+
178
+ function getSyntacticDiagnostics() {
179
+ return Array.from(cssModuleMap.values()).flatMap(({ diagnostics }) => diagnostics);
180
+ }
181
+
182
+ function getSemanticDiagnostics() {
183
+ const allDiagnostics: Diagnostic[] = [];
184
+ for (const cssModule of cssModuleMap.values()) {
185
+ let diagnostics = semanticDiagnosticsMap.get(cssModule.fileName);
186
+ if (!diagnostics) {
187
+ diagnostics = checkCSSModule(cssModule, config, exportBuilder, matchesPattern, resolver, getCSSModule);
188
+ semanticDiagnosticsMap.set(cssModule.fileName, diagnostics);
189
+ }
190
+ allDiagnostics.push(...diagnostics);
191
+ }
192
+ return allDiagnostics;
193
+ }
194
+
195
+ /**
196
+ * @throws {WriteDtsFileError}
197
+ */
198
+ async function emitDtsFiles(): Promise<void> {
199
+ const promises: Promise<void>[] = [];
200
+ for (const cssModule of cssModuleMap.values()) {
201
+ if (emittedSet.has(cssModule.fileName)) continue;
202
+ const dts = generateDts(cssModule, { resolver, matchesPattern }, { ...config, forTsPlugin: false });
203
+ promises.push(
204
+ writeDtsFile(dts.text, cssModule.fileName, {
205
+ outDir: config.dtsOutDir,
206
+ basePath: config.basePath,
207
+ arbitraryExtensions: config.arbitraryExtensions,
208
+ }).then(() => {
209
+ emittedSet.add(cssModule.fileName);
210
+ }),
211
+ );
212
+ }
213
+ await Promise.all(promises);
214
+ }
215
+
216
+ return {
217
+ config,
218
+ isWildcardMatchedFile: (fileName) => matchesPattern(fileName),
219
+ addFile,
220
+ updateFile,
221
+ removeFile,
222
+ getDiagnostics,
223
+ emitDtsFiles,
224
+ };
225
+ }
226
+
227
+ function isNodeJSSystemError(error: unknown): error is NodeJS.ErrnoException {
228
+ return typeof error === 'object' && error !== null && 'code' in error && typeof error.code === 'string';
229
+ }