|
1 | | -import { createReadStream, createWriteStream, promises as $fs, statSync } from 'node:fs'; |
2 | 1 | import { basename, dirname, join, relative } from 'node:path'; |
| 2 | +import { cosmiconfig, defaultLoaders } from 'cosmiconfig'; |
| 3 | +import { createReadStream, createWriteStream, promises as $fs, statSync } from 'node:fs'; |
3 | 4 | import { JSDOM } from 'jsdom'; |
4 | 5 | import { minify as minifier } from 'html-minifier-terser'; |
5 | 6 | import { pipeline } from 'node:stream'; |
6 | 7 | import { promisify } from 'node:util'; |
7 | 8 | import crypto from 'node:crypto'; |
8 | 9 | import glob from 'tiny-glob'; |
| 10 | +import JSON5 from 'json5'; |
9 | 11 | import prettier from 'prettier'; |
| 12 | +import TOML from '@iarna/toml'; |
10 | 13 | import zlib from 'node:zlib'; |
11 | 14 |
|
12 | 15 | const { readFile, writeFile } = $fs; |
@@ -127,10 +130,7 @@ export default function ({ |
127 | 130 | removeRedundantAttributes: true, |
128 | 131 | useShortDoctype: true |
129 | 132 | }) |
130 | | - : prettier.format(dom.serialize(), { |
131 | | - parser: 'html', |
132 | | - printWidth: 120 |
133 | | - }); |
| 133 | + : prettier.format(dom.serialize(), await getPrettierConfig()); |
134 | 134 | builder.log.minor('Formatting markup'); |
135 | 135 | } catch (err) { |
136 | 136 | builder.log.error('Formatting markup failed'); |
@@ -211,3 +211,47 @@ async function compress_file(file, format = 'gz') { |
211 | 211 |
|
212 | 212 | await pipe(source, compress, destination); |
213 | 213 | } |
| 214 | + |
| 215 | +async function getPrettierConfig() { |
| 216 | + const explorer = cosmiconfig('prettier', { |
| 217 | + searchPlaces: [ |
| 218 | + 'package.json', |
| 219 | + '.prettierrc', |
| 220 | + '.prettierrc.json', |
| 221 | + '.prettierrc.yaml', |
| 222 | + '.prettierrc.yml', |
| 223 | + '.prettierrc.json5', |
| 224 | + '.prettierrc.js', |
| 225 | + '.prettierrc.cjs', |
| 226 | + 'prettier.config.js', |
| 227 | + 'prettier.config.cjs', |
| 228 | + '.prettierrc.toml' |
| 229 | + ], |
| 230 | + loaders: { |
| 231 | + '.toml': (filePath, content) => { |
| 232 | + try { |
| 233 | + return TOML.parse(content); |
| 234 | + } catch (error) { |
| 235 | + error.message = `TOML Error in ${filePath}:\n${error.message}`; |
| 236 | + throw error; |
| 237 | + } |
| 238 | + }, |
| 239 | + '.json5': (filePath, content) => { |
| 240 | + try { |
| 241 | + return JSON5.parse(content); |
| 242 | + } catch (error) { |
| 243 | + error.message = `TOML Error in ${filePath}:\n${error.message}`; |
| 244 | + throw error; |
| 245 | + } |
| 246 | + }, |
| 247 | + noExt: defaultLoaders['.json'] |
| 248 | + } |
| 249 | + }); |
| 250 | + |
| 251 | + return ( |
| 252 | + (await explorer.search()).config || { |
| 253 | + parser: 'html', |
| 254 | + printWidth: 120 |
| 255 | + } |
| 256 | + ); |
| 257 | +} |
0 commit comments