Skip to content

Commit 9e56b74

Browse files
committed
enhance: entry 处理为可以批量监听多个
1 parent 02e52f5 commit 9e56b74

File tree

2 files changed

+47
-21
lines changed

2 files changed

+47
-21
lines changed

config/argv.js

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
var path = require('path')
22
var fs = require('fs')
3+
var resolve = require('resolve')
4+
var glob = require('glob')
35
// const { NpmAutoInstallWebpackPlugin } = require('npm-auto-install-webpack-plugin')
46

57
function saveEntryMain(index, src, mpType) {
@@ -22,28 +24,34 @@ export default {
2224
return fileSrc
2325
}
2426

27+
// 如果有 main.js 就不找 app.vue 了,免得混乱
2528
function searchEntry(dir, config, entryName, mpType) {
26-
if (!fs.existsSync(dir)) {
29+
let entryFiles = []
30+
searchFilters.some(filter => {
31+
entryFiles = glob.sync(filter, { cwd: dir })
32+
return entryFiles.length
33+
})
34+
35+
if (!entryFiles || !entryFiles.length) {
2736
return
2837
}
2938

30-
const files = fs.readdirSync(dir)
31-
if (files.includes('main.js')) {
32-
config.entry[entryName || path.basename(dir)] = path.resolve(dir, 'main.js')
39+
if (entryFiles.length === 1) {
40+
config.entry[entryName || path.basename(dir)] = path.resolve(dir, entryFiles[0])
3341
return
3442
}
3543

36-
files.forEach((k, i) => {
37-
const page = path.resolve(dir, k)
38-
if (fs.existsSync(page) && searchFileFilter.some(f => f.test(page))) {
39-
config.entry[entryName || path.parse(page).name] = saveEntryMain(i, page, mpType)
40-
}
44+
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
4148
})
4249
}
4350

4451
let argvOptions = null
4552
let searchPath = ['./', './src', './src/pages', './mpvue-pages']
46-
let searchFileFilter = [/.*?\.vue$/, /main\.js/]
53+
let searchFilters = ['**/**/main.js', '**/**/app.vue', '**/**/App.vue', '**/**/index.vue']
54+
4755
function injectArgvOptions (options) {
4856
argvOptions = options
4957
}
@@ -59,12 +67,13 @@ function mergeArgvConfig (config) {
5967
// 拼接自定义的参数
6068
var argv = Object.assign({}, argvOptions, require('yargs').argv)
6169
const {
62-
entry: argvEntry,
63-
output: argvOutput,
64-
config: argvCnf,
65-
pageName: argvPageName,
66-
searchPath: argvSearchPath,
67-
component: argvComponent,
70+
entry: argvEntry, // ./src/main.js, ./src/app.vue, undefined
71+
output: argvOutput, // pathString, undefined
72+
pageName: argvPageName, // nameStrng, undefined
73+
component: argvComponent, // true, undefined
74+
searchPath: argvSearchPath, // pathString, undefined
75+
config: argvCnf, // pathString, undefined
76+
searchFilters: argvSearchFilters,
6877
...resetConfig
6978
} = argv
7079
const defConfig = { entry: {}, resolve: { alias: {} } }
@@ -74,8 +83,19 @@ function mergeArgvConfig (config) {
7483
searchPath = searchPath.concat(argvPageName.split(','))
7584
}
7685

86+
if (typeof argvSearchFilters === 'string') {
87+
searchFilters = argvSearchFilters,split(',')
88+
}
89+
90+
// 为什么这里面的逻辑这么凌乱呢?
91+
// 可能是因为还没思考清楚吧
7792
if (typeof argvEntry === 'string') {
78-
const entryPath = path.resolve(argvEntry)
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+
7999
if (typeof argvPageName === 'string') {
80100
const entryName = argvPageName || path.parse(entryPath).name
81101
defConfig.entry[entryName] = entryPath
@@ -104,6 +124,12 @@ function mergeArgvConfig (config) {
104124
Object.assign(config.build, resetConfig, defConfig)
105125
Object.assign(config.dev, resetConfig, defConfig)
106126

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+
107133
return config
108134
}
109135

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mpvue-simple",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "辅助 mpvue 快速开发 Page / Component 级小程序页面的工具",
55
"main": "index.js",
66
"bin": "bin/mpvue-simple",
@@ -11,9 +11,9 @@
1111
"build": "node build/build.js"
1212
},
1313
"dependencies": {
14-
"mpvue": "^1.0.3",
15-
"mpvue-loader": "^1.0.4",
16-
"mpvue-template-compiler": "^1.0.3",
14+
"mpvue": "^1.0.5",
15+
"mpvue-loader": "^1.0.6",
16+
"mpvue-template-compiler": "^1.0.5",
1717
"mpvue-webpack-target": "^1.0.0",
1818
"postcss-mpvue-wxss": "^1.0.0",
1919
"px2rpx-loader": "^0.1.8",

0 commit comments

Comments
 (0)