Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix: protect structural children from removal, handle export * as ns correctly",
"packageName": "@griffel/transform-shaker",
"email": "olfedias@microsoft.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`does not break export * as ns when requesting the namespace export 1`] = `"export * as ns from './module';"`;

exports[`keeps ESM default import with batch export 1`] = `
"import blank from './blank.jpg';
export const __mkPreval = { backgroundImage: blank };"
Expand Down Expand Up @@ -141,6 +143,8 @@ const foo = bar();
export const __linariaPreval = [foo];"
`;

exports[`removes \`export * as ns\` if not requested 1`] = `"export * from './other';"`;

exports[`removes all 1`] = `
"
export const __linariaPreval = [];"
Expand Down
20 changes: 17 additions & 3 deletions packages/transform-shaker/src/langs/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,11 +639,25 @@ export const visitors = {
if (!this.graph.imports.has(source)) {
this.graph.imports.set(source, []);
}
this.graph.importTypes.set(source, 'reexport');

this.graph.addEdge(node, node.source);

// Create a sentinel node that represents this re-export
this.graph.reexports.push(node as unknown as IdentifierNode);
if (node.exported) {
// `export * as ns from "module"` — namespace re-export creates a single named export `ns`,
// it does NOT pass through individual exports like `export * from "module"` does.
const name = isIdentifier(node.exported)
? node.exported.name
: (node.exported as unknown as { value: string }).value;

this.graph.addExport(name, node);
this.graph.addEdge(node, node.exported);

this.graph.importTypes.set(source, 'wildcard');
} else {
// `export * from "module"` — true wildcard re-export, passes through all exports
this.graph.importTypes.set(source, 'reexport');
this.graph.reexports.push(node as unknown as IdentifierNode);
}

return 'ignore' as const;
},
Expand Down
17 changes: 17 additions & 0 deletions packages/transform-shaker/src/shaker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,20 @@ it('keeps identifiers used as computed property keys', () => {

expect(shaken).toMatchSnapshot();
});

it('removes `export * as ns` if not requested', () => {
const [shaken] = _shake(['unknownExport'])`
export * as ns from './module';
export * from './other';
`;

expect(shaken).toMatchSnapshot();
});

it('does not break export * as ns when requesting the namespace export', () => {
const [shaken] = _shake(['ns'])`
export * as ns from './module';
`;

expect(shaken).toMatchSnapshot();
});
10 changes: 9 additions & 1 deletion packages/transform-shaker/src/shaker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import MagicString from 'magic-string';
import { isNode, getVisitorKeys, debug } from './utils.js';
import build from './graphBuilder.js';

// Syntactically required children that must not be removed independently —
// removing them produces invalid code (e.g. `export { X }` without `from "module"`).
const STRUCTURAL_CHILDREN: Record<string, Set<string>> = {
ExportNamedDeclaration: new Set(['source']),
ExportAllDeclaration: new Set(['source', 'exported']),
ImportDeclaration: new Set(['source']),
};

function isStatementBody(nodeType: string, key: string): boolean {
return (
(nodeType === 'Program' && key === 'body') ||
Expand Down Expand Up @@ -130,7 +138,7 @@ function removeDeadCode(node: Node, alive: Set<Node>, s: MagicString, sourceCode
} else if (isNode(subNode)) {
if (alive.has(subNode)) {
removeDeadCode(subNode, alive, s, sourceCode);
} else {
} else if (!STRUCTURAL_CHILDREN[node.type]?.has(key)) {
s.remove(subNode.start, subNode.end);
}
}
Expand Down
Loading