Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
mizdra committed May 19, 2024
1 parent e7fac8a commit 784e328
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
21 changes: 21 additions & 0 deletions packages/happy-css-modules/src/locator/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,27 @@ test('tracks other files when `composes` is present', async () => {
`);
});

test('tracks other files when `@value` is present', async () => {
createFixtures({
'/test/1.css': dedent`
@value a from './2.css';
@value b from '3.css';
@value c from '${getFixturePath('/test/4.css')}';
`,
'/test/2.css': dedent`
@value a: 1;
`,
'/test/3.css': dedent`
@value b: 2;
`,
'/test/4.css': dedent`
@value c: 3;
`,
});
const result = await locator.load(getFixturePath('/test/1.css'));
expect(result).toMatchInlineSnapshot(`{ dependencies: [], tokens: [] }`);
});

test('normalizes tokens', async () => {
createFixtures({
'/test/1.css': dedent`
Expand Down
36 changes: 28 additions & 8 deletions packages/happy-css-modules/src/locator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import { createDefaultTransformer, type Transformer } from '../transformer/index
import { unique, uniqueBy } from '../util.js';
import {
getOriginalLocationOfClassSelector,
getOriginalLocationOfAtValue,
generateLocalTokenNames,
parseAtImport,
type Location,
parseComposesDeclarationWithFromUrl,
collectNodes,
parseAtValue,
} from './postcss.js';

export { collectNodes, type Location } from './postcss.js';
Expand All @@ -27,6 +29,8 @@ function isIgnoredSpecifier(specifier: string): boolean {
export type Token = {
/** The token name. */
name: string;
/** The name of the imported token. */
importedName?: string;
/** The original locations of the token in the source file. */
originalLocations: Location[];
};
Expand Down Expand Up @@ -210,14 +214,30 @@ export class Locator {
}

for (const atValue of atValues) {
const name = atValue.params.slice(0, atValue.params.indexOf(':'));
if (!localTokenNames.includes(name)) continue;

tokens.push({
name,
// TODO: `getOriginalLocation` expects a `ClassName`.
originalLocations: [],
});
const parsedAtValue = parseAtValue(atValue);

if (parsedAtValue.type === 'valueDeclaration') {
tokens.push({
name: parsedAtValue.tokenName,
originalLocations: [getOriginalLocationOfAtValue(atValue)],
});
} else if (parsedAtValue.type === 'valueImportDeclaration') {
if (isIgnoredSpecifier(parsedAtValue.from)) continue;
// eslint-disable-next-line no-await-in-loop
const from = await this.resolver(parsedAtValue.from, { request: filePath });
// eslint-disable-next-line no-await-in-loop
const result = await this._load(from);
dependencies.push(from, ...result.dependencies);
for (const token of result.tokens) {
const matchedImport = parsedAtValue.imports.find((i) => i.importedTokenName === token.name);
if (!matchedImport) continue;
tokens.push({
name: matchedImport.localTokenName,
importedName: matchedImport.importedTokenName,
originalLocations: token.originalLocations,
});
}
}
}

const result: LoadResult = {
Expand Down

0 comments on commit 784e328

Please sign in to comment.