happy-css-modules 3.0.0 → 3.1.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.
Files changed (143) hide show
  1. package/package.json +1 -1
  2. package/src/emitter/dts.test.ts +3 -3
  3. package/src/emitter/dts.ts +59 -37
  4. package/src/emitter/index.test.ts +3 -3
  5. package/src/integration-test/go-to-definition.test.ts +0 -35
  6. package/src/locator/index.test.ts +152 -158
  7. package/src/locator/index.ts +44 -37
  8. package/src/locator/postcss.test.ts +118 -76
  9. package/src/locator/postcss.ts +116 -73
  10. package/src/test-util/util.ts +24 -23
  11. package/src/transformer/less-transformer.test.ts +26 -24
  12. package/src/transformer/postcss-transformer.test.ts +5 -3
  13. package/src/transformer/scss-transformer.test.ts +39 -28
  14. package/src/util.ts +1 -1
  15. package/dist/cli.d.ts +0 -6
  16. package/dist/cli.js +0 -121
  17. package/dist/cli.js.map +0 -1
  18. package/dist/cli.test.d.ts +0 -1
  19. package/dist/cli.test.js +0 -66
  20. package/dist/cli.test.js.map +0 -1
  21. package/dist/config.d.ts +0 -1
  22. package/dist/config.js +0 -2
  23. package/dist/config.js.map +0 -1
  24. package/dist/emitter/dts.d.ts +0 -14
  25. package/dist/emitter/dts.js +0 -111
  26. package/dist/emitter/dts.js.map +0 -1
  27. package/dist/emitter/dts.test.d.ts +0 -1
  28. package/dist/emitter/dts.test.js +0 -205
  29. package/dist/emitter/dts.test.js.map +0 -1
  30. package/dist/emitter/file-system.d.ts +0 -6
  31. package/dist/emitter/file-system.js +0 -26
  32. package/dist/emitter/file-system.js.map +0 -1
  33. package/dist/emitter/file-system.test.d.ts +0 -1
  34. package/dist/emitter/file-system.test.js +0 -34
  35. package/dist/emitter/file-system.test.js.map +0 -1
  36. package/dist/emitter/index.d.ts +0 -34
  37. package/dist/emitter/index.js +0 -49
  38. package/dist/emitter/index.js.map +0 -1
  39. package/dist/emitter/index.test.d.ts +0 -1
  40. package/dist/emitter/index.test.js +0 -68
  41. package/dist/emitter/index.test.js.map +0 -1
  42. package/dist/emitter/source-map.d.ts +0 -8
  43. package/dist/emitter/source-map.js +0 -16
  44. package/dist/emitter/source-map.js.map +0 -1
  45. package/dist/emitter/source-map.test.d.ts +0 -1
  46. package/dist/emitter/source-map.test.js +0 -13
  47. package/dist/emitter/source-map.test.js.map +0 -1
  48. package/dist/index.d.ts +0 -5
  49. package/dist/index.js +0 -6
  50. package/dist/index.js.map +0 -1
  51. package/dist/integration-test/go-to-definition.test.d.ts +0 -1
  52. package/dist/integration-test/go-to-definition.test.js +0 -369
  53. package/dist/integration-test/go-to-definition.test.js.map +0 -1
  54. package/dist/library/source-map/index.d.ts +0 -8
  55. package/dist/library/source-map/index.js +0 -5
  56. package/dist/library/source-map/index.js.map +0 -1
  57. package/dist/locator/index.d.ts +0 -44
  58. package/dist/locator/index.js +0 -162
  59. package/dist/locator/index.js.map +0 -1
  60. package/dist/locator/index.test.d.ts +0 -1
  61. package/dist/locator/index.test.js +0 -395
  62. package/dist/locator/index.test.js.map +0 -1
  63. package/dist/locator/postcss.d.ts +0 -64
  64. package/dist/locator/postcss.js +0 -208
  65. package/dist/locator/postcss.js.map +0 -1
  66. package/dist/locator/postcss.test.d.ts +0 -1
  67. package/dist/locator/postcss.test.js +0 -242
  68. package/dist/locator/postcss.test.js.map +0 -1
  69. package/dist/logger.d.ts +0 -9
  70. package/dist/logger.js +0 -28
  71. package/dist/logger.js.map +0 -1
  72. package/dist/regression-test/issue-168.test.d.ts +0 -1
  73. package/dist/regression-test/issue-168.test.js +0 -30
  74. package/dist/regression-test/issue-168.test.js.map +0 -1
  75. package/dist/resolver/index.d.ts +0 -22
  76. package/dist/resolver/index.js +0 -44
  77. package/dist/resolver/index.js.map +0 -1
  78. package/dist/resolver/index.test.d.ts +0 -1
  79. package/dist/resolver/index.test.js +0 -16
  80. package/dist/resolver/index.test.js.map +0 -1
  81. package/dist/resolver/node-resolver.d.ts +0 -2
  82. package/dist/resolver/node-resolver.js +0 -6
  83. package/dist/resolver/node-resolver.js.map +0 -1
  84. package/dist/resolver/node-resolver.test.d.ts +0 -1
  85. package/dist/resolver/node-resolver.test.js +0 -25
  86. package/dist/resolver/node-resolver.test.js.map +0 -1
  87. package/dist/resolver/relative-resolver.d.ts +0 -2
  88. package/dist/resolver/relative-resolver.js +0 -5
  89. package/dist/resolver/relative-resolver.js.map +0 -1
  90. package/dist/resolver/relative-resolver.test.d.ts +0 -1
  91. package/dist/resolver/relative-resolver.test.js +0 -12
  92. package/dist/resolver/relative-resolver.test.js.map +0 -1
  93. package/dist/resolver/webpack-resolver.d.ts +0 -24
  94. package/dist/resolver/webpack-resolver.js +0 -91
  95. package/dist/resolver/webpack-resolver.js.map +0 -1
  96. package/dist/resolver/webpack-resolver.test.d.ts +0 -1
  97. package/dist/resolver/webpack-resolver.test.js +0 -89
  98. package/dist/resolver/webpack-resolver.test.js.map +0 -1
  99. package/dist/runner.d.ts +0 -76
  100. package/dist/runner.js +0 -120
  101. package/dist/runner.js.map +0 -1
  102. package/dist/runner.test.d.ts +0 -1
  103. package/dist/runner.test.js +0 -287
  104. package/dist/runner.test.js.map +0 -1
  105. package/dist/test-util/jest/resolver.cjs +0 -31
  106. package/dist/test-util/jest/resolver.cjs.map +0 -1
  107. package/dist/test-util/jest/resolver.d.cts +0 -16
  108. package/dist/test-util/tsserver.d.ts +0 -31
  109. package/dist/test-util/tsserver.js +0 -112
  110. package/dist/test-util/tsserver.js.map +0 -1
  111. package/dist/test-util/util.d.ts +0 -29
  112. package/dist/test-util/util.js +0 -84
  113. package/dist/test-util/util.js.map +0 -1
  114. package/dist/transformer/index.d.ts +0 -30
  115. package/dist/transformer/index.js +0 -25
  116. package/dist/transformer/index.js.map +0 -1
  117. package/dist/transformer/index.test.d.ts +0 -1
  118. package/dist/transformer/index.test.js +0 -66
  119. package/dist/transformer/index.test.js.map +0 -1
  120. package/dist/transformer/less-transformer.d.ts +0 -2
  121. package/dist/transformer/less-transformer.js +0 -45
  122. package/dist/transformer/less-transformer.js.map +0 -1
  123. package/dist/transformer/less-transformer.test.d.ts +0 -1
  124. package/dist/transformer/less-transformer.test.js +0 -132
  125. package/dist/transformer/less-transformer.test.js.map +0 -1
  126. package/dist/transformer/postcss-transformer.d.ts +0 -12
  127. package/dist/transformer/postcss-transformer.js +0 -32
  128. package/dist/transformer/postcss-transformer.js.map +0 -1
  129. package/dist/transformer/postcss-transformer.test.d.ts +0 -1
  130. package/dist/transformer/postcss-transformer.test.js +0 -176
  131. package/dist/transformer/postcss-transformer.test.js.map +0 -1
  132. package/dist/transformer/scss-transformer.d.ts +0 -2
  133. package/dist/transformer/scss-transformer.js +0 -40
  134. package/dist/transformer/scss-transformer.js.map +0 -1
  135. package/dist/transformer/scss-transformer.test.d.ts +0 -1
  136. package/dist/transformer/scss-transformer.test.js +0 -138
  137. package/dist/transformer/scss-transformer.test.js.map +0 -1
  138. package/dist/util.d.ts +0 -21
  139. package/dist/util.js +0 -72
  140. package/dist/util.js.map +0 -1
  141. package/dist/util.test.d.ts +0 -1
  142. package/dist/util.test.js +0 -74
  143. package/dist/util.test.js.map +0 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "happy-css-modules",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "Creates .d.ts files from CSS Modules .css files",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -211,9 +211,9 @@ describe('generateDtsContentWithSourceMap', () => {
211
211
  });
212
212
  test('emit other directory', async () => {
213
213
  createFixtures({
214
- '/test/1.css': `.a {}`,
214
+ '/test/src/1.css': `.a {}`,
215
215
  });
216
- const result = await locator.load(filePath);
216
+ const result = await locator.load(getFixturePath('/test/src/1.css'));
217
217
  const { dtsContent, sourceMap } = generateDtsContentWithSourceMap(
218
218
  getFixturePath('/test/src/1.css'),
219
219
  getFixturePath('/test/dist/1.css.d.ts'),
@@ -224,7 +224,7 @@ describe('generateDtsContentWithSourceMap', () => {
224
224
  );
225
225
  expect(dtsContent).toMatchInlineSnapshot(`
226
226
  "declare const styles:
227
- & Readonly<Pick<(typeof import("../1.css"))["default"], "a">>
227
+ & Readonly<{ "a": string }>
228
228
  ;
229
229
  export default styles;
230
230
  "
@@ -28,18 +28,26 @@ function dashesCamelCase(str: string): string {
28
28
  }
29
29
 
30
30
  function formatTokens(tokens: Token[], localsConvention: LocalsConvention): Token[] {
31
+ function formatToken(token: Token, formatter: (str: string) => string): Token {
32
+ if ('importedName' in token && typeof token.importedName === 'string') {
33
+ return { ...token, name: formatter(token.name), importedName: formatter(token.importedName) };
34
+ } else {
35
+ return { ...token, name: formatter(token.name) };
36
+ }
37
+ }
38
+
31
39
  const result: Token[] = [];
32
40
  for (const token of tokens) {
33
41
  if (localsConvention === 'camelCaseOnly') {
34
- result.push({ ...token, name: camelcase(token.name) });
42
+ result.push(formatToken(token, camelcase));
35
43
  } else if (localsConvention === 'camelCase') {
36
44
  result.push(token);
37
- result.push({ ...token, name: camelcase(token.name) });
45
+ result.push(formatToken(token, camelcase));
38
46
  } else if (localsConvention === 'dashesOnly') {
39
- result.push({ ...token, name: dashesCamelCase(token.name) });
47
+ result.push(formatToken(token, dashesCamelCase));
40
48
  } else if (localsConvention === 'dashes') {
41
49
  result.push(token);
42
- result.push({ ...token, name: dashesCamelCase(token.name) });
50
+ result.push(formatToken(token, dashesCamelCase));
43
51
  } else {
44
52
  result.push(token); // asIs
45
53
  }
@@ -62,41 +70,55 @@ function generateTokenDeclarations(
62
70
  // This is due to the sourcemap specification. Therefore, we output multiple type definitions
63
71
  // with the same name and assign a separate original position to each.
64
72
 
65
- for (let originalLocation of token.originalLocations) {
66
- if (originalLocation.filePath === undefined) {
67
- // If the original location is not specified, fallback to the source file.
68
- originalLocation = {
69
- filePath,
70
- start: { line: 1, column: 1 },
71
- end: { line: 1, column: 1 },
72
- };
73
- }
73
+ let originalLocation = token.originalLocation;
74
+ if (originalLocation.filePath === undefined) {
75
+ // If the original location is not specified, fallback to the source file.
76
+ originalLocation = {
77
+ filePath,
78
+ start: { line: 1, column: 1 },
79
+ end: { line: 1, column: 1 },
80
+ };
81
+ }
74
82
 
75
- result.push(
76
- originalLocation.filePath === filePath || isExternalFile(originalLocation.filePath)
77
- ? new SourceNode(null, null, null, [
78
- '& Readonly<{ ',
79
- new SourceNode(
80
- originalLocation.start.line ?? null,
81
- // The SourceNode's column is 0-based, but the originalLocation's column is 1-based.
82
- originalLocation.start.column - 1 ?? null,
83
- getRelativePath(sourceMapFilePath, originalLocation.filePath),
84
- `"${token.name}"`,
85
- token.name,
86
- ),
87
- ': string }>',
88
- ])
89
- : // Imported tokens in non-external files are typed by dynamic import.
90
- // See https://github.com/mizdra/happy-css-modules/issues/106.
91
- new SourceNode(null, null, null, [
92
- '& Readonly<Pick<(typeof import(',
93
- `"${getRelativePath(filePath, originalLocation.filePath)}"`,
94
- '))["default"], ',
83
+ result.push(
84
+ originalLocation.filePath === filePath || isExternalFile(originalLocation.filePath)
85
+ ? new SourceNode(null, null, null, [
86
+ '& Readonly<{ ',
87
+ new SourceNode(
88
+ originalLocation.start.line ?? null,
89
+ // The SourceNode's column is 0-based, but the originalLocation's column is 1-based.
90
+ originalLocation.start.column - 1 ?? null,
91
+ getRelativePath(sourceMapFilePath, originalLocation.filePath),
95
92
  `"${token.name}"`,
96
- '>>',
97
- ]),
98
- );
99
- }
93
+ token.name,
94
+ ),
95
+ ': string }>',
96
+ ])
97
+ : typeof token.importedName === 'string'
98
+ ? new SourceNode(null, null, null, [
99
+ `& Readonly<{ `,
100
+ new SourceNode(
101
+ originalLocation.start.line ?? null,
102
+ // The SourceNode's column is 0-based, but the originalLocation's column is 1-based.
103
+ originalLocation.start.column - 1 ?? null,
104
+ getRelativePath(sourceMapFilePath, originalLocation.filePath),
105
+ `"${token.name}"`,
106
+ token.name,
107
+ ),
108
+ `: (typeof import(`,
109
+ `"${getRelativePath(filePath, originalLocation.filePath)}"`,
110
+ `))["default"]["${token.importedName}"] }>`,
111
+ ])
112
+ : // Imported tokens in non-external files are typed by dynamic import.
113
+ // See https://github.com/mizdra/happy-css-modules/issues/106.
114
+ new SourceNode(null, null, null, [
115
+ '& Readonly<Pick<(typeof import(',
116
+ `"${getRelativePath(filePath, originalLocation.filePath)}"`,
117
+ '))["default"], ',
118
+ `"${token.name}"`,
119
+ '>>',
120
+ ]),
121
+ );
100
122
  }
101
123
  return result;
102
124
  }
@@ -25,7 +25,7 @@ test('isSubDirectoryFile', () => {
25
25
  describe('emitGeneratedFiles', () => {
26
26
  const defaultArgs = {
27
27
  filePath: getFixturePath('/test/1.css'),
28
- tokens: [fakeToken({ name: 'foo', originalLocations: [{ start: { line: 1, column: 1 } }] })],
28
+ tokens: [fakeToken({ name: 'foo', originalLocation: { start: { line: 1, column: 1 } } })],
29
29
  emitDeclarationMap: true,
30
30
  dtsFormatOptions: undefined,
31
31
  cwd: getFixturePath('/test'),
@@ -55,7 +55,7 @@ describe('emitGeneratedFiles', () => {
55
55
  expect(await exists(getFixturePath('/test/1.css.d.ts.map'))).toBeFalsy();
56
56
  });
57
57
  test('skips writing to disk if the generated files are the same', async () => {
58
- const tokens1 = [fakeToken({ name: 'foo', originalLocations: [{ start: { line: 1, column: 1 } }] })];
58
+ const tokens1 = [fakeToken({ name: 'foo', originalLocation: { start: { line: 1, column: 1 } } })];
59
59
  await emitGeneratedFiles({ ...defaultArgs, tokens: tokens1 });
60
60
  const mtimeForDts1 = (await stat(getFixturePath('/test/1.css.d.ts'))).mtime;
61
61
  const mtimeForSourceMap1 = (await stat(getFixturePath('/test/1.css.d.ts.map'))).mtime;
@@ -68,7 +68,7 @@ describe('emitGeneratedFiles', () => {
68
68
  expect(mtimeForSourceMap1).toEqual(mtimeForSourceMap2); // skipped
69
69
 
70
70
  await waitForAsyncTask(1); // so that mtime changes.
71
- const tokens2 = [fakeToken({ name: 'bar', originalLocations: [{ start: { line: 1, column: 1 } }] })];
71
+ const tokens2 = [fakeToken({ name: 'bar', originalLocation: { start: { line: 1, column: 1 } } })];
72
72
  await emitGeneratedFiles({ ...defaultArgs, tokens: tokens2 });
73
73
  const mtimeForDts3 = (await stat(getFixturePath('/test/1.css.d.ts'))).mtime;
74
74
  const mtimeForSourceMap3 = (await stat(getFixturePath('/test/1.css.d.ts.map'))).mtime;
@@ -41,10 +41,6 @@ test('basic', async () => {
41
41
  .local_class_name_3 {}
42
42
  }
43
43
  :local(.local_class_name_4) {}
44
- .composes_target {}
45
- .composes {
46
- composes: composes_target;
47
- }
48
44
  `,
49
45
  });
50
46
  await run({ ...defaultOptions });
@@ -65,8 +61,6 @@ test('basic', async () => {
65
61
  'local_class_name_2',
66
62
  'local_class_name_3',
67
63
  'local_class_name_4',
68
- 'composes_target',
69
- 'composes',
70
64
  ]);
71
65
  // FIXME: Fix an issue where the text at definition destination was incorrect.
72
66
  expect(results).toMatchInlineSnapshot(`
@@ -208,23 +202,6 @@ test('basic', async () => {
208
202
  ],
209
203
  "identifier": "local_class_name_4",
210
204
  },
211
- {
212
- "definitions": [
213
- {
214
- file: "<fixtures>/test/1.css",
215
- text: ".composes_target ",
216
- start: { line: 21, offset: 1 },
217
- end: { line: 21, offset: 18 },
218
- },
219
- ],
220
- "identifier": "composes_target",
221
- },
222
- {
223
- "definitions": [
224
- { file: "<fixtures>/test/1.css", text: ".composes ", start: { line: 22, offset: 1 }, end: { line: 22, offset: 11 } },
225
- ],
226
- "identifier": "composes",
227
- },
228
205
  ]
229
206
  `);
230
207
  const moduleDefinitions = await server.getModuleDefinitions(getFixturePath('/test/1.css'));
@@ -295,8 +272,6 @@ test('with transformer', async () => {
295
272
  // sass feature test (nesting)
296
273
  .nesting_1 { dummy: ''; }
297
274
  &_2 { dummy: ''; }
298
- composes: basic; // css module feature test (composes)
299
- composes: d from './4.scss'; // css module feature test (composes from other file)
300
275
  }
301
276
  `,
302
277
  '/test/2.scss': dedent`
@@ -306,9 +281,6 @@ test('with transformer', async () => {
306
281
  '/test/3.scss': dedent`
307
282
  .c { dummy: ''; }
308
283
  `,
309
- '/test/4.scss': dedent`
310
- .d { dummy: ''; }
311
- `,
312
284
  });
313
285
  await run({ ...defaultOptions });
314
286
  const results = await server.getMultipleIdentifierDefinitions(getFixturePath(`/test/1.scss`), [
@@ -319,7 +291,6 @@ test('with transformer', async () => {
319
291
  'b_1',
320
292
  'b_2',
321
293
  'c',
322
- 'd',
323
294
  ]);
324
295
  expect(results).toMatchInlineSnapshot(`
325
296
  [
@@ -364,12 +335,6 @@ test('with transformer', async () => {
364
335
  ],
365
336
  "identifier": "c",
366
337
  },
367
- {
368
- "definitions": [
369
- { file: "<fixtures>/test/4.scss", text: ".d ", start: { line: 1, offset: 1 }, end: { line: 1, offset: 4 } },
370
- ],
371
- "identifier": "d",
372
- },
373
338
  ]
374
339
  `);
375
340
  });