forked from Akryum/vue-cli-plugin-ssr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
186 lines (157 loc) · 5.86 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const config = require('./lib/config')
const defaultConfig = require('./lib/default-config')
const fs = require('fs')
const { chalk } = require('@vue/cli-shared-utils')
const portFinder = require('./lib/utils/portFinder')
const path = require('path')
const { isFunction } = require('./lib/utils/sharedTools')
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)',
usage: 'vue-cli-service ssr:build [options]',
options: {
'--modern': 'modern mode',
},
}, async (args) => {
const rimraf = require('rimraf')
const modernMode = args.modern
if (modernMode) {
process.env.VUE_CLI_MODERN_MODE = true
}
rimraf.sync(api.resolve(config.distPath))
const { getWebpackConfigs } = require('./lib/webpack')
const compile = ({ webpackConfigs, watch, service }) => {
const webpack = require('webpack')
const formatStats = require('@vue/cli-service/lib/commands/build/formatStats')
const options = service.projectOptions
const compiler = webpack(webpackConfigs)
delete require.cache[require.resolve('@vue/cli-plugin-babel')]
return new Promise((resolve) => {
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 resolve()
}
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) {
}
resolve()
}
if (watch) {
compiler.watch({}, onCompilationComplete)
} else {
compiler.run(onCompilationComplete)
}
})
}
if (modernMode) {
const { clientConfigLegacy, clientConfigModern, serverConfig } = getWebpackConfigs(service, true)
process.env.VUE_CLI_MODERN_MODE = true
await compile({ webpackConfigs: [clientConfigLegacy, serverConfig], watch: args.watch, service })
process.env.VUE_CLI_MODERN_BUILD = true
await compile({ webpackConfigs: [clientConfigModern], watch: args.watch, service })
} else {
const { clientConfig, serverConfig } = getWebpackConfigs(service)
await compile({ webpackConfigs: [clientConfig, serverConfig], watch: args.watch, service })
}
})
api.registerCommand('ssr:serve', {
description: 'Run the included server.',
usage: 'vue-cli-service ssr:serve [options]',
options: {
'-p, --port [port]': 'specify port',
'-h, --host [host]': 'specify host',
'-s, --server [path]': 'specify server file path',
'-c, --config [path]': 'specify config file path',
'-m, --mode [mode]': 'specify mode production/development',
},
}, async (args) => {
const rootPath = api.getCwd()
let serverInstance = null
let userConfig = null
const argsServer = args.server || args.s
const argsConfig = args.config || args.c
const argsPort = args.port || args.p
const argsHost = args.host || args.h
const argsMode = args.mode || args.m
if (argsMode && argsMode === 'production') {
process.env.NODE_ENV = 'production'
} else {
process.env.NODE_ENV = 'development'
}
if (config.service && config.service.projectOptions && config.service.projectOptions.pluginOptions && config.service.projectOptions.pluginOptions.vssr) {
userConfig = config.service.projectOptions.pluginOptions.vssr
}
if (argsConfig) {
const fullPath = path.resolve(rootPath, `${argsConfig}`)
if (!fs.existsSync(fullPath)) {
console.error(`${chalk.yellow('[WARN]')}: no config file found, use plugin config !!!`)
} else {
const importedConf = require(fullPath)
userConfig = isFunction(importedConf) ? importedConf(rootPath) : importedConf
// delete service and api from custom user config
// we need use cli service and api
delete userConfig.service
delete userConfig.api
}
}
let usedPort = argsPort || config.port || process.env.PORT
if (!usedPort) {
usedPort = await portFinder()
}
const usedHost = argsHost || config.host || process.env.HOST || 'localhost'
const currentConfig = Object.assign(config, userConfig)
currentConfig.rootPath = rootPath
currentConfig.mode = process.env.NODE_ENV
currentConfig.port = usedPort
currentConfig.host = usedHost
if (argsServer) {
const useServer = require('./lib/core/useServer')
try {
const serverPath = path.resolve(rootPath, `${argsServer}`)
serverInstance = useServer(serverPath, currentConfig)
} catch (error) {
console.error(error.message)
console.error('Fallback to builtIns server')
}
}
if (!serverInstance) {
const Server = require('./lib/core/builtInsServer')
serverInstance = new Server({
config: currentConfig,
})
}
return serverInstance.runServer()
})
}
module.exports.defaultModes = {
'ssr:build': 'production',
'ssr:serve': 'development',
}