forked from vue-styleguidist/vue-styleguidist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
127 lines (111 loc) · 3.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const vsg = require('vue-styleguidist')
const merge = require('webpack-merge')
const configSchemaImport = require('vue-styleguidist/lib/scripts/schemas/config')
const configSchema = configSchemaImport.default || configSchemaImport
const styleguidist = vsg.default || vsg
module.exports = (api, options) => {
api.chainWebpack(webpackConfig => {
// make sure that the docs blocks
// are ignored during normal serve & build
webpackConfig.module
.rule('docs')
.resourceQuery(/blockType=docs/)
.use('docs-ignore-loader')
.loader(require.resolve('./empty-object-loader'))
})
api.registerCommand(
'styleguidist:build',
{
description: 'build the styleguidist website',
usage: 'vue-cli-service styleguidist:build [options]',
options: {
'--config': 'path to the config file',
'--verbose': 'show the full log'
}
},
args => {
getStyleguidist(args, api, options).binutils.build()
}
)
api.registerCommand(
'styleguidist',
{
description: 'launch the styleguidist dev server',
usage: 'vue-cli-service styleguidist [options]',
options: {
'--config': 'path to the config file',
'--verbose': 'show the full log',
'--port': 'port to start the server on'
}
},
args => {
const server = getStyleguidist(args, api, options).binutils.server(args.open).app
// in order to avoid ghosted threads at the end of tests
;['SIGINT', 'SIGTERM'].forEach(signal => {
process.on(signal, () => {
server.close(() => {
process.exit(0)
})
})
})
// in tests, killing the process with SIGTERM causes execa to
// throw
if (process.env.VUE_CLI_TEST) {
process.stdin.on('data', data => {
if (data.toString() === 'close') {
// eslint-disable-next-line no-console
console.log('got close signal!')
server.close(() => {
process.exit(0)
})
}
})
}
}
)
}
function getStyleguidist(args, api, options) {
const conf = api.resolve(args.config || './styleguide.config.js')
const sgConf = conf && conf.length ? require(conf) : {}
// reset the default component expression
sgConf.components = sgConf.components || 'src/components/**/[A-Z]*.vue'
if (args.verbose !== undefined) {
sgConf.verbose = !!args.verbose
}
if (args.port !== undefined) {
sgConf.serverPort = parseInt(args.port, 10)
}
const userWebpackConfig = sgConf.webpackConfig
options.outputDir = sgConf.styleguideDir || configSchema.styleguideDir.default
const cliWebpackConfig = getConfig(api)
return styleguidist(
sgConf,
config => (config.webpackConfig = merge(cliWebpackConfig, userWebpackConfig))
)
}
/**
* Make webpackConfig for styleguidist
* @param {Object} api
*/
function getConfig(api) {
const conf = api.resolveChainableWebpackConfig()
// avoid annoying notification when everything works
if (conf.plugins.has('fork-ts-checker')) {
conf.plugin('fork-ts-checker').tap(args => {
args[0].logger = {
// eslint-disable-next-line no-console
warn: console.warn,
// eslint-disable-next-line no-console
error: console.error,
info: function() {}
}
return args
})
}
// because we are dealing with hot replacement in vsg
// remove duplicate hot module reload plugin
conf.plugins.delete('hmr')
// remove the double compiled successfully message
conf.plugins.delete('friendly-errors')
return api.resolveWebpackConfig(conf)
}