前端 配置环境

1 node

1.官网下载,不选最新版本,少bug,安装一路next ,**cmd以管理员模式**


2.修改全局包路径

在node安装目录下下新建两个文件夹
node_global  //全局包下载存放
node_cache //缓存

修改路径 cmd  
npm config set prefix "D:\node\node_global"
npm config set cache "D:\node\node_cache"


3.修改环境变量(修改全局路径之后才需要修改)

用户变量新增 path为:D:\node\node_global    //用于找下载的express
新增系统变量 NODE_PATH,变量值为D:\node\node_global\node_modules
新增系统变量 path:D:\node\ //安装时自动创建好的,用于找node
淘宝镜像
npm config set registry https://registry.npm.taobao.org
npm install -g cnpm --registry=https://registry.npm.taobao.org

使用:cnpm install xxx

npm使用

全局安装
npm install  -g/--global
使用该命令后,会在package.json的dependencies中出现,是生产环境依赖
npm install  -S/--save
使用该命令后,依赖包会出现在package.json的devDependencies中
npm install -D/--save-dev

2 vscode

官网下载
setting.json

{
  // VScode主题配置
  "workbench.colorCustomizations": {
    "editorIndentGuide.activeBackground": "#ff0000" // 设置guide线高亮颜色,可以改为自己喜欢的颜色
  },
  "editor.formatOnSave": true,
  "[vue]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[javascript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "editor.fontSize": 15,
  "[html]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  }
}

其他插件

ESLint
Git History
Live Server
Path Intellisense
Prettier - Code formatter
Prettier ESLint
Regex Previewer
Vetur
Vue 2 Snippets

3 git

配置忽略文件.gitignore

.DS_Store
node_modules/
dist/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
**/*.log

tests/**/coverage/
tests/e2e/reports
selenium-debug.log

# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
*.local

package-lock.json
yarn.lock

4 vue安装及使用

1 npm install vue@next -g  //全局安装稳定版vue
2 npm install @vue/cli -g  //安装脚手架
3 vue create demo  //创建项目

/****************************/
 (*) Choose Vue version   //2
 (*) Babel
 ( ) TypeScript
 ( ) Progressive Web App (PWA) Support        
 (*) Router               //not history
 (*) Vuex
 (*) CSS Pre-processors   //less
 (*) Linter / Formatter   //ESLint + Prettier
 ( ) Unit Testing
 ( ) E2E Testing

选择 //ESLint + Prettier之后, 会自动下载, 并安装到依赖, 为了在其他编辑器(可能没安装eslint和prettier插件)中使用
右键使用...格式化文档, 选择prettier-code formatter

2 .eslintrc.js自动创建
.prettierrc.js自己写, 根据公司提供的eslintrc

module.exports = {
root: true,
env: {
 node: true
},
//"eslint:recommended" 来启用推荐的规则
extends: ['plugin:vue/essential', 'eslint:recommended', '@vue/prettier'],
parserOptions: {
 parser: 'babel-eslint'
},
rules: {
 'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
 'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
 'prettier/prettier': ['error', { endOfLine: 'auto' }],
 //两个空格不要缩进
 indent: ['error', 2],
 //最大长度80 tab 2字符
 'max-len': ['error', { code: 180, tabWidth: 2 }],
 //禁止使用分号;
 semi: ['error', 'never'],
 //单引号
 quotes: ['error', 'single'],
 //末尾逗号
 'comma-dangle': ['error', 'never'],
 //unix以lf(\n)换行符结尾(默认)  window以crlf(\r\n)换行符结尾,你可以关闭此规则(不写即关闭)
 //'linebreak-style': ['error', 'unix']
 
 // 对象中打印空格 默认always
 // always: { foo: bar }
 // never: {foo: bar}
 'object-curly-spacing': ["error", "always"],
 // 箭头函数参数括号 默认avoid 可选 as-needed| always
 // as-needed 能省略括号的时候就省略 例如x => x
 // always 总是有括号
 'arrow-parens': ["error", "always"]
}
}



★★★★★
现在可以这样写"plugin:prettier/recommended"插件让eslint的校验规则=prettier的,不用重复写一遍. 这也是vue现在默认的写法
module.exports = {
root: true,
env: {
 node: true,
},
extends: [
 "plugin:vue/essential",
 "eslint:recommended",
 "plugin:prettier/recommended",
],
parserOptions: {
 parser: "@babel/eslint-parser",
},
rules: {
 "no-console": process.env.NODE_ENV === "production" ? "warn" : "off",
 "no-debugger": process.env.NODE_ENV === "production" ? "warn" : "off",
},
};


写.prettierrc.js

module.exports = {
//使用空格缩进
useTabs: false,
//缩进的空格数
tabWidth: 2,
//打印宽度
printWidth: 180,
//末尾分号
semi: false,
//单引号
singleQuote: true,
//末尾逗号
trailingComma: 'none',
//在 windows 操作系统中换行符通常是回车 (CR) 加换行分隔符 (LF),也就是回车换行(CRLF),
//然而在 Linux 和 Unix 中只使用简单的换行分隔符 (LF)。
//对应的控制字符为 "\n" (LF) 和 "\r\n"(CRLF)。auto意为保持现有的行尾
endOfLine: 'auto',
 // 对象中打印空格 默认true
 // true: { foo: bar }
 // false: {foo: bar}
 bracketSpacing: true,
 // 箭头函数参数括号 默认avoid 可选 avoid| always
 // avoid 能省略括号的时候就省略 例如x => x
 // always 总是有括号
 arrowParens: 'avoid',
 //属性自动换行
 proseWrap:'never'
}
.prettierignore文件
dist/
src/actions

.eslintignore文件
build/*.js
config/*.js
src/assets
src
scripts/*

vue.config.js自己写

// vue.config.js参考
const path = require('path');
const CompressionWebpackPlugin = require("compression-webpack-plugin"); // 开启gzip压缩, 按需引用
const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i; // 开启gzip压缩, 按需写入
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin; // 打包分析
const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV);
const resolve = (dir) => path.join(__dirname, dir);
module.exports = {
publicPath: process.env.NODE_ENV === 'production' ? '/site/vue-demo/' : '/', // 公共路径
indexPath: 'index.html' , // 相对于打包路径index.html的路径
outputDir: process.env.outputDir || 'dist', // 'dist', 生产环境构建文件的目录
assetsDir: 'static', // 相对于outputDir的静态资源(js、css、img、fonts)目录
lintOnSave: false, // 是否在开发环境下通过 eslint-loader 在每次保存时 lint 代码
runtimeCompiler: true, // 是否使用包含运行时编译器的 Vue 构建版本
productionSourceMap: !IS_PROD, // 生产环境的 source map
parallel: require("os").cpus().length > 1, // 是否为 Babel 或 TypeScript 使用 thread-loader。该选项在系统的 CPU 有多于一个内核时自动启用,仅作用于生产构建。
pwa: {}, // 向 PWA 插件传递选项。
chainWebpack: config => {
 config.resolve.symlinks(true); // 修复热更新失效
 // 如果使用多页面打包,使用vue inspect --plugins查看html是否在结果数组中
 config.plugin("html").tap(args => {
   // 修复 Lazy loading routes Error
   args[0].chunksSortMode = "none";
   return args;
 });
 config.resolve.alias // 添加别名
   .set('@', resolve('src'))
   .set('@assets', resolve('src/assets'))
   .set('@components', resolve('src/components'))
   .set('@views', resolve('src/views'))
   .set('@store', resolve('src/store'));
 // 压缩图片
 // 需要 npm i -D image-webpack-loader
 config.module
   .rule("images")
   .use("image-webpack-loader")
   .loader("image-webpack-loader")
   .options({
     mozjpeg: { progressive: true, quality: 65 },
     optipng: { enabled: false },
     pngquant: { quality: [0.65, 0.9], speed: 4 },
     gifsicle: { interlaced: false },
     webp: { quality: 75 }
   });
 // 打包分析, 打包之后自动生成一个名叫report.html文件(可忽视)
 if (IS_PROD) {
   config.plugin("webpack-report").use(BundleAnalyzerPlugin, [
     {
       analyzerMode: "static"
     }
   ]);
 }
},
configureWebpack: config => {
 // 开启 gzip 压缩
 // 需要 npm i -D compression-webpack-plugin
 const plugins = [];
 if (IS_PROD) {
   plugins.push(
     new CompressionWebpackPlugin({
       filename: "[path].gz[query]",
       algorithm: "gzip",
       test: productionGzipExtensions,
       threshold: 10240,
       minRatio: 0.8
     })
   );
 }
 config.plugins = [...config.plugins, ...plugins];
},
css: {
 extract: IS_PROD,
 requireModuleExtension: false,// 去掉文件名中的 .module
 loaderOptions: {
     // 给 less-loader 传递 Less.js 相关选项
     less: {
       // `globalVars` 定义全局对象,可加入全局变量
       globalVars: {
         primary: '#333'
       }
     }
 }
},
devServer: {
   overlay: { // 让浏览器 overlay 同时显示警告和错误
    warnings: true,
    errors: true
   },
   host: "localhost",
   port: 8080, // 端口号
   https: false, // https:{type:Boolean}
   open: false, //配置自动启动浏览器
   hotOnly: true, // 热更新
   // proxy: 'http://localhost:8080'  // 配置跨域处理,只有一个代理
   proxy: { //配置多个跨域
     "/api": {
       target: "http://172.11.11.11:7071",
       changeOrigin: true,
       // ws: true,//websocket支持
       secure: false,
       pathRewrite: {
         "^/api": "/"
       }
     },
     "/api2": {
       target: "http://172.12.12.12:2018",
       changeOrigin: true,
       //ws: true,//websocket支持
       secure: false,
       pathRewrite: {
         "^/api2": "/"
       }
     },
   }
 }
}

Hbuilder

谷歌chrome 官网下载

postman 官网下载

posted @ 2022-03-08 22:44  波吉国王  阅读(167)  评论(0编辑  收藏  举报