Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support postcss config options in config file in CLI #8226

Merged
merged 2 commits into from Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Allow arbitrary values with commas in `@apply` ([#8125](https://github.com/tailwindlabs/tailwindcss/pull/8125))
- Fix intellisense for plugins with multiple `@apply` rules ([#8213](https://github.com/tailwindlabs/tailwindcss/pull/8213))
- Improve type detection for arbitrary color values ([#8201](https://github.com/tailwindlabs/tailwindcss/pull/8201))
- Support PostCSS config options in config file in CLI ([#8226](https://github.com/tailwindlabs/tailwindcss/pull/8226))

### Added

Expand Down
68 changes: 68 additions & 0 deletions integrations/tailwindcss-cli/tests/cli.test.js
Expand Up @@ -277,6 +277,74 @@ describe('Build command', () => {
)
})

test('--postcss supports process options', async () => {
await writeInputFile('index.html', html`<div class="font-bold"></div>`)

let customConfig = javascript`
let path = require('path')
let postcss = require('postcss')

module.exports = {
map: { inline: true },
plugins: [
function tailwindcss() {
return require(path.resolve('..', '..'))
},
],
}
`

await writeInputFile('../postcss.config.js', customConfig)

await $(`${EXECUTABLE} --output ./dist/main.css --postcss`)

let contents = await readOutputFile('main.css')

expect(contents).toIncludeCss(
css`
.font-bold {
font-weight: 700;
}
`
)

expect(contents).toContain(`/*# sourceMappingURL`)
})

test('--postcss supports process options with custom config', async () => {
await writeInputFile('index.html', html`<div class="font-bold"></div>`)

let customConfig = javascript`
let path = require('path')
let postcss = require('postcss')

module.exports = {
map: { inline: true },
plugins: [
function tailwindcss() {
return require(path.resolve('..', '..'))
},
],
}
`

await writeInputFile('../custom.postcss.config.js', customConfig)

await $(`${EXECUTABLE} --output ./dist/main.css --postcss ./custom.postcss.config.js`)

let contents = await readOutputFile('main.css')

expect(contents).toIncludeCss(
css`
.font-bold {
font-weight: 700;
}
`
)

expect(contents).toContain(`/*# sourceMappingURL`)
})

test('--help', async () => {
let { combined } = await $(`${EXECUTABLE} --help`)

Expand Down
19 changes: 14 additions & 5 deletions src/cli.js
Expand Up @@ -9,6 +9,7 @@ import fs from 'fs'
import postcssrc from 'postcss-load-config'
import { lilconfig } from 'lilconfig'
import loadPlugins from 'postcss-load-config/src/plugins' // Little bit scary, looking at private/internal API
import loadOptions from 'postcss-load-config/src/options' // Little bit scary, looking at private/internal API
import tailwind from './processTailwindFeatures'
import resolveConfigInternal from '../resolveConfig'
import fastGlob from 'fast-glob'
Expand Down Expand Up @@ -415,7 +416,7 @@ async function build() {

async function loadPostCssPlugins() {
let customPostCssPath = typeof args['--postcss'] === 'string' ? args['--postcss'] : undefined
let { plugins: configPlugins } = customPostCssPath
let config = customPostCssPath
? await (async () => {
let file = path.resolve(customPostCssPath)

Expand All @@ -431,10 +432,16 @@ async function build() {
config.plugins = []
}

return { plugins: loadPlugins(config, file) }
return {
file,
plugins: loadPlugins(config, file),
options: loadOptions(config, file),
}
})()
: await postcssrc()

let configPlugins = config.plugins

let configPluginTailwindIdx = configPlugins.findIndex((plugin) => {
if (typeof plugin === 'function' && plugin.name === 'tailwindcss') {
return true
Expand All @@ -454,7 +461,7 @@ async function build() {
? configPlugins
: configPlugins.slice(configPluginTailwindIdx + 1)

return [beforePlugins, afterPlugins]
return [beforePlugins, afterPlugins, config.options]
}

function resolveConfig() {
Expand Down Expand Up @@ -538,7 +545,9 @@ async function build() {

tailwindPlugin.postcss = true

let [beforePlugins, afterPlugins] = includePostCss ? await loadPostCssPlugins() : [[], []]
let [beforePlugins, afterPlugins, postcssOptions] = includePostCss
? await loadPostCssPlugins()
: [[], [], {}]

let plugins = [
...beforePlugins,
Expand Down Expand Up @@ -573,7 +582,7 @@ async function build() {
let start = process.hrtime.bigint()
return Promise.resolve()
.then(() => (output ? fs.promises.mkdir(path.dirname(output), { recursive: true }) : null))
.then(() => processor.process(css, { from: input, to: output }))
.then(() => processor.process(css, { ...postcssOptions, from: input, to: output }))
.then((result) => {
if (!output) {
return process.stdout.write(result.css)
Expand Down