Skip to content

Instantly share code, notes, and snippets.

@sachin-hg
Created January 11, 2023 08:43
Show Gist options
  • Save sachin-hg/3b1b33e0126840d195025a047ce7b759 to your computer and use it in GitHub Desktop.
Save sachin-hg/3b1b33e0126840d195025a047ce7b759 to your computer and use it in GitHub Desktop.
const regex = /(style|Style|Style.jsx|style.jsx)$/
function plugin1 (state) {
const { variables, modules, nodes, moduleVsImport } = state
return {
Program (path) {
state.programPath = path
},
ImportDeclaration (path) {
if (regex.test(path.node.source.value)) {
let added = false
let values = variables
for (let specifier of path.node.specifiers) {
if (
['ImportDefaultSpecifier', 'ImportSpecifier'].includes(
specifier.type
)
) {
values[specifier.local.name] = {
importedAs: specifier.imported
? specifier.imported.name
: 'default',
module: path.node.source.value
}
moduleVsImport[path.node.source.value] = specifier.local.name
added = true
}
}
if (added) {
modules.push(path.node.source.value)
}
}
},
CallExpression (path) {
if (path.node.callee.name === 'cx') {
let valid = true
for (let arg of path.node.arguments) {
if (
!(
arg.type === 'StringLiteral' ||
(arg.type === 'Identifier' && variables[arg.name])
)
) {
valid = false
break
}
}
if (valid) {
nodes.push(path)
}
}
}
}
}
function plugin2 (map) {
return {
ExportNamedDeclaration (path) {
const declaration = path.node.declaration || {}
if (declaration.type === 'VariableDeclaration') {
const dec = declaration.declarations[0]
if (dec && dec.init && dec.init.type === 'StringLiteral') {
map[dec.id.name] = dec.init.value
}
}
}
}
}
module.exports = { plugin2, plugin1 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment