Skip to content

Commit

Permalink
fix(shaker): named exports are removed (fixes #800) (#801)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anber authored Jul 18, 2021
1 parent 45e5de0 commit 3421930
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,28 @@ const Component =
custom.div\`\`;"
`;

exports[`hoists exports 1`] = `
"\\"use strict\\";
var _foo = require(\\"./foo\\");
Object.defineProperty(exports, \\"__esModule\\", {
value: true
});
Object.defineProperty(exports, \\"foo\\", {
enumerable: true,
get: function get() {
return _foo.foo;
}
});
Object.defineProperty(exports, \\"bar\\", {
enumerable: true,
get: function get() {
return _foo.bar;
}
});"
`;

exports[`preserves classNames 1`] = `
"import { styled } from '@linaria/react';
const Component =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,11 @@ Object.defineProperty(exports, \\"greenColor\\", {
exports[`shakes exports 1`] = `
"\\"use strict\\";
var _ = require(\\"\\\\u2026\\");
Object.defineProperty(exports, \\"__esModule\\", {
value: true
});
var _ = require(\\"\\\\u2026\\");
var a = _.whiteColor;
exports.__linariaPreval = [a];"
`;
Expand Down
28 changes: 28 additions & 0 deletions packages/babel/__tests__/evaluators/preeval.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,31 @@ it('replaces constant', async () => {

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

it('hoists exports', async () => {
const { code } = await transpile(
dedent`
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "foo", {
enumerable: true,
get: function get() {
return _foo.foo;
}
});
Object.defineProperty(exports, "bar", {
enumerable: true,
get: function get() {
return _foo.bar;
}
});
var _foo = require("./foo");
`
);

expect(code).toMatchSnapshot();
});
41 changes: 40 additions & 1 deletion packages/preeval/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* It works the same as main `babel/extract` preset, but do not evaluate lazy dependencies.
*/
import type { NodePath } from '@babel/traverse';
import type { Program } from '@babel/types';
import type { Program, Statement, VariableDeclaration } from '@babel/types';
import type { State, StrictOptions } from '@linaria/babel-preset';
import {
GenerateClassNames,
Expand All @@ -14,6 +14,30 @@ import {
} from '@linaria/babel-preset';
import { Core } from './babel';

const isHoistableExport = (
node: NodePath<Statement>
): node is NodePath<Statement> & NodePath<VariableDeclaration> => {
// Only `var` can be hoisted
if (!node.isVariableDeclaration({ kind: 'var' })) return false;

const declarations = node.get('declarations');

// Our target has only one declaration
if (!Array.isArray(declarations) || declarations.length !== 1) return false;

const init = declarations[0].get('init');
// It should be initialized with CallExpression…
if (!init || Array.isArray(init) || !init.isCallExpression()) return false;

const callee = init.get('callee');
// … which callee should be `required` …
if (Array.isArray(callee) || !callee.isIdentifier({ name: 'require' }))
return false;

// … which should be a global identifier
return !callee.scope.hasReference('require');
};

function index(babel: Core, options: StrictOptions) {
return {
visitor: {
Expand All @@ -35,6 +59,21 @@ function index(babel: Core, options: StrictOptions) {
JSXElement,
});
},
exit(path: NodePath<Program>) {
/* A really dirty hack that solves https://github.com/callstack/linaria/issues/800
* Sometimes babel inserts `require` after usages of required modules.
* It makes the shaker sad. As a temporary solution, we hoist requires.
* This hack should be deleted after transition `shaker` to @babel/traverse
*/
path
.get('body')
.filter(isHoistableExport)
.forEach((p) => {
const node = p.node;
p.remove();
path.unshiftContainer('body', node);
});
},
},
CallExpression: ProcessStyled,
TaggedTemplateExpression: ProcessCSS, // TaggedTemplateExpression is processed before CallExpression
Expand Down

0 comments on commit 3421930

Please sign in to comment.