forked from Akryum/vue-cli-plugin-ssr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (83 loc) · 2.67 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
const config = require('./lib/config')
const defaultConfig = require('./lib/default-config')
module.exports = (api, options) => {
// Config
Object.assign(config, defaultConfig(api, options), options.pluginOptions && options.pluginOptions.ssr)
config.api = api
const service = config.service = api.service
api.chainWebpack(webpackConfig => {
// Default entry
if (!process.env.VUE_CLI_SSR_TARGET) {
webpackConfig.entry('app').clear().add(config.entry('client'))
} else {
const { chainWebpack } = require('./lib/webpack')
chainWebpack(webpackConfig)
}
})
api.registerCommand('ssr:build', {
description: 'build for production (SSR)',
}, async (args) => {
const webpack = require('webpack')
const rimraf = require('rimraf')
const formatStats = require('@vue/cli-service/lib/commands/build/formatStats')
const options = service.projectOptions
rimraf.sync(api.resolve(config.distPath))
const { getWebpackConfigs } = require('./lib/webpack')
const [clientConfig, serverConfig] = getWebpackConfigs(service)
const compiler = webpack([clientConfig, serverConfig])
const onCompilationComplete = (err, stats) => {
if (err) {
// eslint-disable-next-line
console.error(err.stack || err)
if (err.details) {
// eslint-disable-next-line
console.error(err.details)
}
return
}
if (stats.hasErrors()) {
stats.toJson().errors.forEach(err => console.error(err))
process.exitCode = 1
}
if (stats.hasWarnings()) {
stats.toJson().warnings.forEach(warn => console.warn(warn))
}
try {
// eslint-disable-next-line
console.log(formatStats(stats, options.outputDir, api));
} catch (e) {
}
}
if (args.watch) {
compiler.watch({}, onCompilationComplete)
} else {
compiler.run(onCompilationComplete)
}
})
api.registerCommand('ssr:serve', {
description: 'Run the included server.',
usage: 'vue-cli-service serve:ssr [options]',
options: {
'-p, --port [port]': 'specify port',
'-h, --host [host]': 'specify host',
},
}, async (args) => {
const { createServer } = require('./lib/server')
let port = args.port || config.port || process.env.PORT
if (!port) {
const portfinder = require('portfinder')
port = await portfinder.getPortPromise()
}
const host = args.host || config.host || process.env.HOST || 'localhost'
config.port = port
config.host = host
await createServer({
port,
host,
})
})
}
module.exports.defaultModes = {
'ssr:build': 'production',
'ssr:serve': 'development',
}