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

Fix usage of postcss.config.js in standalone CLI #8769

Merged
merged 4 commits into from Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
180 changes: 121 additions & 59 deletions standalone-cli/package-lock.json

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

1 change: 1 addition & 0 deletions standalone-cli/package.json
Expand Up @@ -12,6 +12,7 @@
"@tailwindcss/forms": "^0.4.0",
"@tailwindcss/line-clamp": "^0.3.0",
"@tailwindcss/typography": "^0.5.0",
"fs-extra": "^10.1.0",
"jest": "^27.2.5",
"move-file-cli": "^3.0.0",
"pkg": "^5.3.3",
Expand Down
4 changes: 4 additions & 0 deletions standalone-cli/standalone.js
Expand Up @@ -12,6 +12,10 @@ let localModules = {
'@tailwindcss/forms': require('@tailwindcss/forms'),
'@tailwindcss/line-clamp': require('@tailwindcss/line-clamp'),
'@tailwindcss/typography': require('@tailwindcss/typography'),

// These are present to allow them to be specified in the PostCSS config file
autoprefixer: require('autoprefixer'),
tailwindcss: require('tailwindcss'),
}

Module.prototype.require = function (id) {
Expand Down
1 change: 1 addition & 0 deletions standalone-cli/tests/fixtures/basic.html
@@ -1 +1,2 @@
<div class="uppercase"></div>
<div class="[will-change:opacity]"></div>
7 changes: 7 additions & 0 deletions standalone-cli/tests/fixtures/postcss.config.js
@@ -0,0 +1,7 @@
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
'postcss-will-change': {},
},
}
61 changes: 60 additions & 1 deletion standalone-cli/tests/test.js
@@ -1,4 +1,6 @@
const { execSync } = require('child_process')
const os = require('os')
const fs = require('fs-extra')

const platformMap = {
darwin: 'macos',
Expand All @@ -13,7 +15,13 @@ function exec(args) {
}

it('works', () => {
expect(exec('--content tests/fixtures/basic.html')).toContain('.uppercase')
let result = exec('--content tests/fixtures/basic.html')
expect(result).toContain('.uppercase')
expect(result).toContain('.\\[will-change\\:opacity\\]')
expect(result).toContain('will-change: opacity')

// Verify that no plugins are installed that modify the `[will-change:opacity]` class
expect(result).not.toContain('backface-visibility: hidden')
})

it('supports first-party plugins', () => {
Expand All @@ -23,3 +31,54 @@ it('supports first-party plugins', () => {
expect(result).toContain('.line-clamp-2')
expect(result).toContain('.prose')
})

it('supports postcss config files', async () => {
// We have to run this test outside of any place with node_modules for it to properly test this situation
let result = await inIsolatedContext(() => {
// Emulate the user adding their own postcss plugins
execSync(`npm install postcss-will-change`)

return exec('--content tests/fixtures/basic.html --postcss tests/fixtures/postcss.config.js')
})

expect(result).toContain('.uppercase')

// Ensure the custom added postcss plugin is working
expect(result).toContain('will-change: opacity')
expect(result).toContain('backface-visibility: hidden')
})

/**
* @template T
* @param {() => T} fn
* @returns {T}
*/
async function inIsolatedContext(fn) {
// Create a new directory entirely outside of the package for the test
let dest = `${os.tmpdir()}/tailwindcss-cli`

// Recursively copy the dist and tests folders
let dirs = ['dist', 'tests']

await Promise.all(
dirs.map((dir) =>
fs.copy(`${__dirname}/../${dir}`, `${dest}/${dir}`, {
overwrite: true,
recursive: true,
})
)
)

// Change the working directory to the new directory
process.chdir(dest)

try {
return await fn()
} finally {
// Change back to the original working directory
process.chdir(__dirname)

// Delete the new directory
await fs.rmdir(dest, { recursive: true })
}
}