Skip to content

Commit 8982118

Browse files
authored
Convert resolveTailwindConfig.js to ESM (#108)
* Convert resolve script to ESM * Fix missing config default key * Clean up resolveTailwindConfig.js * Remove async statement on parseConfig fn
1 parent a74d4dd commit 8982118

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

src/resolveTailwindConfig.js

+27-22
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,43 @@
11
#!/usr/bin/env node
2-
const fs = require('fs');
2+
const TEMP_DIR = './tmp';
3+
const OUTPUT_PATH = `${TEMP_DIR}/tailwind.config.php`;
34

4-
try {
5-
const resolveConfig = require('tailwindcss/resolveConfig');
6-
const config = require(process.env.CONFIGPATH);
7-
const tempDir = './tmp';
8-
const outputPath = `${tempDir}/tailwind.config.php`;
9-
const fullConfig = resolveConfig(config);
5+
function parseConfig(data) {
6+
let output = '';
107

11-
function parseConfig(data) {
12-
let output = '';
8+
for (const [key, item] of Object.entries(data)) {
9+
const value = item == null || typeof item === 'function' ? null : item;
10+
const str =
11+
value && typeof value === 'object'
12+
? parseConfig(value)
13+
: JSON.stringify(value);
1314

14-
for (const [key, item] of Object.entries(data)) {
15-
const value = item == null || typeof item === 'function' ? null : item;
16-
const str =
17-
value && typeof value === 'object'
18-
? parseConfig(value)
19-
: JSON.stringify(value);
15+
output += `'${key}' => ${str},`;
16+
}
2017

21-
output += `'${key}' => ${str},`;
22-
}
18+
return `[${output}]`;
19+
}
2320

24-
return `[${output}]`;
25-
}
21+
async function resolveTailwindConfig() {
22+
const fs = await import('fs');
23+
const { default: resolveConfig } = await import('tailwindcss/resolveConfig.js')
24+
const { default: config } = await import(process.env.CONFIGPATH)
25+
26+
const fullConfig = resolveConfig(config);
2627

2728
try {
28-
if (!fs.existsSync(tempDir)) {
29-
fs.mkdirSync(tempDir);
29+
if (!fs.existsSync(TEMP_DIR)) {
30+
fs.mkdirSync(TEMP_DIR);
3031
}
3132

32-
fs.writeFileSync(outputPath, `<?php return ${parseConfig(fullConfig)};`);
33+
fs.writeFileSync(OUTPUT_PATH, `<?php return ${parseConfig(fullConfig)};`);
3334
} catch (err) {
3435
console.error(err);
3536
}
37+
}
38+
39+
try {
40+
resolveTailwindConfig()
3641
} catch (err) {
3742
console.error(err);
3843
}

0 commit comments

Comments
 (0)