mpvue 同时开发和打包成H5和微信小程序

一、mpvue只打包小程序

mpvue 同时开发和打包成H5和微信小程序_第1张图片
image

注意(遇到的坑):

  • url 路由参数通过 this.root.root.mp.query获取,并且在page onLoad 时候传递的 options,即mounted生命函数中能获取

  • 之前的this.router.push改成小程序支持的wx.navigateTo(url),this.router.push改成小程序支持的wx.navigateTo(url),this.router.replace 改成 wx.reLaunch({ url }),其他相关的api 均改成对应的微信小程序的api

  • 引用图片路径时,微信小程序中不支持在template中使用require请求图片资源,静态请求src可以使用../../../static/img/或者 /static/img/引用根目录图片,动态引用src 时,使用 /static/img/,在同时能兼容H5的时候,可使用变量+局部路径来实现,如小程序能编译 /static/img/,而H5能编译 static/img/

  • vuex 的使用,官方给的demo是pages 文件每个page页面各一个store.js储存数据;可以直接挂到Vue原型上,在入口文件main.js中:
    import store from ‘./store’
    Vue.prototype.$store = store

mpvue 同时开发和打包成H5和微信小程序_第2张图片
image
  • package.json文件里面:红线里面的去掉


    mpvue 同时开发和打包成H5和微信小程序_第3张图片
    6b27b6e43cd4ad7c92090b4c26d4c7e.png

    然后修改一下配置:

//config文件下面的index.js
build: {
    index: path.resolve(__dirname, `../dist/${fileExt.platform}/index.html`),
    assetsRoot: path.resolve(__dirname, `../dist/${fileExt.platform}`),
.....
}

改成:

build: {
    index: path.resolve(__dirname, '../dist/index.html'),
    assetsRoot: path.resolve(__dirname, '../dist'),
.....
}

//build文件下面的webpack.dev.conf.js
plugins: [
    new ExtractTextPlugin({
      filename: utils.assetsPath(`[name].${config.dev.fileExt.style}`)
    }),
.....
]

改成:

new ExtractTextPlugin({
      filename: utils.assetsPath('css/[name].wxss')
    }),

//project.config.json

改成:
"miniprogramRoot": "dist/",

小程序正常开发打包,使用微信开发者工具调试 源码: github https://github.com/Aimee1608/mpsudoku469

二、添加打包成H5配置

安装

安装对应的module vue 、vuex、vue-loader、vue-style-loader、vue-template-compiler、webpack-dev-server等
具体配置文件如下:

{
  "name": "wxH5sudoku469",
  "version": "1.0.0",
  "description": "A Mpvue project",
  "author": "aimee",
  "private": true,
  "scripts": {
    "dev": "node build/dev-server.js",
    "start": "node build/dev-server.js",
    "build": "node build/build.js",
    "lint": "eslint --ext .js,.vue src",
    "devH5": "webpack-dev-server --inline --progress --config buildH5/webpack.devH5.conf.js",
    "all": "npm run dev && npm run devH5",
    "buildH5": "node buildH5/build.js"
  },
  "dependencies": {
    "mpvue": "^1.0.11",
    "vue": "^2.5.16",
    "vue-router": "^3.0.1",
    "vuex": "^3.0.1"
  },
  "devDependencies": {
    "autoprefixer": "^8.6.3",
    "babel-core": "^6.22.1",
    "babel-eslint": "^8.2.3",
    "babel-loader": "^7.1.1",
    "babel-plugin-transform-runtime": "^6.22.0",
    "babel-preset-env": "^1.3.2",
    "babel-preset-stage-2": "^6.22.0",
    "babel-register": "^6.22.0",
    "chalk": "^2.4.0",
    "clean-webpack-plugin": "^0.1.19",
    "connect-history-api-fallback": "^1.3.0",
    "copy-webpack-plugin": "^4.5.1",
    "css-loader": "^0.28.11",
    "cssnano": "^3.10.0",
    "eslint": "^4.19.1",
    "eslint-friendly-formatter": "^4.0.1",
    "eslint-loader": "^2.0.0",
    "eslint-plugin-html": "^4.0.3",
    "eslint-plugin-import": "^2.11.0",
    "eslint-plugin-node": "^6.0.1",
    "eventsource-polyfill": "^0.9.6",
    "express": "^4.16.3",
    "extract-text-webpack-plugin": "^3.0.2",
    "file-loader": "^1.1.11",
    "friendly-errors-webpack-plugin": "^1.7.0",
    "glob": "^7.1.2",
    "html-webpack-plugin": "^3.2.0",
    "http-proxy-middleware": "^0.18.0",
    "less": "^3.0.4",
    "less-loader": "^4.1.0",
    "mpvue-loader": "^1.0.13",
    "mpvue-template-compiler": "^1.0.11",
    "mpvue-webpack-target": "^1.0.0",
    "node-notifier": "^5.2.1",
    "optimize-css-assets-webpack-plugin": "^3.2.0",
    "ora": "^2.0.0",
    "portfinder": "^1.0.13",
    "postcss-import": "^11.1.0",
    "postcss-loader": "^2.1.4",
    "postcss-mpvue-wxss": "^1.0.0",
    "postcss-url": "^7.3.2",
    "prettier": "~1.12.1",
    "px2rpx-loader": "^0.1.10",
    "rimraf": "^2.6.0",
    "semver": "^5.3.0",
    "shelljs": "^0.8.1",
    "uglifyjs-webpack-plugin": "^1.2.5",
    "url-loader": "^1.0.1",
    "vue-loader": "^13.3.0",
    "vue-style-loader": "^4.1.0",
    "vue-template-compiler": "^2.5.16",
    "webpack": "^3.11.0",
    "webpack-bundle-analyzer": "^2.2.1",
    "webpack-cli": "^3.0.8",
    "webpack-dev-middleware-hard-disk": "^1.12.0",
    "webpack-dev-server": "^3.0.0",
    "webpack-merge": "^4.1.0",
    "webpack-mpvue-asset-plugin": "^0.0.2"
  },
  "engines": {
    "node": ">= 4.0.0",
    "npm": ">= 3.0.0"
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 8"
  ]
}

配置:

  • 在原有的基础上添加webpack 开发打包buildH5 和 configH5配置文件 ,以及对应的路由配置文件router ,AppH5.vue入口vue 文件,mainH5.js 编译入口文件,以及对H5和wx 分别封装的方法及api
    在configH5中配置build 输出的文件目录为distH5 :
    index: path.resolve(__dirname, ‘../distH5/index.html’)
    assetsRoot: path.resolve(__dirname, ‘../distH5’)

  • 在package.json 文件中配置编译和打包文件的命令执行语句
    “devH5”: “webpack-dev-server –inline –progress –config buildH5/webpack.devH5.conf.js”,
    “all”: “npm run dev && npm run devH5”,
    “buildH5”: “node buildH5/build.js”

注意配置文件:
.postcssrc.js 此条为编译微信的时候需用配置 “postcss-mpvue-wxss”: {},转化成wxss有特别棒的效果,可以优化css的样式,编译成wxss可识别的样式代码,但启动H5时要把“postcss-mpvue-wxss”: {}注释掉

mpvue 同时开发和打包成H5和微信小程序_第4张图片
image

打包成H5的配置内容

  • AppH5.vue 添加入口设置



  • main.js 和 mainH5.js 通过Vue.mixin分别配置微信小程序和H5的相同功能但不同api 的方法及变量
    main.js 如下图:


    mpvue 同时开发和打包成H5和微信小程序_第5张图片
    image

    mainH5.js 如下图


    mpvue 同时开发和打包成H5和微信小程序_第6张图片
    image

mpvue同时打包成H5和小程序的源码:github
https://github.com/Aimee1608/wxH5sudoku469
https://github.com/Aimee1608/mpvuedemo

你可能感兴趣的:(mpvue 同时开发和打包成H5和微信小程序)