webpack手动搭建vue项目(三)—— 配置eslint

6 篇文章 0 订阅
2 篇文章 0 订阅

1.安装eslint

> npm install eslint --save-dev

> eslint --init

? How would you like to configure ESLint?

> use a pooular style guide

? Which style guide do you want to follow?

>Standard

? What format do you want your config file to be in?

>javascript

>npm install babel-eslint eslint-loader eslint-plugin-html eslint-plugin-vue eslint-friendly-formatter --save-dev

2.修改.eslintrc.js文件

参照vue-cli2模板,修改成如下

// https://eslint.org/docs/user-guide/configuring

module.exports = {
  root: true,
  parserOptions: {
    parser: 'babel-eslint'
  },
  env: {
    browser: true,
  },
  extends: [
    // https://github.com/vuejs/eslint-plugin-vue#priority-a-essential-error-prevention
    // consider switching to `plugin:vue/strongly-recommended` or `plugin:vue/recommended` for stricter rules.
    'plugin:vue/essential',
    // https://github.com/standard/standard/blob/master/docs/RULES-en.md
    'standard'
  ],
  // required to lint *.vue files
  plugins: [
    'vue'
  ],
  // add your custom rules here
  rules: {   // 这一部分根据个人喜好配置
    // allow async-await
    'generator-star-spacing': 'off',
    "no-tabs":"off",
    "quotes": [0, "single"],
	"new-cap": 0,
	"handle-callback-err": 0,
	'vue/no-side-effects-in-computed-properties': 'off',
	'no-undef':0,
	'no-unused-expressions': process.env.NODE_ENV === 'test' ? 'error' : 'off',
    "indent": [2, 4],//缩进风格
    // allow debugger during development
    'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off'
  }
}

3.修改webpack.base.js

在rules添加如下规则

{
                test: /^((?!bmap).)*\.(js|vue)$/,
                loader: 'eslint-loader',
                enforce: 'pre',
                include: [path.resolve(__dirname, 'src')],
                options: {
                    formatter: require('eslint-friendly-formatter')
                }
            },

4.配置.eslintignore文件

/build/
/config/
/dist/
/*.js

5.添加package.json脚本

"lint": "eslint --ext .js,.vue src"

6.总结

注意:此处各个"babel-eslint做好和上文中的babel-loader保持版本一致为7.1.1版本。

附上我所用版本,和vue-cli2一样的,如下:

    "babel-eslint": "^7.1.1",
    "eslint": "^4.15.0",
    "eslint-config-standard": "^10.2.1",
    "eslint-friendly-formatter": "^3.0.0",
    "eslint-loader": "^1.7.1",
    "eslint-plugin-html": "^3.0.0",
    "eslint-plugin-import": "^2.7.0",
    "eslint-plugin-node": "^5.0.0",
    "eslint-plugin-promise": "^3.4.0",
    "eslint-plugin-standard": "^3.0.1",
    "eslint-plugin-vue": "^4.5.0",
    "extract-text-webpack-plugin": "^3.0.2",

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供有关如何使用Webpack 4来构建Vue项目的步骤: 1. 创建项目文件夹并初始化npm(如果您尚未完成): ``` mkdir your-project-name cd your-project-name npm init -y ``` 2.\u4e3a\u9879\u76ee\u6dfb\u52a0Webpack 4\uff0c\u60a8\u53ef\u4ee5\u9047\u5230\u4ee5\u4e0b\u6b65\u9aa41\u3002\u5728\u9879\u76ee\u7684\u6839\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\u5939\u4ee5\u7ed3\u6784\u4e00\u4e2a\u7f16\u7801\u6587\u4ef6\u5e76\u5c06\u5b83\u5f00\u59cb\u8bbe\u7f6e\u4e3awebpack\u3002 ``` mkdir src touch src/index.js ``` 3. \u5728\u9879\u76ee\u5e93\u4e2d\u5b89\u88c5Webpack 4\uff0c\u4f7f\u7528npm\u5b89\u88c5webpack\u548cwebpack-cli\u3002 ``` npm install webpack webpack-cli --save-dev ``` 4. \u5728\u9879\u76ee\u5e93\u6839\u76ee\u5f55\u4e2d\u6dfb\u52a0\u4e00\u4e2awebpack.config.js\u6587\u4ef6\uff0c\u5e76\u914d\u7f6eWebpack\u7684\u914d\u7f6e\u9879\u3002 ``` touch webpack.config.js ``` 5. \u7f16\u8f91webpack.config.js\u6587\u4ef6\uff0c\u914d\u7f6e\u9879\u76ee\u7684\u8f93\u51fa\u6587\u4ef6\u540d\u79f0\u548c\u8f93\u51fa\u76ee\u5f55\u3002 ``` const path = require('path'); module.exports = { entry: './src/index.js', output: { filename: 'bundle.js', path: path.resolve(__dirname, 'dist'), }, }; ``` 6. \u7f16\u8f91src/index.js\u6587\u4ef6\uff0c\u4e3aVue\u6a21\u5757\u6dfb\u52a0\u4ee5\u53ca\u8fd0\u884c\u4e3aVue\u6a21\u5757\u7684\u5e93\u3002\u5728\u6b64\u4f7f\u7528npm\u5b89\u88c5Vue\u3002 ``` npm install vue --save ``` 7. \u7f16\u8f91src/index.js\u6587\u4ef6\uff0c\u5e76\u5b9e\u73b0\u5728\u9879\u76ee\u4e2d\u4f7f\u7528Vue\u6a21\u5757\u3002 ``` import Vue from 'vue'; new Vue({ el: '#app', template: '<div>Hello, Vue!</div>', }); ``` 8. \u521b\u5efa\u9879\u76ee\u7684index.html\u6587\u4ef6\uff0c\u4e3a好的,让我来简单介绍一下如何使用Webpack 4来构建Vue项目: 1. 安装Node.js和npm 确保你的电脑上已经安装了Node.js和npm。你可以通过访问Node.js官方网站 (https://nodejs.org/) 下载并安装。 2. 创建项目文件夹 在命令行中创建一个新的项目文件夹,例如: ``` mkdir my-vue-project ``` 3. 初始化npm 进入该项目文件夹,并在命令行中输入以下命令来初始化npm: ``` cd my-vue-project npm init -y ``` 4. 安装Vue.js 使用npm安装Vue.js和Vue Loader: ``` npm install vue vue-loader vue-template-compiler --save-dev ``` 5. 安装Webpack 4及其相关插件 使用npm安装Webpack 4及其相关插件: ``` npm install webpack webpack-cli webpack-dev-server html-webpack-plugin --save-dev ``` 6. 创建Webpack配置文件 在项目根目录下创建一个webpack.config.js文件,并在其中添加以下内容: ``` const HtmlWebpackPlugin = require('html-webpack-plugin'); const path = require('path'); module.exports = { entry: './src/main.js', output: { path: path.resolve(__dirname, 'dist'), filename: 'bundle.js', }, module: { rules: [ { test: /\.vue$/, loader: 'vue-loader', }, { test: /\.js$/, exclude: /(node_modules)/, use: { loader: 'babel-loader', options: { presets: ['@babel/preset-env'], }, }, }, ], }, plugins: [ new HtmlWebpackPlugin({ template: './src/index.html', }), ], devServer: { contentBase: path.join(__dirname, 'dist'), compress: true, port: 9000, }, }; ``` 7. 创建项目文件夹结构 在项目根目录下创建一个src文件夹,并在其中创建一个main.js文件和一个index.html文件。在src文件夹中再创建一个components文件夹,并在其中创建一个HelloWorld.vue组件文件。具体的文件夹结构如下: ``` my-vue-project/ src/ components/ HelloWorld.vue main.js index.html webpack.config.js package.json ``` 8. 编写Vue组件 打开HelloWorld.vue文件,并添加以下代码: ```html <template> <div> <h1>{{ msg }}</h1> </div> </template> <script> export default { name: 'HelloWorld', data() { return { msg: 'Welcome to my Vue.js app!', }; }, }; </script> ``` 9. 编写入口文件 打开main.js文件,并添加以下代码: ```javascript import Vue from 'vue'; import HelloWorld from './components/HelloWorld.vue'; new Vue({ el: '#app', components: { HelloWorld, }, template: '<HelloWorld/>', }); ``` 10. 编写HTML文件 打开index.html文件,并添加以下代码: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>My Vue.js好的,以下是使用webpack4搭建Vue项目的基本步骤: 1. 创建项目文件夹,并初始化为npm项目 ``` mkdir my-project cd my-project npm init -y ``` 2. 安装必要的依赖包 ``` npm install webpack webpack-cli vue vue-loader vue-template-compiler css-loader style-loader --save-dev ``` 3. 在项目根目录下创建webpack配置文件webpack.config.js ``` const path = require('path'); const VueLoaderPlugin = require('vue-loader/lib/plugin'); module.exports = { entry: './src/main.js', output: { path: path.resolve(__dirname, 'dist'), filename: 'bundle.js' }, module: { rules: [ { test: /\.vue$/, use: 'vue-loader' }, { test: /\.css$/, use: ['style-loader', 'css-loader'] } ] }, plugins: [ new VueLoaderPlugin() ] }; ``` 4. 在src文件夹下创建一个主入口文件main.js和App.vue组件 ``` // main.js import Vue from 'vue'; import App from './App.vue'; new Vue({ el: '#app', render: h => h(App) }); // App.vue <template> <div> <h1>Hello Vue!</h1> </div> </template> <script> export default { name: 'App' }; </script> <style> h1 { color: red; } </style> ``` 5. 在index.html中创建一个div容器,并引入打包后的bundle.js文件 ``` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Vue Project</title> </head> <body> <div id="app"></div> <script src="./dist/bundle.js"></script> </body> </html> ``` 6. 在package.json中添加启动脚本,方便快速启动项目 ``` "scripts": { "start": "webpack --mode development --watch" }, ``` 7. 在命令行中输入npm start,即可启动项目。在浏览器中打开index.html,即可看到Vue应用程序运行的效果。 希望这个回答能对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值