From 83b16e5359ff7ee6a04559dd53056dfbce73167c Mon Sep 17 00:00:00 2001 From: Adam Wathan Date: Wed, 18 Nov 2020 12:42:00 -0500 Subject: [PATCH] Compat --- package.json | 14 +++--- package.postcss8.json | 112 ++++++++++++++++++++++++++++++++++++++++++ src/index.js | 20 ++++---- src/index.postcss8.js | 83 +++++++++++++++++++++++++++++++ 4 files changed, 210 insertions(+), 19 deletions(-) create mode 100644 package.postcss8.json create mode 100644 src/index.postcss8.js diff --git a/package.json b/package.json index 1d162bb24..dad60f823 100644 --- a/package.json +++ b/package.json @@ -51,10 +51,6 @@ "prettier": "^2.1.2", "rimraf": "^3.0.0" }, - "peerDependencies": { - "autoprefixer": "^10.0.2", - "postcss": "^8.0.9" - }, "dependencies": { "@fullhuman/postcss-purgecss": "^3.0.0", "bytes": "^3.0.0", @@ -69,13 +65,15 @@ "node-emoji": "^1.8.1", "object-hash": "^2.0.3", "postcss-functions": "^3", - "postcss-js": "^3.0.3", - "postcss-nested": "^5.0.1", + "postcss-js": "^2", + "postcss-nested": "^4", "postcss-selector-parser": "^6.0.4", "postcss-value-parser": "^4.1.0", "pretty-hrtime": "^1.0.3", "reduce-css-calc": "^2.1.6", - "resolve": "^1.19.0" + "resolve": "^1.19.0", + "autoprefixer": "^9", + "postcss": "^7" }, "browserslist": [ "> 1%", @@ -109,4 +107,4 @@ "engines": { "node": ">=12.13.0" } -} +} \ No newline at end of file diff --git a/package.postcss8.json b/package.postcss8.json new file mode 100644 index 000000000..1d162bb24 --- /dev/null +++ b/package.postcss8.json @@ -0,0 +1,112 @@ +{ + "name": "tailwindcss", + "version": "2.0.0", + "description": "A utility-first CSS framework for rapidly building custom user interfaces.", + "license": "MIT", + "main": "lib/index.js", + "style": "dist/tailwind.css", + "repository": "https://github.com/tailwindlabs/tailwindcss.git", + "bugs": "https://github.com/tailwindlabs/tailwindcss/issues", + "homepage": "https://tailwindcss.com", + "bin": { + "tailwind": "lib/cli.js", + "tailwindcss": "lib/cli.js" + }, + "contributors": [ + "Adam Wathan ", + "Jonathan Reinink ", + "David Hemphill " + ], + "scripts": { + "prebabelify": "rimraf lib", + "babelify": "babel src --out-dir lib --copy-files", + "rebuild-fixtures": "npm run babelify && babel-node scripts/rebuildFixtures.js", + "prepublishOnly": "npm run babelify && babel-node scripts/build.js", + "style": "eslint .", + "test": "jest && eslint .", + "compat": "node scripts/compat.js --prepare", + "compat:restore": "node scripts/compat.js --restore" + }, + "files": [ + "dist/*.css", + "lib/*", + "scripts/*.js", + "stubs/*.stub.js", + "*.css", + "*.js" + ], + "devDependencies": { + "@babel/cli": "^7.11.6", + "@babel/core": "^7.12.3", + "@babel/node": "^7.0.0", + "@babel/preset-env": "^7.0.0", + "autoprefixer": "^10.0.2", + "babel-jest": "^26.6.3", + "clean-css": "^4.1.9", + "eslint": "^7.12.1", + "eslint-config-prettier": "^6.15.0", + "eslint-plugin-prettier": "^3.1.4", + "jest": "^26.6.3", + "postcss": "^8.0.9", + "prettier": "^2.1.2", + "rimraf": "^3.0.0" + }, + "peerDependencies": { + "autoprefixer": "^10.0.2", + "postcss": "^8.0.9" + }, + "dependencies": { + "@fullhuman/postcss-purgecss": "^3.0.0", + "bytes": "^3.0.0", + "chalk": "^4.1.0", + "color": "^3.1.3", + "detective": "^5.2.0", + "didyoumean": "^1.2.1", + "fs-extra": "^9.0.1", + "html-tags": "^3.1.0", + "lodash": "^4.17.20", + "modern-normalize": "^1.0.0", + "node-emoji": "^1.8.1", + "object-hash": "^2.0.3", + "postcss-functions": "^3", + "postcss-js": "^3.0.3", + "postcss-nested": "^5.0.1", + "postcss-selector-parser": "^6.0.4", + "postcss-value-parser": "^4.1.0", + "pretty-hrtime": "^1.0.3", + "reduce-css-calc": "^2.1.6", + "resolve": "^1.19.0" + }, + "browserslist": [ + "> 1%", + "not edge <= 18", + "not ie 11", + "not op_mini all" + ], + "babel": { + "presets": [ + [ + "@babel/preset-env", + { + "targets": { + "node": "12.13.0" + } + } + ] + ] + }, + "jest": { + "collectCoverage": true, + "testTimeout": 30000, + "setupFilesAfterEnv": [ + "/jest/customMatchers.js" + ], + "testPathIgnorePatterns": [ + "/__tests__/fixtures/", + "/__tests__/util/" + ] + }, + "engines": { + "node": ">=12.13.0" + } +} diff --git a/src/index.js b/src/index.js index 1393f8af7..98fd13c4b 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ import path from 'path' import fs from 'fs' import _ from 'lodash' +import postcss from 'postcss' import getModuleDependencies from './lib/getModuleDependencies' import registerConfigAsDependency from './lib/registerConfigAsDependency' @@ -62,7 +63,7 @@ const getConfigFunction = (config) => () => { return resolveConfig([...getAllConfigs(configObject)]) } -module.exports = function (config) { +const plugin = postcss.plugin('tailwindcss', (config) => { const plugins = [] const resolvedConfigPath = resolveConfigPath(config) @@ -70,14 +71,11 @@ module.exports = function (config) { plugins.push(registerConfigAsDependency(resolvedConfigPath)) } - return { - postcssPlugin: 'tailwindcss', - plugins: [ - ...plugins, - processTailwindFeatures(getConfigFunction(resolvedConfigPath || config)), - formatCSS, - ], - } -} + return postcss([ + ...plugins, + processTailwindFeatures(getConfigFunction(resolvedConfigPath || config)), + formatCSS, + ]) +}) -module.exports.postcss = true +module.exports = plugin diff --git a/src/index.postcss8.js b/src/index.postcss8.js new file mode 100644 index 000000000..1393f8af7 --- /dev/null +++ b/src/index.postcss8.js @@ -0,0 +1,83 @@ +import path from 'path' +import fs from 'fs' + +import _ from 'lodash' + +import getModuleDependencies from './lib/getModuleDependencies' +import registerConfigAsDependency from './lib/registerConfigAsDependency' +import processTailwindFeatures from './processTailwindFeatures' +import formatCSS from './lib/formatCSS' +import resolveConfig from './util/resolveConfig' +import getAllConfigs from './util/getAllConfigs' +import { defaultConfigFile } from './constants' +import defaultConfig from '../stubs/defaultConfig.stub.js' + +function resolveConfigPath(filePath) { + // require('tailwindcss')({ theme: ..., variants: ... }) + if (_.isObject(filePath) && !_.has(filePath, 'config') && !_.isEmpty(filePath)) { + return undefined + } + + // require('tailwindcss')({ config: 'custom-config.js' }) + if (_.isObject(filePath) && _.has(filePath, 'config') && _.isString(filePath.config)) { + return path.resolve(filePath.config) + } + + // require('tailwindcss')({ config: { theme: ..., variants: ... } }) + if (_.isObject(filePath) && _.has(filePath, 'config') && _.isObject(filePath.config)) { + return undefined + } + + // require('tailwindcss')('custom-config.js') + if (_.isString(filePath)) { + return path.resolve(filePath) + } + + // require('tailwindcss') + try { + const defaultConfigPath = path.resolve(defaultConfigFile) + fs.accessSync(defaultConfigPath) + return defaultConfigPath + } catch (err) { + return undefined + } +} + +const getConfigFunction = (config) => () => { + if (_.isUndefined(config)) { + return resolveConfig([...getAllConfigs(defaultConfig)]) + } + + // Skip this if Jest is running: https://github.com/facebook/jest/pull/9841#issuecomment-621417584 + if (process.env.JEST_WORKER_ID === undefined) { + if (!_.isObject(config)) { + getModuleDependencies(config).forEach((mdl) => { + delete require.cache[require.resolve(mdl.file)] + }) + } + } + + const configObject = _.isObject(config) ? _.get(config, 'config', config) : require(config) + + return resolveConfig([...getAllConfigs(configObject)]) +} + +module.exports = function (config) { + const plugins = [] + const resolvedConfigPath = resolveConfigPath(config) + + if (!_.isUndefined(resolvedConfigPath)) { + plugins.push(registerConfigAsDependency(resolvedConfigPath)) + } + + return { + postcssPlugin: 'tailwindcss', + plugins: [ + ...plugins, + processTailwindFeatures(getConfigFunction(resolvedConfigPath || config)), + formatCSS, + ], + } +} + +module.exports.postcss = true