tailwind-ctp-intellisense/packages/tailwindcss-class-names/src/getPlugins.js

89 lines
2.0 KiB
JavaScript
Raw Normal View History

2020-04-11 21:20:45 +00:00
import * as path from 'path'
import stackTrace from 'stack-trace'
import pkgUp from 'pkg-up'
2020-04-25 21:41:17 +00:00
import { glob } from './glob'
import { isObject } from './isObject'
2020-04-11 21:20:45 +00:00
2020-04-25 21:41:17 +00:00
export async function getBuiltInPlugins(cwd) {
try {
2020-04-29 18:01:34 +00:00
// TODO: just require('tailwindcss/lib/corePlugins.js') instead of globbing
// TODO: add v0 support ("generators")
2020-04-25 21:41:17 +00:00
return (
await glob(path.resolve(cwd, 'node_modules/tailwindcss/lib/plugins/*.js'))
)
.map((x) => {
try {
const mod = __non_webpack_require__(x)
return mod.default ? mod.default() : mod()
} catch (_) {}
})
.filter(Boolean)
} catch (_) {
return []
}
2020-04-11 21:20:45 +00:00
}
export default function getPlugins(config) {
let plugins = config.plugins
if (!Array.isArray(plugins)) {
return []
}
2020-04-25 21:41:17 +00:00
return plugins.map((plugin) => {
2020-04-11 21:20:45 +00:00
let pluginConfig = plugin.config
if (!isObject(pluginConfig)) {
pluginConfig = {}
}
let contributes = {
theme: isObject(pluginConfig.theme)
? Object.keys(pluginConfig.theme)
: [],
variants: isObject(pluginConfig.variants)
? Object.keys(pluginConfig.variants)
2020-04-25 21:41:17 +00:00
: [],
2020-04-11 21:20:45 +00:00
}
const fn = plugin.handler || plugin
const fnName =
typeof fn.name === 'string' && fn.name !== 'handler' && fn.name !== ''
? fn.name
: null
try {
fn()
} catch (e) {
const trace = stackTrace.parse(e)
if (trace.length === 0)
return {
2020-04-25 21:41:17 +00:00
name: fnName,
2020-04-11 21:20:45 +00:00
}
const file = trace[0].fileName
const dir = path.dirname(file)
let pkg = pkgUp.sync({ cwd: dir })
if (!pkg)
return {
2020-04-25 21:41:17 +00:00
name: fnName,
2020-04-11 21:20:45 +00:00
}
try {
pkg = __non_webpack_require__(pkg)
} catch (_) {
return {
2020-04-25 21:41:17 +00:00
name: fnName,
2020-04-11 21:20:45 +00:00
}
}
if (pkg.name && path.resolve(dir, pkg.main || 'index.js') === file) {
return {
name: pkg.name,
homepage: pkg.homepage,
2020-04-25 21:41:17 +00:00
contributes,
2020-04-11 21:20:45 +00:00
}
}
}
return {
2020-04-25 21:41:17 +00:00
name: fnName,
2020-04-11 21:20:45 +00:00
}
})
}