├── .babelrc ├── .editorconfig ├── .eslintignore ├── .eslintrc.js ├── .gitignore ├── .postcssrc.js ├── README.md ├── build ├── build.js ├── check-versions.js ├── dev-client.js ├── dev-server.js ├── utils.js ├── vue-loader.conf.js ├── webpack.base.conf.js ├── webpack.dev.conf.js └── webpack.prod.conf.js ├── buildH5 ├── build.js ├── check-versions.js ├── dev-client.js ├── utils.js ├── vue-loader.conf.js ├── webpack.baseH5.conf.js ├── webpack.devH5.conf.js └── webpack.prodH5.conf.js ├── config ├── dev.env.js ├── index.js └── prod.env.js ├── configH5 ├── dev.env.js ├── index.js └── prod.env.js ├── dist ├── app.js ├── app.json ├── app.wxss ├── components │ ├── card$79c1b934.wxml │ ├── counter$6c3d87bf.wxml │ ├── index$622cddd6.wxml │ ├── logs$31942962.wxml │ └── slots.wxml ├── copy-asset │ └── assets │ │ └── logo.png ├── pages │ ├── counter │ │ ├── main.js │ │ ├── main.wxml │ │ └── main.wxss │ ├── index │ │ ├── main.js │ │ ├── main.wxml │ │ └── main.wxss │ └── logs │ │ ├── main.js │ │ ├── main.json │ │ ├── main.wxml │ │ └── main.wxss └── static │ ├── css │ ├── app.wxss │ ├── pages │ │ ├── counter │ │ │ └── main.wxss │ │ ├── index │ │ │ └── main.wxss │ │ └── logs │ │ │ └── main.wxss │ └── vendor.wxss │ ├── img │ └── girl.png │ └── js │ ├── app.js │ ├── manifest.js │ ├── pages │ ├── counter │ │ └── main.js │ ├── index │ │ └── main.js │ └── logs │ │ └── main.js │ └── vendor.js ├── distH5 ├── index.html └── static │ ├── css │ └── app.css │ ├── img │ ├── girl.png │ └── logo.png │ └── js │ ├── app.js │ ├── manifest.js │ └── vendor.js ├── index.html ├── package-lock.json ├── package.json ├── project.config.json ├── src ├── App.vue ├── AppH5.vue ├── api │ ├── httpService.js │ └── wxService.js ├── assets │ └── logo.png ├── components │ └── card.vue ├── main.js ├── mainH5.js ├── pages │ ├── counter │ │ ├── index.vue │ │ └── main.js │ ├── index │ │ ├── index.vue │ │ └── main.js │ └── logs │ │ ├── index.vue │ │ └── main.js ├── router │ └── index.js ├── store │ └── index.js └── utils │ └── index.js └── static ├── .gitkeep └── img └── girl.png /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | ["env", { 4 | "modules": false, 5 | "targets": { 6 | "browsers": ["> 1%", "last 2 versions", "not ie <= 8"] 7 | } 8 | }], 9 | "stage-2" 10 | ], 11 | "plugins": ["transform-runtime"], 12 | "env": { 13 | "test": { 14 | "presets": ["env", "stage-2"], 15 | "plugins": ["istanbul"] 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | build/*.js 2 | config/*.js 3 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | // http://eslint.org/docs/user-guide/configuring 2 | 3 | module.exports = { 4 | root: true, 5 | parser: 'babel-eslint', 6 | parserOptions: { 7 | sourceType: 'module' 8 | }, 9 | env: { 10 | browser: false, 11 | node: true, 12 | es6: true 13 | }, 14 | // required to lint *.vue files 15 | plugins: [ 16 | 'html' 17 | ], 18 | // add your custom rules here 19 | 'rules': { 20 | // allow debugger during development 21 | 'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0 22 | }, 23 | globals: { 24 | App: true, 25 | Page: true, 26 | wx: true, 27 | getApp: true, 28 | getPage: true, 29 | requirePlugin: true 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules/ 3 | #dist/ 4 | #distH5/ 5 | npm-debug.log* 6 | yarn-debug.log* 7 | yarn-error.log* 8 | 9 | # Editor directories and files 10 | .idea 11 | *.suo 12 | *.ntvs* 13 | *.njsproj 14 | *.sln 15 | -------------------------------------------------------------------------------- /.postcssrc.js: -------------------------------------------------------------------------------- 1 | // https://github.com/michael-ciniawsky/postcss-load-config 2 | 3 | module.exports = { 4 | "plugins": { 5 | // "postcss-mpvue-wxss": {}, 6 | "postcss-import": {}, 7 | "postcss-url": {}, 8 | // to edit target browsers: use "browserslist" field in package.json 9 | "autoprefixer": {} 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mpvue demo 2 | 3 | > A Mpvue project 4 | 5 | ## Build Setup 6 | 7 | ``` bash 8 | # 安装依赖(注意查看package.json里面模块的安装版本号) 9 | npm install 10 | 11 | # 小程序运行 12 | npm run dev 13 | 14 | # H5运行 15 | npm run devH5 16 | 17 | # 小程序打包(打包到dist目录) 18 | npm run build 19 | 20 | # H5打包(打包到distH5目录) 21 | npm run buildH5 22 | 23 | ``` 24 | 25 | 26 | 博客详细说明 https://blog.csdn.net/Aimee1608/article/details/81084553 27 | 28 | 29 | ## 开始 30 | 这个项目是一个mpvue 的demo, 没有具体的业务实现方法,只有简单的页面切换,还有常用的一些方法封装,总体提供分开打包开发的思路 31 | >github源码 => https://github.com/Aimee1608/mpvuedemo 32 | 33 | 需要看详细版有项目内容的,可以看这篇文章,有详细说明 https://blog.csdn.net/aimee1608/article/details/80757077 34 | ## 目录结构 35 | 36 | ```bash 37 | . 38 | ├── README.md 39 | ├── build 小程序运行打包配置文件 40 | │ ├── build.js 41 | │ ├── check-versions.js 42 | │ ├── dev-client.js 43 | │ ├── dev-server.js 44 | │ ├── utils.js 45 | │ ├── vue-loader.conf.js 46 | │ ├── webpack.base.conf.js 47 | │ ├── webpack.dev.conf.js 48 | │ └── webpack.prod.conf.js 49 | ├── buildH5 H5运行打包配置文件 50 | │ ├── build.js 51 | │ ├── check-versions.js 52 | │ ├── dev-client.js 53 | │ ├── utils.js 54 | │ ├── vue-loader.conf.js 55 | │ ├── webpack.baseH5.conf.js 56 | │ ├── webpack.devH5.conf.js 57 | │ └── webpack.prodH5.conf.js 58 | ├── config 小程序运行打包配置文件 59 | │ ├── dev.env.js 60 | │ ├── index.js 61 | │ └── prod.env.js 62 | ├── configH5 H5运行打包配置文件 63 | │ ├── dev.env.js 64 | │ ├── index.js 65 | │ └── prod.env.js 66 | ├── dist 小程序打包生成的文件目录 67 | │ ├── app.js 68 | │ ├── app.json 69 | │ ├── app.wxss 70 | │ ├── components 71 | │ │ ├── card$79c1b934.wxml 72 | │ │ ├── counter$6c3d87bf.wxml 73 | │ │ ├── index$622cddd6.wxml 74 | │ │ ├── logs$31942962.wxml 75 | │ │ └── slots.wxml 76 | │ ├── copy-asset 77 | │ │ └── assets 78 | │ │ └── logo.png 79 | │ ├── pages 80 | │ │ ├── counter 81 | │ │ │ ├── main.js 82 | │ │ │ ├── main.wxml 83 | │ │ │ └── main.wxss 84 | │ │ ├── index 85 | │ │ │ ├── main.js 86 | │ │ │ ├── main.wxml 87 | │ │ │ └── main.wxss 88 | │ │ └── logs 89 | │ │ ├── main.js 90 | │ │ ├── main.json 91 | │ │ ├── main.wxml 92 | │ │ └── main.wxss 93 | │ └── static 94 | │ ├── css 95 | │ │ ├── app.wxss 96 | │ │ ├── pages 97 | │ │ │ ├── counter 98 | │ │ │ │ └── main.wxss 99 | │ │ │ ├── index 100 | │ │ │ │ └── main.wxss 101 | │ │ │ └── logs 102 | │ │ │ └── main.wxss 103 | │ │ └── vendor.wxss 104 | │ ├── img 105 | │ │ └── girl.png 106 | │ └── js 107 | │ ├── app.js 108 | │ ├── manifest.js 109 | │ ├── pages 110 | │ │ ├── counter 111 | │ │ │ └── main.js 112 | │ │ ├── index 113 | │ │ │ └── main.js 114 | │ │ └── logs 115 | │ │ └── main.js 116 | │ └── vendor.js 117 | ├── distH5 H5打包生成的文件目录 118 | │ ├── index.html 119 | │ └── static 120 | │ ├── css 121 | │ │ └── app.css 122 | │ ├── img 123 | │ │ ├── girl.png 124 | │ │ └── logo.png 125 | │ └── js 126 | │ ├── app.js 127 | │ ├── manifest.js 128 | │ └── vendor.js 129 | ├── index.html 入口index.html 页面 130 | ├── package-lock.json 131 | ├── package.json 安装配置文件 132 | ├── project.config.json 133 | ├── src 134 | │ ├── App.vue 小程序入口vue 135 | │ ├── AppH5.vue H5入口vue 136 | │ ├── api 小程序和H5分别封装的方法 137 | │ │ ├── httpService.js 138 | │ │ └── wxService.js 139 | │ ├── assets 静态资源文件 140 | │ │ └── logo.png 141 | │ ├── components 组件 142 | │ │ └── card.vue 143 | │ ├── main.js 小程序入口js 144 | │ ├── mainH5.js H5入口js 145 | │ ├── pages 页面内容 146 | │ │ ├── counter 147 | │ │ │ ├── index.vue 148 | │ │ │ └── main.js 149 | │ │ ├── index 150 | │ │ │ ├── index.vue 151 | │ │ │ └── main.js 152 | │ │ └── logs 153 | │ │ ├── index.vue 154 | │ │ └── main.js 155 | │ ├── router H5路由 156 | │ │ └── index.js 157 | │ ├── store vuex存储 158 | │ │ └── index.js 159 | │ └── utils js封装方法 160 | │ └── index.js 161 | └── static 静态资源文件 162 | └── img 163 | └── girl.png 164 | ``` 165 | 166 | ## 简易说明 167 | ### 路由跳转 168 | 169 | ```bash 170 | 185 | 186 | 206 | ``` 207 | 208 | ### 分别封装方法 209 | 210 | > H5 mainH5.js方法 211 | ```bash 212 | Vue.mixin({ 213 | data () { 214 | return { 215 | service: '', // 服务 216 | router: '/', // 路由路径 217 | imgSrc: '' // 图片路径 218 | } 219 | }, 220 | methods: { 221 | newroot () { 222 | return this.$route 223 | }, 224 | navigatePageTo (url) { 225 | this.$router.push(url) 226 | }, 227 | reLaunchPageTo (url) { 228 | this.$router.replace(url) 229 | }, 230 | setStorageSync (name, data) { 231 | sessionStorage.setItem(name, JSON.stringify(data)) 232 | }, 233 | getStorageSync (name) { 234 | return JSON.parse(sessionStorage.getItem(name)) 235 | } 236 | }, 237 | created () { 238 | console.log('chrome') 239 | this.service = httpService 240 | } 241 | }) 242 | ``` 243 | > 小程序main.js 244 | 245 | ```bash 246 | Vue.mixin({ 247 | data() { 248 | return { 249 | service: '', 250 | router: '/', 251 | imgSrc: '/' 252 | } 253 | }, 254 | methods: { 255 | newroot () { 256 | return this.$root.$mp 257 | }, 258 | navigatePageTo (url) { 259 | wx.navigateTo({url: url}) 260 | }, 261 | reLaunchPageTo (url) { 262 | wx.reLaunch({url: url}) 263 | }, 264 | setStorageSync (name, data) { 265 | wx.setStorageSync(name, data) 266 | }, 267 | getStorageSync (name) { 268 | return wx.getStorageSync(name) 269 | } 270 | }, 271 | created() { 272 | // console.log('wx') 273 | this.service = wxService 274 | } 275 | }) 276 | ``` 277 | 278 | ### vuex 数据存储 279 | >小程序store 直接挂在 Vue原型上 280 | 281 | ```bash 282 | Vue.prototype.$store = store 283 | ``` 284 | 285 | >H5vue 还是和之前一样 286 | 287 | ```bash 288 | new Vue({ 289 | el: '#app', 290 | router, 291 | components: { App }, 292 | template: '', 293 | store 294 | }) 295 | ``` 296 | -------------------------------------------------------------------------------- /build/build.js: -------------------------------------------------------------------------------- 1 | require('./check-versions')() 2 | 3 | process.env.NODE_ENV = 'production' 4 | 5 | var ora = require('ora') 6 | var rm = require('rimraf') 7 | var path = require('path') 8 | var chalk = require('chalk') 9 | var webpack = require('webpack') 10 | var config = require('../config') 11 | var webpackConfig = require('./webpack.prod.conf') 12 | 13 | var spinner = ora('building for production...') 14 | spinner.start() 15 | 16 | rm(path.join(config.build.assetsRoot, '*'), err => { 17 | if (err) throw err 18 | webpack(webpackConfig, function (err, stats) { 19 | spinner.stop() 20 | if (err) throw err 21 | process.stdout.write(stats.toString({ 22 | colors: true, 23 | modules: false, 24 | children: false, 25 | chunks: false, 26 | chunkModules: false 27 | }) + '\n\n') 28 | 29 | if (stats.hasErrors()) { 30 | console.log(chalk.red(' Build failed with errors.\n')) 31 | process.exit(1) 32 | } 33 | 34 | console.log(chalk.cyan(' Build complete.\n')) 35 | console.log(chalk.yellow( 36 | ' Tip: built files are meant to be served over an HTTP server.\n' + 37 | ' Opening index.html over file:// won\'t work.\n' 38 | )) 39 | }) 40 | }) 41 | -------------------------------------------------------------------------------- /build/check-versions.js: -------------------------------------------------------------------------------- 1 | var chalk = require('chalk') 2 | var semver = require('semver') 3 | var packageConfig = require('../package.json') 4 | var shell = require('shelljs') 5 | function exec (cmd) { 6 | return require('child_process').execSync(cmd).toString().trim() 7 | } 8 | 9 | var versionRequirements = [ 10 | { 11 | name: 'node', 12 | currentVersion: semver.clean(process.version), 13 | versionRequirement: packageConfig.engines.node 14 | } 15 | ] 16 | 17 | if (shell.which('npm')) { 18 | versionRequirements.push({ 19 | name: 'npm', 20 | currentVersion: exec('npm --version'), 21 | versionRequirement: packageConfig.engines.npm 22 | }) 23 | } 24 | 25 | module.exports = function () { 26 | var warnings = [] 27 | for (var i = 0; i < versionRequirements.length; i++) { 28 | var mod = versionRequirements[i] 29 | if (!semver.satisfies(mod.currentVersion, mod.versionRequirement)) { 30 | warnings.push(mod.name + ': ' + 31 | chalk.red(mod.currentVersion) + ' should be ' + 32 | chalk.green(mod.versionRequirement) 33 | ) 34 | } 35 | } 36 | 37 | if (warnings.length) { 38 | console.log('') 39 | console.log(chalk.yellow('To use this template, you must update following to modules:')) 40 | console.log() 41 | for (var i = 0; i < warnings.length; i++) { 42 | var warning = warnings[i] 43 | console.log(' ' + warning) 44 | } 45 | console.log() 46 | process.exit(1) 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /build/dev-client.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable */ 2 | require('eventsource-polyfill') 3 | var hotClient = require('webpack-hot-middleware/client?noInfo=true&reload=true') 4 | 5 | hotClient.subscribe(function (event) { 6 | if (event.action === 'reload') { 7 | window.location.reload() 8 | } 9 | }) 10 | -------------------------------------------------------------------------------- /build/dev-server.js: -------------------------------------------------------------------------------- 1 | require('./check-versions')() 2 | 3 | var config = require('../config') 4 | if (!process.env.NODE_ENV) { 5 | process.env.NODE_ENV = JSON.parse(config.dev.env.NODE_ENV) 6 | } 7 | 8 | // var opn = require('opn') 9 | var path = require('path') 10 | var express = require('express') 11 | var webpack = require('webpack') 12 | var proxyMiddleware = require('http-proxy-middleware') 13 | var portfinder = require('portfinder') 14 | var webpackConfig = require('./webpack.dev.conf') 15 | 16 | // default port where dev server listens for incoming traffic 17 | var port = process.env.PORT || config.dev.port 18 | // automatically open browser, if not set will be false 19 | var autoOpenBrowser = !!config.dev.autoOpenBrowser 20 | // Define HTTP proxies to your custom API backend 21 | // https://github.com/chimurai/http-proxy-middleware 22 | var proxyTable = config.dev.proxyTable 23 | 24 | var app = express() 25 | var compiler = webpack(webpackConfig) 26 | 27 | // var devMiddleware = require('webpack-dev-middleware')(compiler, { 28 | // publicPath: webpackConfig.output.publicPath, 29 | // quiet: true 30 | // }) 31 | 32 | // var hotMiddleware = require('webpack-hot-middleware')(compiler, { 33 | // log: false, 34 | // heartbeat: 2000 35 | // }) 36 | // force page reload when html-webpack-plugin template changes 37 | // compiler.plugin('compilation', function (compilation) { 38 | // compilation.plugin('html-webpack-plugin-after-emit', function (data, cb) { 39 | // hotMiddleware.publish({ action: 'reload' }) 40 | // cb() 41 | // }) 42 | // }) 43 | 44 | // proxy api requests 45 | Object.keys(proxyTable).forEach(function (context) { 46 | var options = proxyTable[context] 47 | if (typeof options === 'string') { 48 | options = { target: options } 49 | } 50 | app.use(proxyMiddleware(options.filter || context, options)) 51 | }) 52 | 53 | // handle fallback for HTML5 history API 54 | app.use(require('connect-history-api-fallback')()) 55 | 56 | // serve webpack bundle output 57 | // app.use(devMiddleware) 58 | 59 | // enable hot-reload and state-preserving 60 | // compilation error display 61 | // app.use(hotMiddleware) 62 | 63 | // serve pure static assets 64 | var staticPath = path.posix.join(config.dev.assetsPublicPath, config.dev.assetsSubDirectory) 65 | app.use(staticPath, express.static('./static')) 66 | 67 | // var uri = 'http://localhost:' + port 68 | 69 | var _resolve 70 | var readyPromise = new Promise(resolve => { 71 | _resolve = resolve 72 | }) 73 | 74 | // console.log('> Starting dev server...') 75 | // devMiddleware.waitUntilValid(() => { 76 | // console.log('> Listening at ' + uri + '\n') 77 | // // when env is testing, don't need open it 78 | // if (autoOpenBrowser && process.env.NODE_ENV !== 'testing') { 79 | // opn(uri) 80 | // } 81 | // _resolve() 82 | // }) 83 | 84 | module.exports = new Promise((resolve, reject) => { 85 | portfinder.basePort = port 86 | portfinder.getPortPromise() 87 | .then(newPort => { 88 | if (port !== newPort) { 89 | console.log(`${port}端口被占用,开启新端口${newPort}`) 90 | } 91 | var server = app.listen(newPort, 'localhost') 92 | // for 小程序的文件保存机制 93 | require('webpack-dev-middleware-hard-disk')(compiler, { 94 | publicPath: webpackConfig.output.publicPath, 95 | quiet: true 96 | }) 97 | resolve({ 98 | ready: readyPromise, 99 | close: () => { 100 | server.close() 101 | } 102 | }) 103 | }).catch(error => { 104 | console.log('没有找到空闲端口,请打开任务管理器杀死进程端口再试', error) 105 | }) 106 | }) 107 | -------------------------------------------------------------------------------- /build/utils.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | var config = require('../config') 3 | var ExtractTextPlugin = require('extract-text-webpack-plugin') 4 | 5 | exports.assetsPath = function (_path) { 6 | var assetsSubDirectory = process.env.NODE_ENV === 'production' 7 | ? config.build.assetsSubDirectory 8 | : config.dev.assetsSubDirectory 9 | return path.posix.join(assetsSubDirectory, _path) 10 | } 11 | 12 | exports.cssLoaders = function (options) { 13 | options = options || {} 14 | 15 | var cssLoader = { 16 | loader: 'css-loader', 17 | options: { 18 | minimize: process.env.NODE_ENV === 'production', 19 | sourceMap: options.sourceMap 20 | } 21 | } 22 | 23 | var postcssLoader = { 24 | loader: 'postcss-loader', 25 | options: { 26 | sourceMap: true 27 | } 28 | } 29 | 30 | var px2rpxLoader = { 31 | loader: 'px2rpx-loader', 32 | options: { 33 | baseDpr: 1, 34 | rpxUnit: 0.5 35 | } 36 | } 37 | 38 | // generate loader string to be used with extract text plugin 39 | function generateLoaders (loader, loaderOptions) { 40 | var loaders = [cssLoader, px2rpxLoader, postcssLoader] 41 | if (loader) { 42 | loaders.push({ 43 | loader: loader + '-loader', 44 | options: Object.assign({}, loaderOptions, { 45 | sourceMap: options.sourceMap 46 | }) 47 | }) 48 | } 49 | 50 | // Extract CSS when that option is specified 51 | // (which is the case during production build) 52 | if (options.extract) { 53 | return ExtractTextPlugin.extract({ 54 | use: loaders, 55 | fallback: 'vue-style-loader' 56 | }) 57 | } else { 58 | return ['vue-style-loader'].concat(loaders) 59 | } 60 | } 61 | 62 | // https://vue-loader.vuejs.org/en/configurations/extract-css.html 63 | return { 64 | css: generateLoaders(), 65 | wxss: generateLoaders(), 66 | postcss: generateLoaders(), 67 | less: generateLoaders('less'), 68 | sass: generateLoaders('sass', { indentedSyntax: true }), 69 | scss: generateLoaders('sass'), 70 | stylus: generateLoaders('stylus'), 71 | styl: generateLoaders('stylus') 72 | } 73 | } 74 | 75 | // Generate loaders for standalone style files (outside of .vue) 76 | exports.styleLoaders = function (options) { 77 | var output = [] 78 | var loaders = exports.cssLoaders(options) 79 | for (var extension in loaders) { 80 | var loader = loaders[extension] 81 | output.push({ 82 | test: new RegExp('\\.' + extension + '$'), 83 | use: loader 84 | }) 85 | } 86 | return output 87 | } 88 | -------------------------------------------------------------------------------- /build/vue-loader.conf.js: -------------------------------------------------------------------------------- 1 | var utils = require('./utils') 2 | var config = require('../config') 3 | // var isProduction = process.env.NODE_ENV === 'production' 4 | // for mp 5 | var isProduction = true 6 | 7 | module.exports = { 8 | loaders: utils.cssLoaders({ 9 | sourceMap: isProduction 10 | ? config.build.productionSourceMap 11 | : config.dev.cssSourceMap, 12 | extract: isProduction 13 | }), 14 | transformToRequire: { 15 | video: 'src', 16 | source: 'src', 17 | img: 'src', 18 | image: 'xlink:href' 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /build/webpack.base.conf.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | var fs = require('fs') 3 | var utils = require('./utils') 4 | var config = require('../config') 5 | var vueLoaderConfig = require('./vue-loader.conf') 6 | var MpvuePlugin = require('webpack-mpvue-asset-plugin') 7 | var glob = require('glob') 8 | 9 | function resolve (dir) { 10 | return path.join(__dirname, '..', dir) 11 | } 12 | 13 | function getEntry (rootSrc, pattern) { 14 | var files = glob.sync(path.resolve(rootSrc, pattern)) 15 | return files.reduce((res, file) => { 16 | var info = path.parse(file) 17 | var key = info.dir.slice(rootSrc.length + 1) + '/' + info.name 18 | res[key] = path.resolve(file) 19 | return res 20 | }, {}) 21 | } 22 | 23 | const appEntry = { app: resolve('./src/main.js') } 24 | const pagesEntry = getEntry(resolve('./src'), 'pages/**/main.js') 25 | const entry = Object.assign({}, appEntry, pagesEntry) 26 | 27 | module.exports = { 28 | // 如果要自定义生成的 dist 目录里面的文件路径, 29 | // 可以将 entry 写成 {'toPath': 'fromPath'} 的形式, 30 | // toPath 为相对于 dist 的路径, 例:index/demo,则生成的文件地址为 dist/index/demo.js 31 | entry, 32 | target: require('mpvue-webpack-target'), 33 | output: { 34 | path: config.build.assetsRoot, 35 | filename: '[name].js', 36 | publicPath: process.env.NODE_ENV === 'production' 37 | ? config.build.assetsPublicPath 38 | : config.dev.assetsPublicPath 39 | }, 40 | resolve: { 41 | extensions: ['.js', '.vue', '.json'], 42 | alias: { 43 | 'vue': 'mpvue', 44 | '@': resolve('src') 45 | }, 46 | symlinks: false, 47 | aliasFields: ['mpvue', 'weapp', 'browser'], 48 | mainFields: ['browser', 'module', 'main'] 49 | }, 50 | module: { 51 | rules: [ 52 | { 53 | test: /\.(js|vue)$/, 54 | loader: 'eslint-loader', 55 | enforce: 'pre', 56 | include: [resolve('src'), resolve('test')], 57 | options: { 58 | formatter: require('eslint-friendly-formatter') 59 | } 60 | }, 61 | { 62 | test: /\.vue$/, 63 | loader: 'mpvue-loader', 64 | options: vueLoaderConfig 65 | }, 66 | { 67 | test: /\.js$/, 68 | include: [resolve('src'), resolve('test')], 69 | use: [ 70 | 'babel-loader', 71 | { 72 | loader: 'mpvue-loader', 73 | options: { 74 | checkMPEntry: true 75 | } 76 | }, 77 | ] 78 | }, 79 | { 80 | test: /\.(png|jpe?g|gif|svg)(\?.*)?$/, 81 | loader: 'url-loader', 82 | options: { 83 | limit: 10000, 84 | name: utils.assetsPath('img/[name].[ext]') 85 | } 86 | }, 87 | { 88 | test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/, 89 | loader: 'url-loader', 90 | options: { 91 | limit: 10000, 92 | name: utils.assetsPath('media/[name]].[ext]') 93 | } 94 | }, 95 | { 96 | test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/, 97 | loader: 'url-loader', 98 | options: { 99 | limit: 10000, 100 | name: utils.assetsPath('fonts/[name].[ext]') 101 | } 102 | } 103 | ] 104 | }, 105 | plugins: [ 106 | new MpvuePlugin() 107 | ] 108 | } 109 | -------------------------------------------------------------------------------- /build/webpack.dev.conf.js: -------------------------------------------------------------------------------- 1 | var utils = require('./utils') 2 | var webpack = require('webpack') 3 | var config = require('../config') 4 | var merge = require('webpack-merge') 5 | var baseWebpackConfig = require('./webpack.base.conf') 6 | // var HtmlWebpackPlugin = require('html-webpack-plugin') 7 | var FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin') 8 | 9 | // copy from ./webpack.prod.conf.js 10 | var path = require('path') 11 | var ExtractTextPlugin = require('extract-text-webpack-plugin') 12 | var CopyWebpackPlugin = require('copy-webpack-plugin') 13 | var OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin') 14 | 15 | // add hot-reload related code to entry chunks 16 | // Object.keys(baseWebpackConfig.entry).forEach(function (name) { 17 | // baseWebpackConfig.entry[name] = ['./build/dev-client'].concat(baseWebpackConfig.entry[name]) 18 | // }) 19 | 20 | module.exports = merge(baseWebpackConfig, { 21 | module: { 22 | rules: utils.styleLoaders({ 23 | sourceMap: config.dev.cssSourceMap, 24 | extract: true 25 | }) 26 | }, 27 | // cheap-module-eval-source-map is faster for development 28 | // devtool: '#cheap-module-eval-source-map', 29 | devtool: '#source-map', 30 | output: { 31 | path: config.build.assetsRoot, 32 | // filename: utils.assetsPath('js/[name].[chunkhash].js'), 33 | // chunkFilename: utils.assetsPath('js/[id].[chunkhash].js') 34 | filename: utils.assetsPath('js/[name].js'), 35 | chunkFilename: utils.assetsPath('js/[id].js') 36 | }, 37 | plugins: [ 38 | new webpack.DefinePlugin({ 39 | 'process.env': config.dev.env 40 | }), 41 | 42 | // copy from ./webpack.prod.conf.js 43 | // extract css into its own file 44 | new ExtractTextPlugin({ 45 | // filename: utils.assetsPath('css/[name].[contenthash].css') 46 | filename: utils.assetsPath('css/[name].wxss') 47 | }), 48 | // Compress extracted CSS. We are using this plugin so that possible 49 | // duplicated CSS from different components can be deduped. 50 | new OptimizeCSSPlugin({ 51 | cssProcessorOptions: { 52 | safe: true 53 | } 54 | }), 55 | new webpack.optimize.CommonsChunkPlugin({ 56 | name: 'vendor', 57 | minChunks: function (module, count) { 58 | // any required modules inside node_modules are extracted to vendor 59 | return ( 60 | module.resource && 61 | /\.js$/.test(module.resource) && 62 | module.resource.indexOf('node_modules') >= 0 63 | ) || count > 1 64 | } 65 | }), 66 | new webpack.optimize.CommonsChunkPlugin({ 67 | name: 'manifest', 68 | chunks: ['vendor'] 69 | }), 70 | // copy custom static assets 71 | new CopyWebpackPlugin([ 72 | { 73 | from: path.resolve(__dirname, '../static'), 74 | to: config.build.assetsSubDirectory, 75 | ignore: ['.*'] 76 | } 77 | ]), 78 | 79 | // https://github.com/glenjamin/webpack-hot-middleware#installation--usage 80 | // new webpack.HotModuleReplacementPlugin(), 81 | new webpack.NoEmitOnErrorsPlugin(), 82 | // https://github.com/ampedandwired/html-webpack-plugin 83 | // new HtmlWebpackPlugin({ 84 | // filename: 'index.html', 85 | // template: 'index.html', 86 | // inject: true 87 | // }), 88 | new FriendlyErrorsPlugin() 89 | ] 90 | }) 91 | -------------------------------------------------------------------------------- /build/webpack.prod.conf.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | var utils = require('./utils') 3 | var webpack = require('webpack') 4 | var config = require('../config') 5 | var merge = require('webpack-merge') 6 | var baseWebpackConfig = require('./webpack.base.conf') 7 | var UglifyJsPlugin = require('uglifyjs-webpack-plugin') 8 | var CopyWebpackPlugin = require('copy-webpack-plugin') 9 | // var HtmlWebpackPlugin = require('html-webpack-plugin') 10 | var ExtractTextPlugin = require('extract-text-webpack-plugin') 11 | var OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin') 12 | 13 | var env = config.build.env 14 | 15 | var webpackConfig = merge(baseWebpackConfig, { 16 | module: { 17 | rules: utils.styleLoaders({ 18 | sourceMap: config.build.productionSourceMap, 19 | extract: true 20 | }) 21 | }, 22 | devtool: config.build.productionSourceMap ? '#source-map' : false, 23 | output: { 24 | path: config.build.assetsRoot, 25 | // filename: utils.assetsPath('js/[name].[chunkhash].js'), 26 | // chunkFilename: utils.assetsPath('js/[id].[chunkhash].js') 27 | filename: utils.assetsPath('js/[name].js'), 28 | chunkFilename: utils.assetsPath('js/[id].js') 29 | }, 30 | plugins: [ 31 | // http://vuejs.github.io/vue-loader/en/workflow/production.html 32 | new webpack.DefinePlugin({ 33 | 'process.env': env 34 | }), 35 | new UglifyJsPlugin({ 36 | sourceMap: true 37 | }), 38 | // extract css into its own file 39 | new ExtractTextPlugin({ 40 | // filename: utils.assetsPath('css/[name].[contenthash].css') 41 | filename: utils.assetsPath('css/[name].wxss') 42 | }), 43 | // Compress extracted CSS. We are using this plugin so that possible 44 | // duplicated CSS from different components can be deduped. 45 | new OptimizeCSSPlugin({ 46 | cssProcessorOptions: { 47 | safe: true 48 | } 49 | }), 50 | // generate dist index.html with correct asset hash for caching. 51 | // you can customize output by editing /index.html 52 | // see https://github.com/ampedandwired/html-webpack-plugin 53 | // new HtmlWebpackPlugin({ 54 | // filename: config.build.index, 55 | // template: 'index.html', 56 | // inject: true, 57 | // minify: { 58 | // removeComments: true, 59 | // collapseWhitespace: true, 60 | // removeAttributeQuotes: true 61 | // // more options: 62 | // // https://github.com/kangax/html-minifier#options-quick-reference 63 | // }, 64 | // // necessary to consistently work with multiple chunks via CommonsChunkPlugin 65 | // chunksSortMode: 'dependency' 66 | // }), 67 | // keep module.id stable when vender modules does not change 68 | new webpack.HashedModuleIdsPlugin(), 69 | // split vendor js into its own file 70 | new webpack.optimize.CommonsChunkPlugin({ 71 | name: 'vendor', 72 | minChunks: function (module, count) { 73 | // any required modules inside node_modules are extracted to vendor 74 | return ( 75 | module.resource && 76 | /\.js$/.test(module.resource) && 77 | module.resource.indexOf('node_modules') >= 0 78 | ) || count > 1 79 | } 80 | }), 81 | // extract webpack runtime and module manifest to its own file in order to 82 | // prevent vendor hash from being updated whenever app bundle is updated 83 | new webpack.optimize.CommonsChunkPlugin({ 84 | name: 'manifest', 85 | chunks: ['vendor'] 86 | }), 87 | // copy custom static assets 88 | new CopyWebpackPlugin([ 89 | { 90 | from: path.resolve(__dirname, '../static'), 91 | to: config.build.assetsSubDirectory, 92 | ignore: ['.*'] 93 | } 94 | ]) 95 | ] 96 | }) 97 | 98 | // if (config.build.productionGzip) { 99 | // var CompressionWebpackPlugin = require('compression-webpack-plugin') 100 | 101 | // webpackConfig.plugins.push( 102 | // new CompressionWebpackPlugin({ 103 | // asset: '[path].gz[query]', 104 | // algorithm: 'gzip', 105 | // test: new RegExp( 106 | // '\\.(' + 107 | // config.build.productionGzipExtensions.join('|') + 108 | // ')$' 109 | // ), 110 | // threshold: 10240, 111 | // minRatio: 0.8 112 | // }) 113 | // ) 114 | // } 115 | 116 | if (config.build.bundleAnalyzerReport) { 117 | var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin 118 | webpackConfig.plugins.push(new BundleAnalyzerPlugin()) 119 | } 120 | 121 | module.exports = webpackConfig 122 | -------------------------------------------------------------------------------- /buildH5/build.js: -------------------------------------------------------------------------------- 1 | require('./check-versions')() 2 | 3 | process.env.NODE_ENV = 'production' 4 | 5 | var ora = require('ora') 6 | var rm = require('rimraf') 7 | var path = require('path') 8 | var chalk = require('chalk') 9 | var webpack = require('webpack') 10 | var config = require('../configH5') 11 | var webpackConfig = require('./webpack.prodH5.conf') 12 | 13 | var spinner = ora('building for production...') 14 | spinner.start() 15 | 16 | rm(path.join(config.build.assetsRoot, config.build.assetsSubDirectory), err => { 17 | if (err) throw err 18 | webpack(webpackConfig, (err, stats) => { 19 | spinner.stop() 20 | if (err) throw err 21 | process.stdout.write(stats.toString({ 22 | colors: true, 23 | modules: false, 24 | children: false, // If you are using ts-loader, setting this to true will make TypeScript errors show up during build. 25 | chunks: false, 26 | chunkModules: false 27 | }) + '\n\n') 28 | 29 | if (stats.hasErrors()) { 30 | console.log(chalk.red(' Build failed with errors.\n')) 31 | process.exit(1) 32 | } 33 | 34 | console.log(chalk.cyan(' Build complete.\n')) 35 | console.log(chalk.yellow( 36 | ' Tip: built files are meant to be served over an HTTP server.\n' + 37 | ' Opening index.html over file:// won\'t work.\n' 38 | )) 39 | }) 40 | }) 41 | -------------------------------------------------------------------------------- /buildH5/check-versions.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | const chalk = require('chalk') 3 | const semver = require('semver') 4 | const packageConfig = require('../package.json') 5 | const shell = require('shelljs') 6 | 7 | function exec (cmd) { 8 | return require('child_process').execSync(cmd).toString().trim() 9 | } 10 | 11 | const versionRequirements = [ 12 | { 13 | name: 'node', 14 | currentVersion: semver.clean(process.version), 15 | versionRequirement: packageConfig.engines.node 16 | } 17 | ] 18 | 19 | if (shell.which('npm')) { 20 | versionRequirements.push({ 21 | name: 'npm', 22 | currentVersion: exec('npm --version'), 23 | versionRequirement: packageConfig.engines.npm 24 | }) 25 | } 26 | 27 | module.exports = function () { 28 | const warnings = [] 29 | 30 | for (let i = 0; i < versionRequirements.length; i++) { 31 | const mod = versionRequirements[i] 32 | 33 | if (!semver.satisfies(mod.currentVersion, mod.versionRequirement)) { 34 | warnings.push(mod.name + ': ' + 35 | chalk.red(mod.currentVersion) + ' should be ' + 36 | chalk.green(mod.versionRequirement) 37 | ) 38 | } 39 | } 40 | 41 | if (warnings.length) { 42 | console.log('') 43 | console.log(chalk.yellow('To use this template, you must update following to modules:')) 44 | console.log() 45 | 46 | for (let i = 0; i < warnings.length; i++) { 47 | const warning = warnings[i] 48 | console.log(' ' + warning) 49 | } 50 | 51 | console.log() 52 | process.exit(1) 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /buildH5/dev-client.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable */ 2 | require('eventsource-polyfill') 3 | var hotClient = require('webpack-hot-middleware/client?noInfo=true&reload=true') 4 | 5 | hotClient.subscribe(function (event) { 6 | if (event.action === 'reload') { 7 | window.location.reload() 8 | } 9 | }) 10 | -------------------------------------------------------------------------------- /buildH5/utils.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | const path = require('path') 3 | const config = require('../configH5') 4 | const ExtractTextPlugin = require('extract-text-webpack-plugin') 5 | const packageConfig = require('../package.json') 6 | 7 | exports.assetsPath = function (_path) { 8 | const assetsSubDirectory = process.env.NODE_ENV === 'production' 9 | ? config.build.assetsSubDirectory 10 | : config.dev.assetsSubDirectory 11 | return path.posix.join(assetsSubDirectory, _path) 12 | } 13 | 14 | exports.cssLoaders = function (options) { 15 | // console.log(options || {}, options) 16 | options = options || {} 17 | // console.log('generateLoaders', options.usePostCSS) 18 | const cssLoader = { 19 | loader: 'css-loader', 20 | options: { 21 | sourceMap: options.sourceMap 22 | } 23 | } 24 | 25 | const postcssLoader = { 26 | loader: 'postcss-loader', 27 | options: { 28 | sourceMap: options.sourceMap 29 | } 30 | } 31 | 32 | // generate loader string to be used with extract text plugin 33 | function generateLoaders (loader, loaderOptions) { 34 | const loaders = options.usePostCSS ? [cssLoader, postcssLoader] : [cssLoader] 35 | // , postcssLoader 36 | if (loader) { 37 | loaders.push({ 38 | loader: loader + '-loader', 39 | options: Object.assign({}, loaderOptions, { 40 | sourceMap: options.sourceMap 41 | }) 42 | }) 43 | } 44 | // Extract CSS when that option is specified 45 | // (which is the case during production build) 46 | if (options.extract) { 47 | return ExtractTextPlugin.extract({ 48 | use: loaders, 49 | fallback: 'vue-style-loader' 50 | }) 51 | } else { 52 | return ['vue-style-loader'].concat(loaders) 53 | } 54 | } 55 | // https://vue-loader.vuejs.org/en/configurations/extract-css.html 56 | return { 57 | css: generateLoaders(), 58 | postcss: generateLoaders(), 59 | less: generateLoaders('less'), 60 | sass: generateLoaders('sass', { indentedSyntax: true }), 61 | scss: generateLoaders('sass'), 62 | stylus: generateLoaders('stylus'), 63 | styl: generateLoaders('stylus') 64 | } 65 | } 66 | // Generate loaders for standalone style files (outside of .vue) 67 | exports.styleLoaders = function (options) { 68 | const output = [] 69 | const loaders = exports.cssLoaders(options) 70 | for (const extension in loaders) { 71 | const loader = loaders[extension] 72 | output.push({ 73 | test: new RegExp('\\.' + extension + '$'), 74 | use: loader 75 | }) 76 | } 77 | return output 78 | } 79 | 80 | exports.createNotifierCallback = () => { 81 | const notifier = require('node-notifier') 82 | return (severity, errors) => { 83 | if (severity !== 'error') return 84 | const error = errors[0] 85 | const filename = error.file && error.file.split('!').pop() 86 | notifier.notify({ 87 | title: packageConfig.name, 88 | message: severity + ': ' + error.name, 89 | subtitle: filename || '', 90 | icon: path.join(__dirname, 'logo.png') 91 | }) 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /buildH5/vue-loader.conf.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | const utils = require('./utils') 3 | const config = require('../configH5') 4 | const isProduction = process.env.NODE_ENV === 'production' 5 | const sourceMapEnabled = isProduction 6 | ? config.build.productionSourceMap 7 | : config.dev.cssSourceMap 8 | 9 | module.exports = { 10 | loaders: utils.cssLoaders({ 11 | sourceMap: sourceMapEnabled, 12 | extract: isProduction 13 | }), 14 | cssSourceMap: sourceMapEnabled, 15 | cacheBusting: config.dev.cacheBusting, 16 | transformToRequire: { 17 | video: ['src', 'poster'], 18 | source: 'src', 19 | img: 'src', 20 | image: 'xlink:href' 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /buildH5/webpack.baseH5.conf.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | const path = require('path') 3 | const utils = require('./utils') 4 | const config = require('../configH5') 5 | const vueLoaderConfig = require('./vue-loader.conf') 6 | 7 | function resolve (dir) { 8 | return path.join(__dirname, '..', dir) 9 | } 10 | const createLintingRule = () => ({ 11 | test: /\.(js|vue)$/, 12 | loader: 'eslint-loader', 13 | enforce: 'pre', 14 | include: [resolve('src'), resolve('test')], 15 | options: { 16 | formatter: require('eslint-friendly-formatter'), 17 | emitWarning: !config.dev.showEslintErrorsInOverlay 18 | } 19 | }) 20 | module.exports = { 21 | context: path.resolve(__dirname, '../'), 22 | entry: { 23 | app: './src/mainH5.js' 24 | // app: './module/' + enterdir + '/src/main.js' 25 | }, 26 | output: { 27 | path: config.build.assetsRoot, 28 | filename: '[name].js', 29 | publicPath: process.env.NODE_ENV === 'production' 30 | ? '.' + config.build.assetsPublicPath 31 | : config.dev.assetsPublicPath 32 | }, 33 | resolve: { 34 | extensions: ['.js', '.vue', '.json', '.html'], 35 | alias: { 36 | 'vue$': 'vue/dist/vue.esm.js', 37 | '@': resolve('src') 38 | } 39 | }, 40 | module: { 41 | rules: [ 42 | ...(config.dev.useEslint ? [createLintingRule()] : []), 43 | { 44 | test: /\.vue$/, 45 | loader: 'vue-loader', 46 | options: vueLoaderConfig 47 | }, 48 | { 49 | test: /\.js$/, 50 | loader: 'babel-loader', 51 | include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')] 52 | }, 53 | { 54 | test: /\.css$/, 55 | use: [ 56 | 'vue-style-loader', 57 | 'css-loader' 58 | ], 59 | }, 60 | { 61 | test: /\.less$/, 62 | 63 | loader: "style-loader!css-loader!less-loader", 64 | }, 65 | { 66 | test: /\.(png|jpe?g|gif|svg)(\?.*)?$/, 67 | loader: 'url-loader', 68 | options: { 69 | limit: 1000, 70 | name: utils.assetsPath('img/[name].[ext]?v=[hash:7]') 71 | } 72 | }, 73 | { 74 | test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/, 75 | loader: 'url-loader', 76 | options: { 77 | limit: 1000, 78 | name: utils.assetsPath('media/[name].[ext]?v=[hash:7]') 79 | } 80 | }, 81 | { 82 | test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/, 83 | loader: 'url-loader', 84 | options: { 85 | limit: 10000, 86 | name: utils.assetsPath('fonts/[name].[ext]?v=[hash:7]') 87 | } 88 | } 89 | ] 90 | }, 91 | node: { 92 | // prevent webpack from injecting useless setImmediate polyfill because Vue 93 | // source contains it (although only uses it if it's native). 94 | setImmediate: false, 95 | // prevent webpack from injecting mocks to Node native modules 96 | // that does not make sense for the client 97 | dgram: 'empty', 98 | fs: 'empty', 99 | net: 'empty', 100 | tls: 'empty', 101 | child_process: 'empty' 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /buildH5/webpack.devH5.conf.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | const utils = require('./utils') 3 | const webpack = require('webpack') 4 | const config = require('../configH5') 5 | const merge = require('webpack-merge') 6 | const path = require('path') 7 | const baseWebpackConfig = require('./webpack.baseH5.conf') 8 | const CopyWebpackPlugin = require('copy-webpack-plugin') 9 | const HtmlWebpackPlugin = require('html-webpack-plugin') 10 | const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin') 11 | const portfinder = require('portfinder') 12 | 13 | const HOST = process.env.HOST 14 | const PORT = process.env.PORT && Number(process.env.PORT) 15 | 16 | const devWebpackConfig = merge(baseWebpackConfig, { 17 | module: { 18 | rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap, usePostCSS: false}) 19 | }, 20 | // cheap-module-eval-source-map is faster for development 21 | devtool: config.dev.devtool, 22 | 23 | // these devServer options should be customized in /config/index.js 24 | devServer: { 25 | clientLogLevel: 'warning', 26 | historyApiFallback: { 27 | rewrites: [ 28 | { from: /.*/, to: path.posix.join(config.dev.assetsPublicPath, 'index.html') }, 29 | ], 30 | }, 31 | hot: true, 32 | contentBase: false, // since we use CopyWebpackPlugin. 33 | compress: true, 34 | host: HOST || config.dev.host, 35 | port: PORT || config.dev.port, 36 | open: config.dev.autoOpenBrowser, 37 | overlay: config.dev.errorOverlay 38 | ? { warnings: false, errors: true } 39 | : false, 40 | publicPath: config.dev.assetsPublicPath, 41 | proxy: config.dev.proxyTable, 42 | quiet: true, // necessary for FriendlyErrorsPlugin 43 | watchOptions: { 44 | poll: config.dev.poll, 45 | } 46 | }, 47 | plugins: [ 48 | new webpack.DefinePlugin({ 49 | 'process.env': require('../configH5/dev.env') 50 | }), 51 | new webpack.HotModuleReplacementPlugin(), 52 | new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update. 53 | new webpack.NoEmitOnErrorsPlugin(), 54 | // https://github.com/ampedandwired/html-webpack-plugin 55 | new HtmlWebpackPlugin({ 56 | filename: 'index.html', 57 | template: 'index.html', 58 | inject: true 59 | }), 60 | // copy custom static assets 61 | new CopyWebpackPlugin([ 62 | { 63 | from: path.resolve(__dirname, '../static'), 64 | to: config.dev.assetsSubDirectory, 65 | ignore: ['.*'] 66 | } 67 | ]) 68 | ] 69 | }) 70 | 71 | module.exports = new Promise((resolve, reject) => { 72 | portfinder.basePort = process.env.PORT || config.dev.port 73 | portfinder.getPort((err, port) => { 74 | if (err) { 75 | reject(err) 76 | } else { 77 | // publish the new Port, necessary for e2e tests 78 | process.env.PORT = port 79 | // add port to devServer config 80 | devWebpackConfig.devServer.port = port 81 | 82 | // Add FriendlyErrorsPlugin 83 | devWebpackConfig.plugins.push(new FriendlyErrorsPlugin({ 84 | compilationSuccessInfo: { 85 | messages: [`Your application is running here: http://${devWebpackConfig.devServer.host}:${port}`], 86 | }, 87 | onErrors: config.dev.notifyOnErrors 88 | ? utils.createNotifierCallback() 89 | : undefined 90 | })) 91 | 92 | resolve(devWebpackConfig) 93 | } 94 | }) 95 | }) 96 | -------------------------------------------------------------------------------- /buildH5/webpack.prodH5.conf.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | const path = require('path') 3 | const utils = require('./utils') 4 | const webpack = require('webpack') 5 | const config = require('../configH5') 6 | const merge = require('webpack-merge') 7 | const baseWebpackConfig = require('./webpack.baseH5.conf') 8 | const CopyWebpackPlugin = require('copy-webpack-plugin') 9 | const HtmlWebpackPlugin = require('html-webpack-plugin') 10 | const ExtractTextPlugin = require('extract-text-webpack-plugin') 11 | const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin') 12 | const UglifyJsPlugin = require('uglifyjs-webpack-plugin') 13 | 14 | const env = process.env.NODE_ENV === 'testing' 15 | ? require('../configH5/test.env') 16 | : require('../configH5/prod.env') 17 | 18 | const webpackConfig = merge(baseWebpackConfig, { 19 | module: { 20 | rules: utils.styleLoaders({ 21 | sourceMap: config.build.productionSourceMap, 22 | extract: false, 23 | usePostCSS: false 24 | }) 25 | }, 26 | devtool: config.build.productionSourceMap ? config.build.devtool : false, 27 | output: { 28 | path: config.build.assetsRoot, 29 | filename: utils.assetsPath('js/[name].js?v=[chunkhash]'), 30 | chunkFilename: utils.assetsPath('js/[id].js?v=[chunkhash]') 31 | }, 32 | plugins: [ 33 | // http://vuejs.github.io/vue-loader/en/workflow/production.html 34 | new webpack.DefinePlugin({ 35 | 'process.env': env 36 | }), 37 | new UglifyJsPlugin({ 38 | uglifyOptions: { 39 | compress: { 40 | warnings: false 41 | } 42 | }, 43 | sourceMap: true, 44 | parallel: true 45 | }), 46 | // extract css into its own file 47 | // 48 | new ExtractTextPlugin({ 49 | filename: utils.assetsPath('css/[name].css?v=[contenthash]'), 50 | // Setting the following option to `false` will not extract CSS from codesplit chunks. 51 | // Their CSS will instead be inserted dynamically with style-loader when the codesplit chunk has been loaded by webpack. 52 | // It's currently set to `true` because we are seeing that sourcemaps are included in the codesplit bundle as well when it's `false`, 53 | // increasing file size: https://github.com/vuejs-templates/webpack/issues/1110 54 | allChunks: true, 55 | }), 56 | // Compress extracted CSS. We are using this plugin so that possible 57 | // duplicated CSS from different components can be deduped. 58 | new OptimizeCSSPlugin({ 59 | cssProcessorOptions: config.build.productionSourceMap 60 | ? { safe: true, map: { inline: false } } 61 | : { safe: true } 62 | }), 63 | // generate dist index.html with correct asset hash for caching. 64 | // you can customize output by editing /index.html 65 | // see https://github.com/ampedandwired/html-webpack-plugin 66 | new HtmlWebpackPlugin({ 67 | filename: config.build.index, 68 | template: 'index.html', 69 | inject: true, 70 | minify: false, 71 | // minify: { 72 | // removeComments: true, 73 | // collapseWhitespace: true, 74 | // removeAttributeQuotes: true 75 | // // more options: 76 | // // https://github.com/kangax/html-minifier#options-quick-reference 77 | // }, 78 | // necessary to consistently work with multiple chunks via CommonsChunkPlugin 79 | chunksSortMode: 'dependency' 80 | }), 81 | // keep module.id stable when vendor modules does not change 82 | new webpack.HashedModuleIdsPlugin(), 83 | // enable scope hoisting 84 | new webpack.optimize.ModuleConcatenationPlugin(), 85 | // split vendor js into its own file 86 | new webpack.optimize.CommonsChunkPlugin({ 87 | name: 'vendor', 88 | minChunks (module) { 89 | // any required modules inside node_modules are extracted to vendor 90 | return ( 91 | module.resource && 92 | /\.js$/.test(module.resource) && 93 | module.resource.indexOf( 94 | path.join(__dirname, '../node_modules') 95 | ) === 0 96 | ) 97 | } 98 | }), 99 | // extract webpack runtime and module manifest to its own file in order to 100 | // prevent vendor hash from being updated whenever app bundle is updated 101 | new webpack.optimize.CommonsChunkPlugin({ 102 | name: 'manifest', 103 | minChunks: Infinity 104 | }), 105 | // This instance extracts shared chunks from code splitted chunks and bundles them 106 | // in a separate chunk, similar to the vendor chunk 107 | // see: https://webpack.js.org/plugins/commons-chunk-plugin/#extra-async-commons-chunk 108 | new webpack.optimize.CommonsChunkPlugin({ 109 | name: 'app', 110 | async: 'vendor-async', 111 | children: true, 112 | minChunks: 3 113 | }), 114 | // copy custom static assets 115 | new CopyWebpackPlugin([ 116 | { 117 | from: path.resolve(__dirname, '../static'), 118 | to: config.build.assetsSubDirectory, 119 | ignore: ['.*'] 120 | } 121 | ]) 122 | ] 123 | }) 124 | 125 | if (config.build.productionGzip) { 126 | const CompressionWebpackPlugin = require('compression-webpack-plugin') 127 | 128 | webpackConfig.plugins.push( 129 | new CompressionWebpackPlugin({ 130 | asset: '[path].gz[query]', 131 | algorithm: 'gzip', 132 | test: new RegExp( 133 | '\\.(' + 134 | config.build.productionGzipExtensions.join('|') + 135 | ')$' 136 | ), 137 | threshold: 10240, 138 | minRatio: 0.8 139 | }) 140 | ) 141 | } 142 | 143 | if (config.build.bundleAnalyzerReport) { 144 | const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin 145 | webpackConfig.plugins.push(new BundleAnalyzerPlugin()) 146 | } 147 | 148 | module.exports = webpackConfig 149 | -------------------------------------------------------------------------------- /config/dev.env.js: -------------------------------------------------------------------------------- 1 | var merge = require('webpack-merge') 2 | var prodEnv = require('./prod.env') 3 | 4 | module.exports = merge(prodEnv, { 5 | NODE_ENV: '"development"' 6 | }) 7 | -------------------------------------------------------------------------------- /config/index.js: -------------------------------------------------------------------------------- 1 | // see http://vuejs-templates.github.io/webpack for documentation. 2 | var path = require('path') 3 | 4 | module.exports = { 5 | build: { 6 | env: require('./prod.env'), 7 | index: path.resolve(__dirname, '../dist/index.html'), 8 | assetsRoot: path.resolve(__dirname, '../dist'), 9 | assetsSubDirectory: 'static', 10 | assetsPublicPath: '/', 11 | productionSourceMap: false, 12 | // Gzip off by default as many popular static hosts such as 13 | // Surge or Netlify already gzip all static assets for you. 14 | // Before setting to `true`, make sure to: 15 | // npm install --save-dev compression-webpack-plugin 16 | productionGzip: false, 17 | productionGzipExtensions: ['js', 'css'], 18 | // Run the build command with an extra argument to 19 | // View the bundle analyzer report after build finishes: 20 | // `npm run build --report` 21 | // Set to `true` or `false` to always turn it on or off 22 | bundleAnalyzerReport: process.env.npm_config_report 23 | }, 24 | dev: { 25 | env: require('./dev.env'), 26 | port: 8080, 27 | // 在小程序开发者工具中不需要自动打开浏览器 28 | autoOpenBrowser: false, 29 | assetsSubDirectory: 'static', 30 | assetsPublicPath: '/', 31 | proxyTable: {}, 32 | // CSS Sourcemaps off by default because relative paths are "buggy" 33 | // with this option, according to the CSS-Loader README 34 | // (https://github.com/webpack/css-loader#sourcemaps) 35 | // In our experience, they generally work as expected, 36 | // just be aware of this issue when enabling this option. 37 | cssSourceMap: false 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /config/prod.env.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | NODE_ENV: '"production"' 3 | } 4 | -------------------------------------------------------------------------------- /configH5/dev.env.js: -------------------------------------------------------------------------------- 1 | var merge = require('webpack-merge') 2 | var prodEnv = require('./prod.env') 3 | 4 | module.exports = merge(prodEnv, { 5 | NODE_ENV: '"development"' 6 | }) 7 | -------------------------------------------------------------------------------- /configH5/index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | // Template version: 1.3.1 3 | // see http://vuejs-templates.github.io/webpack for documentation. 4 | 5 | const path = require('path') 6 | 7 | module.exports = { 8 | dev: { 9 | 10 | // Paths 11 | assetsSubDirectory: 'static', 12 | assetsPublicPath: '/', 13 | proxyTable: {}, 14 | 15 | // Various Dev Server settings 16 | host: 'localhost', // can be overwritten by process.env.HOST 17 | port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined 18 | autoOpenBrowser: false, 19 | errorOverlay: true, 20 | notifyOnErrors: true, 21 | poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions- 22 | 23 | // Use Eslint Loader? 24 | // If true, your code will be linted during bundling and 25 | // linting errors and warnings will be shown in the console. 26 | useEslint: true, 27 | // If true, eslint errors and warnings will also be shown in the error overlay 28 | // in the browser. 29 | showEslintErrorsInOverlay: false, 30 | 31 | /** 32 | * Source Maps 33 | */ 34 | 35 | // https://webpack.js.org/configuration/devtool/#development 36 | devtool: 'cheap-module-eval-source-map', 37 | 38 | // If you have problems debugging vue-files in devtools, 39 | // set this to false - it *may* help 40 | // https://vue-loader.vuejs.org/en/options.html#cachebusting 41 | cacheBusting: true, 42 | 43 | cssSourceMap: true 44 | }, 45 | 46 | build: { 47 | // Template for index.html 48 | index: path.resolve(__dirname, '../distH5/index.html'), 49 | 50 | // Paths 51 | assetsRoot: path.resolve(__dirname, '../distH5'), 52 | assetsSubDirectory: 'static', 53 | assetsPublicPath: '/', 54 | 55 | /** 56 | * Source Maps 57 | */ 58 | 59 | productionSourceMap: false, 60 | // https://webpack.js.org/configuration/devtool/#production 61 | devtool: '#source-map', 62 | 63 | // Gzip off by default as many popular static hosts such as 64 | // Surge or Netlify already gzip all static assets for you. 65 | // Before setting to `true`, make sure to: 66 | // npm install --save-dev compression-webpack-plugin 67 | productionGzip: false, 68 | productionGzipExtensions: ['js', 'css'], 69 | 70 | // Run the build command with an extra argument to 71 | // View the bundle analyzer report after build finishes: 72 | // `npm run build --report` 73 | // Set to `true` or `false` to always turn it on or off 74 | bundleAnalyzerReport: process.env.npm_config_report 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /configH5/prod.env.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | NODE_ENV: '"production"' 3 | } 4 | -------------------------------------------------------------------------------- /dist/app.js: -------------------------------------------------------------------------------- 1 | 2 | require('./static/js/manifest') 3 | require('./static/js/vendor') 4 | require('./static/js/app') 5 | -------------------------------------------------------------------------------- /dist/app.json: -------------------------------------------------------------------------------- 1 | { 2 | "pages": [ 3 | "pages/index/main", 4 | "pages/counter/main", 5 | "pages/logs/main" 6 | ], 7 | "window": { 8 | "backgroundTextStyle": "light", 9 | "navigationBarBackgroundColor": "#fff", 10 | "navigationBarTitleText": "mpvue demo", 11 | "navigationBarTextStyle": "black" 12 | } 13 | } -------------------------------------------------------------------------------- /dist/app.wxss: -------------------------------------------------------------------------------- 1 | @import "./static/css/app.wxss"; -------------------------------------------------------------------------------- /dist/components/card$79c1b934.wxml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /dist/components/counter$6c3d87bf.wxml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /dist/components/index$622cddd6.wxml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /dist/components/logs$31942962.wxml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /dist/components/slots.wxml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /dist/copy-asset/assets/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aimee1608/mpvuedemo/1d942ee5fadb7b9435a17caaab8beb75d4522fe0/dist/copy-asset/assets/logo.png -------------------------------------------------------------------------------- /dist/pages/counter/main.js: -------------------------------------------------------------------------------- 1 | 2 | require('../../static/js/manifest') 3 | require('../../static/js/vendor') 4 | require('../../static/js/pages/counter/main') 5 | -------------------------------------------------------------------------------- /dist/pages/counter/main.wxml: -------------------------------------------------------------------------------- 1 |