How to use the resolveConfig function from prettier

Find comprehensive JavaScript prettier.resolveConfig code examples handpicked from public code repositorys.

The prettier.resolveConfig method returns the resolved configuration object for Prettier.

116
117
118
119
120
121
122
123
124
125
    return undefined;
  },
});
const codeWithPropTypes = typescriptToProptypes.inject(propTypesAST, code);

const prettierConfig = prettier.resolveConfig.sync(jsPath, {
  config: path.join(workspaceRoot, 'prettier.config.js'),
});
const prettierFormat = (jsSource) =>
  prettier.format(jsSource, { ...prettierConfig, filepath: jsPath });
fork icon582
star icon0
watch icon48

+ 9 other calls in file

390
391
392
393
394
395
396
397
398
399
 * @param content
 */
exports.formatFile = async function (content) {
  return new Promise((resolve, reject) => {
    // eslint-disable-next-line promise/catch-or-return
    prettier.resolveConfig(process.cwd()).then((options) => {
      // eslint-disable-next-line promise/always-return
      if (options) {
        try {
          const content_ = prettier.format(content, {
fork icon1
star icon4
watch icon1

+ 3 other calls in file

How does prettier.resolveConfig work?

prettier.resolveConfig() is a method in the Prettier library for resolving configuration options by searching the current working directory and parent directories for configuration files, and merging them together to create a final configuration object.

38
39
40
41
42
43
44
45
46
47
48
if (!files.length) {
  return;
}


files.forEach(file => {
  const options = prettier.resolveConfig.sync(file, {
    config: prettierConfigPath,
  });
  try {
    const input = fs.readFileSync(file, 'utf8');
fork icon1
star icon0
watch icon0

24
25
26
27
28
29
30
31
32
33
    pluginSearchDirs: [context.extensionPath],
    plugins: [prettierPluginSolidity],
};

const config = () => {
    const conf = prettier.resolveConfig.sync(document.uri.fsPath, options);
    if (null !== conf) {
        return conf;
    } else {
        return defaultConfig();
fork icon0
star icon6
watch icon0

Ai Example

1
2
3
4
5
6
7
8
const prettier = require("prettier");

const filePath = "/path/to/my/file.js";

(async () => {
  const options = await prettier.resolveConfig(filePath);
  console.log(options);
})();

This code will asynchronously get the Prettier configuration options for the file located at /path/to/my/file.js and log them to the console.

14
15
16
17
18
19
20
21
22
23
24
let didError = false;
let didWarn = false;


files.forEach(file => {
  Promise.all([
    prettier.resolveConfig(file, {
      config: prettierConfigPath,
    }),
    prettier.getFileInfo(file),
  ])
fork icon1
star icon2
watch icon0

88
89
90
91
92
93
94
95
96
97
98
function getConfig(filePath) {
  const info = prettier.getFileInfo.sync(filePath, {
    ignorePath: path.join(rootDir, '.eslintignore'),
  });


  let config = prettier.resolveConfig.sync(filePath);
  if (!config) {
    return;
  }

fork icon1
star icon0
watch icon0

21
22
23
24
25
26
27
28
29
30
    partialsMapHasChanged = true;
    break;
  }
}
if (partialsMapHasChanged) {
  const prettierOptions = await prettier.resolveConfig(filePath);
  const newPartialsMap = prettier.format(JSON.stringify(partialsMap, null, 2), {
    ...(prettierOptions || {}),
    parser: 'json',
  });
fork icon1
star icon1
watch icon4

52
53
54
55
56
57
58
59
60
61
62
 * @param {所有的行内容} allRow
 * @param {所有的语言} langs
 */
async function generateLocalesFile(allRow, langs, writePath) {
  let count = 0;
  const prettierOptions = await prettier.resolveConfig(
    path.resolve("./.prettierrc.js")
  );


  try {
fork icon0
star icon0
watch icon1

526
527
528
529
530
531
532
533
534
535
writeNewCode = () => {
  Object.keys(this.filenameMap).forEach(async filename => {
    const { newCode, isReplace } = this.filenameMap[filename];
    if (isReplace) {
      try {
        const options = await prettier.resolveConfig(filename);
        const output = prettier.format(newCode, { ...this.withParserOptions, ...options });
        fs.writeFileSync(filename, output, 'utf-8');
      } catch (err) {
        this.filenameMap[filename].isWriteError = true;
fork icon0
star icon0
watch icon1

+ 4 other calls in file

43
44
45
46
47
48
49
50
51
52
  null,
  selectorGroups,
  true,
);

const prettierOptions = prettier.resolveConfig.sync(hashMapPath);
const prettyHashmap = prettier.format(
  `module.exports = ${JSON.stringify(selectorGroups)};`,
  prettierOptions,
);
fork icon0
star icon0
watch icon2

+ 16 other calls in file

23
24
25
26
27
28
29
30
31
32
const latestDeploy = await findLatestDeploy();
if (latestDeploy === undefined) {
	throw new Error(`Could not find deploy matching ${targetUrl}`);
}
const latestDeploySnapshot = JSON.stringify(latestDeploy, null, 2);
const prettierConfig = await prettier.resolveConfig(deploySnapshotPath);
await fs.writeFile(
	deploySnapshotPath,
	prettier.format(latestDeploySnapshot, {
		filepath: latestDeploySnapshot,
fork icon0
star icon0
watch icon1

+ 9 other calls in file

31
32
33
34
35
36
37
38
39
40
}

const prettierConfigPath = path.join(__dirname, '../prettier.config.js');

files.forEach((file) => {
  const prettierOptions = prettier.resolveConfig.sync(file, {
    config: prettierConfigPath,
  });

  try {
fork icon0
star icon0
watch icon0