master
Brad Cornes 2020-11-24 13:27:19 +00:00
commit 321098dac4
5 changed files with 193 additions and 105 deletions

View File

@ -185,6 +185,7 @@
"dset": "^2.0.1", "dset": "^2.0.1",
"esm": "^3.2.25", "esm": "^3.2.25",
"fast-glob": "^3.2.4", "fast-glob": "^3.2.4",
"find-up": "^5.0.0",
"glob-exec": "^0.1.1", "glob-exec": "^0.1.1",
"import-from": "^3.0.0", "import-from": "^3.0.0",
"jest": "^25.5.4", "jest": "^25.5.4",

View File

@ -0,0 +1,71 @@
import * as path from 'path'
import Module from 'module'
import findUp from 'find-up'
import resolveFrom from 'resolve-from'
import importFrom from 'import-from'
export function withUserEnvironment(base, cb) {
const pnpPath = findUp.sync('.pnp.js', { cwd: base })
if (pnpPath) {
return withPnpEnvironment(pnpPath, cb)
}
return withNonPnpEnvironment(base, cb)
}
function withPnpEnvironment(pnpPath, cb) {
const basePath = path.dirname(pnpPath)
// pnp will patch `module` and `fs` to load package in pnp environment
// backup the functions which will be patched here
const originalModule = Object.create(null)
originalModule._load = Module._load
originalModule._resolveFilename = Module._resolveFilename
originalModule._findPath = Module._findPath
const pnpapi = __non_webpack_require__(pnpPath)
// get into pnp environment
pnpapi.setup()
// restore the patched function, we can not load any package after called this
const restore = () => Object.assign(Module, originalModule)
const pnpResolve = (request, from = basePath) => {
return pnpapi.resolveRequest(request, from + '/')
}
const pnpRequire = (request, from) => {
return __non_webpack_require__(pnpResolve(request, from))
}
const res = cb({ resolve: pnpResolve, require: pnpRequire })
// check if it return a thenable
if (res != null && res.then) {
return res.then(
(x) => {
restore()
return x
},
(err) => {
restore()
throw err
}
)
}
restore()
return res
}
function withNonPnpEnvironment(base, cb) {
return cb({
require(request, from = base) {
return importFrom(from, request)
},
resolve(request, from = base) {
return resolveFrom(from, request)
},
})
}

View File

@ -2,22 +2,19 @@ import * as path from 'path'
import stackTrace from 'stack-trace' import stackTrace from 'stack-trace'
import pkgUp from 'pkg-up' import pkgUp from 'pkg-up'
import { isObject } from './isObject' import { isObject } from './isObject'
import resolveFrom from 'resolve-from' import { withUserEnvironment } from './environment'
import importFrom from 'import-from'
export async function getBuiltInPlugins({ cwd, resolvedConfig }) { export async function getBuiltInPlugins({ cwd, resolvedConfig }) {
const tailwindBase = path.dirname( return withUserEnvironment(cwd, ({ require, resolve }) => {
resolveFrom(cwd, 'tailwindcss/package.json') const tailwindBase = path.dirname(resolve('tailwindcss/package.json'))
)
try { try {
// TODO: add v0 support ("generators") return require('./lib/corePlugins.js', tailwindBase).default({
return importFrom(tailwindBase, './lib/corePlugins.js').default({
corePlugins: resolvedConfig.corePlugins, corePlugins: resolvedConfig.corePlugins,
}) })
} catch (_) { } catch (_) {
return [] return []
} }
})
} }
export default function getPlugins(config) { export default function getPlugins(config) {
@ -34,19 +31,12 @@ export default function getPlugins(config) {
} }
let contributes = { let contributes = {
theme: isObject(pluginConfig.theme) theme: isObject(pluginConfig.theme) ? Object.keys(pluginConfig.theme) : [],
? Object.keys(pluginConfig.theme) variants: isObject(pluginConfig.variants) ? Object.keys(pluginConfig.variants) : [],
: [],
variants: isObject(pluginConfig.variants)
? Object.keys(pluginConfig.variants)
: [],
} }
const fn = plugin.handler || plugin const fn = plugin.handler || plugin
const fnName = const fnName = typeof fn.name === 'string' && fn.name !== 'handler' && fn.name !== '' ? fn.name : null
typeof fn.name === 'string' && fn.name !== 'handler' && fn.name !== ''
? fn.name
: null
try { try {
fn() fn()

View File

@ -2,8 +2,6 @@ import extractClassNames from './extractClassNames'
import Hook from './hook' import Hook from './hook'
import dlv from 'dlv' import dlv from 'dlv'
import dset from 'dset' import dset from 'dset'
import resolveFrom from 'resolve-from'
import importFrom from 'import-from'
import chokidar from 'chokidar' import chokidar from 'chokidar'
import semver from 'semver' import semver from 'semver'
import invariant from 'tiny-invariant' import invariant from 'tiny-invariant'
@ -15,6 +13,7 @@ import * as fs from 'fs'
import { getUtilityConfigMap } from './getUtilityConfigMap' import { getUtilityConfigMap } from './getUtilityConfigMap'
import glob from 'fast-glob' import glob from 'fast-glob'
import normalizePath from 'normalize-path' import normalizePath from 'normalize-path'
import { withUserEnvironment } from './environment'
function arraysEqual(arr1, arr2) { function arraysEqual(arr1, arr2) {
return ( return (
@ -31,12 +30,6 @@ export default async function getClassNames(
{ onChange = () => {} } = {} { onChange = () => {} } = {}
) { ) {
async function run() { async function run() {
let postcss
let tailwindcss
let browserslistModule
let version
let featureFlags = { future: [], experimental: [] }
const configPaths = ( const configPaths = (
await glob(CONFIG_GLOB, { await glob(CONFIG_GLOB, {
cwd, cwd,
@ -53,21 +46,10 @@ export default async function getClassNames(
invariant(configPaths.length > 0, 'No Tailwind CSS config found.') invariant(configPaths.length > 0, 'No Tailwind CSS config found.')
const configPath = configPaths[0] const configPath = configPaths[0]
const configDir = path.dirname(configPath) const configDir = path.dirname(configPath)
const tailwindBase = path.dirname( const {
resolveFrom(configDir, 'tailwindcss/package.json') version,
) featureFlags = { future: [], experimental: [] },
postcss = importFrom(tailwindBase, 'postcss') } = loadMeta(configDir)
tailwindcss = importFrom(configDir, 'tailwindcss')
version = importFrom(configDir, 'tailwindcss/package.json').version
try {
// this is not required
browserslistModule = importFrom(tailwindBase, 'browserslist')
} catch (_) {}
try {
featureFlags = importFrom(tailwindBase, './lib/featureFlags.js').default
} catch (_) {}
const sepLocation = semver.gte(version, '0.99.0') const sepLocation = semver.gte(version, '0.99.0')
? ['separator'] ? ['separator']
@ -94,8 +76,17 @@ export default async function getClassNames(
hook.unwatch() hook.unwatch()
const {
base,
components,
utilities,
resolvedConfig,
browserslist,
postcss,
} = await withPackages(
configDir,
async ({ postcss, tailwindcss, browserslistModule }) => {
let postcssResult let postcssResult
try { try {
postcssResult = await Promise.all( postcssResult = await Promise.all(
[ [
@ -103,9 +94,12 @@ export default async function getClassNames(
'components', 'components',
'utilities', 'utilities',
].map((group) => ].map((group) =>
postcss([tailwindcss(configPath)]).process(`@tailwind ${group};`, { postcss([tailwindcss(configPath)]).process(
`@tailwind ${group};`,
{
from: undefined, from: undefined,
}) }
)
) )
) )
} catch (error) { } catch (error) {
@ -134,6 +128,17 @@ export default async function getClassNames(
}) })
: [] : []
return {
base,
components,
utilities,
resolvedConfig,
postcss,
browserslist,
}
}
)
return { return {
version, version,
configPath, configPath,
@ -154,7 +159,6 @@ export default async function getClassNames(
browserslist, browserslist,
}), }),
modules: { modules: {
tailwindcss,
postcss, postcss,
}, },
featureFlags, featureFlags,
@ -193,7 +197,8 @@ export default async function getClassNames(
let result let result
try { try {
result = await run() result = await run()
} catch (_) { } catch (e) {
console.log(e)
return null return null
} }
@ -201,3 +206,32 @@ export default async function getClassNames(
return result return result
} }
function loadMeta(configDir) {
return withUserEnvironment(configDir, ({ require, resolve }) => {
const version = require('tailwindcss/package.json').version
let featureFlags
try {
const tailwindBase = path.dirname(resolve('tailwindcss/package.json'))
featureFlags = require('./lib/featureFlags.js', tailwindBase).default
} catch (_) {}
return { version, featureFlags }
})
}
function withPackages(configDir, cb) {
return withUserEnvironment(configDir, async ({ require, resolve }) => {
const tailwindBase = path.dirname(resolve('tailwindcss/package.json'))
const postcss = require('postcss', tailwindBase)
const tailwindcss = require('tailwindcss')
let browserslistModule
try {
// this is not required
browserslistModule = require('browserslist', tailwindBase)
} catch (_) {}
return cb({ postcss, tailwindcss, browserslistModule })
})
}

View File

@ -1,14 +1,8 @@
import resolveFrom from 'resolve-from'
import importFrom from 'import-from'
import * as path from 'path' import * as path from 'path'
import decache from './decache' import decache from './decache'
import { withUserEnvironment } from './environment'
export default function resolveConfig({ cwd, config }) { export default function resolveConfig({ cwd, config }) {
const tailwindBase = path.dirname(
resolveFrom(cwd, 'tailwindcss/package.json')
)
let resolve = (x) => x
if (typeof config === 'string') { if (typeof config === 'string') {
if (!cwd) { if (!cwd) {
cwd = path.dirname(config) cwd = path.dirname(config)
@ -17,21 +11,19 @@ export default function resolveConfig({ cwd, config }) {
config = __non_webpack_require__(config) config = __non_webpack_require__(config)
} }
let resolve = (x) => x
withUserEnvironment(cwd, ({ require, resolve }) => {
const tailwindBase = path.dirname(resolve('tailwindcss/package.json'))
try { try {
resolve = importFrom(tailwindBase, './resolveConfig.js') resolve = require('./resolveConfig.js', tailwindBase)
} catch (_) { } catch (_) {
try { try {
const resolveConfig = importFrom( const resolveConfig = require('./lib/util/resolveConfig.js', tailwindBase)
tailwindBase, const defaultConfig = require('./stubs/defaultConfig.stub.js', tailwindBase)
'./lib/util/resolveConfig.js'
)
const defaultConfig = importFrom(
tailwindBase,
'./stubs/defaultConfig.stub.js'
)
resolve = (config) => resolveConfig([config, defaultConfig]) resolve = (config) => resolveConfig([config, defaultConfig])
} catch (_) {} } catch (_) {}
} }
})
return resolve(config) return resolve(config)
} }