Vue build mobile terminal h5 project steps
Introduction
Recently, other front-end partners in the team need to build their own mobile h5 projects, and there is no overall idea, so I wrote this step.
preparation stage
-
Determine the project
ui
frameworkVue better stage moving end frame:
ui
, , , , , and the like.vant
vux
cube-ui
Mint-UI
Muse UI
NutUI
-
Select
css
preprocessing style language, (vue-cli
select in)less
,sass
,stylus
Can,vue-cli 3
after corresponding versionswebpack
ofloader
all configured, simply download the corresponding package just fine. Usage suggestion: Generally, it is better to choose the samecss
preprocessing language as the preprocessing language of the ui framework. If the class isvant
usedless
, it will be used first in the projectless
. -
The need for integration
eslint
, ,stylelint
,prettier
,git hooks
,lint-staged
general recommendationseslint
are necessary, others not mandatory, these are behind you can add your own hand. -
Using the
Vue-cli
generated project, integrationvuex
,vue-router
,css预处理语言
,eslint
and so on.Project directory
├── babel.config.js ├── package.json ├── public │ ├── favicon.ico │ └── index.html └── src ├── App.vue ├── assets ├── components ├── main.js ├── router ├── store └── views
Style and adaptation
-
Reset style
Generally, reset.css or normalize.css needs to be loaded to reset and protect the styles of major browsers. Generally recommended
normalize.css
. -
Install and import the ui framework selected in the preparation phase
-
Choose a mobile adaptation plan
Now the mainstream adaptation scheme
rem
andvw
the old project withrem
more, over time,vw
the adaptation scheme will prevail. It is generally recommended that new projects be directlyvw
adapted to the plan.Adaptation scheme link:
-
Global style definition. Global style variable definition: color, font size, width, etc.
Adding tool plug-ins
-
Request tool addition (set baseUrl with environment variable.env configuration, mentioned later)
Mainstream request library:
axios
(Support React Native, Node, browser)fly.js
(Support Node.js, WeChat applet, Weex, React Native, Quick App and browser).Generally, you need to encapsulate according to the project yourself. Commonly, you need to encapsulate the request interceptor and response interceptor.
-
vuex
The addition of global status involves login, global ui status management, etc. -
Vue
Adding plugins, filters, mixins, hooks, etc. -
Add
vue-router
the navigation guard for. Optional -
fastclick
Solve the mobile terminal 300 millisecond delay problem -
vconsole
Convenient for mobile debugging -
The addition of other tools, time processing, deep copy, function throttling and anti-shake, etc.
Project eslint, stylelint, prettier configuration
-
Configuration
.eslintrc.js
Generally inherit
@vue/eslint-config-standard
this plug-in, the specific rules need to berules
defined in the field -
Configuration
.stylelintrc
, optionalGenerally inherit
stylelint-config-standard
this plug-in, the specific rules need to berules
defined in the field -
Configuration
.prettierrc.js
, optionalGeneral configuration
module.exports = { trailingComma: 'none', tabWidth: 2, semi: false, singleQuote: true, jsxSingleQuote: true, bracketSpacing: true, arrowParens: 'avoid' }
Project command and environment variable configuration
-
Define environment variable files according to webpack mode, mode can be defined by yourself
.env.dev #测试环境变量文件,不打包的话不用配置 .env.development #开发环境变量文件 .env.production #生产环境变量文件
Vue project environment variables
VUE_APP
start with, except for special project variables. E.g:.env.development
fileNODE_ENV=development VUE_APP_BASE_API=https://api.ex.com VUE_CLI_BABEL_TRANSPILE_MODULES=true
-
Configure the npm script according to the mode defined above
package.json
file"scripts": { "serve": "vue-cli-service serve", "dev": "vue-cli-service serve", "lint": "vue-cli-service lint", "build:prod": "vue-cli-service build --mode production", "build:dev": "vue-cli-service build --mode dev" }
-
gitHooks
lint-staged
Configuration, optional
Configuration of vue.config.js (webpack)
-
deServer
Configuration,Common ports, proxy, whether to open the browser, etc.
-
webpack
ConfigurationGenerally need to configure
gzip
anddrop_console
wait. -
Alias configuration, the project will generally be set by default, optional
Generally need to configure
@
:src
-
Other common configurations, optional
publicPath: './', outputDir: 'dist', assetsDir: 'static', productionSourceMap: false, lintOnSave: true