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

[legacy-framework] Update Emotion recipe to better leverage the installer package #2156

Merged
merged 1 commit into from
Mar 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 7 additions & 31 deletions recipes/emotion/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import {addImport, paths, RecipeBuilder} from "@blitzjs/installer"
import {
addBabelPlugin,
addImport,
findModuleExportsExpressions,
paths,
RecipeBuilder,
} from "@blitzjs/installer"
import j from "jscodeshift"
import {Collection} from "jscodeshift/src/Collection"
import {join} from "path"
Expand All @@ -22,36 +28,6 @@ function applyGlobalStyles(program: Collection<j.Program>) {
return program
}

function findModuleExportsExpressions(program: Collection<j.Program>) {
return program.find(j.AssignmentExpression).filter((path) => {
const {left, right} = path.value
return (
left.type === "MemberExpression" &&
left.object.type === "Identifier" &&
left.property.type === "Identifier" &&
left.property.name === "exports" &&
right.type === "ObjectExpression"
)
})
}

function addBabelPlugin(program: Collection<j.Program>) {
findModuleExportsExpressions(program).forEach((moduleExportsExpression) => {
j(moduleExportsExpression)
.find(j.ObjectProperty, {key: {name: "plugins"}})
.forEach((plugins) => {
const pluginsArrayExpression = plugins.node.value
if (pluginsArrayExpression.type !== "ArrayExpression") {
return
}

pluginsArrayExpression.elements.push(j.stringLiteral("@emotion"))
})
})

return program
}

function replaceBabelPreset(program: Collection<j.Program>) {
findModuleExportsExpressions(program).forEach((moduleExportsExpression) => {
j(moduleExportsExpression)
Expand Down