Skip to content

Commit

Permalink
fix(shaker): shaker dropping exports (#1116)
Browse files Browse the repository at this point in the history
* fix shaker dropping exports

* add space

* add changeset

* fix snapshots for #1116

* docs: add mention to the changset that it fixes #1114

Co-authored-by: Tim Kutnick <tim.kutnick@airbnb.com>
Co-authored-by: Anton Evzhakov <anton@evz.name>
  • Loading branch information
3 people committed Nov 19, 2022
1 parent bc2532a commit e222434
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/eight-steaks-rhyme.md
@@ -0,0 +1,6 @@
---
'@linaria/shaker': patch
'@linaria/testkit': patch
---

Fix @linaria/shaker from removing exported renamed imports. Fixes #1114.
10 changes: 10 additions & 0 deletions packages/shaker/src/plugins/shaker-plugin.ts
Expand Up @@ -38,6 +38,10 @@ export interface IMetadata {
__linariaShaker: IShakerMetadata;
}

interface NodeWithName {
name: string;
}

export const hasShakerMetadata = (
metadata: object | undefined
): metadata is IMetadata =>
Expand Down Expand Up @@ -196,9 +200,15 @@ export default function shakerPlugin(

if (!onlyExports.includes('*')) {
const aliveExports = new Set<IExport | IReexport>();
const importNames = collected.imports.map(({ imported }) => imported);

exports.forEach((exp) => {
if (onlyExports.includes(exp.exported)) {
aliveExports.add(exp);
} else if (
importNames.includes((exp.local.node as NodeWithName).name || '')
) {
aliveExports.add(exp);
}
});

Expand Down
1 change: 1 addition & 0 deletions packages/testkit/src/__fixtures__/foo.js
@@ -1,2 +1,3 @@
export const foo1 = "foo1";
export const foo2 = "foo2";
export const foo3 = () => 'foo3';
18 changes: 18 additions & 0 deletions packages/testkit/src/__snapshots__/babel.test.ts.snap
Expand Up @@ -1819,6 +1819,24 @@ Dependencies: ./__fixtures__/complex-component
`;
exports[`strategy shaker should not drop exported vars of renamed imports 1`] = `
"import { foo3 } from \\"./__fixtures__/reexports\\";
export const bar3 = foo3;
export const square = \\"square_s13jq05\\";"
`;
exports[`strategy shaker should not drop exported vars of renamed imports 2`] = `
CSS:
.square_s13jq05 {
color: foo3;
}
Dependencies: ./__fixtures__/reexports
`;
exports[`strategy shaker should process \`css\` calls inside components 1`] = `
"import React from 'react';
export function Component() {
Expand Down
19 changes: 19 additions & 0 deletions packages/testkit/src/babel.test.ts
Expand Up @@ -2448,6 +2448,25 @@ describe('strategy shaker', () => {
expect(metadata).toMatchSnapshot();
});

it('should not drop exported vars of renamed imports', async () => {
const { code, metadata } = await transform(
dedent`
import { css } from "@linaria/core";
import { foo3 } from "./__fixtures__/reexports";
export const bar3 = foo3;
export const square = css\`
color: ${'${bar3("thing")}'};
\`;
`,
[evaluator]
);

expect(code).toMatchSnapshot();
expect(metadata).toMatchSnapshot();
});

it('should interpolate imported components', async () => {
const { code, metadata } = await transform(
dedent`
Expand Down

0 comments on commit e222434

Please sign in to comment.