Skip to content

Commit c6e6d4b

Browse files
committed
Support config options from postcss CLI
1 parent 9221914 commit c6e6d4b

File tree

2 files changed

+82
-5
lines changed

2 files changed

+82
-5
lines changed

Diff for: integrations/tailwindcss-cli/tests/cli.test.js

+68
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,74 @@ describe('Build command', () => {
277277
)
278278
})
279279

280+
test('--postcss supports process options', async () => {
281+
await writeInputFile('index.html', html`<div class="font-bold"></div>`)
282+
283+
let customConfig = javascript`
284+
let path = require('path')
285+
let postcss = require('postcss')
286+
287+
module.exports = {
288+
map: { inline: true },
289+
plugins: [
290+
function tailwindcss() {
291+
return require(path.resolve('..', '..'))
292+
},
293+
],
294+
}
295+
`
296+
297+
await writeInputFile('../postcss.config.js', customConfig)
298+
299+
await $(`${EXECUTABLE} --output ./dist/main.css --postcss`)
300+
301+
let contents = await readOutputFile('main.css')
302+
303+
expect(contents).toIncludeCss(
304+
css`
305+
.font-bold {
306+
font-weight: 700;
307+
}
308+
`
309+
)
310+
311+
expect(contents).toContain(`/*# sourceMappingURL`)
312+
})
313+
314+
test('--postcss supports process options with custom config', async () => {
315+
await writeInputFile('index.html', html`<div class="font-bold"></div>`)
316+
317+
let customConfig = javascript`
318+
let path = require('path')
319+
let postcss = require('postcss')
320+
321+
module.exports = {
322+
map: { inline: true },
323+
plugins: [
324+
function tailwindcss() {
325+
return require(path.resolve('..', '..'))
326+
},
327+
],
328+
}
329+
`
330+
331+
await writeInputFile('../custom.postcss.config.js', customConfig)
332+
333+
await $(`${EXECUTABLE} --output ./dist/main.css --postcss ./custom.postcss.config.js`)
334+
335+
let contents = await readOutputFile('main.css')
336+
337+
expect(contents).toIncludeCss(
338+
css`
339+
.font-bold {
340+
font-weight: 700;
341+
}
342+
`
343+
)
344+
345+
expect(contents).toContain(`/*# sourceMappingURL`)
346+
})
347+
280348
test('--help', async () => {
281349
let { combined } = await $(`${EXECUTABLE} --help`)
282350

Diff for: src/cli.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import fs from 'fs'
99
import postcssrc from 'postcss-load-config'
1010
import { lilconfig } from 'lilconfig'
1111
import loadPlugins from 'postcss-load-config/src/plugins' // Little bit scary, looking at private/internal API
12+
import loadOptions from 'postcss-load-config/src/options' // Little bit scary, looking at private/internal API
1213
import tailwind from './processTailwindFeatures'
1314
import resolveConfigInternal from '../resolveConfig'
1415
import fastGlob from 'fast-glob'
@@ -415,7 +416,7 @@ async function build() {
415416

416417
async function loadPostCssPlugins() {
417418
let customPostCssPath = typeof args['--postcss'] === 'string' ? args['--postcss'] : undefined
418-
let { plugins: configPlugins } = customPostCssPath
419+
let config = customPostCssPath
419420
? await (async () => {
420421
let file = path.resolve(customPostCssPath)
421422

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

434-
return { plugins: loadPlugins(config, file) }
435+
return {
436+
file,
437+
plugins: loadPlugins(config, file),
438+
options: loadOptions(config, file),
439+
}
435440
})()
436441
: await postcssrc()
437442

443+
let configPlugins = config.plugins
444+
438445
let configPluginTailwindIdx = configPlugins.findIndex((plugin) => {
439446
if (typeof plugin === 'function' && plugin.name === 'tailwindcss') {
440447
return true
@@ -454,7 +461,7 @@ async function build() {
454461
? configPlugins
455462
: configPlugins.slice(configPluginTailwindIdx + 1)
456463

457-
return [beforePlugins, afterPlugins]
464+
return [beforePlugins, afterPlugins, config.options]
458465
}
459466

460467
function resolveConfig() {
@@ -538,7 +545,9 @@ async function build() {
538545

539546
tailwindPlugin.postcss = true
540547

541-
let [beforePlugins, afterPlugins] = includePostCss ? await loadPostCssPlugins() : [[], []]
548+
let [beforePlugins, afterPlugins, postcssOptions] = includePostCss
549+
? await loadPostCssPlugins()
550+
: [[], [], {}]
542551

543552
let plugins = [
544553
...beforePlugins,
@@ -573,7 +582,7 @@ async function build() {
573582
let start = process.hrtime.bigint()
574583
return Promise.resolve()
575584
.then(() => (output ? fs.promises.mkdir(path.dirname(output), { recursive: true }) : null))
576-
.then(() => processor.process(css, { from: input, to: output }))
585+
.then(() => processor.process(css, { ...postcssOptions, from: input, to: output }))
577586
.then((result) => {
578587
if (!output) {
579588
return process.stdout.write(result.css)

0 commit comments

Comments
 (0)