Skip to content

Commit

Permalink
ci(cli): 改造脚手架提供,packages内可以直接注册webpack配置跟脚手架命令
Browse files Browse the repository at this point in the history
  • Loading branch information
luoxue committed Dec 19, 2019
1 parent 0828922 commit f340e6f
Show file tree
Hide file tree
Showing 121 changed files with 1,317 additions and 10,361 deletions.
24 changes: 24 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach",
"port": 9229,
"skipFiles": ["<node_internals>/**"]
},
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceFolder}/index.js",
"preLaunchTask": "tsc: build - tsconfig.json",
"outFiles": ["${workspaceFolder}/dist/**/*.js"]
}
]
}
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"editor.codeActionsOnSave": {
"source.fixAll.eslint": false
"source.fixAll.eslint": true
}
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
范围|描述|commitId
--|--|--
dist | 删除dist | [9600d82](https://github.com/luoxue-victor/learn_webpack/commit/9600d82)
packages | 新加cli跟utils包 | [0828922](https://github.com/luoxue-victor/learn_webpack/commit/0828922)


### 🐛 Bug 修复
Expand Down
19 changes: 15 additions & 4 deletions api/CommandAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,27 @@ const packageConfig = require('../package.json')
const { cleanArgs } = require('../lib')
const boxPath = path.join(process.cwd(), 'box.config.js')
const chalk = require('chalk')
const semver = require('semver')

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

checkNodeVersionForWarning()

program
.usage('<command> [options]')
.version(packageConfig.version)

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

module.exports.commandComplete = function() {
commandValidate()
// commandValidate()
parse()
status = 'done'
}
Expand All @@ -35,7 +37,7 @@ function parse() {
}

function commandValidate() {
if (process.argv[2] && !commandName.includes(process.argv[2])) {
if (process.argv[2]) {
console.log()
console.log(chalk.red(` 没有找到 ${process.argv[2]} 命令`))
console.log()
Expand All @@ -46,3 +48,12 @@ function commandValidate() {
program.help()
}
}

function checkNodeVersionForWarning () {
if (semver.satisfies(process.version, '10.x')) {
console.log(chalk.red(
`你正在用的 node 版本是:${process.version}.\n` +
'未来版本将不再支持 10.x 版本.\n'
))
}
}
14 changes: 14 additions & 0 deletions api/CwdAPI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports.getAllCwds = function() {
const path = require('path')
const fs = require('fs')
const localCwdPath = path.join(__dirname, '..', 'cwd')
const localCwdNames = [...fs.readdirSync(localCwdPath)]
const cwdFns = []
const { getConfigsByName } = require('../util/getLocalConfigByPath')
localCwdNames.forEach(name => {
const cwdPath = path.join(localCwdPath, name)
cwdFns.push(require(cwdPath))
})
cwdFns.push(...getConfigsByName('packages', 'cwd.config.js'))
return cwdFns
}
13 changes: 5 additions & 8 deletions bin/webpack-box.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
#!/usr/bin/env node
process.env.NODE_ENV = 'none'
const commandsName = ['build', 'dev', 'dll', 'build:ssr', 'ssr:server', 'lint', 'upgrade', 'inspect', 'server:gql']
const { getAllCwds } = require('../api/CwdAPI')

const {
injectCommand,
commandComplete,
commandName
commandComplete
} = require('../api/CommandAPI')
const PluginAPI = require('../api/PluginAPI')

commandName.push(...commandsName)
// 注册命令行
commandsName.forEach(name => {
const cwd = require(`../cwd/${name}`)
cwd({ cliName: name, injectCommand, api: PluginAPI })
getAllCwds().forEach(cwd => {
cwd({ injectCommand, api: PluginAPI })
})
// 命令行注册完成
commandComplete()
24 changes: 14 additions & 10 deletions build/base.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const { findSync } = require('../lib')
const fs = require('fs')
const Config = require('webpack-chain')
const config = new Config()
const files = findSync('../config')
const path = require('path')
const PluginAPI = require('../api/PluginAPI')
const resolve = (p) => {
Expand All @@ -11,13 +10,18 @@ const resolve = (p) => {
const webpackVersion = require(resolve('node_modules/webpack/package.json')).version

module.exports = (options) => {
const map = new Map()
files.map(_ => {
const name = path.basename(_, '.js')
return map.set(name, require(_)({ config, webpackVersion, resolve, options, api: PluginAPI }))
})

map.forEach(v => v())

const configPath = path.join(__dirname, '..', 'config')
const files = fs.readdirSync(configPath)
const { getConfigsByName } = require('../util/getLocalConfigByPath')
const configs = []
files.forEach(fileName => configs.push(require(`${configPath}/${fileName}`)))
configs.push(...getConfigsByName('packages', 'chain.config.js'))
configs.forEach(c => c({
config,
webpackVersion,
resolve,
options,
api: PluginAPI
})())
return config
}
96 changes: 93 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"rimraf": "^3.0.0",
"sass": "^1.23.7",
"sass-loader": "^8.0.0",
"semver": "^7.1.1",
"style-loader": "^1.0.0",
"style-resources-loader": "^1.3.2",
"stylelint": "^12.0.0",
Expand Down
Loading

0 comments on commit f340e6f

Please sign in to comment.