When front-end development inevitably involves.babelrc,.vuerc,.prettierrc,.eslintrc,.npmrc, etc., try to see how many rc files there are on your computer:

$ ls -l ~/.*rc -rw-r--r-- 1 keliq staff 1768 12 3 21:52 /Users/keliq/.bashrc -rw-r--r-- 1 keliq staff 8 2 4 2016 /Users/keliq/.cnpmrc -rw-r--r-- 1 keliq staff 122 2 18 2020 /Users/keliq/.gemrc -rw------- 1 keliq staff 3257 11 10 2016  /Users/keliq/.linphonerc -rw-r--r-- 1 keliq staff 68 10 27 2016 /Users/keliq/.mkshrc -rw------- 1 keliq staff 192 12 1 14:34 /Users/keliq/.npmrc -rw------- 1 keliq staff 612 6 28 2019 /Users/keliq/.opmrc -rw-r--r-- 1 keliq staff 513 11 6 2017 /Users/keliq/.vimrc -rw-r--r-- 1 keliq staff 120 12 2 10:09 /Users/keliq/.vuerc -rw-r--r-- 1 keliq staff 386 12 7 23:31 /Users/keliq/.yarnrc -rw-r--r-- 1 keliq staff 3468 10 19 2017 /Users/keliq/.zshrcCopy the code

So what does the rc suffix mean? [XXX]rc to save the configuration. Why are some in JSON, others in YAML, or whatever?

Origin of rc file

In Unix/Linux, rc stands for Run Commands. Early programs started to execute a series of commands, so these commands were stored in a configuration file with the suffix.rc and executed during initialization. This file is called RUNCOM. This custom can be traced back to 1965 when the CTSS system startup script was /etc/rc. Later, it has been inherited as a naming convention. Now all major projects use the rc suffix as the runtime configuration file, which has lost the original meaning of saving executable commands. Therefore, many people feel that RC is better interpreted as Runtime configuration.

Rc file format

Rc file has no agreed format, each program can parse, for example, json format with the following code:

const fs = require('fs');
fs.readFile('~/.foorc'.'utf8'.(err, data) = > {
  if (err) throw new Error(err)
  console.log(JSON.parse(data))
})
Copy the code

Yaml or any other format is fine, but the key is how the program that defines the RC file parses it.

Location of the RC file

[XXX]rc ~/.[XXX]rc ~/.[XXX]rc

async function configure (value, options) {
  const file = path.resolve(homedir, '.vuerc')
  const config = await fs.readJson(file)
}
Copy the code

The obvious answer is to look in os.homedir(), which is ~/.vuerc.

Rc file function

Vuerc is used to save preset and user-defined configurations for application startup. Again, take.vuerc as an example.

{
  "useTaobaoRegistry": false."packageManager": "yarn"."latestVersion": "4.5.9"."lastChecked": 1606874972974
}
Copy the code

The corresponding source code looks like this:

const schema = createSchema(joi= > joi.object().keys({
  latestVersion: joi.string().regex(/^\d+\.\d+\.\d+(-(alpha|beta|rc)\.\d+)? $/),
  lastChecked: joi.date().timestamp(),
  packageManager: joi.string().only(['yarn'.'npm'.'pnpm']),
  useTaobaoRegistry: joi.boolean(),
  presets: joi.object().pattern(/ ^ /, presetSchema)
}))
Copy the code

When are these configurations written and used? UseTaobaoRegistry = useTaobaoRegistry

module.exports = async function shouldUseTaobao (command) {
  let faster
  try {
    faster = await Promise.race([
      ping(defaultRegistry),
      ping(registries.taobao)
    ])
  } catch (e) {
    return save(false)}const { useTaobaoRegistry } = await inquirer.prompt([
    {
      name: 'useTaobaoRegistry'.type: 'confirm'.message: chalk.yellow(
        ` Your connection to the default ${command} registry seems to be slow.\n` +
          `   Use ${chalk.cyan(registries.taobao)}for faster installation? `)})return save(useTaobaoRegistry)
}
Copy the code

As you can see, Vue uses ping to test whether the default source or Taobao source is faster. Apparently, taobao source is faster if the user is in mainland China, so it asks the user whether to switch to Taobao source, and if the user agrees, it writes it into the configuration.