在macos下配置vue环境

前端开发配置

Posted by czk on October 20, 2019

MacOS 中搭建vue工作环境

  • Node

官网下载 https://nodejs.org/en/download/ 测试Node安装 测试是否安装成功。 打开终端,输入node,出现>交互命令行,然后可以输入

console.log("Hello,World")。

> console.log("Hello,World");
Hello,World
undefined

输入.exit退出node,查看node和npm的版本。

node -v
npm -v

设置npm的默认安装路径和缓存路径

  • 查看用户配置 npm config ls npm config list
  • 查看全部配置 npm config ls -l npm config list -l

  • Vue
npm i -g yarn
npm install vue

如果在安装过程中出过一些错误,可能是npm再mac权限不够可以简单的提权安装

  • webpack
    sudo -s
    npm install webpack -g
    
  • vuecli
npm install -g @vue/cli
$ yarn global add @vue/cli-init

测试初始化 如果安装的是vuex是4.0版本直接使用

vue create test-app

npm权限不够 有以下几种方法

  1. 使用nvm:https://github.com/creationix/nvm/blob/master/README.md#installation 在linux上,一条命令搞定nvm安装:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

or

wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.9/install.sh | bash

然后,安装node,注意:如果你已经安装了node,不用卸载旧的,直接安装就好,nvm会替你管理版本的:

nvm install node

之后则不会出现问题。

p.s如果出现安装后

Command 'nvm' not found, did you mean:...
nvm is already installed in /home/richard/.nvm, trying to update using git

的问题可以重新执行curl安装nvm,nvm的配置是放在~/.bash_profile里面。之后通过vim编辑来将下面配置写入

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
  1. 更新npm权限
 $ sudo npm i -g npm
  • 终端美化

1.检查环境 输入命令,检查是否有/bin/zshmacOS自带zsh

cat /etc/shells

2.修改环境 修改默认的bash为zsh,重启Terminal或者iterm2.

chsh -s /bin/zsh

检查修改结果,显示/bin/zsh即成功.

echo $SHELL
  • 安装配置oh-my-zsh

1.安装oh-my-zsh运行一键安装包命令

sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

2.配置oh-my-zsh主题下载powerlevel9k主题

git clone https://github.com/bhilburn/powerlevel9k.git ~/.oh-my-zsh/custom/themes/powerlevel9k

编辑.zshrc文件来修改oh-my-zsh主题

vi .zshrc
找到ZSH_THEME="robbyrussell"
改为ZSH_THEME="powerlevel9k/powerlevel9k"

Mac中VIM的 相关操作

i   insert 进入编辑
Esc 退出编辑模式,输入以下命令:

:wq 保存后退出vi,若为 :wq! 则为强制储存后退出(常用)

:w 保存但不退出(常用)

:w! 若文件属性为『只读』时,强制写入该档案

:q 离开 vi (常用)

:q! 若曾修改过档案,又不想储存,使用 ! 为强制离开不储存档案。

:e! 将档案还原到最原始的状态!
  • 配置Terminal,iTerm2,VSCode.

1.下载字体及主题下载Menlo-for-Powerline字体包,包内字体分别双击安装到系统内.

下载地址:https://github.com/stilleshan/files/raw/master/projects/vscode-zsh/Menlo-for-Powerline.zip

下载Solarized主题,包内包含Terminal和iTerm2两个软件的5个主题.

推荐使用Solarized Dark Higher Contrast风格,包内含有全部5种风格,请自行选择. 下载地址:https://github.com/stilleshan/files/raw/master/projects/vscode-zsh/Solarized.zip

2.配置Terminal

  • 在刚下载的Solarized包内,找到Solarized-for-Terminal中的Solarized Dark Higher Contrast.terminal,双击运行即打开Terminal.

  • 此时因为字体文件未设置,有乱码.
  • 偏好设置-描述文件
  • 左侧Solarized Dark Higher Contrast点击下面的默认,设置为默认主题.
  • 字体选择安装好的Menlo for Powerline即配置完毕.

image

image

3.配置iTerm2 官网 https://www.iterm2.com/ 下载并安装iTerm2

  • Preferences-Profiles-Colors
  • 右下角Color Presets选择import导入Solarized Dark Higher Contrast,并选择此主题.
  • Preferences-Profiles-Text
  • Font选择安装好的Menlo for Powerline即配置完毕.

image image image

4.配置VSCode 打开Vscode-找到设置-搜索setting.json并编辑

image

"terminal.external.osxExec": "iTerm.app",
"terminal.integrated.shell.osx": "zsh",
"terminal.integrated.fontFamily": "Menlo for Powerline"

自用vscode插件列表

  • Atom One Dark Theme
  • Beautify
  • Code Runner
  • Debugger for Chrome
  • ESLint
  • eslint-disable-snippets
  • GitLens — Git supercharged
  • HTML CSS Support
  • HTML Snippets
  • JavaScript (ES6) code snippets
  • Material Icon Theme
  • npm
  • open in browser
  • Path Intellisense
  • Power Mode
  • Prettier - Code formatter
  • Prettier - JavaScript formatter
  • TabNine
  • Vetur
  • vscode-icons
  • vscode-icons-mac
  • vue
  • Vue 2 Snippets
  • Vue VSCode Snippets
  • vue-helper

vscode使用的setting.json

{
  // "workbench.colorTheme": "Github Light Theme - Gray",
  "window.zoomLevel": 0,
  "explorer.confirmDelete": false,
  "git.confirmSync": false,
  "editor.suggestSelection": "first",
  "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
  "breadcrumbs.enabled": true,
  "editor.minimap.enabled": false,
  // vscode默认启用了根据文件类型自动设置tabsize的选项
  "editor.detectIndentation": false,
  // 重新设定tabsize
  "editor.tabSize": 2,
  // #每次保存的时候自动格式化
  "editor.formatOnSave": true,
  // #每次保存的时候将代码按eslint格式进行修复
  "eslint.autoFixOnSave": true,
  // 添加 vue 支持
  "eslint.validate": [
    "javascript",
    "javascriptreact",
    {
      "language": "vue",
      "autoFix": true
    }
  ],
  //  #让prettier使用eslint的代码格式进行校验
  "prettier.eslintIntegration": true,
  //  #去掉代码结尾的分号
  "prettier.semi": false,
  //  #使用带引号替代双引号
  "prettier.singleQuote": true,
  //  #让函数(名)和后面的括号之间加个空格
  "javascript.format.insertSpaceBeforeFunctionParenthesis": true,
  "prettier.arrowParens": "always", 
  "vetur.format.defaultFormatterOptions": {
    "js-beautify-html": {
      "wrap_attributes": "force-expand-multiline"
    },
    "prettier": {
      "eslintIntegration": true,
      "arrowParens": "always",
      "semi": false,
      "singleQuote": true
    }
  },
  "workbench.iconTheme": "material-icon-theme",
  "workbench.tree.indent": 40,
  "workbench.tree.renderIndentGuides": "always",
  "[javascript]": {
    "editor.defaultFormatter": "vscode.typescript-language-features"
  },
  "explorer.confirmDragAndDrop": false,
  "editor.fontWeight": "700",
  "scss.lint.unknownProperties": "ignore",
  "css.lint.unknownProperties": "ignore",
  "scss.lint.vendorPrefix": "ignore",
  "vetur.validation.style": false,
  "window.menuBarVisibility": "default",
  "workbench.sideBar.location": "left",
  "files.associations": {
    "*.cjson": "jsonc",
    "*.wxss": "css",
    "*.wxs": "javascript"
  },
  "emmet.includeLanguages": {
    "wxml": "html"
  },
  "minapp-vscode.disableAutoConfig": true,
  "workbench.activityBar.visible": true,
  "editor.wordWrap": "on",
  "fileheader.customMade": {
    "Date": "Do not edit", // 文件创建时间(不变)
    "LastEditors": "last_modified_by", // 文件最后编辑者
    "LastEditTime": "Do not edit" // 文件最后编辑时间
  },
  "powermode.enabled": true,
  "vetur.format.defaultFormatter.html": "js-beautify-html",
  "vsicons.dontShowNewVersionMessage": true,
  "terminal.integrated.shell.windows": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
  "fileheader.Author": "czk",
  "fileheader.LastModifiedBy": "czk",
  "fileheader.tpl": "",
  "terminal.integrated.rendererType": "dom", // 函数注释
  "alias-skip.mappings": {
    "@": "/src" // 默认只有`@`映射,映射到`/src`,你可以添加更多映射,映射路径必须以`/`开头
    // ...更多映射关系
  },
  "workbench.colorTheme": "Atom One Dark",
  "workbench.startupEditor": "welcomePage",
  "terminal.external.osxExec": "iTerm.app",
  "terminal.integrated.shell.osx": "zsh",
  "terminal.integrated.fontFamily": "Menlo for Powerline",
  "diffEditor.ignoreTrimWhitespace": false
}

image