1 | const url = require('url')
|
2 | const opn = require('opn')
|
3 | const chalk = require('chalk')
|
4 | const launchEditorMiddlewarre = require('launch-editor-middleware')
|
5 | const getPort = require('get-port')
|
6 | const address = require('address')
|
7 | const launchEditorEndpoint = require('@poi/dev-utils/launchEditorEndpoint')
|
8 | const createDevServer = require('@poi/core/createDevServer')
|
9 | const logger = require('@poi/logger')
|
10 | const unspecifiedAddress = require('../utils/unspecifiedAddress')
|
11 | const isPath = require('../utils/isPath')
|
12 | const sharedCLIOptions = require('../utils/sharedCLIOptions')
|
13 |
|
14 | module.exports = {
|
15 | name: 'builtin:develop',
|
16 | apply(poi) {
|
17 | const command = poi.cli.handleCommand(
|
18 | 'develop',
|
19 | {
|
20 | desc: 'Run app in development mode',
|
21 | match(name) {
|
22 | return !name || isPath(name)
|
23 | }
|
24 | },
|
25 | async () => {
|
26 | const compiler = poi.createCompiler()
|
27 |
|
28 | const devServerOptions = Object.assign(
|
29 | {
|
30 | hot: poi.options.hotReload !== false,
|
31 | quiet: true,
|
32 | historyApiFallback: true,
|
33 | overlay: true,
|
34 | disableHostCheck: true,
|
35 | publicPath: compiler.options.output.publicPath
|
36 | },
|
37 | compiler.options.devServer,
|
38 | poi.options.devServer
|
39 | )
|
40 |
|
41 | const existingBefore = devServerOptions.before
|
42 | devServerOptions.before = app => {
|
43 | app.use(
|
44 | launchEditorEndpoint,
|
45 | launchEditorMiddlewarre(() =>
|
46 | console.log(
|
47 | `To specify an editor, sepcify the EDITOR env variable or ` +
|
48 | `add "editor" field to your Vue project config.\n`
|
49 | )
|
50 | )
|
51 | )
|
52 | poi.hooks.invoke('configureDevServer', app)
|
53 | existingBefore && existingBefore(app)
|
54 | }
|
55 |
|
56 | const host = poi.options.devServer.host
|
57 | const port = await getPort({ port: poi.options.devServer.port, host })
|
58 | if (port !== poi.options.devServer.port) {
|
59 | logger.warn(
|
60 | `Port ${
|
61 | poi.options.devServer.port
|
62 | } has been used, switched to ${port}.`
|
63 | )
|
64 | }
|
65 |
|
66 | const server = createDevServer(compiler, devServerOptions)
|
67 | server.listen(port, host)
|
68 | let lanIP
|
69 | poi.on('show-develop-logs', () => {
|
70 | let msg = `\n ${chalk.green('App running at:')}`
|
71 | const isUnspecifiedAddress = unspecifiedAddress(host)
|
72 |
|
73 | const localURL = url.format({
|
74 | protocol: 'http',
|
75 | hostname: isUnspecifiedAddress ? 'localhost' : host,
|
76 | port
|
77 | })
|
78 | msg += `\n${chalk.bold(` - Local: ${localURL}`)}`
|
79 | if (isUnspecifiedAddress) {
|
80 | const lanURL = url.format({
|
81 | protocol: 'http',
|
82 | hostname: lanIP || (lanIP = address.ip()),
|
83 | port
|
84 | })
|
85 | msg += `\n${chalk.dim(` - On Your Network: ${lanURL}`)}`
|
86 | }
|
87 |
|
88 | logger.log(msg + '\n')
|
89 |
|
90 | if (poi.options.open) {
|
91 | opn(
|
92 | url.format({
|
93 | protocol: 'http',
|
94 | hostname: unspecifiedAddress(host) ? 'localhost' : host,
|
95 | port
|
96 | })
|
97 | )
|
98 | }
|
99 | })
|
100 |
|
101 | return {
|
102 | devServer: server
|
103 | }
|
104 | }
|
105 | )
|
106 |
|
107 | sharedCLIOptions(command)
|
108 |
|
109 | command
|
110 | .option('host', {
|
111 | desc: 'Server host'
|
112 | })
|
113 | .option('port', {
|
114 | desc: 'Server port'
|
115 | })
|
116 | }
|
117 | }
|