mirror of
https://github.com/vuejs/vue-cli.git
synced 2026-04-27 07:09:16 -05:00
b70e0f6970
BREAKING CHANGE: Users will have to explicitly list their required ESLint version in the project's package.json. The major concern behind this change is that ESLint release schedule is never aligned with Vue CLI's. See https://eslint.org/blog/2019/04/eslint-v6.0.0-alpha.0-released So even if we upgraded our built-in ESLint version to v5, we'll see a new major release of ESLint in the coming months. Meanwhile we can't easily upgrade the built-in ESLint version again without bumping our major version number. This could become a maintenance nightmare.
122 lines
3.1 KiB
JavaScript
122 lines
3.1 KiB
JavaScript
const fs = require('fs')
|
|
const path = require('path')
|
|
|
|
module.exports = (api, { config, lintOn = [] }, _, invoking) => {
|
|
if (typeof lintOn === 'string') {
|
|
lintOn = lintOn.split(',')
|
|
}
|
|
|
|
const eslintConfig = require('../eslintOptions').config(api)
|
|
|
|
const pkg = {
|
|
scripts: {
|
|
lint: 'vue-cli-service lint'
|
|
},
|
|
eslintConfig,
|
|
devDependencies: {
|
|
'eslint': '^5.16.0',
|
|
'eslint-plugin-vue': '^5.0.0'
|
|
}
|
|
}
|
|
|
|
if (!api.hasPlugin('typescript')) {
|
|
pkg.devDependencies['babel-eslint'] = '^10.0.1'
|
|
}
|
|
|
|
const injectEditorConfig = (config) => {
|
|
const filePath = api.resolve('.editorconfig')
|
|
if (fs.existsSync(filePath)) {
|
|
// Append to existing .editorconfig
|
|
api.render(files => {
|
|
const configPath = path.resolve(__dirname, `./template/${config}/_editorconfig`)
|
|
const editorconfig = fs.readFileSync(configPath, 'utf-8')
|
|
|
|
files['.editorconfig'] += `\n${editorconfig}`
|
|
})
|
|
} else {
|
|
api.render(`./template/${config}`)
|
|
}
|
|
}
|
|
|
|
if (config === 'airbnb') {
|
|
eslintConfig.extends.push('@vue/airbnb')
|
|
Object.assign(pkg.devDependencies, {
|
|
'@vue/eslint-config-airbnb': '^4.0.0'
|
|
})
|
|
injectEditorConfig('airbnb')
|
|
} else if (config === 'standard') {
|
|
eslintConfig.extends.push('@vue/standard')
|
|
Object.assign(pkg.devDependencies, {
|
|
'@vue/eslint-config-standard': '^4.0.0'
|
|
})
|
|
injectEditorConfig('standard')
|
|
} else if (config === 'prettier') {
|
|
eslintConfig.extends.push('@vue/prettier')
|
|
Object.assign(pkg.devDependencies, {
|
|
'@vue/eslint-config-prettier': '^4.0.1'
|
|
})
|
|
// prettier & default config do not have any style rules
|
|
// so no need to generate an editorconfig file
|
|
} else {
|
|
// default
|
|
eslintConfig.extends.push('eslint:recommended')
|
|
}
|
|
|
|
if (!lintOn.includes('save')) {
|
|
pkg.vue = {
|
|
lintOnSave: false // eslint-loader configured in runtime plugin
|
|
}
|
|
}
|
|
|
|
if (lintOn.includes('commit')) {
|
|
Object.assign(pkg.devDependencies, {
|
|
'lint-staged': '^8.1.5'
|
|
})
|
|
pkg.gitHooks = {
|
|
'pre-commit': 'lint-staged'
|
|
}
|
|
pkg['lint-staged'] = {
|
|
'*.{js,vue}': ['vue-cli-service lint', 'git add']
|
|
}
|
|
}
|
|
|
|
api.extendPackage(pkg)
|
|
|
|
// typescript support
|
|
if (api.hasPlugin('typescript')) {
|
|
applyTS(api)
|
|
}
|
|
|
|
// invoking only
|
|
if (invoking) {
|
|
if (api.hasPlugin('unit-mocha')) {
|
|
// eslint-disable-next-line node/no-extraneous-require
|
|
require('@vue/cli-plugin-unit-mocha/generator').applyESLint(api)
|
|
} else if (api.hasPlugin('unit-jest')) {
|
|
// eslint-disable-next-line node/no-extraneous-require
|
|
require('@vue/cli-plugin-unit-jest/generator').applyESLint(api)
|
|
}
|
|
}
|
|
|
|
// lint & fix after create to ensure files adhere to chosen config
|
|
if (config && config !== 'base') {
|
|
api.onCreateComplete(() => {
|
|
require('../lint')({ silent: true }, api)
|
|
})
|
|
}
|
|
}
|
|
|
|
const applyTS = module.exports.applyTS = api => {
|
|
api.extendPackage({
|
|
eslintConfig: {
|
|
extends: ['@vue/typescript'],
|
|
parserOptions: {
|
|
parser: '@typescript-eslint/parser'
|
|
}
|
|
},
|
|
devDependencies: {
|
|
'@vue/eslint-config-typescript': '^4.0.0'
|
|
}
|
|
})
|
|
}
|