Skip to content

Commit 445970d

Browse files
Fix usage of postcss.config.js in standalone CLI (#8769)
* Update deps * Fix usage of postcss config file in standalone CLI The config file created with `--postcss` would fail because we didn’t stub require to load `tailwindcss` or `autoprefixer` when we should. * Update tests * WIP
1 parent d4f1f15 commit 445970d

File tree

6 files changed

+194
-60
lines changed

6 files changed

+194
-60
lines changed

standalone-cli/package-lock.json

+121-59
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

standalone-cli/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"@tailwindcss/forms": "^0.4.0",
1313
"@tailwindcss/line-clamp": "^0.3.0",
1414
"@tailwindcss/typography": "^0.5.0",
15+
"fs-extra": "^10.1.0",
1516
"jest": "^27.2.5",
1617
"move-file-cli": "^3.0.0",
1718
"pkg": "^5.3.3",

standalone-cli/standalone.js

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ let localModules = {
1212
'@tailwindcss/forms': require('@tailwindcss/forms'),
1313
'@tailwindcss/line-clamp': require('@tailwindcss/line-clamp'),
1414
'@tailwindcss/typography': require('@tailwindcss/typography'),
15+
16+
// These are present to allow them to be specified in the PostCSS config file
17+
autoprefixer: require('autoprefixer'),
18+
tailwindcss: require('tailwindcss'),
1519
}
1620

1721
Module.prototype.require = function (id) {
+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
<div class="uppercase"></div>
2+
<div class="[will-change:opacity]"></div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
plugins: {
3+
tailwindcss: {},
4+
autoprefixer: {},
5+
'postcss-will-change': {},
6+
},
7+
}

standalone-cli/tests/test.js

+60-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const { execSync } = require('child_process')
2+
const os = require('os')
3+
const fs = require('fs-extra')
24

35
const platformMap = {
46
darwin: 'macos',
@@ -13,7 +15,13 @@ function exec(args) {
1315
}
1416

1517
it('works', () => {
16-
expect(exec('--content tests/fixtures/basic.html')).toContain('.uppercase')
18+
let result = exec('--content tests/fixtures/basic.html')
19+
expect(result).toContain('.uppercase')
20+
expect(result).toContain('.\\[will-change\\:opacity\\]')
21+
expect(result).toContain('will-change: opacity')
22+
23+
// Verify that no plugins are installed that modify the `[will-change:opacity]` class
24+
expect(result).not.toContain('backface-visibility: hidden')
1725
})
1826

1927
it('supports first-party plugins', () => {
@@ -23,3 +31,54 @@ it('supports first-party plugins', () => {
2331
expect(result).toContain('.line-clamp-2')
2432
expect(result).toContain('.prose')
2533
})
34+
35+
it('supports postcss config files', async () => {
36+
// We have to run this test outside of any place with node_modules for it to properly test this situation
37+
let result = await inIsolatedContext(() => {
38+
// Emulate the user adding their own postcss plugins
39+
execSync(`npm install postcss-will-change`)
40+
41+
return exec('--content tests/fixtures/basic.html --postcss tests/fixtures/postcss.config.js')
42+
})
43+
44+
expect(result).toContain('.uppercase')
45+
46+
// Ensure the custom added postcss plugin is working
47+
expect(result).toContain('will-change: opacity')
48+
expect(result).toContain('backface-visibility: hidden')
49+
})
50+
51+
/**
52+
* @template T
53+
* @param {() => T} fn
54+
* @returns {T}
55+
*/
56+
async function inIsolatedContext(fn) {
57+
// Create a new directory entirely outside of the package for the test
58+
let dest = `${os.tmpdir()}/tailwindcss-cli`
59+
60+
// Recursively copy the dist and tests folders
61+
let dirs = ['dist', 'tests']
62+
63+
await Promise.all(
64+
dirs.map((dir) =>
65+
fs.copy(`${__dirname}/../${dir}`, `${dest}/${dir}`, {
66+
overwrite: true,
67+
recursive: true,
68+
})
69+
)
70+
)
71+
72+
// Change the working directory to the new directory
73+
process.chdir(dest)
74+
75+
try {
76+
return await fn()
77+
} finally {
78+
// Change back to the original working directory
79+
process.chdir(__dirname)
80+
81+
// Delete the new directory
82+
await fs.rmdir(dest, { recursive: true })
83+
}
84+
}

0 commit comments

Comments
 (0)