Skip to content

Commit 9e3f799

Browse files
authored
Merge pull request #4 from mpvue/develop
Develop
2 parents 2fe82c7 + dbf7865 commit 9e3f799

File tree

5 files changed

+112
-69
lines changed

5 files changed

+112
-69
lines changed

bin/mpvue-simple

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ upgradeLogger()
55

66
var argv = require('yargs').argv;
77

8+
if (argv.version || argv.v) {
9+
const { version } = require('../package.json')
10+
return process.stdout.write(version)
11+
}
12+
813
if (argv.build) {
914
require('../build/build.js')
1015
} else {

build/dev-server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var proxyMiddleware = require('http-proxy-middleware')
1313
var webpackConfig = require('./webpack.dev.conf')
1414

1515
// default port where dev server listens for incoming traffic
16-
var port = process.env.PORT || config.dev.port
16+
var port = config.dev.port || 8080
1717
// automatically open browser, if not set will be false
1818
var autoOpenBrowser = !!config.dev.autoOpenBrowser
1919
// Define HTTP proxies to your custom API backend

config/argv.js

Lines changed: 89 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
var path = require('path')
22
var fs = require('fs')
3+
var util = require('util')
34
var resolve = require('resolve')
45
var glob = require('glob')
6+
var webpack = require('webpack')
57
// const { NpmAutoInstallWebpackPlugin } = require('npm-auto-install-webpack-plugin')
68

79
function saveEntryMain(index, src, mpType) {
@@ -24,97 +26,135 @@ export default {
2426
return fileSrc
2527
}
2628

27-
// 如果有 main.js 就不找 app.vue 了,免得混乱
28-
function searchEntry(dir, config, entryName, mpType) {
29-
let entryFiles = []
30-
searchFilters.some(filter => {
31-
entryFiles = glob.sync(filter, { cwd: dir })
32-
return entryFiles.length
33-
})
29+
function resolveEntry (src, cwd) {
30+
try {
31+
return resolve.sync(src, { basedir: cwd })
32+
} catch (err) {
33+
// console.log(err)
34+
}
35+
return ''
36+
}
3437

35-
if (!entryFiles || !entryFiles.length) {
36-
return
38+
// 如果有 main.js 就不找 app.vue 了,免得混乱
39+
function searchEntry(pattern, entryName, cwd, config) {
40+
const entryFiles = glob.sync(pattern, { cwd })
41+
if (!entryFiles.length) {
42+
return false
3743
}
3844

3945
if (entryFiles.length === 1) {
40-
config.entry[entryName || path.basename(dir)] = path.resolve(dir, entryFiles[0])
41-
return
46+
const realSrc = path.resolve(cwd, entryFiles[0])
47+
config.entry[getEntryName(realSrc, entryName)] = realSrc
48+
return true
4249
}
4350

4451
entryFiles.forEach((k, i) => {
45-
const src = path.resolve(dir, k)
46-
const entryPath = path.extname(src) === '.vue' ? saveEntryMain(0, src, mpType) : src
47-
config.entry[path.basename(dir)] = entryPath
52+
const src = path.resolve(cwd, k)
53+
config.entry[getEntryName(src)] = src
4854
})
55+
56+
return true
4957
}
5058

51-
let argvOptions = null
52-
let searchPath = ['./', './src', './src/pages', './mpvue-pages']
53-
let searchFilters = ['**/**/main.js', '**/**/app.vue', '**/**/App.vue', '**/**/index.vue']
59+
function getEntryName (src, pageName) {
60+
if (pageName) {
61+
return pageName
62+
}
63+
const { dir, name } = path.parse(src)
64+
if (['app', 'App', 'main', 'index'].includes(name)) {
65+
return path.parse(dir).name
66+
}
67+
return name
68+
}
5469

70+
// 暂时只想到这么做缓存
71+
let argvOptions = null
5572
function injectArgvOptions (options) {
5673
argvOptions = options
5774
}
5875

76+
// 默认优先为完整路径的单文件
77+
// 其次是 glob 格式扫描到的文件
78+
// 其次是 node.js 本地运行时传递的 object
79+
// 再次是一个路径下,默认扫描
80+
// 最后才是默认路径,默认扫描
81+
// component 针对所有扫描到的 .vue SFC entry 才有用
82+
// 有且只有一个 entry 的时候 pageName 才有效
5983
function mergeArgvConfig (config) {
6084
// 默认初始化
85+
const cwd = path.resolve()
86+
const defConfig = { entry: {}, resolve: { alias: {} } }
87+
const floders = ['./src/', './mpvue-pages/']
88+
const searchFilters = ['./**/**/main.js', './**/**/app.vue', './**/**/App.vue', './**/**/index.vue']
6189
config.argvConfig = {
62-
// plugins: [
63-
// new NpmAutoInstallWebpackPlugin()
64-
// ]
90+
plugins: [
91+
// new NpmAutoInstallWebpackPlugin()
92+
]
6593
}
6694

6795
// 拼接自定义的参数
6896
var argv = Object.assign({}, argvOptions, require('yargs').argv)
6997
const {
70-
entry: argvEntry, // ./src/main.js, ./src/app.vue, undefined
71-
output: argvOutput, // pathString, undefined
98+
entry: argvEntry, // ./src/main.js, ./src/app.vue, ./src/, ./src/**/main.js, { page: './page/main.js' }, undefined
7299
pageName: argvPageName, // nameStrng, undefined
73100
component: argvComponent, // true, undefined
74-
searchPath: argvSearchPath, // pathString, undefined
101+
output: argvOutput, // pathString, undefined
75102
config: argvCnf, // pathString, undefined
76-
searchFilters: argvSearchFilters,
103+
definePlugin: argvDefinePlugin, // object, undefined
77104
...resetConfig
78105
} = argv
79-
const defConfig = { entry: {}, resolve: { alias: {} } }
80106
const mpType = argvComponent ? 'component' : 'page'
81107

82-
if (typeof argvPageName === 'string') {
83-
searchPath = searchPath.concat(argvPageName.split(','))
84-
}
85-
86-
if (typeof argvSearchFilters === 'string') {
87-
searchFilters = argvSearchFilters,split(',')
88-
}
89-
90-
// 为什么这里面的逻辑这么凌乱呢?
91-
// 可能是因为还没思考清楚吧
108+
// 开始获取 entry
92109
if (typeof argvEntry === 'string') {
93-
let entryPath = resolve.sync(argvEntry, { basedir: path.resolve(), extensions: ['.js', '.vue'] })
94-
95-
if (path.extname(entryPath) === '.vue') {
96-
entryPath = saveEntryMain(0, entryPath, mpType)
97-
}
98-
99-
if (typeof argvPageName === 'string') {
100-
const entryName = argvPageName || path.parse(entryPath).name
101-
defConfig.entry[entryName] = entryPath
102-
} else if (fs.existsSync(entryPath)) {
103-
defConfig.entry[path.parse(entryPath).name] = entryPath
110+
// 首先判断是不是完整路径
111+
const curEntry = resolveEntry(argvEntry, cwd)
112+
// const absEntry = path.resolve(argvEntry)
113+
if (curEntry) {
114+
defConfig.entry[getEntryName(curEntry, argvPageName)] = curEntry
104115
} else {
105-
searchEntry(entryPath, defConfig, argvPageName, mpType)
116+
// 先检查是否是 glob 格式的 filter
117+
// 再检查这是不是路径
118+
if (!searchEntry(argvEntry, argvPageName, cwd, defConfig)) {
119+
floders.unpop()
120+
}
106121
}
122+
} else if (util.isObject(argvEntry)) {
123+
defConfig.entry = argvEntry
107124
} else {
108-
searchPath.forEach(s => {
109-
searchEntry(path.resolve(s), defConfig, argvPageName, mpType)
125+
const allPattern = floders.reduce((r, floder) => {
126+
return r.concat(searchFilters.map(v => path.join(floder, v)))
127+
}, [])
128+
allPattern.some(pattern => {
129+
return searchEntry(pattern, argvPageName, cwd, defConfig)
110130
})
111131
}
112132

133+
// 输出文件夹
113134
if (argvOutput) {
114135
const assetsRoot = path.resolve(argvOutput)
115136
defConfig.assetsRoot = assetsRoot
116137
}
117138

139+
const allEntry = Object.keys(defConfig.entry)
140+
141+
if (!allEntry.length) {
142+
throw Error('At least one entry is needed: http://mpvue.com/mpvue/simple/')
143+
}
144+
145+
// 处理 component 和 .vue 的 entry
146+
allEntry.forEach((v, i) => {
147+
if (path.extname(v) === '.vue') {
148+
defConfig.entry[v] = saveEntryMain(i, v, mpType)
149+
}
150+
})
151+
152+
// DefinePlugin
153+
if (argvDefinePlugin) {
154+
config.argvConfig.plugins.push(new webpack.DefinePlugin(argvDefinePlugin))
155+
}
156+
157+
// 合并自定义 webpack config
118158
if (typeof argvCnf === 'string') {
119159
try {
120160
config.argvConfig = require('webpack-merge')(config.argvConfig, require(path.resolve(argvCnf)))
@@ -124,12 +164,6 @@ function mergeArgvConfig (config) {
124164
Object.assign(config.build, resetConfig, defConfig)
125165
Object.assign(config.dev, resetConfig, defConfig)
126166

127-
console.log(defConfig)
128-
129-
if (!Object.keys(defConfig.entry).length) {
130-
throw Error('At least one entry is needed: http://mpvue.com/mpvue/simple/')
131-
}
132-
133167
return config
134168
}
135169

package.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mpvue-simple",
3-
"version": "1.0.4",
3+
"version": "1.0.6",
44
"description": "辅助 mpvue 快速开发 Page / Component 级小程序页面的工具",
55
"main": "index.js",
66
"bin": "bin/mpvue-simple",
@@ -19,7 +19,6 @@
1919
"px2rpx-loader": "^0.1.8",
2020
"npm-auto-install-webpack-plugin": "^0.2.0",
2121
"yargs": "^11.0.0",
22-
"autoprefixer": "^7.1.2",
2322
"babel-core": "^6.22.1",
2423
"babel-loader": "^7.1.1",
2524
"babel-plugin-transform-runtime": "^6.22.0",
@@ -38,7 +37,6 @@
3837
"friendly-errors-webpack-plugin": "^1.1.3",
3938
"html-webpack-plugin": "^2.28.0",
4039
"http-proxy-middleware": "^0.17.3",
41-
"opn": "^5.1.0",
4240
"optimize-css-assets-webpack-plugin": "^2.0.0",
4341
"ora": "^1.2.0",
4442
"postcss-loader": "^2.0.6",
@@ -49,9 +47,7 @@
4947
"vue-style-loader": "^3.0.1",
5048
"webpack": "^2.6.1",
5149
"webpack-bundle-analyzer": "^2.2.1",
52-
"webpack-dev-middleware": "^1.10.0",
5350
"webpack-dev-middleware-hard-disk": "^1.10.0",
54-
"webpack-hot-middleware": "^2.18.0",
5551
"webpack-merge": "^4.1.0"
5652
},
5753
"engines": {

utils/index.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
const path = require('path')
2-
const childProcess = require('child_process')
3-
const { execSync } = childProcess
4-
2+
const http = require('http');
53
// upgrade logger
64
function upgradeLogger () {
75
const currPkg = require('../package.json')
86
const cwd = path.resolve(__dirname, '../')
97
const pkgName = currPkg.name || 'mpvue-simple'
10-
const pkg = JSON.parse(execSync(`curl \`npm config get registry\`${pkgName} -s`, { cwd }))
11-
const latestVesion = pkg['dist-tags'].latest
128

13-
if (latestVesion !== currPkg.version) {
14-
console.log(`${pkgName} 有新版本 ${latestVesion} 啦,请注意升级。\n例如: npm install ${pkgName} -g\n`)
15-
}
9+
http.get('http://registry.npmjs.org/' + currPkg.name, (res) => {
10+
let rawData = '';
11+
res.on('data', (chunk) => rawData += chunk);
12+
res.on('end', () => {
13+
try {
14+
const parsedData = JSON.parse(rawData);
15+
let latestVesion = parsedData['dist-tags'].latest;
16+
if (latestVesion !== currPkg.version) {
17+
console.log(`${pkgName} 有新版本 ${latestVesion} 啦,请注意升级。\n例如: npm install ${pkgName} -g\n`)
18+
}
19+
} catch (e) {
20+
console.log(e.message);
21+
}
22+
});
23+
})
1624
}
1725

1826
exports.upgradeLogger = upgradeLogger

0 commit comments

Comments
 (0)