@css-modules-kit/core 0.0.1

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.
Files changed (99) hide show
  1. package/LICENSE +21 -0
  2. package/dist/checker.d.ts +7 -0
  3. package/dist/checker.d.ts.map +1 -0
  4. package/dist/checker.js +46 -0
  5. package/dist/checker.js.map +1 -0
  6. package/dist/config.d.ts +101 -0
  7. package/dist/config.d.ts.map +1 -0
  8. package/dist/config.js +200 -0
  9. package/dist/config.js.map +1 -0
  10. package/dist/diagnostic.d.ts +34 -0
  11. package/dist/diagnostic.d.ts.map +1 -0
  12. package/dist/diagnostic.js +3 -0
  13. package/dist/diagnostic.js.map +1 -0
  14. package/dist/dts-creator.d.ts +56 -0
  15. package/dist/dts-creator.d.ts.map +1 -0
  16. package/dist/dts-creator.js +95 -0
  17. package/dist/dts-creator.js.map +1 -0
  18. package/dist/error.d.ts +8 -0
  19. package/dist/error.d.ts.map +1 -0
  20. package/dist/error.js +18 -0
  21. package/dist/error.js.map +1 -0
  22. package/dist/export-builder.d.ts +23 -0
  23. package/dist/export-builder.d.ts.map +1 -0
  24. package/dist/export-builder.js +34 -0
  25. package/dist/export-builder.js.map +1 -0
  26. package/dist/external-file.d.ts +2 -0
  27. package/dist/external-file.d.ts.map +1 -0
  28. package/dist/external-file.js +3 -0
  29. package/dist/external-file.js.map +1 -0
  30. package/dist/file.d.ts +28 -0
  31. package/dist/file.d.ts.map +1 -0
  32. package/dist/file.js +73 -0
  33. package/dist/file.js.map +1 -0
  34. package/dist/index.d.ts +14 -0
  35. package/dist/index.d.ts.map +1 -0
  36. package/dist/index.js +39 -0
  37. package/dist/index.js.map +1 -0
  38. package/dist/language-plugin.d.ts +23 -0
  39. package/dist/language-plugin.d.ts.map +1 -0
  40. package/dist/language-plugin.js +73 -0
  41. package/dist/language-plugin.js.map +1 -0
  42. package/dist/parser/at-import-parser.d.ts +14 -0
  43. package/dist/parser/at-import-parser.d.ts.map +1 -0
  44. package/dist/parser/at-import-parser.js +47 -0
  45. package/dist/parser/at-import-parser.js.map +1 -0
  46. package/dist/parser/at-value-parser.d.ts +44 -0
  47. package/dist/parser/at-value-parser.d.ts.map +1 -0
  48. package/dist/parser/at-value-parser.js +147 -0
  49. package/dist/parser/at-value-parser.js.map +1 -0
  50. package/dist/parser/css-module-parser.d.ts +93 -0
  51. package/dist/parser/css-module-parser.d.ts.map +1 -0
  52. package/dist/parser/css-module-parser.js +96 -0
  53. package/dist/parser/css-module-parser.js.map +1 -0
  54. package/dist/parser/diagnostic.d.ts +34 -0
  55. package/dist/parser/diagnostic.d.ts.map +1 -0
  56. package/dist/parser/diagnostic.js +3 -0
  57. package/dist/parser/diagnostic.js.map +1 -0
  58. package/dist/parser/location.d.ts +34 -0
  59. package/dist/parser/location.d.ts.map +1 -0
  60. package/dist/parser/location.js +9 -0
  61. package/dist/parser/location.js.map +1 -0
  62. package/dist/parser/rule-parser.d.ts +19 -0
  63. package/dist/parser/rule-parser.d.ts.map +1 -0
  64. package/dist/parser/rule-parser.js +122 -0
  65. package/dist/parser/rule-parser.js.map +1 -0
  66. package/dist/path.d.ts +11 -0
  67. package/dist/path.d.ts.map +1 -0
  68. package/dist/path.js +43 -0
  69. package/dist/path.js.map +1 -0
  70. package/dist/resolver.d.ts +13 -0
  71. package/dist/resolver.d.ts.map +1 -0
  72. package/dist/resolver.js +50 -0
  73. package/dist/resolver.js.map +1 -0
  74. package/dist/semantic-builder.d.ts +23 -0
  75. package/dist/semantic-builder.d.ts.map +1 -0
  76. package/dist/semantic-builder.js +33 -0
  77. package/dist/semantic-builder.js.map +1 -0
  78. package/dist/util.d.ts +2 -0
  79. package/dist/util.d.ts.map +1 -0
  80. package/dist/util.js +7 -0
  81. package/dist/util.js.map +1 -0
  82. package/package.json +54 -0
  83. package/src/checker.ts +66 -0
  84. package/src/config.ts +285 -0
  85. package/src/diagnostic.ts +37 -0
  86. package/src/dts-creator.ts +128 -0
  87. package/src/error.ts +13 -0
  88. package/src/export-builder.ts +51 -0
  89. package/src/file.ts +83 -0
  90. package/src/index.ts +38 -0
  91. package/src/parser/at-import-parser.ts +47 -0
  92. package/src/parser/at-value-parser.ts +177 -0
  93. package/src/parser/css-module-parser.ts +193 -0
  94. package/src/parser/location.ts +43 -0
  95. package/src/parser/rule-parser.ts +140 -0
  96. package/src/path.ts +40 -0
  97. package/src/resolver.ts +57 -0
  98. package/src/typing/typescript.d.ts +19 -0
  99. package/src/util.ts +3 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 mizdra
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,7 @@
1
+ import type { SemanticDiagnostic } from './diagnostic.js';
2
+ import type { ExportBuilder } from './export-builder.js';
3
+ import type { MatchesPattern } from './file.js';
4
+ import type { CSSModule } from './parser/css-module-parser.js';
5
+ import type { Resolver } from './resolver.js';
6
+ export declare function checkCSSModule(cssModule: CSSModule, exportBuilder: ExportBuilder, matchesPattern: MatchesPattern, resolver: Resolver, getCSSModule: (path: string) => CSSModule | undefined): SemanticDiagnostic[];
7
+ //# sourceMappingURL=checker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checker.d.ts","sourceRoot":"","sources":["../src/checker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAC1D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,KAAK,EAGV,SAAS,EAEV,MAAM,+BAA+B,CAAC;AACvC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAE9C,wBAAgB,cAAc,CAC5B,SAAS,EAAE,SAAS,EACpB,aAAa,EAAE,aAAa,EAC5B,cAAc,EAAE,cAAc,EAC9B,QAAQ,EAAE,QAAQ,EAClB,YAAY,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,SAAS,GAAG,SAAS,GACpD,kBAAkB,EAAE,CAsBtB"}
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.checkCSSModule = checkCSSModule;
4
+ function checkCSSModule(cssModule, exportBuilder, matchesPattern, resolver, getCSSModule) {
5
+ const diagnostics = [];
6
+ for (const tokenImporter of cssModule.tokenImporters) {
7
+ const from = resolver(tokenImporter.from, { request: cssModule.fileName });
8
+ if (!from || !matchesPattern(from))
9
+ continue;
10
+ const imported = getCSSModule(from);
11
+ if (!imported) {
12
+ diagnostics.push(createCannotImportModuleDiagnostic(cssModule, tokenImporter));
13
+ continue;
14
+ }
15
+ if (tokenImporter.type === 'value') {
16
+ const exportRecord = exportBuilder.build(imported);
17
+ for (const value of tokenImporter.values) {
18
+ if (!exportRecord.allTokens.includes(value.name)) {
19
+ diagnostics.push(createModuleHasNoExportedTokenDiagnostic(cssModule, tokenImporter, value));
20
+ }
21
+ }
22
+ }
23
+ }
24
+ return diagnostics;
25
+ }
26
+ function createCannotImportModuleDiagnostic(cssModule, tokenImporter) {
27
+ return {
28
+ type: 'semantic',
29
+ text: `Cannot import module '${tokenImporter.from}'`,
30
+ category: 'error',
31
+ fileName: cssModule.fileName,
32
+ start: { line: tokenImporter.fromLoc.start.line, column: tokenImporter.fromLoc.start.column },
33
+ end: { line: tokenImporter.fromLoc.end.line, column: tokenImporter.fromLoc.end.column },
34
+ };
35
+ }
36
+ function createModuleHasNoExportedTokenDiagnostic(cssModule, tokenImporter, value) {
37
+ return {
38
+ type: 'semantic',
39
+ text: `Module '${tokenImporter.from}' has no exported token '${value.name}'.`,
40
+ category: 'error',
41
+ fileName: cssModule.fileName,
42
+ start: { line: value.loc.start.line, column: value.loc.start.column },
43
+ end: { line: value.loc.end.line, column: value.loc.end.column },
44
+ };
45
+ }
46
+ //# sourceMappingURL=checker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checker.js","sourceRoot":"","sources":["../src/checker.ts"],"names":[],"mappings":";;AAWA,wCA4BC;AA5BD,SAAgB,cAAc,CAC5B,SAAoB,EACpB,aAA4B,EAC5B,cAA8B,EAC9B,QAAkB,EAClB,YAAqD;IAErD,MAAM,WAAW,GAAyB,EAAE,CAAC;IAE7C,KAAK,MAAM,aAAa,IAAI,SAAS,CAAC,cAAc,EAAE,CAAC;QACrD,MAAM,IAAI,GAAG,QAAQ,CAAC,aAAa,CAAC,IAAI,EAAE,EAAE,OAAO,EAAE,SAAS,CAAC,QAAQ,EAAE,CAAC,CAAC;QAC3E,IAAI,CAAC,IAAI,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC;YAAE,SAAS;QAC7C,MAAM,QAAQ,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC;QACpC,IAAI,CAAC,QAAQ,EAAE,CAAC;YACd,WAAW,CAAC,IAAI,CAAC,kCAAkC,CAAC,SAAS,EAAE,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AAC,QAAQ;QAC5B,KAAK,EAAE,EAAE,IAAI,EAAE,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,EAAE;QACrE,GAAG,EAAE,EAAE,IAAI,EAAE,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,EAAE;KAChE,CAAC;AACJ,CAAC"}
@@ -0,0 +1,101 @@
1
+ import type { SemanticDiagnostic } from './diagnostic.js';
2
+ type RemoveUndefined<T> = {
3
+ [K in keyof T]: Exclude<T[K], undefined>;
4
+ };
5
+ /**
6
+ * The config used by css-modules-kit.
7
+ * This is normalized. Paths are resolved from relative to absolute, and default values are set for missing options.
8
+ */
9
+ export interface CMKConfig {
10
+ includes: string[];
11
+ excludes: string[];
12
+ paths: Record<string, string[]>;
13
+ dtsOutDir: string;
14
+ arbitraryExtensions: boolean;
15
+ dashedIdents: boolean;
16
+ /**
17
+ * A root directory to resolve relative path entries in the config file to.
18
+ * This is an absolute path.
19
+ *
20
+ * This is also used to determine the output directory of the d.ts file.
21
+ * For example, let’s say you have some input files:
22
+ * ```
23
+ * /app
24
+ * ├── tsconfig.json
25
+ * ├── src
26
+ * │ ├── a.module.css
27
+ * │ ├── b.module.css
28
+ * │ ├── sub
29
+ * │ │ ├── c.module.css
30
+ * ```
31
+ *
32
+ * If you set `basePath` to `/app/src`, the output files will be:
33
+ * ```
34
+ * /app
35
+ * ├── dist
36
+ * │ ├── a.module.css.d.ts
37
+ * │ ├── b.module.css.d.ts
38
+ * │ ├── sub
39
+ * │ │ ├── c.module.css.d.ts
40
+ * ```
41
+ *
42
+ * If you set `basePath` to `/app`, the output files will be:
43
+ * ```
44
+ * /app
45
+ * ├── dist
46
+ * │ ├── src
47
+ * │ │ ├── a.module.css.d.ts
48
+ * │ │ ├── b.module.css.d.ts
49
+ * │ │ ├── sub
50
+ * │ │ │ ├── c.module.css.d.ts
51
+ * ```
52
+ */
53
+ basePath: string;
54
+ configFileName: string;
55
+ /** The diagnostics that occurred while reading the config file. */
56
+ diagnostics: SemanticDiagnostic[];
57
+ }
58
+ /**
59
+ * The config loaded from `tsconfig.json`.
60
+ * This is unnormalized. Paths are relative, and some options may be omitted.
61
+ */
62
+ interface UnnormalizedCMKConfig {
63
+ includes: string[] | undefined;
64
+ excludes: string[] | undefined;
65
+ paths: Record<string, string[]> | undefined;
66
+ dtsOutDir: string | undefined;
67
+ arbitraryExtensions: boolean | undefined;
68
+ }
69
+ /**
70
+ * The validated data of `ts.ParsedCommandLine['raw']`.
71
+ */
72
+ interface ParsedRawData {
73
+ config: UnnormalizedCMKConfig;
74
+ diagnostics: SemanticDiagnostic[];
75
+ }
76
+ declare function parseRawData(raw: unknown, configFileName: string): ParsedRawData;
77
+ export { parseRawData as parseRawDataForTest };
78
+ /**
79
+ * Reads the `tsconfig.json` file and returns the normalized config.
80
+ * Even if the `tsconfig.json` file contains syntax or semantic errors,
81
+ * this function attempts to parse as much as possible and still returns a valid config.
82
+ *
83
+ * @param project The absolute path to the project directory or the path to `tsconfig.json`.
84
+ * @throws {TsConfigFileNotFoundError}
85
+ */
86
+ export declare function readConfigFile(project: string): CMKConfig;
87
+ export declare function findTsConfigFile(project: string): string | undefined;
88
+ /**
89
+ * @throws {TsConfigFileNotFoundError}
90
+ */
91
+ export declare function readTsConfigFile(project: string): {
92
+ configFileName: string;
93
+ } & ParsedRawData;
94
+ /**
95
+ * Normalize the config. Resolve relative paths to absolute paths, and set default values for missing options.
96
+ * @param basePath A root directory to resolve relative path entries in the config file to.
97
+ */
98
+ export declare function normalizeConfig(config: UnnormalizedCMKConfig, basePath: string): RemoveUndefined<UnnormalizedCMKConfig> & {
99
+ dashedIdents: boolean;
100
+ };
101
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources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;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,CASzD;AAED,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAOpE;AAcD;;GAEG;AAEH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG;IACjD,cAAc,EAAE,MAAM,CAAC;CACxB,GAAG,aAAa,CA+ChB;AAWD;;;GAGG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,qBAAqB,EAC7B,QAAQ,EAAE,MAAM,GACf,eAAe,CAAC,qBAAqB,CAAC,GAAG;IAAE,YAAY,EAAE,OAAO,CAAA;CAAE,CAWpE"}
package/dist/config.js ADDED
@@ -0,0 +1,200 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.parseRawDataForTest = parseRawData;
7
+ exports.readConfigFile = readConfigFile;
8
+ exports.findTsConfigFile = findTsConfigFile;
9
+ exports.readTsConfigFile = readTsConfigFile;
10
+ exports.normalizeConfig = normalizeConfig;
11
+ const typescript_1 = __importDefault(require("typescript"));
12
+ const error_js_1 = require("./error.js");
13
+ const path_js_1 = require("./path.js");
14
+ // https://github.com/microsoft/TypeScript/blob/caf1aee269d1660b4d2a8b555c2d602c97cb28d7/src/compiler/commandLineParser.ts#L3006
15
+ const DEFAULT_INCLUDE_SPEC = '**/*';
16
+ // eslint-disable-next-line complexity
17
+ function parseRawData(raw, configFileName) {
18
+ const result = {
19
+ config: {
20
+ includes: undefined,
21
+ excludes: undefined,
22
+ paths: undefined,
23
+ dtsOutDir: undefined,
24
+ arbitraryExtensions: undefined,
25
+ },
26
+ diagnostics: [],
27
+ };
28
+ if (typeof raw !== 'object' || raw === null)
29
+ return result;
30
+ // `tsConfigSourceFile.configFileSpecs` contains `includes` and `excludes`. However, it is an internal API.
31
+ // So we collect `includes` and `excludes` from `parsedCommandLine.raw` without the internal API.
32
+ if ('include' in raw) {
33
+ if (Array.isArray(raw.include)) {
34
+ const includes = raw.include.filter((i) => typeof i === 'string');
35
+ result.config.includes = includes;
36
+ }
37
+ // MEMO: The errors for this option are reported by `tsc` or `tsserver`, so we don't need to report.
38
+ }
39
+ if ('exclude' in raw) {
40
+ if (Array.isArray(raw.exclude)) {
41
+ const excludes = raw.exclude.filter((e) => typeof e === 'string');
42
+ result.config.excludes = excludes;
43
+ }
44
+ // MEMO: The errors for this option are reported by `tsc` or `tsserver`, so we don't need to report.
45
+ }
46
+ if ('compilerOptions' in raw && typeof raw.compilerOptions === 'object' && raw.compilerOptions !== null) {
47
+ if ('paths' in raw.compilerOptions) {
48
+ if (typeof raw.compilerOptions.paths === 'object' && raw.compilerOptions.paths !== null) {
49
+ const paths = {};
50
+ for (const [key, value] of Object.entries(raw.compilerOptions.paths)) {
51
+ if (Array.isArray(value)) {
52
+ const resolvedValue = value.filter((v) => typeof v === 'string');
53
+ paths[key] = resolvedValue;
54
+ }
55
+ }
56
+ result.config.paths = paths;
57
+ }
58
+ // MEMO: The errors for this option are reported by `tsc` or `tsserver`, so we don't need to report.
59
+ }
60
+ }
61
+ if ('cmkOptions' in raw && typeof raw.cmkOptions === 'object' && raw.cmkOptions !== null) {
62
+ if ('dtsOutDir' in raw.cmkOptions) {
63
+ if (typeof raw.cmkOptions.dtsOutDir === 'string') {
64
+ result.config.dtsOutDir = raw.cmkOptions.dtsOutDir;
65
+ }
66
+ else {
67
+ result.diagnostics.push({
68
+ type: 'semantic',
69
+ category: 'error',
70
+ text: '`dtsOutDir` must be a string.',
71
+ fileName: configFileName,
72
+ });
73
+ }
74
+ }
75
+ if ('arbitraryExtensions' in raw.cmkOptions) {
76
+ if (typeof raw.cmkOptions.arbitraryExtensions === 'boolean') {
77
+ result.config.arbitraryExtensions = raw.cmkOptions.arbitraryExtensions;
78
+ }
79
+ else {
80
+ result.diagnostics.push({
81
+ type: 'semantic',
82
+ category: 'error',
83
+ text: '`arbitraryExtensions` must be a boolean.',
84
+ fileName: configFileName,
85
+ });
86
+ }
87
+ }
88
+ }
89
+ return result;
90
+ }
91
+ /**
92
+ * Reads the `tsconfig.json` file and returns the normalized config.
93
+ * Even if the `tsconfig.json` file contains syntax or semantic errors,
94
+ * this function attempts to parse as much as possible and still returns a valid config.
95
+ *
96
+ * @param project The absolute path to the project directory or the path to `tsconfig.json`.
97
+ * @throws {TsConfigFileNotFoundError}
98
+ */
99
+ function readConfigFile(project) {
100
+ const { configFileName, config, diagnostics } = readTsConfigFile(project);
101
+ const basePath = (0, path_js_1.dirname)(configFileName);
102
+ return {
103
+ ...normalizeConfig(config, basePath),
104
+ basePath,
105
+ configFileName,
106
+ diagnostics,
107
+ };
108
+ }
109
+ function findTsConfigFile(project) {
110
+ const configFile = typescript_1.default.sys.directoryExists(project) ?
111
+ typescript_1.default.findConfigFile(project, typescript_1.default.sys.fileExists.bind(typescript_1.default.sys), 'tsconfig.json')
112
+ : typescript_1.default.findConfigFile((0, path_js_1.dirname)(project), typescript_1.default.sys.fileExists.bind(typescript_1.default.sys), (0, path_js_1.basename)(project));
113
+ if (!configFile)
114
+ return undefined;
115
+ return (0, path_js_1.resolve)(configFile);
116
+ }
117
+ function mergeParsedRawData(base, overrides) {
118
+ const result = { config: { ...base.config }, diagnostics: [...base.diagnostics] };
119
+ if (overrides.config.includes !== undefined)
120
+ result.config.includes = overrides.config.includes;
121
+ if (overrides.config.excludes !== undefined)
122
+ result.config.excludes = overrides.config.excludes;
123
+ if (overrides.config.paths !== undefined)
124
+ result.config.paths = overrides.config.paths;
125
+ if (overrides.config.dtsOutDir !== undefined)
126
+ result.config.dtsOutDir = overrides.config.dtsOutDir;
127
+ if (overrides.config.arbitraryExtensions !== undefined)
128
+ result.config.arbitraryExtensions = overrides.config.arbitraryExtensions;
129
+ result.diagnostics.push(...overrides.diagnostics);
130
+ return result;
131
+ }
132
+ /**
133
+ * @throws {TsConfigFileNotFoundError}
134
+ */
135
+ // TODO: Allow `extends` options to inherit `cmkOptions`
136
+ function readTsConfigFile(project) {
137
+ const configFileName = findTsConfigFile(project);
138
+ if (!configFileName)
139
+ throw new error_js_1.TsConfigFileNotFoundError();
140
+ const tsConfigSourceFile = typescript_1.default.readJsonConfigFile(configFileName, typescript_1.default.sys.readFile.bind(typescript_1.default.sys));
141
+ // MEMO: `tsConfigSourceFile.parseDiagnostics` (Internal API) contains a syntax error for `tsconfig.json`.
142
+ // However, it is ignored so that ts-plugin will work even if `tsconfig.json` is somewhat broken.
143
+ // Also, this error is reported to the user by `tsc` or `tsserver`.
144
+ // We discard it since there is no need to report it from css-modules-kit.
145
+ const parsedCommandLine = typescript_1.default.parseJsonSourceFileConfigFileContent(tsConfigSourceFile, typescript_1.default.sys, (0, path_js_1.dirname)(configFileName), undefined, configFileName, undefined, [
146
+ {
147
+ extension: 'css',
148
+ isMixedContent: false,
149
+ scriptKind: typescript_1.default.ScriptKind.Deferred,
150
+ },
151
+ ]);
152
+ // Read options from `parsedCommandLine.raw`
153
+ let parsedRawData = parseRawData(parsedCommandLine.raw, configFileName);
154
+ // The options read from `parsedCommandLine.raw` do not inherit values from the file specified in `extends`.
155
+ // So here we read the options from those files and merge them into `parsedRawData`.
156
+ if (tsConfigSourceFile.extendedSourceFiles) {
157
+ for (const extendedSourceFile of tsConfigSourceFile.extendedSourceFiles) {
158
+ let base;
159
+ try {
160
+ base = readTsConfigFile(extendedSourceFile);
161
+ }
162
+ catch (error) {
163
+ if (error instanceof error_js_1.TsConfigFileNotFoundError)
164
+ continue;
165
+ throw error;
166
+ }
167
+ parsedRawData = mergeParsedRawData(base, parsedRawData);
168
+ }
169
+ }
170
+ return {
171
+ configFileName,
172
+ ...parsedRawData,
173
+ };
174
+ }
175
+ function resolvePaths(paths, cwd) {
176
+ if (paths === undefined)
177
+ return {};
178
+ const resolvedPaths = {};
179
+ for (const [key, value] of Object.entries(paths)) {
180
+ resolvedPaths[key] = value.map((path) => (0, path_js_1.join)(cwd, path));
181
+ }
182
+ return resolvedPaths;
183
+ }
184
+ /**
185
+ * Normalize the config. Resolve relative paths to absolute paths, and set default values for missing options.
186
+ * @param basePath A root directory to resolve relative path entries in the config file to.
187
+ */
188
+ function normalizeConfig(config, basePath) {
189
+ return {
190
+ // If `include` is not specified, fallback to the default include spec.
191
+ // ref: https://github.com/microsoft/TypeScript/blob/caf1aee269d1660b4d2a8b555c2d602c97cb28d7/src/compiler/commandLineParser.ts#L3102
192
+ includes: (config.includes ?? [DEFAULT_INCLUDE_SPEC]).map((i) => (0, path_js_1.join)(basePath, i)),
193
+ excludes: (config.excludes ?? []).map((e) => (0, path_js_1.join)(basePath, e)),
194
+ paths: resolvePaths(config.paths, basePath),
195
+ dtsOutDir: (0, path_js_1.join)(basePath, config.dtsOutDir ?? 'generated'),
196
+ arbitraryExtensions: config.arbitraryExtensions ?? false,
197
+ dashedIdents: false, // TODO: Support dashedIdents
198
+ };
199
+ }
200
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":";;;;;AAgKyB,2CAAmB;AAU5C,wCASC;AAED,4CAOC;AAkBD,4CAiDC;AAeD,0CAcC;AA5RD,4DAA4B;AAE5B,yCAAuD;AACvD,uCAA6D;AAE7D,gIAAgI;AAChI,MAAM,oBAAoB,GAAG,MAAM,CAAC;AAgFpC,sCAAsC;AACtC,SAAS,YAAY,CAAC,GAAY,EAAE,cAAsB;IACxD,MAAM,MAAM,GAAkB;QAC5B,MAAM,EAAE;YACN,QAAQ,EAAE,SAAS;YACnB,QAAQ,EAAE,SAAS;YACnB,KAAK,EAAE,SAAS;YAChB,SAAS,EAAE,SAAS;YACpB,mBAAmB,EAAE,SAAS;SAC/B;QACD,WAAW,EAAE,EAAE;KAChB,CAAC;IACF,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,IAAI;QAAE,OA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vB,CAAC;AAED;;;GAGG;AACH,SAAgB,eAAe,CAC7B,MAA6B,EAC7B,QAAgB;IAEhB,OAAO;QACL,uEAAuE;QACvE,qIAAqI;QACrI,QAAQ,EAAE,CAAC,MAAM,CAAC,QAAQ,IAAI,CAAC,oBAAoB,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAA,cAAI,EAAC,QAAQ,EAAE,CAAC,CAAC,CAAC;QACnF,QAAQ,EAAE,CAAC,MAAM,CAAC,QAAQ,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAA,cAAI,EAAC,QAAQ,EAAE,CAAC,CAAC,CAAC;QAC/D,KAAK,EAAE,YAAY,CAAC,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC;QAC3C,SAAS,EAAE,IAAA,cAAI,EAAC,QAAQ,EAAE,MAAM,CAAC,SAAS,IAAI,WAAW,CAAC;QAC1D,mBAAmB,EAAE,MAAM,CAAC,mBAAmB,IAAI,KAAK;QACxD,YAAY,EAAE,KAAK,EAAE,6BAA6B;KACnD,CAAC;AACJ,CAAC"}
@@ -0,0 +1,34 @@
1
+ export type DiagnosticCategory = 'error' | 'warning';
2
+ export interface DiagnosticPosition {
3
+ /** The line number in the source file. It is 1-based. */
4
+ line: number;
5
+ /** The column number in the source file. It is 1-based. */
6
+ column: number;
7
+ }
8
+ export type Diagnostic = SemanticDiagnostic | SyntacticDiagnostic;
9
+ interface DiagnosticBase {
10
+ /** Text of diagnostic message. */
11
+ text: string;
12
+ /** The category of the diagnostic message. */
13
+ category: DiagnosticCategory;
14
+ }
15
+ export interface SemanticDiagnostic extends DiagnosticBase {
16
+ type: 'semantic';
17
+ /** The filename of the file in which the diagnostic occurred */
18
+ fileName?: string;
19
+ /** Starting file position at which text applies. It is inclusive. */
20
+ start?: DiagnosticPosition;
21
+ /** The last file position at which the text applies. It is exclusive. */
22
+ end?: DiagnosticPosition;
23
+ }
24
+ export interface SyntacticDiagnostic extends DiagnosticBase {
25
+ type: 'syntactic';
26
+ /** The filename of the file in which the diagnostic occurred */
27
+ fileName: string;
28
+ /** Starting file position at which text applies. It is inclusive. */
29
+ start: DiagnosticPosition;
30
+ /** The last file position at which the text applies. It is exclusive. */
31
+ end?: DiagnosticPosition;
32
+ }
33
+ export {};
34
+ //# sourceMappingURL=diagnostic.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"diagnostic.d.ts","sourceRoot":"","sources":["../src/diagnostic.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,kBAAkB,GAAG,OAAO,GAAG,SAAS,CAAC;AAErD,MAAM,WAAW,kBAAkB;IACjC,yDAAyD;IACzD,IAAI,EAAE,MAAM,CAAC;IACb,2DAA2D;IAC3D,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,MAAM,UAAU,GAAG,kBAAkB,GAAG,mBAAmB,CAAC;AAElE,UAAU,cAAc;IACtB,kCAAkC;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,8CAA8C;IAC9C,QAAQ,EAAE,kBAAkB,CAAC;CAC9B;AAED,MAAM,WAAW,kBAAmB,SAAQ,cAAc;IACxD,IAAI,EAAE,UAAU,CAAC;IACjB,gEAAgE;IAChE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,qEAAqE;IACrE,KAAK,CAAC,EAAE,kBAAkB,CAAC;IAC3B,0EAA0E;IAC1E,GAAG,CAAC,EAAE,kBAAkB,CAAC;CAC1B;AAED,MAAM,WAAW,mBAAoB,SAAQ,cAAc;IACzD,IAAI,EAAE,WAAW,CAAC;IAClB,gEAAgE;IAChE,QAAQ,EAAE,MAAM,CAAC;IACjB,qEAAqE;IACrE,KAAK,EAAE,kBAAkB,CAAC;IAC1B,0EAA0E;IAC1E,GAAG,CAAC,EAAE,kBAAkB,CAAC;CAC1B"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=diagnostic.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"diagnostic.js","sourceRoot":"","sources":["../src/diagnostic.ts"],"names":[],"mappings":""}
@@ -0,0 +1,56 @@
1
+ import type { MatchesPattern } from './file.js';
2
+ import type { CSSModule } from './parser/css-module-parser.js';
3
+ import type { Resolver } from './resolver.js';
4
+ export declare const STYLES_EXPORT_NAME = "styles";
5
+ export interface CreateDtsOptions {
6
+ resolver: Resolver;
7
+ matchesPattern: MatchesPattern;
8
+ }
9
+ interface CodeMapping {
10
+ /** The source offsets of the tokens in the *.module.css file. */
11
+ sourceOffsets: number[];
12
+ /** The lengths of the tokens in the *.module.css file. */
13
+ lengths: number[];
14
+ /** The generated offsets of the tokens in the *.d.ts file. */
15
+ generatedOffsets: number[];
16
+ }
17
+ /** The map linking the two codes in *.d.ts */
18
+ interface LinkedCodeMapping extends CodeMapping {
19
+ /** The offset of the first code to be linked. */
20
+ sourceOffsets: number[];
21
+ /** The length of the first code to be linked. */
22
+ lengths: number[];
23
+ /** The offset of the second code to be linked. */
24
+ generatedOffsets: number[];
25
+ /** The length of the second code to be linked. */
26
+ generatedLengths: number[];
27
+ }
28
+ /**
29
+ * Create a d.ts file from a CSS module file.
30
+ * @example
31
+ * If the CSS module file is:
32
+ * ```css
33
+ * @import './a.module.css';
34
+ * @value local1: string;
35
+ * @value imported1, imported2 as aliasedImported2 from './b.module.css';
36
+ * .local2 { color: red }
37
+ * ```
38
+ * The d.ts file would be:
39
+ * ```ts
40
+ * const styles = {
41
+ * local1: '' as readonly string,
42
+ * local2: '' as readonly string,
43
+ * ...(await import('./a.module.css')).default,
44
+ * imported1: (await import('./b.module.css')).default.imported1,
45
+ * aliasedImported2: (await import('./b.module.css')).default.imported2,
46
+ * };
47
+ * export default styles;
48
+ * ```
49
+ */
50
+ export declare function createDts({ fileName, localTokens, tokenImporters: _tokenImporters }: CSSModule, options: CreateDtsOptions): {
51
+ text: string;
52
+ mapping: CodeMapping;
53
+ linkedCodeMapping: LinkedCodeMapping;
54
+ };
55
+ export {};
56
+ //# sourceMappingURL=dts-creator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dts-creator.d.ts","sourceRoot":"","sources":["../src/dts-creator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,+BAA+B,CAAC;AAC/D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAE9C,eAAO,MAAM,kBAAkB,WAAW,CAAC;AAE3C,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,EAAE,QAAQ,CAAC;IACnB,cAAc,EAAE,cAAc,CAAC;CAChC;AAED,UAAU,WAAW;IACnB,iEAAiE;IACjE,aAAa,EAAE,MAAM,EAAE,CAAC;IACxB,0DAA0D;IAC1D,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,8DAA8D;IAC9D,gBAAgB,EAAE,MAAM,EAAE,CAAC;CAC5B;AAED,8CAA8C;AAE9C,UAAU,iBAAkB,SAAQ,WAAW;IAC7C,iDAAiD;IACjD,aAAa,EAAE,MAAM,EAAE,CAAC;IACxB,iDAAiD;IACjD,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,kDAAkD;IAClD,gBAAgB,EAAE,MAAM,EAAE,CAAC;IAC3B,kDAAkD;IAClD,gBAAgB,EAAE,MAAM,EAAE,CAAC;CAC5B;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,SAAS,CACvB,EAAE,QAAQ,EAAE,WAAW,EAAE,cAAc,EAAE,eAAe,EAAE,EAAE,SAAS,EACrE,OAAO,EAAE,gBAAgB,GACxB;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,WAAW,CAAC;IAAC,iBAAiB,EAAE,iBAAiB,CAAA;CAAE,CAqE9E"}
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.STYLES_EXPORT_NAME = void 0;
4
+ exports.createDts = createDts;
5
+ exports.STYLES_EXPORT_NAME = 'styles';
6
+ /**
7
+ * Create a d.ts file from a CSS module file.
8
+ * @example
9
+ * If the CSS module file is:
10
+ * ```css
11
+ * @import './a.module.css';
12
+ * @value local1: string;
13
+ * @value imported1, imported2 as aliasedImported2 from './b.module.css';
14
+ * .local2 { color: red }
15
+ * ```
16
+ * The d.ts file would be:
17
+ * ```ts
18
+ * const styles = {
19
+ * local1: '' as readonly string,
20
+ * local2: '' as readonly string,
21
+ * ...(await import('./a.module.css')).default,
22
+ * imported1: (await import('./b.module.css')).default.imported1,
23
+ * aliasedImported2: (await import('./b.module.css')).default.imported2,
24
+ * };
25
+ * export default styles;
26
+ * ```
27
+ */
28
+ function createDts({ fileName, localTokens, tokenImporters: _tokenImporters }, options) {
29
+ const mapping = { sourceOffsets: [], lengths: [], generatedOffsets: [] };
30
+ const linkedCodeMapping = {
31
+ sourceOffsets: [],
32
+ lengths: [],
33
+ generatedOffsets: [],
34
+ generatedLengths: [],
35
+ };
36
+ // Filter external files
37
+ const tokenImporters = _tokenImporters.filter((tokenImporter) => {
38
+ const resolved = options.resolver(tokenImporter.from, { request: fileName });
39
+ return resolved !== undefined && options.matchesPattern(resolved);
40
+ });
41
+ // If the CSS module file has no tokens, return an .d.ts file with an empty object.
42
+ if (localTokens.length === 0 && tokenImporters.length === 0) {
43
+ return {
44
+ text: `declare const ${exports.STYLES_EXPORT_NAME} = {};\nexport default ${exports.STYLES_EXPORT_NAME};\n`,
45
+ mapping,
46
+ linkedCodeMapping,
47
+ };
48
+ }
49
+ let text = `declare const ${exports.STYLES_EXPORT_NAME} = {\n`;
50
+ for (const token of localTokens) {
51
+ text += ` `;
52
+ mapping.sourceOffsets.push(token.loc.start.offset);
53
+ mapping.generatedOffsets.push(text.length);
54
+ mapping.lengths.push(token.name.length);
55
+ text += `${token.name}: '' as readonly string,\n`;
56
+ }
57
+ for (const tokenImporter of tokenImporters) {
58
+ if (tokenImporter.type === 'import') {
59
+ text += ` ...(await import(`;
60
+ mapping.sourceOffsets.push(tokenImporter.fromLoc.start.offset - 1);
61
+ mapping.lengths.push(tokenImporter.from.length + 2);
62
+ mapping.generatedOffsets.push(text.length);
63
+ text += `'${tokenImporter.from}')).default,\n`;
64
+ }
65
+ else {
66
+ // eslint-disable-next-line no-loop-func
67
+ tokenImporter.values.forEach((value, i) => {
68
+ const localName = value.localName ?? value.name;
69
+ const localLoc = value.localLoc ?? value.loc;
70
+ text += ` `;
71
+ mapping.sourceOffsets.push(localLoc.start.offset);
72
+ mapping.lengths.push(localName.length);
73
+ mapping.generatedOffsets.push(text.length);
74
+ linkedCodeMapping.sourceOffsets.push(text.length);
75
+ linkedCodeMapping.lengths.push(localName.length);
76
+ text += `${localName}: (await import(`;
77
+ if (i === 0) {
78
+ mapping.sourceOffsets.push(tokenImporter.fromLoc.start.offset - 1);
79
+ mapping.lengths.push(tokenImporter.from.length + 2);
80
+ mapping.generatedOffsets.push(text.length);
81
+ }
82
+ text += `'${tokenImporter.from}')).default.`;
83
+ mapping.sourceOffsets.push(value.loc.start.offset);
84
+ mapping.lengths.push(value.name.length);
85
+ mapping.generatedOffsets.push(text.length);
86
+ linkedCodeMapping.generatedOffsets.push(text.length);
87
+ linkedCodeMapping.generatedLengths.push(value.name.length);
88
+ text += `${value.name},\n`;
89
+ });
90
+ }
91
+ }
92
+ text += `};\nexport default ${exports.STYLES_EXPORT_NAME};\n`;
93
+ return { text, mapping, linkedCodeMapping };
94
+ }
95
+ //# sourceMappingURL=dts-creator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dts-creator.js","sourceRoot":"","sources":["../src/dts-creator.ts"],"names":[],"mappings":";;;AAuDA,8BAwEC;AA3HY,QAAA,kBAAkB,GAAG,QAAQ,CAAC;AA6B3C;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,SAAgB,SAAS,CACvB,EAAE,QAAQ,EAAE,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I,IAAI,GAAG,SAAS,kBAAkB,CAAC;gBACvC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;oBACZ,OAAO,CAAC,aAAa,CAAC,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;oBACnE,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;oBACpD,OAAO,CAAC,gBAAgB,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;gBAC7C,CAAC;gBACD,IAAI,IAAI,IAAI,aAAa,CAAC,IAAI,cAAc,CAAC;gBAC7C,OAAO,CAAC,aAAa,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;gBACnD,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;gBACxC,OAAO,CAAC,gBAAgB,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;gBAC3C,iBAAiB,CAAC,gBAAgB,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;gBACrD,iBAAiB,CAAC,gBAAgB,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;gBAC3D,IAAI,IAAI,GAAG,KAAK,CAAC,IAAI,KAAK,CAAC;YAC7B,CAAC,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IACD,IAAI,IAAI,sBAAsB,0BAAkB,KAAK,CAAC;IACtD,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,iBAAiB,EAAE,CAAC;AAC9C,CAAC"}
@@ -0,0 +1,8 @@
1
+ export declare class SystemError extends Error {
2
+ code: string;
3
+ constructor(code: string, message: string, cause?: unknown);
4
+ }
5
+ export declare class TsConfigFileNotFoundError extends SystemError {
6
+ constructor();
7
+ }
8
+ //# sourceMappingURL=error.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":"AAAA,qBAAa,WAAY,SAAQ,KAAK;IACpC,IAAI,EAAE,MAAM,CAAC;gBACD,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO;CAI3D;AAED,qBAAa,yBAA0B,SAAQ,WAAW;;CAIzD"}
package/dist/error.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TsConfigFileNotFoundError = exports.SystemError = void 0;
4
+ class SystemError extends Error {
5
+ code;
6
+ constructor(code, message, cause) {
7
+ super(message, { cause });
8
+ this.code = code;
9
+ }
10
+ }
11
+ exports.SystemError = SystemError;
12
+ class TsConfigFileNotFoundError extends SystemError {
13
+ constructor() {
14
+ super('TS_CONFIG_NOT_FOUND', 'No tsconfig.json found.');
15
+ }
16
+ }
17
+ exports.TsConfigFileNotFoundError = TsConfigFileNotFoundError;
18
+ //# sourceMappingURL=error.js.map