Skip to content

Commit f340e6f

Browse files
author
luoxue
committed
ci(cli): 改造脚手架提供,packages内可以直接注册webpack配置跟脚手架命令
1 parent 0828922 commit f340e6f

File tree

121 files changed

+1317
-10361
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

121 files changed

+1317
-10361
lines changed

.vscode/launch.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "attach",
10+
"name": "Attach",
11+
"port": 9229,
12+
"skipFiles": ["<node_internals>/**"]
13+
},
14+
{
15+
"type": "node",
16+
"request": "launch",
17+
"name": "Launch Program",
18+
"skipFiles": ["<node_internals>/**"],
19+
"program": "${workspaceFolder}/index.js",
20+
"preLaunchTask": "tsc: build - tsconfig.json",
21+
"outFiles": ["${workspaceFolder}/dist/**/*.js"]
22+
}
23+
]
24+
}

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,6 @@
7070
"editor.defaultFormatter": "esbenp.prettier-vscode"
7171
},
7272
"editor.codeActionsOnSave": {
73-
"source.fixAll.eslint": false
73+
"source.fixAll.eslint": true
7474
}
7575
}

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
范围|描述|commitId
55
--|--|--
66
dist | 删除dist | [9600d82](https://github.com/luoxue-victor/learn_webpack/commit/9600d82)
7+
packages | 新加cli跟utils包 | [0828922](https://github.com/luoxue-victor/learn_webpack/commit/0828922)
78

89

910
### 🐛 Bug 修复

api/CommandAPI.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,27 @@ const packageConfig = require('../package.json')
66
const { cleanArgs } = require('../lib')
77
const boxPath = path.join(process.cwd(), 'box.config.js')
88
const chalk = require('chalk')
9+
const semver = require('semver')
910

10-
const commandName = exports.commandName = []
1111
const commandStore = exports.commandStore = []
1212
let boxConfig = {}
1313
if (fs.existsSync(boxPath)) boxConfig = require(path.join(process.cwd(), 'box.config.js'))()
1414
let status = 'pending'
1515

16+
checkNodeVersionForWarning()
17+
1618
program
1719
.usage('<command> [options]')
1820
.version(packageConfig.version)
1921

2022
module.exports.injectCommand = function (cmd) {
2123
if (status === 'done') return console.error('注册命令行时机已经是 done,请提前注册~')
2224
if (typeof cmd !== 'function') return console.error(cmd, '必须是一个函数')
23-
cmd({ program, boxConfig, commandName, commandStore, cleanArgs })
25+
cmd({ program, boxConfig, commandStore, cleanArgs })
2426
}
2527

2628
module.exports.commandComplete = function() {
27-
commandValidate()
29+
// commandValidate()
2830
parse()
2931
status = 'done'
3032
}
@@ -35,7 +37,7 @@ function parse() {
3537
}
3638

3739
function commandValidate() {
38-
if (process.argv[2] && !commandName.includes(process.argv[2])) {
40+
if (process.argv[2]) {
3941
console.log()
4042
console.log(chalk.red(` 没有找到 ${process.argv[2]} 命令`))
4143
console.log()
@@ -46,3 +48,12 @@ function commandValidate() {
4648
program.help()
4749
}
4850
}
51+
52+
function checkNodeVersionForWarning () {
53+
if (semver.satisfies(process.version, '10.x')) {
54+
console.log(chalk.red(
55+
`你正在用的 node 版本是:${process.version}.\n` +
56+
'未来版本将不再支持 10.x 版本.\n'
57+
))
58+
}
59+
}

api/CwdAPI.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports.getAllCwds = function() {
2+
const path = require('path')
3+
const fs = require('fs')
4+
const localCwdPath = path.join(__dirname, '..', 'cwd')
5+
const localCwdNames = [...fs.readdirSync(localCwdPath)]
6+
const cwdFns = []
7+
const { getConfigsByName } = require('../util/getLocalConfigByPath')
8+
localCwdNames.forEach(name => {
9+
const cwdPath = path.join(localCwdPath, name)
10+
cwdFns.push(require(cwdPath))
11+
})
12+
cwdFns.push(...getConfigsByName('packages', 'cwd.config.js'))
13+
return cwdFns
14+
}

bin/webpack-box.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
#!/usr/bin/env node
2-
process.env.NODE_ENV = 'none'
3-
const commandsName = ['build', 'dev', 'dll', 'build:ssr', 'ssr:server', 'lint', 'upgrade', 'inspect', 'server:gql']
2+
const { getAllCwds } = require('../api/CwdAPI')
3+
44
const {
55
injectCommand,
6-
commandComplete,
7-
commandName
6+
commandComplete
87
} = require('../api/CommandAPI')
98
const PluginAPI = require('../api/PluginAPI')
109

11-
commandName.push(...commandsName)
1210
// 注册命令行
13-
commandsName.forEach(name => {
14-
const cwd = require(`../cwd/${name}`)
15-
cwd({ cliName: name, injectCommand, api: PluginAPI })
11+
getAllCwds().forEach(cwd => {
12+
cwd({ injectCommand, api: PluginAPI })
1613
})
1714
// 命令行注册完成
1815
commandComplete()

build/base.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
const { findSync } = require('../lib')
1+
const fs = require('fs')
22
const Config = require('webpack-chain')
33
const config = new Config()
4-
const files = findSync('../config')
54
const path = require('path')
65
const PluginAPI = require('../api/PluginAPI')
76
const resolve = (p) => {
@@ -11,13 +10,18 @@ const resolve = (p) => {
1110
const webpackVersion = require(resolve('node_modules/webpack/package.json')).version
1211

1312
module.exports = (options) => {
14-
const map = new Map()
15-
files.map(_ => {
16-
const name = path.basename(_, '.js')
17-
return map.set(name, require(_)({ config, webpackVersion, resolve, options, api: PluginAPI }))
18-
})
19-
20-
map.forEach(v => v())
21-
13+
const configPath = path.join(__dirname, '..', 'config')
14+
const files = fs.readdirSync(configPath)
15+
const { getConfigsByName } = require('../util/getLocalConfigByPath')
16+
const configs = []
17+
files.forEach(fileName => configs.push(require(`${configPath}/${fileName}`)))
18+
configs.push(...getConfigsByName('packages', 'chain.config.js'))
19+
configs.forEach(c => c({
20+
config,
21+
webpackVersion,
22+
resolve,
23+
options,
24+
api: PluginAPI
25+
})())
2226
return config
2327
}

package-lock.json

Lines changed: 93 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
"rimraf": "^3.0.0",
109109
"sass": "^1.23.7",
110110
"sass-loader": "^8.0.0",
111+
"semver": "^7.1.1",
111112
"style-loader": "^1.0.0",
112113
"style-resources-loader": "^1.3.2",
113114
"stylelint": "^12.0.0",

0 commit comments

Comments
 (0)