Skip to content

Commit c238ed1

Browse files
authored
Improve compat mode (#2775)
* simplify compat mode * make sure postcss is included * make sure we cannot go into compatibility mode twice
1 parent 83c685a commit c238ed1

File tree

3 files changed

+47
-20
lines changed

3 files changed

+47
-20
lines changed

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
"prepublishOnly": "npm run babelify && babel-node scripts/build.js",
2525
"style": "eslint .",
2626
"test": "jest && eslint .",
27-
"precompat": "npm run babelify",
2827
"compat": "node scripts/compat.js --prepare",
2928
"compat:restore": "node scripts/compat.js --restore"
3029
},

scripts/compat.js

+46-19
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,80 @@ function fromRootPath(...paths) {
66
return path.resolve(process.cwd(), ...paths)
77
}
88

9-
function backupPath(...paths) {
10-
return path.resolve(process.cwd(), 'node_modules', '__tw_cache__', ...paths)
11-
}
12-
139
function copy(fromPath, toPath) {
14-
fs.mkdirSync(path.dirname(toPath), { recursive: true })
1510
fs.copyFileSync(fromPath, toPath)
1611
}
1712

1813
if (process.argv.includes('--prepare')) {
14+
if (
15+
fs.existsSync(fromRootPath('package.postcss8.json')) ||
16+
fs.existsSync(fromRootPath('src', 'index.postcss8.js'))
17+
) {
18+
console.error('\n\n[ABORT] Already in PostCSS 7 compatibility mode!\n\n')
19+
process.exit(1)
20+
}
21+
1922
const mainPackageJson = require('../package.json')
2023
const compatPackageJson = require('../package.postcss7.json')
2124

2225
// 1. Backup original package.json file
23-
copy(fromRootPath('package.json'), backupPath('package.json'))
26+
copy(fromRootPath('package.json'), fromRootPath('package.postcss8.json'))
2427

25-
// 2. Backup lib/index.js file
26-
copy(fromRootPath('lib', 'index.js'), backupPath('lib', 'index.js'))
28+
// 2. Backup src/index.js file
29+
copy(fromRootPath('src', 'index.js'), fromRootPath('src', 'index.postcss8.js'))
2730

28-
// 3. Use the postcss7 compat file
29-
copy(fromRootPath('lib', 'index.postcss7.js'), fromRootPath('lib', 'index.js'))
31+
// 3. Use the PostCSS 7 compat file
32+
copy(fromRootPath('src', 'index.postcss7.js'), fromRootPath('src', 'index.js'))
3033

3134
// 4. Deep merge package.json contents
3235
const packageJson = merge({}, mainPackageJson, compatPackageJson)
3336

34-
// 5. Write package.json with the new contents
37+
// 5. Remove peerDependencies
38+
delete packageJson.peerDependencies
39+
40+
// 6. Write package.json with the new contents
3541
fs.writeFileSync(fromRootPath('package.json'), JSON.stringify(packageJson, null, 2), 'utf8')
3642

37-
// 6. Print some useful information to make publishing easy
43+
// 7. Print some useful information to make publishing easy
3844
console.log()
3945
console.log('You can safely publish `tailwindcss` in PostCSS 7 compatibility mode:\n')
4046
console.log(
41-
['npm version', 'npm publish --tag compat', 'npm run compat:restore']
47+
[
48+
// Not necessary, but a quick 'hash', basically the current date/time
49+
`git checkout -b compat-${new Date()
50+
.toJSON()
51+
.replace(/[-:.TZ]/g, '') // Remove weird characters
52+
.slice(0, -3)}`, // Remove milliseconds precision
53+
'git add .',
54+
'git commit -m "compat"',
55+
'npm version',
56+
'npm publish --tag compat',
57+
'npm run compat:restore',
58+
]
4259
.map((v) => ` ${v}`)
4360
.join('\n')
4461
)
4562
console.log()
4663
} else if (process.argv.includes('--restore')) {
64+
if (
65+
!fs.existsSync(fromRootPath('package.postcss8.json')) ||
66+
!fs.existsSync(fromRootPath('src', 'index.postcss8.js'))
67+
) {
68+
console.error('\n\n[ABORT] Already in latest PostCSS mode!\n\n')
69+
process.exit(1)
70+
}
71+
4772
// 1. Restore original package.json file
48-
copy(backupPath('package.json'), fromRootPath('package.json'))
49-
fs.unlinkSync(backupPath('package.json'))
73+
copy(fromRootPath('package.postcss8.json'), fromRootPath('package.json'))
74+
75+
// 2. Restore src/index.js file
76+
copy(fromRootPath('src', 'index.postcss8.js'), fromRootPath('src', 'index.js'))
5077

51-
// 2. Restore lib/index.js file
52-
copy(backupPath('lib', 'index.js'), fromRootPath('lib', 'index.js'))
53-
fs.unlinkSync(backupPath('lib', 'index.js'))
78+
// 3. Cleanup PostCSS 8 related files
79+
fs.unlinkSync(fromRootPath('package.postcss8.json'))
80+
fs.unlinkSync(fromRootPath('src', 'index.postcss8.js'))
5481

55-
// 3. Done
82+
// 4. Done
5683
console.log()
5784
console.log('Restored from PostCSS 7 mode to latest PostCSS mode!')
5885
console.log()

src/index.postcss7.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import path from 'path'
22
import fs from 'fs'
33

44
import _ from 'lodash'
5+
import postcss from 'postcss'
56

67
import getModuleDependencies from './lib/getModuleDependencies'
78
import registerConfigAsDependency from './lib/registerConfigAsDependency'

0 commit comments

Comments
 (0)