How to configure eslint and prettier with nuxt 3? - vue.js

I have installed these dependencies
Package.json:
{
"devDependencies": {
"#intlify/nuxt3": "^0.1.6",
"#nuxtjs/eslint-config": "^7.0.0",
"#nuxtjs/eslint-module": "^3.0.2",
"eslint": "^8.1.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-nuxt": "^3.0.0",
"eslint-plugin-vue": "^7.20.0",
"nuxt3": "latest",
"prettier": "2.4.1",
"sass": "^1.43.3",
"vite-plugin-eslint": "^1.3.0"
}
}
At .eslintrc.js
extends: [
'eslint:recommended',
'plugin:nuxt/recommended',
'prettier'
],
At nuxt.config.ts
import eslintPlugin from 'vite-plugin-eslint';
export default defineNuxtConfig({
...
vite: {
plugins: [eslintPlugin()]
},
buildModules: ['#intlify/nuxt3', '#nuxtjs/eslint-module',],
})
And none of these options are working with nuxt 3.

A simple ESLint + Prettier + TypeScript + Nuxt 3 (or Bridge) setup would look like this:
yarn add --dev eslint prettier eslint-config-prettier eslint-plugin-prettier #nuxtjs/eslint-config-typescript
.eslintrc.js
module.exports = {
root: true,
extends: ['#nuxtjs/eslint-config-typescript', 'plugin:prettier/recommended'],
}
package.json
{
"scripts": {
"lint": "eslint --ext .ts,.js,.vue ."
}
}

Here's a config i found here : https://github.com/nuxt/framework/discussions/2815#discussioncomment-2050408
// .eslintrc.json
{
"env": {
"browser": true,
"es2021": true,
"node": true
},
"extends": [
"eslint:recommended",
"plugin:vue/vue3-recommended",
"plugin:#typescript-eslint/recommended",
"#nuxtjs/eslint-config-typescript"
],
"parserOptions": {
"ecmaVersion": "latest",
"parser": "#typescript-eslint/parser",
"sourceType": "module"
},
"plugins": [
"vue",
"#typescript-eslint"
],
"rules": {}
}
If you really want to use prettier (imo eslint already does the job, using both can be very annoying to configure) you could add eslint-plugin-prettier library, then add "plugin:prettier/recommended" to eslint extends.
Idk what IDE you're using but if it's vscode, I advise you to use the linting on save instead of relying on formatters (Volar, prettier, eslint-prettier).
Mostly because it forces all devs to have the same format and rules

Related

eslint-plugin-vue vue/valid-v-model appears to warn using the rules in Vue.js 2

I'm using Vue.js 3.
The following eslint-plugin-vue warning appears to warn using the rules in Vue.js 2.
<MyComponent v-model:propName="foo"/> This writing style is supported in Vue.js 3.
How to make it compatible with Vue.js 3?
<MyInputComponent
v-model:value="state.value"
/>
// [vue/valid-v-model] 'v-model' directives require no argument. eslint-plugin-vue [7, 9]
.eslintrc.js
module.exports = {
extends: [
'plugin:vue/vue3-recommended',
]
}
package.json
{
"name": "ProjectName",
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build"
},
"dependencies": {
"typescript": "^4.1.2",
"vue": "^3.0.2"
},
"devDependencies": {
"#vue/compiler-sfc": "^3.0.2",
"eslint-plugin-vue": "^7.1.0",
"vite": "^1.0.0-rc.8"
}
}
This worked for me - ignore the rule in .eslintrc.js:
rules: {
"vue/no-v-model-argument": "off",
},
Add these deps:
"#babel/core": "^7.12.10",
"#babel/eslint-parser": "^7.12.1",
"eslint": "^7.18.0",
install them:
npm i -D eslint #babel/core #babel/eslint-parser
change eslint.js
parser: '#babel/eslint-parser',

npm run build generates more files than just bundle.js bundle.css

I'm working on a SPA with Vue.js and Django.
I run npm run build to generate the bundle files that will be used in production. I was expecting to only generate two files: dist/bundle.js and dist/bundle.css. However, besides the aforemetnioned files, a third js file is also being generated: dist/js/about.add8abe8.js.
Why is this?
In my Django project, inside the frontend/src/views/ directory I have 3 Vue components: Home.vue, List.vue and About.vue.
EDIT:
I don't have a webpack.config file. But these are some other files that may be relevant:
package.json:
{
"name": "frontend",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
"dependencies": {
"bootstrap": "^4.3.1",
"bootstrap-vue": "^2.0.0-rc.19",
"core-js": "^2.6.5",
"vue": "^2.6.10",
"vue-router": "^3.0.3"
},
"devDependencies": {
"#vue/cli-plugin-babel": "^3.7.0",
"#vue/cli-plugin-eslint": "^3.7.0",
"#vue/cli-service": "^3.7.0",
"#vue/eslint-config-prettier": "^4.0.1",
"babel-eslint": "^10.0.1",
"eslint": "^5.16.0",
"eslint-plugin-vue": "^5.0.0",
"vue-template-compiler": "^2.5.21",
"webpack-bundle-tracker": "^0.4.2-beta"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"#vue/prettier"
],
"rules": {},
"parserOptions": {
"parser": "babel-eslint"
}
},
"postcss": {
"plugins": {
"autoprefixer": {}
}
},
"browserslist": [
"> 1%",
"last 2 versions"
]
}
vue.config:
const BundleTracker = require("webpack-bundle-tracker");
module.exports = {
// on Windows you might want to set publicPath: "http://127.0.0.1:8080/"
publicPath: "http://0.0.0.0:8080/",
outputDir: "./dist/",
chainWebpack: config => {
config
.plugin("BundleTracker")
.use(BundleTracker, [{ filename: "./webpack-stats.json" }]);
config.output.filename("bundle.js");
config.optimization.splitChunks(false);
config.resolve.alias.set("__STATIC__", "static");
config.devServer
.hotOnly(true)
.watchOptions({ poll: 1000 })
.https(false)
.disableHostCheck(true)
.headers({ "Access-Control-Allow-Origin": ["*"] });
},
// uncomment before executing 'npm run build'
css: {
extract: {
filename: "bundle.css",
chunkFilename: "bundle.css"
}
}
};

Installing jest & vue test utils manually no - trouble with import

I am trying to install vue-test-utils and jest in a project. It is vue-cli project but I did not setup the testing when I ran it initially.
It finds the test but fails on the import statements.
Here is the error.
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
It is almost certainly a babel thing (I think) but I cannot work it out.
Here are the config files.
babel.config.js
module.exports = {
presets: [
'#vue/app'
]
}
jest.config.js
module.exports = {
moduleFileExtensions: [
'js',
'jsx',
'json',
'vue'
],
transform: {
'^.+\\.vue$': 'vue-jest',
'.+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
'^.+\\.jsx?$': 'babel-jest',
"^.+\\.js$": "<rootDir>/node_modules/babel-jest"
},
transformIgnorePatterns: [
'/node_modules/'
],
moduleNameMapper: {
'^#/(.*)$': '<rootDir>/src/$1'
},
snapshotSerializers: [
'jest-serializer-vue'
],
testMatch: [
'**/tests/unit/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)'
],
testURL: 'http://localhost/'
}
package.json
{
"name": "test",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint",
"test": "jest"
},
"dependencies": {
"core-js": "^2.6.5",
"vue": "^2.6.10",
"vue-moment": "^4.0.0"
},
"devDependencies": {
"#babel/core": "^7.4.3",
"#vue/cli-plugin-babel": "^3.6.0",
"#vue/cli-plugin-eslint": "^3.6.0",
"#vue/cli-service": "^3.6.0",
"#vue/test-utils": "^1.0.0-beta.29",
"babel-eslint": "^10.0.1",
"babel-jest": "^24.7.1",
"eslint": "^5.16.0",
"eslint-plugin-vue": "^5.0.0",
"jest": "^24.7.1",
"jest-transform-stub": "^2.0.0",
"node-sass": "^4.11.0",
"sass-loader": "^7.1.0",
"vue-jest": "^3.0.4",
"vue-template-compiler": "^2.5.21",
"webpack": "^4.30.0"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"eslint:recommended"
],
"rules": {},
"parserOptions": {
"parser": "babel-eslint"
}
},
"postcss": {
"plugins": {
"autoprefixer": {}
}
},
"browserslist": [
"> 1%",
"last 2 versions",
"not ie <= 8"
]
}
Well, looks like you need global section in your jest config. I'm using vue + ts, so my globals looks like that:
globals: {
'ts-jest': {
babelConfig: true
}
}

Dynamic Imports for Code Splitting cause: ESLint Parsing Error 'import'

I'm using the VueJS Webpack template found here: https://github.com/vuejs-templates/webpack
Example Route:
const AuthRoute = () => import(/* webpackChunkName: "authroute" */ './AuthRoute.vue')
Example Error:
[eslint] Parsing error: Unexpected token import
I've followed the steps provided in Webpack's Dynamic import section, as well as Anthony Gore's blog post on how to accomplish code splitting with VueJS at the router level. More specifically, here is my setup:
Package.json
...
"babel-core": "^6.22.1",
"babel-eslint": "^8.0.3",
"babel-jest": "^21.2.0",
"babel-loader": "^7.1.1",
"babel-plugin-dynamic-import-webpack": "^1.0.2",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
"babel-plugin-transform-runtime": "^6.22.0",
"babel-preset-env": "^1.3.2",
"babel-preset-stage-2": "^6.24.1",
"eslint": "^4.13.1"
...
.babelrc
{
"presets": [
["env", {
"modules": false
}],
"stage-2"
],
"plugins": [
"dynamic-import-webpack",
"syntax-dynamic-import",
"transform-runtime"
],
"env": {
"test": {
"presets": ["env", "stage-2"] }
}
}
.eslintrc.js
parserOptions: {
parser: 'babel-eslint',
sourceType: 'module',
allowImportExportEverywhere: true
}
I'm at a bit of a loss as to why I'm still seeing this error. My code runs and works as expected when using npm run dev and npm run build, but this parsing error prevents the rest of the file from being linted, and I am unable to suppress it.
Any/all help is appreciated!
.eslintrc.js
parserOptions: {
parser: 'babel-eslint',
sourceType: 'module',
allowImportExportEverywhere: true
}
Should Be:
parser: 'babel-eslint',
parserOptions: {
sourceType: 'module',
allowImportExportEverywhere: true
}
Source: https://eslint.org/docs/user-guide/configuring#specifying-parser
With (#vue/cli)
.eslintrc.json
{
"parser": "vue-eslint-parser",
"parserOptions": {
"parser": "babel-eslint",
"ecmaVersion": 8,
"sourceType": "module"
}
}
This StackOverflow question and answer did help me solve this issue, but currently in April 2020, the parser key seems to be required inside parserOptions, or at least for my combination of settings.
I will show my current day config that I use with Laravel 7/Laravel Mix and Vue 2.6~.
.eslintrc.json
{
"env": {
"browser": true,
"es6": true,
"node": true
},
"extends": [
"eslint:recommended",
"airbnb-base",
"plugin:vue/essential"
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"parser": "babel-eslint",
"ecmaVersion": 2019,
"sourceType": "module"
},
"plugins": [
"vue"
],
"rules": {
// dat ruleset
},
"settings": {
"import/resolver": {
"alias": {
"map": [
["#", "./resources"]
],
"extensions": [".vue"]
}
}
}
}
You'll only need that settings key if you are using Webpack aliases.
package.json
"devDependencies": {
"#babel/plugin-syntax-dynamic-import": "^7.8.3",
"babel-eslint": "^10.1.0",
"eslint": "^6.8.0",
"eslint-config-airbnb-base": "^14.1.0",
"eslint-import-resolver-alias": "^1.1.2",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-vue": "^6.2.2",
"laravel-mix": "^5.0.1",
}
.babelrc
{
"plugins": ["#babel/plugin-syntax-dynamic-import"]
}
webpack.config.js
If you are using Webpack "normally" without Laravel Mix, you will find your syntax is a little different here, but if you are using Webpack aliases, you will find this useful:
// use named JS bundles
mix.config.webpackConfig.output = {
chunkFilename: 'js/[name].bundle.js',
publicPath: '/',
};
// alias the ~/resources folder
mix.webpackConfig({
resolve: {
extensions: ['.js', '.vue'],
alias: {
'#': `${__dirname}/resources`,
},
},
});
Final note: I always recommend usage of airbnb-base config and to rely on it to form the basis of your lint rules because it was created for multi-developer environments running die hard Functional Programming techniques for immutable, unidirectional data flow--and thus, Functional Reactive Programming while avoiding the pitfalls of JavaScript... and to focus on declarative code while avoiding reasonably bad kinds of imperative code.
I wrote a couple more words about that in my article about setting up ES Lint in Vue for Airbnb: https://medium.com/#agm1984/how-to-set-up-es-lint-for-airbnb-vue-js-and-vs-code-a5ef5ac671e8
Put the below code into .eslintrc. It expects parser options; by default it will be 2013:
"parserOptions": {
"ecmaVersion":"latest"
},

Babel Error: Unknown Option: babelrc.presets

When running tests using Jest, I need my .babelrc file for it to run.
When running npm start, it only works without the .babelrc file, with the error:
Unknown option: C:\...\babelrc.presets
I'm guessing it's to do with the version of babel I have, but I have tried to following "answer" to this question: Unknown option: .../.babelrc.presets
but to no avail.
Here is my package.json:
{
"name": "reactjs",
"version": "1.0.0",
"description": "",
"main": "src/app.js",
"author": "x",
"license": "ISC",
"scripts": {
"start": "webpack-dev-server --port 3000",
"test": "jest"
},
"jest": {
"scriptPreprocessor": "<rootDir>/node_modules/babel-jest",
"unmockedModulePathPatterns": [
"react",
"react-dom",
"react-addons-test-utils",
"fbjs"
]
},
"devDependencies": {
"babel-core": "^6.7.*",
"babel-jest": "^11.0.2",
"babel-loader": "^5.0.0",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"jest-cli": "^11.0.2",
"react-addons-test-utils": "^0.14.8",
"webpack": "^1.12.*",
"webpack-dev-server": "^1.10.*"
},
"dependencies": {
"react": "^0.13.3"
}
}
and my .babelrc:
{
"presets": [
"react",
"es2015"
]
}
and my webpack config, if it's relevant:
module.exports = {
entry: [
'./src/app.js'
],
output: {
path: __dirname,
filename: "bundle.js"
},
module: {
loaders: [{
test: /\.jsx?$/,
loader: 'babel',
exclude: /node_modules/
}]
}
};
You've listed babel-core#^6 but are using babel-loader#5, update your babel-loader to the most recent version.
I can work with babel src --out-dir lib, but not with npm run XXX using babel-core#6.20.0 and babel-loader#6.2.9 .While I install Babel-cli#6.18.0 CLI globally on my machine, after I install babel-cli# locally project, it can works with npm run.