Skip to content

Commit 5cc0c7c

Browse files
Support ESM and TS config files (#137)
* add failing tests * fix loading esm/ts configs * Update ESM/TS support --------- Co-authored-by: Brad Cornes <[email protected]>
1 parent 6533049 commit 5cc0c7c

22 files changed

+1605
-169
lines changed

package-lock.json

Lines changed: 262 additions & 157 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"build": "npm run _esbuild -- --minify",
2222
"predev": "npm run _pre",
2323
"dev": "npm run _esbuild -- --watch",
24+
"pretest": "node scripts/install-fixture-deps.js",
2425
"test": "jest",
2526
"prepublishOnly": "npm run build && npm test && node scripts/copy-licenses.js",
2627
"format": "prettier \"src/**/*.js\" \"scripts/**/*.js\" \"tests/test.js\" --write --print-width 100 --single-quote --no-semi --plugin-search-dir ./tests"
@@ -33,10 +34,10 @@
3334
"@shufo/prettier-plugin-blade": "^1.8.4",
3435
"@tailwindcss/line-clamp": "^0.3.0",
3536
"@trivago/prettier-plugin-sort-imports": "^3.4.0",
37+
"clear-module": "^4.1.2",
3638
"cpy-cli": "^3.1.1",
3739
"esbuild": "^0.14.11",
3840
"escalade": "^3.1.1",
39-
"import-fresh": "^3.3.0",
4041
"import-from": "^4.0.0",
4142
"import-sort-style-module": "^6.0.0",
4243
"jest": "^27.4.7",
@@ -57,7 +58,7 @@
5758
"recast": "^0.20.5",
5859
"rimraf": "^3.0.2",
5960
"svelte": "^3.55.0",
60-
"tailwindcss": "^3.0.23"
61+
"tailwindcss": "^3.3.0"
6162
},
6263
"peerDependencies": {
6364
"@ianvs/prettier-plugin-sort-imports": "*",

scripts/install-fixture-deps.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
const { execSync } = require('child_process')
4+
5+
let fixturesDir = path.resolve(__dirname, '../tests/fixtures')
6+
let fixtures = fs.readdirSync(fixturesDir).map((name) => path.join(fixturesDir, name))
7+
8+
for (let fixture of fixtures) {
9+
if (fs.existsSync(path.join(fixture, 'package.json'))) {
10+
execSync('npm install', { cwd: fixture })
11+
}
12+
}

src/index.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ import prettierParserTypescript from 'prettier/parser-typescript'
1111
import { createContext as createContextFallback } from 'tailwindcss/lib/lib/setupContextUtils'
1212
import { generateRules as generateRulesFallback } from 'tailwindcss/lib/lib/generateRules'
1313
import resolveConfigFallback from 'tailwindcss/resolveConfig'
14+
import loadConfigFallback from 'tailwindcss/loadConfig'
1415
import * as recast from 'recast'
1516
import * as astTypes from 'ast-types'
1617
import * as path from 'path'
1718
import requireFrom from 'import-from'
18-
import requireFresh from 'import-fresh'
1919
import objectHash from 'object-hash'
2020
import lineColumn from 'line-column'
2121
import jsesc from 'jsesc'
2222
import escalade from 'escalade/sync'
23+
import clearModule from 'clear-module'
2324

2425
let base = getBasePlugins()
2526

@@ -148,20 +149,36 @@ function createParser(parserFormat, transform) {
148149
let resolveConfig = resolveConfigFallback
149150
let createContext = createContextFallback
150151
let generateRules = generateRulesFallback
152+
let loadConfig = loadConfigFallback
151153

152154
let baseDir
153155
let prettierConfigPath = prettier.resolveConfigFile.sync(options.filepath)
154156

155157
if (options.tailwindConfig) {
156158
baseDir = prettierConfigPath ? path.dirname(prettierConfigPath) : process.cwd()
157-
tailwindConfigPath = path.resolve(baseDir, options.tailwindConfig)
158-
tailwindConfig = requireFresh(tailwindConfigPath)
159159
} else {
160160
baseDir = prettierConfigPath
161161
? path.dirname(prettierConfigPath)
162162
: options.filepath
163163
? path.dirname(options.filepath)
164164
: process.cwd()
165+
}
166+
167+
try {
168+
resolveConfig = requireFrom(baseDir, 'tailwindcss/resolveConfig')
169+
createContext = requireFrom(baseDir, 'tailwindcss/lib/lib/setupContextUtils').createContext
170+
generateRules = requireFrom(baseDir, 'tailwindcss/lib/lib/generateRules').generateRules
171+
172+
// Prior to `[email protected]` this won't exist so we load it last
173+
loadConfig = requireFrom(baseDir, 'tailwindcss/loadConfig')
174+
} catch {}
175+
176+
if (options.tailwindConfig) {
177+
tailwindConfigPath = path.resolve(baseDir, options.tailwindConfig)
178+
clearModule(tailwindConfigPath)
179+
const loadedConfig = loadConfig(tailwindConfigPath)
180+
tailwindConfig = loadedConfig.default ?? loadedConfig
181+
} else {
165182
let configPath
166183
try {
167184
configPath = escalade(baseDir, (_dir, names) => {
@@ -171,20 +188,22 @@ function createParser(parserFormat, transform) {
171188
if (names.includes('tailwind.config.cjs')) {
172189
return 'tailwind.config.cjs'
173190
}
191+
if (names.includes('tailwind.config.mjs')) {
192+
return 'tailwind.config.mjs'
193+
}
194+
if (names.includes('tailwind.config.ts')) {
195+
return 'tailwind.config.ts'
196+
}
174197
})
175198
} catch {}
176199
if (configPath) {
177200
tailwindConfigPath = configPath
178-
tailwindConfig = requireFresh(configPath)
201+
clearModule(tailwindConfigPath)
202+
const loadedConfig = loadConfig(tailwindConfigPath)
203+
tailwindConfig = loadedConfig.default ?? loadedConfig
179204
}
180205
}
181206

182-
try {
183-
resolveConfig = requireFrom(baseDir, 'tailwindcss/resolveConfig')
184-
createContext = requireFrom(baseDir, 'tailwindcss/lib/lib/setupContextUtils').createContext
185-
generateRules = requireFrom(baseDir, 'tailwindcss/lib/lib/generateRules').generateRules
186-
} catch {}
187-
188207
// suppress "empty content" warning
189208
tailwindConfig.content = ['no-op']
190209

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default {
2+
theme: {
3+
extend: {
4+
colors: {
5+
hotpink: "hotpink",
6+
},
7+
},
8+
},
9+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div class="sm:bg-hotpink bg-red-500"></div>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
tailwindConfig: './config.mjs'
3+
};

tests/fixtures/esm/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div class="sm:bg-hotpink bg-red-500"></div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = {};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default {
2+
theme: {
3+
extend: {
4+
colors: {
5+
hotpink: "hotpink",
6+
},
7+
},
8+
},
9+
};

0 commit comments

Comments
 (0)