Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic hoisting. #414

Merged
merged 19 commits into from
Oct 18, 2017
20 changes: 18 additions & 2 deletions packages/babel-plugin-emotion/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ export function hashArray(arr) {

const staticStylis = new Stylis({ keyframe: false })

export function hoistPureArgs(path) {
const args = path.get('arguments')

if (args && Array.isArray(args)) {
args.forEach(arg => {
if (!arg.isTemplateLiteral() && arg.isPure()) {
console.log('hoisting')
arg.hoist()
}
})
}
}

export function replaceCssWithCallExpression(
path,
identifier,
Expand Down Expand Up @@ -53,7 +66,7 @@ export function replaceCssWithCallExpression(
src += addSourceMaps(path.node.quasi.loc.start, state)
}

return path.replaceWith(
path.replaceWith(
t.callExpression(
identifier,
new ASTObject(
Expand All @@ -63,6 +76,7 @@ export function replaceCssWithCallExpression(
).toExpressions()
)
)
return
} catch (e) {
if (path) {
throw path.buildCodeFrameError(e)
Expand Down Expand Up @@ -257,7 +271,7 @@ export default function(babel) {
} catch (e) {
throw path.buildCodeFrameError(e)
}

hoistPureArgs(path)
path[visited] = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will hoist every single CallExpression

},
TaggedTemplateExpression(path, state) {
Expand Down Expand Up @@ -330,6 +344,8 @@ export default function(babel) {
)
}
}

hoistPureArgs(path)
}
}
}
Expand Down