Skip to content

Commit 9c4c763

Browse files
committed
refactor: use built-in toString from webpack-chain
1 parent a1aa1b9 commit 9c4c763

File tree

3 files changed

+10
-138
lines changed

3 files changed

+10
-138
lines changed

packages/@vue/cli-service/lib/commands/inspect.js

Lines changed: 5 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,3 @@
1-
let patched = false
2-
function patchWebpackChain () {
3-
if (patched) return
4-
5-
const Config = require('webpack-chain')
6-
7-
const toConfig = Config.prototype.toConfig
8-
Config.prototype.toConfig = function () {
9-
const config = toConfig.call(this)
10-
11-
// inject plugin metadata
12-
const { entries: plugins, order: pluginNames } = this.plugins.order()
13-
config.plugins.forEach((p, i) => {
14-
Object.defineProperties(p, {
15-
__pluginName: {
16-
value: pluginNames[i]
17-
},
18-
__pluginArgs: {
19-
value: plugins[pluginNames[i]].get('args')
20-
}
21-
})
22-
})
23-
24-
// inject rule metadata
25-
const { entries: rules, order: ruleNames } = this.module.rules.order()
26-
config.module.rules.forEach((rawRule, i) => {
27-
const ruleName = ruleNames[i]
28-
const rule = rules[ruleName]
29-
Object.defineProperties(rawRule, {
30-
__ruleName: {
31-
value: ruleName
32-
}
33-
})
34-
35-
if (rawRule.oneOf) {
36-
const { entries: oneOfs, order: oneOfNames } = rule.oneOfs.order()
37-
rawRule.oneOf.forEach((o, i) => {
38-
const oneOfName = oneOfNames[i]
39-
const oneOf = oneOfs[oneOfName]
40-
Object.defineProperties(o, {
41-
__ruleName: {
42-
value: ruleName
43-
},
44-
__oneOfName: {
45-
value: oneOfName
46-
}
47-
})
48-
patchUse(oneOf, ruleName, oneOfName, o)
49-
})
50-
}
51-
52-
patchUse(rule, ruleName, null, rawRule)
53-
})
54-
55-
return config
56-
}
57-
58-
patched = true
59-
}
60-
61-
function patchUse (rule, ruleName, oneOfName, rawRule) {
62-
if (Array.isArray(rawRule.use)) {
63-
const { order: useNames } = rule.uses.order()
64-
rawRule.use.forEach((use, i) => {
65-
Object.defineProperties(use, {
66-
__ruleName: {
67-
value: ruleName
68-
},
69-
__oneOfName: {
70-
value: oneOfName
71-
},
72-
__useName: {
73-
value: useNames[i]
74-
}
75-
})
76-
})
77-
}
78-
}
79-
801
module.exports = (api, options) => {
812
api.registerCommand('inspect', {
823
description: 'inspect internal webpack config',
@@ -90,20 +11,18 @@ module.exports = (api, options) => {
9011
'--verbose': 'show full function definitions in output'
9112
}
9213
}, args => {
93-
patchWebpackChain()
94-
9514
const get = require('get-value')
96-
const stringify = require('javascript-stringify')
15+
const { toString } = require('webpack-chain')
9716
const config = api.resolveWebpackConfig()
98-
const paths = args._
17+
const { _: paths, verbose } = args
9918

10019
let res
10120
if (args.rule) {
102-
res = config.module.rules.find(r => r.__ruleName === args.rule)
21+
res = config.module.rules.find(r => r.__ruleNames[0] === args.rule)
10322
} else if (args.plugin) {
10423
res = config.plugins.find(p => p.__pluginName === args.plugin)
10524
} else if (args.rules) {
106-
res = config.module.rules.map(r => r.__ruleName)
25+
res = config.module.rules.map(r => r.__ruleNames[0])
10726
} else if (args.plugins) {
10827
res = config.plugins.map(p => p.__pluginName)
10928
} else if (paths.length > 1) {
@@ -117,54 +36,7 @@ module.exports = (api, options) => {
11736
res = config
11837
}
11938

120-
const pluginRE = /(?:function|class) (\w+Plugin)/
121-
const output = stringify(res, (value, indent, stringify) => {
122-
// shorten long functions
123-
if (
124-
!args.verbose &&
125-
typeof value === 'function' &&
126-
value.toString().length > 100
127-
) {
128-
return `function () { /* omitted long function */ }`
129-
}
130-
131-
// improve plugin output
132-
if (value && value.__pluginName) {
133-
let match = (
134-
value.constructor &&
135-
value.constructor.toString().match(pluginRE)
136-
)
137-
// copy-webpack-plugin
138-
if (value.__pluginName === 'copy') {
139-
match = [null, `CopyWebpackPlugin`]
140-
}
141-
const name = match[1]
142-
const prefix = `/* config.plugin('${value.__pluginName}') */\n`
143-
144-
if (name) {
145-
return prefix + `new ${name}(${
146-
value.__pluginArgs.map(arg => stringify(arg)).join(',\n')
147-
})`
148-
} else {
149-
return prefix + stringify({
150-
args: value.__pluginArgs || []
151-
})
152-
}
153-
}
154-
155-
// improve rule output
156-
if (value && value.__ruleName) {
157-
const prefix = `/* config.module.rule('${value.__ruleName}')${
158-
value.__oneOfName ? `.oneOf('${value.__oneOfName}')` : ``
159-
}${
160-
value.__useName ? `.use('${value.__useName}')` : ``
161-
} */\n`
162-
return prefix + stringify(value)
163-
}
164-
165-
return stringify(value)
166-
}, 2)
167-
39+
const output = toString(config, { verbose })
16840
console.log(output)
16941
})
17042
}

packages/@vue/cli-service/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
"get-value": "^3.0.1",
4040
"globby": "^8.0.1",
4141
"html-webpack-plugin": "^3.2.0",
42-
"javascript-stringify": "^1.6.0",
4342
"launch-editor-middleware": "^2.2.1",
4443
"lodash.defaultsdeep": "^4.6.0",
4544
"mini-css-extract-plugin": "^0.4.0",
@@ -60,7 +59,7 @@
6059
"vue-loader": "^15.0.11",
6160
"vue-template-compiler": "^2.5.16",
6261
"webpack": "^4.8.2",
63-
"webpack-chain": "^4.6.0",
62+
"webpack-chain": "^4.8.0",
6463
"webpack-dev-server": "^3.1.4",
6564
"webpack-merge": "^4.1.2",
6665
"yorkie": "^1.0.3"

yarn.lock

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10610,11 +10610,12 @@ webidl-conversions@^4.0.2:
1061010610
version "4.0.2"
1061110611
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-4.0.2.tgz#a855980b1f0b6b359ba1d5d9fb39ae941faa63ad"
1061210612

10613-
webpack-chain@^4.6.0:
10614-
version "4.6.0"
10615-
resolved "https://registry.yarnpkg.com/webpack-chain/-/webpack-chain-4.6.0.tgz#3ff51bd6241ed78a62691b8da7b9dec3fcc346a0"
10613+
webpack-chain@^4.8.0:
10614+
version "4.8.0"
10615+
resolved "https://registry.yarnpkg.com/webpack-chain/-/webpack-chain-4.8.0.tgz#06fc3dbb9f2707d4c9e899fc6250fbcf2afe6fd1"
1061610616
dependencies:
1061710617
deepmerge "^1.5.2"
10618+
javascript-stringify "^1.6.0"
1061810619

1061910620
webpack-dev-middleware@3.1.3:
1062010621
version "3.1.3"

0 commit comments

Comments
 (0)