Skip to content

Commit

Permalink
Add postcss-import support to the CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
thecrypticace committed May 25, 2022
1 parent 22f9dc8 commit 974ae9f
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 4 deletions.
110 changes: 109 additions & 1 deletion integrations/tailwindcss-cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions integrations/tailwindcss-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"build": "NODE_ENV=production node ../../lib/cli.js -i ./src/index.css -o ./dist/main.css",
"test": "jest --runInBand --forceExit"
},
"dependencies": {
"tailwindcss": "file:../../"
},
"jest": {
"testTimeout": 10000,
"displayName": "Tailwind CSS CLI",
Expand Down
5 changes: 5 additions & 0 deletions integrations/tailwindcss-cli/src/imported.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@layer utilities {
.something-cool {
color: red;
}
}
54 changes: 54 additions & 0 deletions integrations/tailwindcss-cli/tests/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,60 @@ describe('Build command', () => {
expect(contents).toContain(`/*# sourceMappingURL`)
})

test('postcss-import is supported by default', 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';
`
)

await $(
`${EXECUTABLE} --input ./src/test.css --content ./src/index.html --output ./dist/main.css`
)

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')

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';
`
)

await $(
`${EXECUTABLE} --input ./src/test.css --content ./src/index.html --output ./dist/main.css --postcss`
)

expect(await readOutputFile('main.css')).toIncludeCss(
css`
@import './imported.css';
`
)
})

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

Expand Down
56 changes: 56 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@
"normalize-path": "^3.0.0",
"object-hash": "^3.0.0",
"picocolors": "^1.0.0",
"postcss": "^8.4.13",
"postcss": "^8.0.9",
"postcss-import": "^14.1.0",
"postcss-js": "^4.0.0",
"postcss-load-config": "^3.1.4",
"postcss-nested": "5.0.6",
Expand Down
4 changes: 4 additions & 0 deletions src/cli-peer-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ export function lazyPostcss() {
return require('postcss')
}

export function lazyPostcssImport() {
return require('postcss-import')
}

export function lazyAutoprefixer() {
return require('autoprefixer')
}
Expand Down
16 changes: 14 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

import { lazyPostcss, lazyCssnano, lazyAutoprefixer } from '../peers/index.js'
import { lazyPostcss, lazyPostcssImport, lazyCssnano, lazyAutoprefixer } from '../peers/index.js'

import chokidar from 'chokidar'
import path from 'path'
Expand Down Expand Up @@ -581,7 +581,19 @@ async function build() {

let [beforePlugins, afterPlugins, postcssOptions] = includePostCss
? await loadPostCssPlugins()
: [[], [], {}]
: [
[
(() => {
try {
return require('postcss-import')
} catch {}

return lazyPostcssImport()
})(),
],
[],
{},
]

let plugins = [
...beforePlugins,
Expand Down

0 comments on commit 974ae9f

Please sign in to comment.