Skip to content

Commit

Permalink
fix(shaker): process functions and classes definitions in default exp…
Browse files Browse the repository at this point in the history
…orts (fixes #1308) (#1343)

* feat(babel): overrideContext option

* fix(shaker): process functions and classes definitions in default exports
  • Loading branch information
Anber committed Sep 23, 2023
1 parent 9f9005c commit 6fb6eb6
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-moles-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@linaria/babel-preset': patch
---

The new option, 'overrideContext,' allows the extension of the module evaluation context.
8 changes: 8 additions & 0 deletions .changeset/plenty-mice-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@linaria/babel-preset': patch
'@linaria/shaker': patch
'@linaria/testkit': patch
'@linaria/utils': patch
---

Fixes error with $RefreshReg$ in webpack and resolves the issue with the shaker when default export is a function or class definition.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ exports[`shaker should keep side-effects from modules 1`] = `
export const a = 1;"
`;

exports[`shaker should not remove referenced export 1`] = `
"export default class Media {}
const _c = Media;
export const __linariaPreval = {};"
`;

exports[`shaker should process array patterns 1`] = `
"const [,, c] = array;
export { c };"
Expand Down
12 changes: 12 additions & 0 deletions packages/shaker/src/plugins/__tests__/shaker-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,4 +435,16 @@ describe('shaker', () => {
expect(code).toMatchSnapshot();
expect(metadata.imports.size).toBe(0);
});

it('should not remove referenced export', () => {
const { code, metadata } = keep(['__linariaPreval'])`
export default class Media {
}
const _c = Media;
export const __linariaPreval = {};
`;

expect(code).toMatchSnapshot();
expect(metadata.imports.size).toBe(0);
});
});
4 changes: 4 additions & 0 deletions packages/shaker/src/plugins/shaker-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ function getBindingForExport(exportPath: NodePath): Binding | undefined {
}
}

if (exportPath.isFunctionDeclaration() || exportPath.isClassDeclaration()) {
return exportPath.scope.getBinding(exportPath.node.id!.name);
}

return undefined;
}

Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/removeDangerousCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const isGlobal = (id: NodePath<Identifier>): boolean => {
};

const forbiddenGlobals = new Set([
'$RefreshReg$',
'XMLHttpRequest',
'clearImmediate',
'clearInterval',
Expand Down

0 comments on commit 6fb6eb6

Please sign in to comment.