Skip to content

Commit fb5a597

Browse files
committed
enhance: 优化 entry 识别 scan 代码
1 parent 9e56b74 commit fb5a597

File tree

4 files changed

+82
-50
lines changed

4 files changed

+82
-50
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 {

config/argv.js

Lines changed: 75 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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')
56
// const { NpmAutoInstallWebpackPlugin } = require('npm-auto-install-webpack-plugin')
@@ -24,40 +25,66 @@ export default {
2425
return fileSrc
2526
}
2627

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-
})
28+
function resolveEntry (src, cwd) {
29+
try {
30+
return resolve.sync(src, { basedir: cwd })
31+
} catch (err) {
32+
// console.log(err)
33+
}
34+
return ''
35+
}
3436

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

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

4450
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
51+
const src = path.resolve(cwd, k)
52+
config.entry[getEntryName(src)] = src
4853
})
54+
55+
return true
4956
}
5057

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

69+
// 暂时只想到这么做缓存
70+
let argvOptions = null
5571
function injectArgvOptions (options) {
5672
argvOptions = options
5773
}
5874

75+
// 默认优先为完整路径的单文件
76+
// 其次是 glob 格式扫描到的文件
77+
// 其次是 node.js 本地运行时传递的 object
78+
// 再次是一个路径下,默认扫描
79+
// 最后才是默认路径,默认扫描
80+
// component 针对所有扫描到的 .vue SFC entry 才有用
81+
// 有且只有一个 entry 的时候 pageName 才有效
5982
function mergeArgvConfig (config) {
6083
// 默认初始化
84+
const cwd = path.resolve()
85+
const defConfig = { entry: {}, resolve: { alias: {} } }
86+
const floders = ['./src/', './mpvue-pages/']
87+
const searchFilters = ['./**/**/main.js', './**/**/app.vue', './**/**/App.vue', './**/**/index.vue']
6188
config.argvConfig = {
6289
// plugins: [
6390
// new NpmAutoInstallWebpackPlugin()
@@ -67,54 +94,47 @@ function mergeArgvConfig (config) {
6794
// 拼接自定义的参数
6895
var argv = Object.assign({}, argvOptions, require('yargs').argv)
6996
const {
70-
entry: argvEntry, // ./src/main.js, ./src/app.vue, undefined
71-
output: argvOutput, // pathString, undefined
97+
entry: argvEntry, // ./src/main.js, ./src/app.vue, ./src/, ./src/**/main.js, { page: './page/main.js' }, undefined
7298
pageName: argvPageName, // nameStrng, undefined
7399
component: argvComponent, // true, undefined
74-
searchPath: argvSearchPath, // pathString, undefined
100+
output: argvOutput, // pathString, undefined
75101
config: argvCnf, // pathString, undefined
76-
searchFilters: argvSearchFilters,
77102
...resetConfig
78103
} = argv
79-
const defConfig = { entry: {}, resolve: { alias: {} } }
80104
const mpType = argvComponent ? 'component' : 'page'
81105

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-
// 可能是因为还没思考清楚吧
106+
// 开始获取 entry
92107
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
108+
// 首先判断是不是完整路径
109+
const curEntry = resolveEntry(argvEntry, cwd)
110+
// const absEntry = path.resolve(argvEntry)
111+
if (curEntry) {
112+
defConfig.entry[getEntryName(curEntry, argvPageName)] = curEntry
104113
} else {
105-
searchEntry(entryPath, defConfig, argvPageName, mpType)
114+
// 先检查是否是 glob 格式的 filter
115+
// 再检查这是不是路径
116+
if (!searchEntry(argvEntry, argvPageName, cwd, defConfig)) {
117+
floders.unpop()
118+
}
106119
}
120+
} else if (util.isObject(argvEntry)) {
121+
defConfig.entry = argvEntry
107122
} else {
108-
searchPath.forEach(s => {
109-
searchEntry(path.resolve(s), defConfig, argvPageName, mpType)
123+
const allPattern = floders.reduce((r, floder) => {
124+
return r.concat(searchFilters.map(v => path.join(floder, v)))
125+
}, [])
126+
allPattern.some(pattern => {
127+
return searchEntry(pattern, argvPageName, cwd, defConfig)
110128
})
111129
}
112130

131+
// 输出文件夹
113132
if (argvOutput) {
114133
const assetsRoot = path.resolve(argvOutput)
115134
defConfig.assetsRoot = assetsRoot
116135
}
117136

137+
// 合并自定义 webpack config
118138
if (typeof argvCnf === 'string') {
119139
try {
120140
config.argvConfig = require('webpack-merge')(config.argvConfig, require(path.resolve(argvCnf)))
@@ -124,12 +144,19 @@ function mergeArgvConfig (config) {
124144
Object.assign(config.build, resetConfig, defConfig)
125145
Object.assign(config.dev, resetConfig, defConfig)
126146

127-
console.log(defConfig)
147+
const allEntry = Object.keys(defConfig.entry)
128148

129-
if (!Object.keys(defConfig.entry).length) {
149+
if (!allEntry.length) {
130150
throw Error('At least one entry is needed: http://mpvue.com/mpvue/simple/')
131151
}
132152

153+
// 处理 component 和 .vue 的 entry
154+
allEntry.forEach((v, i) => {
155+
if (path.extname(v) === '.vue') {
156+
defConfig.entry[v] = saveEntryMain(i, v, mpType)
157+
}
158+
})
159+
133160
return config
134161
}
135162

package.json

Lines changed: 1 addition & 1 deletion
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.5",
44
"description": "辅助 mpvue 快速开发 Page / Component 级小程序页面的工具",
55
"main": "index.js",
66
"bin": "bin/mpvue-simple",

utils/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function upgradeLogger () {
1111
const latestVesion = pkg['dist-tags'].latest
1212

1313
if (latestVesion !== currPkg.version) {
14-
console.log(`${pkgName} 有新版本 ${latestVesion} 啦,请注意升级。\n例如: npm install ${pkgName} -g\n`)
14+
process.stdout.write(`${pkgName} 有新版本 ${latestVesion} 啦,请注意升级。\n例如: npm install ${pkgName} -g\n`)
1515
}
1616
}
1717

0 commit comments

Comments
 (0)