diff --git a/packages/tailwindcss-class-names/src/getPlugins.js b/packages/tailwindcss-class-names/src/getPlugins.js index 25a27b394101d76d5267d38b7c13ade166405a86..d831a2dc0c7b333c898f9d45df46932673136b04 100644 --- a/packages/tailwindcss-class-names/src/getPlugins.js +++ b/packages/tailwindcss-class-names/src/getPlugins.js @@ -1,24 +1,9 @@ import * as path from 'path' import stackTrace from 'stack-trace' import pkgUp from 'pkg-up' -import { glob } from './glob' -import { isObject } from './isObject' -export async function getBuiltInPlugins(cwd) { - try { - 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 [] - } +function isObject(variable) { + return Object.prototype.toString.call(variable) === '[object Object]' } export default function getPlugins(config) { @@ -28,7 +13,7 @@ if (!Array.isArray(plugins)) { return [] } - return plugins.map((plugin) => { + return plugins.map(plugin => { let pluginConfig = plugin.config if (!isObject(pluginConfig)) { pluginConfig = {} @@ -40,7 +25,7 @@ ? Object.keys(pluginConfig.theme) : [], variants: isObject(pluginConfig.variants) ? Object.keys(pluginConfig.variants) - : [], + : [] } const fn = plugin.handler || plugin @@ -55,32 +40,32 @@ } catch (e) { const trace = stackTrace.parse(e) if (trace.length === 0) return { - name: fnName, + name: fnName } const file = trace[0].fileName const dir = path.dirname(file) let pkg = pkgUp.sync({ cwd: dir }) if (!pkg) return { - name: fnName, + name: fnName } try { pkg = __non_webpack_require__(pkg) } catch (_) { return { - name: fnName, + name: fnName } } if (pkg.name && path.resolve(dir, pkg.main || 'index.js') === file) { return { name: pkg.name, homepage: pkg.homepage, - contributes, + contributes } } } return { - name: fnName, + name: fnName } }) } diff --git a/packages/tailwindcss-class-names/src/getUtilityConfigMap.js b/packages/tailwindcss-class-names/src/getUtilityConfigMap.js deleted file mode 100644 index 9362feae7233af8d21f683448bb3c03626af6ac4..0000000000000000000000000000000000000000 --- a/packages/tailwindcss-class-names/src/getUtilityConfigMap.js +++ /dev/null @@ -1,57 +0,0 @@ -import { runPlugin } from './runPlugin' -import { getBuiltInPlugins } from './getPlugins' -import { isObject } from './isObject' - -const proxyHandler = (base = []) => ({ - get(target, key) { - if (isObject(target[key])) { - return new Proxy(target[key], proxyHandler([...base, key])) - } else { - if ( - [...base, key].every((x) => typeof x === 'string') && - target.hasOwnProperty(key) - ) { - return '$dep$' + [...base, key].join('.') - } - return target[key] - } - }, -}) - -export async function getUtilityConfigMap({ cwd, resolvedConfig, postcss }) { - const builtInPlugins = await getBuiltInPlugins(cwd) - const userPlugins = Array.isArray(resolvedConfig.plugins) - ? resolvedConfig.plugins - : [] - - try { - const classNameConfigMap = {} - const proxiedConfig = new Proxy(resolvedConfig, proxyHandler()) - - ;[...builtInPlugins, ...userPlugins].forEach((plugin) => { - runPlugin(plugin, { - postcss, - config: proxiedConfig, - addUtilities: (utilities) => { - Object.keys(utilities).forEach((util) => { - let props = Object.keys(utilities[util]) - if ( - props.length === 1 && - /^\.[^\s]+$/.test(util) && - typeof utilities[util][props[0]] === 'string' && - utilities[util][props[0]].substr(0, 5) === '$dep$' - ) { - classNameConfigMap[util.substr(1)] = utilities[util][ - props[0] - ].substr(5) - } - }) - }, - }) - }) - - return classNameConfigMap - } catch (_) { - return {} - } -} diff --git a/packages/tailwindcss-class-names/src/getVariants.js b/packages/tailwindcss-class-names/src/getVariants.js index 9688ef49a7a8b822d1275829c94d04be8d51730a..c7df96475180305955f2e5dd7d1d22ce355f3103 100644 --- a/packages/tailwindcss-class-names/src/getVariants.js +++ b/packages/tailwindcss-class-names/src/getVariants.js @@ -1,5 +1,5 @@ import semver from 'semver' -import { runPlugin } from './runPlugin' +import dlv from 'dlv' export default function getVariants({ config, version, postcss }) { let variants = ['responsive', 'hover'] @@ -11,16 +11,28 @@ semver.gte(version, '1.1.0') && variants.push('first', 'last', 'odd', 'even', 'disabled', 'visited') semver.gte(version, '1.3.0') && variants.push('group-focus') - let plugins = Array.isArray(config.plugins) ? config.plugins : [] - + let plugins = config.plugins + if (!Array.isArray(plugins)) { + plugins = [] + } plugins.forEach((plugin) => { - runPlugin(plugin, { - postcss, - config, - addVariant: (name) => { - variants.push(name) - }, - }) + try { + ;(plugin.handler || plugin)({ + addUtilities: () => {}, + addComponents: () => {}, + addBase: () => {}, + addVariant: (name) => { + variants.push(name) + }, + e: (x) => x, + prefix: (x) => x, + theme: (path, defaultValue) => + dlv(config, `theme.${path}`, defaultValue), + variants: () => [], + config: (path, defaultValue) => dlv(config, path, defaultValue), + postcss, + }) + } catch (_) {} }) return variants diff --git a/packages/tailwindcss-class-names/src/glob.js b/packages/tailwindcss-class-names/src/glob.js deleted file mode 100644 index 22e3eb83feb93bbe7369902446426da1ffb01b00..0000000000000000000000000000000000000000 --- a/packages/tailwindcss-class-names/src/glob.js +++ /dev/null @@ -1,22 +0,0 @@ -import nodeGlob from 'glob' -import dlv from 'dlv' -import * as path from 'path' - -export function glob(pattern, options = {}) { - return new Promise((resolve, reject) => { - let g = new nodeGlob.Glob(pattern, options) - let matches = [] - let max = dlv(options, 'max', Infinity) - g.on('match', (match) => { - matches.push(path.resolve(options.cwd || process.cwd(), match)) - if (matches.length === max) { - g.abort() - resolve(matches) - } - }) - g.on('end', () => { - resolve(matches) - }) - g.on('error', reject) - }) -} diff --git a/packages/tailwindcss-class-names/src/index.js b/packages/tailwindcss-class-names/src/index.js index e7a78ef2631705cd256f89f1009e49e20ab74eaa..4b55f764d6dcd4cc0fcd94de0eb57d01dfdf3a6e 100644 --- a/packages/tailwindcss-class-names/src/index.js +++ b/packages/tailwindcss-class-names/src/index.js @@ -3,6 +3,8 @@ import Hook from './hook.mjs' import dlv from 'dlv' import dset from 'dset' import importFrom from 'import-from' +import nodeGlob from 'glob' +import * as path from 'path' import chokidar from 'chokidar' import semver from 'semver' import invariant from 'tiny-invariant' @@ -10,8 +12,6 @@ import getPlugins from './getPlugins' import getVariants from './getVariants' import resolveConfig from './resolveConfig' import * as util from 'util' -import { glob } from './glob' -import { getUtilityConfigMap } from './getUtilityConfigMap' function TailwindConfigError(error) { Error.call(this) @@ -23,6 +23,25 @@ this.stack = error.stack } util.inherits(TailwindConfigError, Error) + +function glob(pattern, options = {}) { + return new Promise((resolve, reject) => { + let g = new nodeGlob.Glob(pattern, options) + let matches = [] + let max = dlv(options, 'max', Infinity) + g.on('match', (match) => { + matches.push(path.resolve(options.cwd || process.cwd(), match)) + if (matches.length === max) { + g.abort() + resolve(matches) + } + }) + g.on('end', () => { + resolve(matches) + }) + g.on('error', reject) + }) +} function arraysEqual(arr1, arr2) { return ( @@ -90,21 +109,14 @@ } else { delete config[sepLocation] } - const resolvedConfig = resolveConfig({ cwd, config }) - return { configPath, - config: resolvedConfig, + config: resolveConfig({ cwd, config }), separator: typeof userSeperator === 'undefined' ? ':' : userSeperator, classNames: await extractClassNames(ast), dependencies: hook.deps, plugins: getPlugins(config), variants: getVariants({ config, version, postcss }), - utilityConfigMap: await getUtilityConfigMap({ - cwd, - resolvedConfig, - postcss, - }), } } diff --git a/packages/tailwindcss-class-names/src/isObject.js b/packages/tailwindcss-class-names/src/isObject.js deleted file mode 100644 index 502dc97bd94e34f38b7bc8754f80a2bd6f4b4eab..0000000000000000000000000000000000000000 --- a/packages/tailwindcss-class-names/src/isObject.js +++ /dev/null @@ -1,3 +0,0 @@ -export function isObject(thing) { - return Object.prototype.toString.call(thing) === '[object Object]' -} diff --git a/packages/tailwindcss-class-names/src/runPlugin.js b/packages/tailwindcss-class-names/src/runPlugin.js deleted file mode 100644 index 059b9a9c0337b279f2d55e9ec5911d365772f394..0000000000000000000000000000000000000000 --- a/packages/tailwindcss-class-names/src/runPlugin.js +++ /dev/null @@ -1,19 +0,0 @@ -import dlv from 'dlv' - -export function runPlugin(plugin, params = {}) { - const { config, ...rest } = params - try { - ;(plugin.handler || plugin)({ - addUtilities: () => {}, - addComponents: () => {}, - addBase: () => {}, - addVariant: () => {}, - e: (x) => x, - prefix: (x) => x, - theme: (path, defaultValue) => dlv(config, `theme.${path}`, defaultValue), - variants: () => [], - config: (path, defaultValue) => dlv(config, path, defaultValue), - ...rest, - }) - } catch (_) {} -}