wip

cleanup
This commit is contained in:
Jordan Pittman 2022-09-22 11:31:27 -04:00
parent 377f372536
commit d76e5db551
4 changed files with 15 additions and 43 deletions

View file

@ -3,13 +3,12 @@ import path from 'path'
/**
* Find the @config at-rule in the given CSS AST and return the relative path to the config file
*
*
* @param {import('postcss').Root} root
* @param {import('postcss').Result} result
*/
export function findAtConfigPath(root, result) {
let configPath = null
let relativeTo = root.source.input.file ?? result.opts.from ?? null
let relativeTo = root.source?.input.file ?? result.opts.from ?? null
root.walkAtRules('config', (rule) => {
if (relativeTo === undefined) {
@ -22,12 +21,19 @@ export function findAtConfigPath(root, result) {
throw rule.error('Only `@config` at-rule is allowed per file.')
}
configPath = rule.params.replace(/^['"]|['"]$/g, '')
rule.remove()
let matches = rule.params.match(/(['"])(.*?)\1/)
if (!matches) {
throw rule.error(
'The `@config` at-rule must be followed by a string containing the path to the config file.'
)
}
configPath = matches[2]
if (path.isAbsolute(configPath)) {
throw rule.error('The `@config` at-rule cannot be used with an absolute path.')
}
rule.remove()
})
return configPath ? path.resolve(path.dirname(relativeTo), configPath) : null

View file

@ -25,8 +25,8 @@ function getCandidateFiles(context, tailwindConfig) {
}
// Get the config object based on a path
function getTailwindConfig(configOrPath, inputPath) {
let userConfigPath = resolveConfigPath(configOrPath, inputPath)
function getTailwindConfig(configOrPath) {
let userConfigPath = resolveConfigPath(configOrPath)
if (userConfigPath !== null) {
let [prevConfig, prevConfigHash, prevDeps, prevModified] =
@ -79,7 +79,7 @@ export default function setupTrackingContext(configOrPath) {
return ({ tailwindDirectives, registerDependency }) => {
return (root, result) => {
let [tailwindConfig, userConfigPath, tailwindConfigHash, configDependencies] =
getTailwindConfig(configOrPath, result.opts.from)
getTailwindConfig(configOrPath)
let contextDependencies = new Set(configDependencies)

View file

@ -59,7 +59,7 @@ export function normalizeConfig(config) {
// Only `files`, `relative`, `extract`, and `transform` can exist in `config.content`
if (
Object.keys(config.content).some(
(key) => !['files', 'extract', 'transform', 'relative'].includes(key)
(key) => !['files', 'relative', 'extract', 'transform'].includes(key)
)
) {
return false

View file

@ -1,6 +1,5 @@
import fs from 'fs'
import path from 'path'
import { flagEnabled } from '../featureFlags.js'
function isObject(value) {
return typeof value === 'object' && value !== null
@ -14,36 +13,7 @@ function isString(value) {
return typeof value === 'string' || value instanceof String
}
/**
* This will take a possibly-relative path to a config and resolve
* it relative to the input path IF that config file exists and
* has the `resolveConfigRelativeToInput` flag enabled.
*
* If that file does not exist, or the flag is disabled, it will
* resolve the path relative to the current working directory.
*
* @param {string|undefined} inputPath
* @param {string} configPath
* @returns {string}
*/
function pickResolvedPath(configPath, inputPath) {
if (path.isAbsolute(configPath)) {
return configPath
}
if (inputPath) {
try {
// Use require.resolve so we can find config file in parent directories
return require.resolve(configPath, {
paths: [path.dirname(inputPath)],
})
} catch {}
}
return path.resolve(configPath)
}
export default function resolveConfigPath(pathOrConfig, inputPath) {
export default function resolveConfigPath(pathOrConfig) {
// require('tailwindcss')({ theme: ..., variants: ... })
if (isObject(pathOrConfig) && pathOrConfig.config === undefined && !isEmpty(pathOrConfig)) {
return null
@ -55,10 +25,6 @@ export default function resolveConfigPath(pathOrConfig, inputPath) {
pathOrConfig.config !== undefined &&
isString(pathOrConfig.config)
) {
// if (pathOrConfig.experimental?.contextualPaths) {
// return pickResolvedPath(pathOrConfig.config, inputPath)
// }
return path.resolve(pathOrConfig.config)
}