Skip to content

Commit 0d064ea

Browse files
Enable postcss-import in the CLI by default in watch mode (#8580)
* Add support for postcss-import in watch mode * Add regression test * Extract shared logic * restructure test a little bit Instead of relying on a arbitrary setTimout value, let's wait for the file to be created instead. * update changelog Co-authored-by: Adam Bergman <adam@fransvilhelm.com>
1 parent d32728b commit 0d064ea

File tree

3 files changed

+85
-38
lines changed

3 files changed

+85
-38
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Fixed
1111

1212
- Ensure `\` is a valid arbitrary variant token ([#8576](https://github.com/tailwindlabs/tailwindcss/pull/8576))
13+
- Enable `postcss-import` in the CLI by default in watch mode ([#8574](https://github.com/tailwindlabs/tailwindcss/pull/8574), [#8580](https://github.com/tailwindlabs/tailwindcss/pull/8580))
1314

1415
## [3.1.1] - 2022-06-09
1516

integrations/tailwindcss-cli/tests/cli.test.js

+41-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ let resolveToolRoot = require('../../resolve-tool-root')
55

66
let version = require('../../../package.json').version
77

8-
let { readOutputFile, writeInputFile, cleanupFile, fileExists, removeFile } = require('../../io')({
8+
let {
9+
cleanupFile,
10+
fileExists,
11+
readOutputFile,
12+
removeFile,
13+
waitForOutputFileCreation,
14+
writeInputFile,
15+
} = require('../../io')({
916
output: 'dist',
1017
input: 'src',
1118
})
@@ -374,6 +381,39 @@ describe('Build command', () => {
374381
)
375382
})
376383

384+
test('postcss-import is supported by default in watch mode', async () => {
385+
cleanupFile('src/test.css')
386+
387+
await writeInputFile('index.html', html`<div class="md:something-cool"></div>`)
388+
await writeInputFile(
389+
'test.css',
390+
css`
391+
@import 'tailwindcss/base';
392+
@import 'tailwindcss/components';
393+
@import 'tailwindcss/utilities';
394+
@import './imported.css';
395+
`
396+
)
397+
398+
let runningProcess = $(
399+
`${EXECUTABLE} --watch --input ./src/test.css --content ./src/index.html --output ./dist/main.css`
400+
)
401+
402+
await waitForOutputFileCreation('main.css')
403+
404+
expect(await readOutputFile('main.css')).toIncludeCss(
405+
css`
406+
@media (min-width: 768px) {
407+
.md\:something-cool {
408+
color: red;
409+
}
410+
}
411+
`
412+
)
413+
414+
return runningProcess.stop()
415+
})
416+
377417
test('postcss-import is included when using a custom postcss configuration', async () => {
378418
cleanupFile('src/test.css')
379419

src/cli.js

+43-37
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,45 @@ async function build() {
484484
return [beforePlugins, afterPlugins, config.options]
485485
}
486486

487+
function loadBuiltinPostcssPlugins() {
488+
let postcss = loadPostcss()
489+
let IMPORT_COMMENT = '__TAILWIND_RESTORE_IMPORT__: '
490+
return [
491+
[
492+
(root) => {
493+
root.walkAtRules('import', (rule) => {
494+
if (rule.params.slice(1).startsWith('tailwindcss/')) {
495+
rule.after(postcss.comment({ text: IMPORT_COMMENT + rule.params }))
496+
rule.remove()
497+
}
498+
})
499+
},
500+
(() => {
501+
try {
502+
return require('postcss-import')
503+
} catch {}
504+
505+
return lazyPostcssImport()
506+
})(),
507+
(root) => {
508+
root.walkComments((rule) => {
509+
if (rule.text.startsWith(IMPORT_COMMENT)) {
510+
rule.after(
511+
postcss.atRule({
512+
name: 'import',
513+
params: rule.text.replace(IMPORT_COMMENT, ''),
514+
})
515+
)
516+
rule.remove()
517+
}
518+
})
519+
},
520+
],
521+
[],
522+
{},
523+
]
524+
}
525+
487526
function resolveConfig() {
488527
let config = configPath ? require(configPath) : {}
489528

@@ -568,44 +607,9 @@ async function build() {
568607

569608
tailwindPlugin.postcss = true
570609

571-
let IMPORT_COMMENT = '__TAILWIND_RESTORE_IMPORT__: '
572-
573610
let [beforePlugins, afterPlugins, postcssOptions] = includePostCss
574611
? await loadPostCssPlugins()
575-
: [
576-
[
577-
(root) => {
578-
root.walkAtRules('import', (rule) => {
579-
if (rule.params.slice(1).startsWith('tailwindcss/')) {
580-
rule.after(postcss.comment({ text: IMPORT_COMMENT + rule.params }))
581-
rule.remove()
582-
}
583-
})
584-
},
585-
(() => {
586-
try {
587-
return require('postcss-import')
588-
} catch {}
589-
590-
return lazyPostcssImport()
591-
})(),
592-
(root) => {
593-
root.walkComments((rule) => {
594-
if (rule.text.startsWith(IMPORT_COMMENT)) {
595-
rule.after(
596-
postcss.atRule({
597-
name: 'import',
598-
params: rule.text.replace(IMPORT_COMMENT, ''),
599-
})
600-
)
601-
rule.remove()
602-
}
603-
})
604-
},
605-
],
606-
[],
607-
{},
608-
]
612+
: loadBuiltinPostcssPlugins()
609613

610614
let plugins = [
611615
...beforePlugins,
@@ -705,7 +709,9 @@ async function build() {
705709
return resolveConfig()
706710
}
707711

708-
let [beforePlugins, afterPlugins] = includePostCss ? await loadPostCssPlugins() : [[], []]
712+
let [beforePlugins, afterPlugins] = includePostCss
713+
? await loadPostCssPlugins()
714+
: loadBuiltinPostcssPlugins()
709715

710716
let plugins = [
711717
...beforePlugins,

0 commit comments

Comments
 (0)