Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 3 additions & 36 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@
"tailwindcss": "^3.2.2"
},
"dependencies": {
"lodash.castarray": "^4.4.0",
"lodash.isplainobject": "^4.0.6",
"lodash.merge": "^4.6.2",
"postcss-selector-parser": "6.0.10"
},
"jest": {
Expand Down
9 changes: 2 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const plugin = require('tailwindcss/plugin')
const merge = require('lodash.merge')
const castArray = require('lodash.castarray')
const styles = require('./styles')
const { commonTrailingPseudos } = require('./utils')
const { commonTrailingPseudos, isObject, isPlainObject, merge, castArray } = require('./utils')

const computed = {
// Reserved for future "magic properties", for example:
Expand All @@ -25,10 +23,6 @@ function inWhere(selector, { className, modifier, prefix }) {
return `:where(${selectorPrefix}${selector}):not(:where([class~="${prefixedNot}"],[class~="${prefixedNot}"] *))`
}

function isObject(value) {
return typeof value === 'object' && value !== null
}

function configToCss(config = {}, { target, className, modifier, prefix }) {
function updateSelector(k, v) {
if (target === 'legacy') {
Expand Down Expand Up @@ -138,3 +132,4 @@ module.exports = plugin.withOptions(
}
}
)

62 changes: 60 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,67 @@
const isPlainObject = require('lodash.isplainobject')

const parser = require('postcss-selector-parser')
const parseSelector = parser()

function isObject(value) {
return typeof value === 'object' && value !== null
}

function isPlainObject(value) {
if (typeof value !== 'object' || value === null) {
return false
}

if (Object.prototype.toString.call(value) !== '[object Object]') {
return false
}

if (Object.getPrototypeOf(value) === null) {
return true
}

let proto = value
while (Object.getPrototypeOf(proto) !== null) {
proto = Object.getPrototypeOf(proto)
}

return Object.getPrototypeOf(value) === proto
}

function merge(target, ...sources) {
if (!sources.length) return target
const source = sources.shift()

if (isObject(target) && isObject(source)) {
for (const key in source) {
if (Array.isArray(source[key])) {
if (!target[key]) target[key] = []
source[key].forEach((item, index) => {
if (isPlainObject(item) && isPlainObject(target[key][index])) {
target[key][index] = merge(target[key][index], item)
} else {
target[key][index] = item
}
})
} else if (isPlainObject(source[key])) {
if (!target[key]) target[key] = {}
merge(target[key], source[key])
} else {
target[key] = source[key]
}
}
}

return merge(target, ...sources)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The code that uses merge could probably be simplified to not need it at all tbh


function castArray(value) {
return Array.isArray(value) ? value : [value]
Copy link
Contributor

Choose a reason for hiding this comment

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

if we simplified that code and dropped the entries -> fromEntries dance we could inline this too

}

module.exports = {
isObject,
isPlainObject,
merge,
castArray,
isUsableColor(color, values) {
return isPlainObject(values) && color !== 'gray' && values[600]
},
Expand Down