Skip to content

Commit 884f348

Browse files
committed
add support for custom prettier configs
1 parent f1a1053 commit 884f348

File tree

3 files changed

+97
-25
lines changed

3 files changed

+97
-25
lines changed

index.js

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import { createReadStream, createWriteStream, promises as $fs, statSync } from 'node:fs';
21
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';
34
import { JSDOM } from 'jsdom';
45
import { minify as minifier } from 'html-minifier-terser';
56
import { pipeline } from 'node:stream';
67
import { promisify } from 'node:util';
78
import crypto from 'node:crypto';
89
import glob from 'tiny-glob';
10+
import JSON5 from 'json5';
911
import prettier from 'prettier';
12+
import TOML from '@iarna/toml';
1013
import zlib from 'node:zlib';
1114

1215
const { readFile, writeFile } = $fs;
@@ -127,10 +130,7 @@ export default function ({
127130
removeRedundantAttributes: true,
128131
useShortDoctype: true
129132
})
130-
: prettier.format(dom.serialize(), {
131-
parser: 'html',
132-
printWidth: 120
133-
});
133+
: prettier.format(dom.serialize(), await getPrettierConfig());
134134
builder.log.minor('Formatting markup');
135135
} catch (err) {
136136
builder.log.error('Formatting markup failed');
@@ -211,3 +211,47 @@ async function compress_file(file, format = 'gz') {
211211

212212
await pipe(source, compress, destination);
213213
}
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+
}

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@
3737
"test": "npm run lint"
3838
},
3939
"dependencies": {
40+
"@iarna/toml": "^2.2.5",
41+
"cosmiconfig": "^7.0.1",
4042
"html-minifier-terser": "^7.0.0-alpha.1",
4143
"jsdom": "^19.0.0",
44+
"json": "^11.0.0",
4245
"prettier": "^2.5.1",
4346
"tiny-glob": "^0.2.9"
4447
},

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)