Skip to content

Commit

Permalink
feat: prevent traspiling components in node modules
Browse files Browse the repository at this point in the history
  • Loading branch information
jpudysz committed Dec 9, 2024
1 parent f35df97 commit 652285b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
9 changes: 8 additions & 1 deletion plugin/import.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = function addUnistylesImport(t, path, state) {
function addUnistylesImport(t, path, state) {
const localNames = Object.keys(state.reactNativeImports)
const names = Object.values(state.reactNativeImports)
const pairs = Object.entries(state.reactNativeImports)
Expand Down Expand Up @@ -59,3 +59,10 @@ module.exports = function addUnistylesImport(t, path, state) {
// cleanup
nodesToRemove.forEach(node => path.node.body.splice(path.node.body.indexOf(node), 1))
}

const isInsideNodeModules = state => state.file.opts.filename.includes('node_modules')

module.exports = {
isInsideNodeModules,
addUnistylesImport
}
22 changes: 21 additions & 1 deletion plugin/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const addUnistylesImport = require('./import')
const { addUnistylesImport, isInsideNodeModules } = require('./import')
const { getStyleMetadata, getStyleAttribute, styleAttributeToArray, handlePressable } = require('./style')
const { hasStringRef } = require('./ref')
const { isUnistylesStyleSheet, analyzeDependencies, addStyleSheetTag, getUnistyles } = require('./stylesheet')
Expand Down Expand Up @@ -32,6 +32,10 @@ module.exports = function ({ types: t }) {
visitor: {
Program: {
enter(path, state) {
if (isInsideNodeModules(state)) {
return
}

state.file.hasAnyUnistyle = false
state.file.hasUnistylesImport = false
state.file.shouldIncludePressable = false
Expand Down Expand Up @@ -67,6 +71,10 @@ module.exports = function ({ types: t }) {
}
},
VariableDeclaration(path, state) {
if (isInsideNodeModules(state)) {
return
}

path.node.declarations.forEach((declaration) => {
if (t.isArrowFunctionExpression(declaration.init) || t.isFunctionExpression(declaration.init)) {
const componentName = declaration.id
Expand All @@ -80,6 +88,10 @@ module.exports = function ({ types: t }) {
})
},
ImportDeclaration(path, state) {
if (isInsideNodeModules(state)) {
return
}

const importSource = path.node.source.value

if (importSource.includes('react-native-unistyles')) {
Expand Down Expand Up @@ -109,6 +121,10 @@ module.exports = function ({ types: t }) {
}
},
JSXElement(path, state) {
if (isInsideNodeModules(state)) {
return
}

if (state.file.isClassComponent) {
return
}
Expand Down Expand Up @@ -154,6 +170,10 @@ module.exports = function ({ types: t }) {
}
},
CallExpression(path, state) {
if (isInsideNodeModules(state)) {
return
}

if (isUsingVariants(t, path)) {
extractVariants(t, path, state)
}
Expand Down

0 comments on commit 652285b

Please sign in to comment.