Skip to content

Commit

Permalink
fix(shaker): improve dependency resolution for wildcard exports (#826,
Browse files Browse the repository at this point in the history
…fixes #816)
  • Loading branch information
Anber authored Aug 31, 2021
1 parent c1fdb7c commit 5aac3eb
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/babel/__fixtures__/reexports/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const foo = 'foo';
export const bar = 'bar';
3 changes: 3 additions & 0 deletions packages/babel/__fixtures__/reexports/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as fooStyles from './constants';

export { fooStyles };
1 change: 1 addition & 0 deletions packages/babel/__fixtures__/reexports/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './foo';
2 changes: 1 addition & 1 deletion packages/babel/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ class Module {
} else {
// For JS/TS files, evaluate the module
// The module will be transpiled using provided transform
m.evaluate(code, only.includes('*') ? null : only);
m.evaluate(code, only.includes('*') ? ['*'] : only);
}
} else {
// For non JS/JSON requires, just export the id
Expand Down
22 changes: 22 additions & 0 deletions packages/shaker/__tests__/__snapshots__/shaker.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,28 @@ Dependencies: @babel/runtime/helpers/interopRequireDefault, @babel/runtime/helpe
`;
exports[`shaker evaluates chain of reexports 1`] = `
"import { styled } from '@linaria/react';
import { fooStyles } from \\"@linaria/babel-preset/__fixtures__/reexports\\";
const value = fooStyles.foo;
export const H1 = /*#__PURE__*/styled(\\"h1\\")({
name: \\"H1\\",
class: \\"H1_h1t92lw9\\"
});"
`;
exports[`shaker evaluates chain of reexports 2`] = `
CSS:
.H1_h1t92lw9 {
color: foo;
}
Dependencies: @linaria/babel-preset/__fixtures__/reexports
`;
exports[`shaker evaluates complex styles with functions and nested selectors 1`] = `
"import { css } from '@linaria/core';
export const bareIconClass = \\"bareIconClass_b1t92lw9\\";
Expand Down
18 changes: 18 additions & 0 deletions packages/shaker/__tests__/shaker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,23 @@ describe('shaker', () => {
expect(code).toMatchSnapshot();
expect(metadata).toMatchSnapshot();
});

it('evaluates chain of reexports', async () => {
const { code, metadata } = await transpile(
dedent`
import { styled } from '@linaria/react';
import { fooStyles } from "@linaria/babel-preset/__fixtures__/reexports";
const value = fooStyles.foo;
export const H1 = styled.h1\`
color: ${'${value}'};
\`
`
);

expect(code).toMatchSnapshot();
expect(metadata).toMatchSnapshot();
});
});
});
6 changes: 6 additions & 0 deletions packages/shaker/src/shaker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ function shakeNode<TNode extends Node>(node: TNode, alive: Set<Node>): Node {
return Object.keys(changes).length ? { ...node, ...changes } : node;
}

const isDefined = <T>(i: T | undefined): i is T => i !== undefined;

/*
* Gets AST and a list of nodes for evaluation
* Removes unrelated “dead” code.
Expand All @@ -73,6 +75,10 @@ export default function shake(
const reexports: string[] = [];
let deps = (exports ?? [])
.map((token) => {
if (token === '*') {
return depsGraph.getLeaves(null).filter(isDefined);
}

const node = depsGraph.getLeaf(token);
if (node) return [node];
// We have some unknown token. Do we have `export * from …` in that file?
Expand Down

0 comments on commit 5aac3eb

Please sign in to comment.