Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regenerate.d.ts when dependent files are updated #264

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
45 changes: 45 additions & 0 deletions packages/happy-css-modules/src/directed-graph.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { DirectedGraph } from './directed-graph.js';

describe('DirectedGraph', () => {
it('getReachableNodes', () => {
const graph1 = new DirectedGraph();
graph1.addNodeAndEdges('a', ['c']);
graph1.addNodeAndEdges('b', ['c']);
graph1.addNodeAndEdges('c', []);
// eslint-disable-next-line @typescript-eslint/require-array-sort-compare
expect(graph1.getReachableNodes('c').sort()).toStrictEqual(['a', 'b']);

const graph2 = new DirectedGraph();
graph2.addNodeAndEdges('a', ['b']);
graph2.addNodeAndEdges('b', ['c']);
graph2.addNodeAndEdges('c', []);
// eslint-disable-next-line @typescript-eslint/require-array-sort-compare
expect(graph2.getReachableNodes('c').sort()).toStrictEqual(['a', 'b']);

const graph3 = new DirectedGraph();
graph3.addNodeAndEdges('a', []);
// eslint-disable-next-line @typescript-eslint/require-array-sort-compare
expect(graph3.getReachableNodes('a').sort()).toStrictEqual([]);

const graph4 = new DirectedGraph();
// eslint-disable-next-line @typescript-eslint/require-array-sort-compare
expect(graph4.getReachableNodes('a').sort()).toStrictEqual([]);

const graph5 = new DirectedGraph();
graph5.addNodeAndEdges('a', ['b']);
graph5.addNodeAndEdges('b', ['a']);
// eslint-disable-next-line @typescript-eslint/require-array-sort-compare
expect(graph5.getReachableNodes('b').sort()).toStrictEqual(['a']);
});
it('removeNode', () => {
const graph1 = new DirectedGraph();
graph1.addNodeAndEdges('a', ['c']);
graph1.addNodeAndEdges('b', ['c']);
graph1.addNodeAndEdges('c', []);
// eslint-disable-next-line @typescript-eslint/require-array-sort-compare
expect(graph1.getReachableNodes('c').sort()).toStrictEqual(['a', 'b']);
graph1.removeNode('a');
// eslint-disable-next-line @typescript-eslint/require-array-sort-compare
expect(graph1.getReachableNodes('c').sort()).toStrictEqual(['b']);
});
});
48 changes: 48 additions & 0 deletions packages/happy-css-modules/src/directed-graph.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
export class DirectedGraph {
readonly #graph: Map<string, Set<string>> = new Map();
readonly #reverseGraph: Map<string, Set<string>> = new Map();

addNodeAndEdges(node: string, edges: string[]) {
// Update graph
const edgesSet = this.#graph.get(node) ?? new Set();
for (const edge of edges) {
edgesSet.add(edge);
}
this.#graph.set(node, edgesSet);

// Update reverse graph
for (const edge of edges) {
const reverseEdgesSet = this.#reverseGraph.get(edge) ?? new Set();
reverseEdgesSet.add(node);
this.#reverseGraph.set(edge, reverseEdgesSet);
}
}
removeNode(node: string) {
// Update graph
const edgesSet = this.#graph.get(node);
if (edgesSet === undefined) return;

this.#graph.delete(node);

// Update reverse graph
for (const edge of edgesSet) {
const reverseSet = this.#reverseGraph.get(edge)!;
reverseSet.delete(node);
}
}
getReachableNodes(node: string): string[] {
const visited = new Set<string>();
const queue = [node];
while (queue.length > 0) {
const current = queue.pop()!;
visited.add(current);
for (const edge of this.#reverseGraph.get(current) ?? []) {
if (!visited.has(edge)) {
queue.push(edge);
}
}
}
visited.delete(node);
return [...visited];
}
}
15 changes: 15 additions & 0 deletions packages/happy-css-modules/src/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,3 +351,18 @@ test('support symlink', async () => {
`"{"version":3,"sources":["./1.css"],"names":["a"],"mappings":"AAAA;AAAA,E,aAAAA,G,WAAA;AAAA;AAAA","file":"1.css.d.ts","sourceRoot":""}"`,
);
});

test('regenerate .d.ts if dependent files are updated', async () => {
createFixtures({
'/test/1.scss': '@import "./2.scss"',
'/test/2.scss': '.a { dummy: ""; }',
});
await run({ ...defaultOptions, cache: true });
const content1 = await readFile(getFixturePath('/test/1.scss.d.ts'), 'utf8');

// Update 2.scss
await writeFile(getFixturePath('/test/2.scss'), '.b { dummy: ""; }');
await run({ ...defaultOptions, cache: true });
const content2 = await readFile(getFixturePath('/test/1.scss.d.ts'), 'utf8');
expect(content1).not.toEqual(content2);
});
Loading