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

Enable postcss-import in the CLI by default in watch mode #8574

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions integrations/tailwindcss-cli/tests/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,37 @@ describe('Build command', () => {
)
})

test('postcss-import is supported by default in watch mode', async () => {
cleanupFile('src/test.css')

await writeInputFile('index.html', html`<div class="md:something-cool"></div>`)
await writeInputFile(
'test.css',
css`
@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';
@import './imported.css';
`
)

let proc = $(
`${EXECUTABLE} --watch --input ./src/test.css --content ./src/index.html --output ./dist/main.css`
)
await new Promise((r) => setTimeout(r, 500))
adambrgmn marked this conversation as resolved.
Show resolved Hide resolved
await proc.stop()

expect(await readOutputFile('main.css')).toIncludeCss(
css`
@media (min-width: 768px) {
.md\:something-cool {
color: red;
}
}
`
)
})

test('postcss-import is included when using a custom postcss configuration', async () => {
cleanupFile('src/test.css')

Expand Down
80 changes: 43 additions & 37 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,45 @@ async function build() {
return [beforePlugins, afterPlugins, config.options]
}

function loadBuiltinPostcssPlugins() {
let postcss = loadPostcss()
let IMPORT_COMMENT = '__TAILWIND_RESTORE_IMPORT__: '
return [
[
(root) => {
root.walkAtRules('import', (rule) => {
if (rule.params.slice(1).startsWith('tailwindcss/')) {
rule.after(postcss.comment({ text: IMPORT_COMMENT + rule.params }))
rule.remove()
}
})
},
(() => {
try {
return require('postcss-import')
} catch {}

return lazyPostcssImport()
})(),
(root) => {
root.walkComments((rule) => {
if (rule.text.startsWith(IMPORT_COMMENT)) {
rule.after(
postcss.atRule({
name: 'import',
params: rule.text.replace(IMPORT_COMMENT, ''),
})
)
rule.remove()
}
})
},
],
[],
{},
]
}

function resolveConfig() {
let config = configPath ? require(configPath) : {}

Expand Down Expand Up @@ -568,44 +607,9 @@ async function build() {

tailwindPlugin.postcss = true

let IMPORT_COMMENT = '__TAILWIND_RESTORE_IMPORT__: '

let [beforePlugins, afterPlugins, postcssOptions] = includePostCss
? await loadPostCssPlugins()
: [
[
(root) => {
root.walkAtRules('import', (rule) => {
if (rule.params.slice(1).startsWith('tailwindcss/')) {
rule.after(postcss.comment({ text: IMPORT_COMMENT + rule.params }))
rule.remove()
}
})
},
(() => {
try {
return require('postcss-import')
} catch {}

return lazyPostcssImport()
})(),
(root) => {
root.walkComments((rule) => {
if (rule.text.startsWith(IMPORT_COMMENT)) {
rule.after(
postcss.atRule({
name: 'import',
params: rule.text.replace(IMPORT_COMMENT, ''),
})
)
rule.remove()
}
})
},
],
[],
{},
]
: loadBuiltinPostcssPlugins()

let plugins = [
...beforePlugins,
Expand Down Expand Up @@ -705,7 +709,9 @@ async function build() {
return resolveConfig()
}

let [beforePlugins, afterPlugins] = includePostCss ? await loadPostCssPlugins() : [[], []]
let [beforePlugins, afterPlugins] = includePostCss
? await loadPostCssPlugins()
: loadBuiltinPostcssPlugins()

let plugins = [
...beforePlugins,
Expand Down