Skip to content

Commit 1d8679d

Browse files
authored
Postcss7 compatibility (#2773)
* add postcss7 compatibility layers * add compatibility mode scripts
1 parent d4cde73 commit 1d8679d

File tree

4 files changed

+152
-1
lines changed

4 files changed

+152
-1
lines changed

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
"rebuild-fixtures": "npm run babelify && babel-node scripts/rebuildFixtures.js",
2424
"prepublishOnly": "npm run babelify && babel-node scripts/build.js",
2525
"style": "eslint .",
26-
"test": "jest && eslint ."
26+
"test": "jest && eslint .",
27+
"precompat": "npm run babelify",
28+
"compat": "node scripts/compat.js --prepare",
29+
"compat:restore": "node scripts/compat.js --restore"
2730
},
2831
"files": [
2932
"dist/*.css",

package.postcss7.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"dependencies": {
3+
"autoprefixer": "^9",
4+
"postcss": "^7",
5+
"postcss-functions": "^3",
6+
"postcss-js": "^2",
7+
"postcss-nested": "^4"
8+
}
9+
}

scripts/compat.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
const merge = require('lodash/merge')
4+
5+
function fromRootPath(...paths) {
6+
return path.resolve(process.cwd(), ...paths)
7+
}
8+
9+
function backupPath(...paths) {
10+
return path.resolve(process.cwd(), 'node_modules', '__tw_cache__', ...paths)
11+
}
12+
13+
function copy(fromPath, toPath) {
14+
fs.mkdirSync(path.dirname(toPath), { recursive: true })
15+
fs.copyFileSync(fromPath, toPath)
16+
}
17+
18+
if (process.argv.includes('--prepare')) {
19+
const mainPackageJson = require('../package.json')
20+
const compatPackageJson = require('../package.postcss7.json')
21+
22+
// 1. Backup original package.json file
23+
copy(fromRootPath('package.json'), backupPath('package.json'))
24+
25+
// 2. Backup lib/index.js file
26+
copy(fromRootPath('lib', 'index.js'), backupPath('lib', 'index.js'))
27+
28+
// 3. Use the postcss7 compat file
29+
copy(fromRootPath('lib', 'index.postcss7.js'), fromRootPath('lib', 'index.js'))
30+
31+
// 4. Deep merge package.json contents
32+
const packageJson = merge({}, mainPackageJson, compatPackageJson)
33+
34+
// 5. Write package.json with the new contents
35+
fs.writeFileSync(fromRootPath('package.json'), JSON.stringify(packageJson, null, 2), 'utf8')
36+
37+
// 6. Print some useful information to make publishing easy
38+
console.log()
39+
console.log('You can safely publish `tailwindcss` in PostCSS 7 compatibility mode:\n')
40+
console.log(
41+
['npm version', 'npm publish --tag compat', 'npm run compat:restore']
42+
.map((v) => ` ${v}`)
43+
.join('\n')
44+
)
45+
console.log()
46+
} else if (process.argv.includes('--restore')) {
47+
// 1. Restore original package.json file
48+
copy(backupPath('package.json'), fromRootPath('package.json'))
49+
fs.unlinkSync(backupPath('package.json'))
50+
51+
// 2. Restore lib/index.js file
52+
copy(backupPath('lib', 'index.js'), fromRootPath('lib', 'index.js'))
53+
fs.unlinkSync(backupPath('lib', 'index.js'))
54+
55+
// 3. Done
56+
console.log()
57+
console.log('Restored from PostCSS 7 mode to latest PostCSS mode!')
58+
console.log()
59+
}

src/index.postcss7.js

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import path from 'path'
2+
import fs from 'fs'
3+
4+
import _ from 'lodash'
5+
6+
import getModuleDependencies from './lib/getModuleDependencies'
7+
import registerConfigAsDependency from './lib/registerConfigAsDependency'
8+
import processTailwindFeatures from './processTailwindFeatures'
9+
import formatCSS from './lib/formatCSS'
10+
import resolveConfig from './util/resolveConfig'
11+
import getAllConfigs from './util/getAllConfigs'
12+
import { defaultConfigFile } from './constants'
13+
import defaultConfig from '../stubs/defaultConfig.stub.js'
14+
15+
function resolveConfigPath(filePath) {
16+
// require('tailwindcss')({ theme: ..., variants: ... })
17+
if (_.isObject(filePath) && !_.has(filePath, 'config') && !_.isEmpty(filePath)) {
18+
return undefined
19+
}
20+
21+
// require('tailwindcss')({ config: 'custom-config.js' })
22+
if (_.isObject(filePath) && _.has(filePath, 'config') && _.isString(filePath.config)) {
23+
return path.resolve(filePath.config)
24+
}
25+
26+
// require('tailwindcss')({ config: { theme: ..., variants: ... } })
27+
if (_.isObject(filePath) && _.has(filePath, 'config') && _.isObject(filePath.config)) {
28+
return undefined
29+
}
30+
31+
// require('tailwindcss')('custom-config.js')
32+
if (_.isString(filePath)) {
33+
return path.resolve(filePath)
34+
}
35+
36+
// require('tailwindcss')
37+
try {
38+
const defaultConfigPath = path.resolve(defaultConfigFile)
39+
fs.accessSync(defaultConfigPath)
40+
return defaultConfigPath
41+
} catch (err) {
42+
return undefined
43+
}
44+
}
45+
46+
const getConfigFunction = (config) => () => {
47+
if (_.isUndefined(config)) {
48+
return resolveConfig([...getAllConfigs(defaultConfig)])
49+
}
50+
51+
// Skip this if Jest is running: https://github.com/facebook/jest/pull/9841#issuecomment-621417584
52+
if (process.env.JEST_WORKER_ID === undefined) {
53+
if (!_.isObject(config)) {
54+
getModuleDependencies(config).forEach((mdl) => {
55+
delete require.cache[require.resolve(mdl.file)]
56+
})
57+
}
58+
}
59+
60+
const configObject = _.isObject(config) ? _.get(config, 'config', config) : require(config)
61+
62+
return resolveConfig([...getAllConfigs(configObject)])
63+
}
64+
65+
const plugin = postcss.plugin('tailwind', (config) => {
66+
const plugins = []
67+
const resolvedConfigPath = resolveConfigPath(config)
68+
69+
if (!_.isUndefined(resolvedConfigPath)) {
70+
plugins.push(registerConfigAsDependency(resolvedConfigPath))
71+
}
72+
73+
return postcss([
74+
...plugins,
75+
processTailwindFeatures(getConfigFunction(resolvedConfigPath || config)),
76+
formatCSS,
77+
])
78+
})
79+
80+
module.exports = plugin

0 commit comments

Comments
 (0)