cnf
2025-05-10 386fa0eca75ddc88165f9b73038f2a2239e1072e
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
const {
  info,
  openBrowser,
  IpcMessenger
} = require('@vue/cli-shared-utils')
 
const defaults = {
  host: '0.0.0.0',
  port: 8080,
  https: false
}
 
module.exports = (api, options) => {
  api.registerCommand('uni-serve', {
    description: 'start development server',
    usage: 'vue-cli-service uni-serve [options] [entry]',
    options: {
      '--open': 'open browser on server start',
      '--copy': 'copy url to clipboard on server start',
      '--mode': 'specify env mode (default: development)',
      '--host': `specify host (default: ${defaults.host})`,
      '--port': `specify port (default: ${defaults.port})`,
      '--https': `use https (default: ${defaults.https})`,
      '--public': 'specify the public network URL for the HMR client',
      '--auto-host': 'specify automator host',
      '--auto-port': 'specify automator port'
    }
  }, async function serve (args) {
    info('Starting development server...')
 
    require('./util').initAutomator(args)
 
    // although this is primarily a dev server, it is possible that we
    // are running it in a mode with a production env, e.g. in E2E tests.
    const isInContainer = checkInContainer()
    const isProduction = process.env.NODE_ENV === 'production'
 
    const url = require('url')
    const path = require('path')
    const { chalk } = require('@vue/cli-shared-utils')
    const webpack = require('webpack')
    const WebpackDevServer = require('webpack-dev-server')
    const portfinder = require('portfinder')
    const prepareURLs = require('@vue/cli-service/lib/util/prepareURLs')
    const prepareProxy = require('@vue/cli-service/lib/util/prepareProxy')
    const launchEditorMiddleware = require('launch-editor-middleware')
    const validateWebpackConfig = require('@vue/cli-service/lib/util/validateWebpackConfig')
    const isAbsoluteUrl = require('@vue/cli-service/lib/util/isAbsoluteUrl')
 
    // resolve webpack config
    const webpackConfig = api.resolveWebpackConfig()
 
    // check for common config errors
    validateWebpackConfig(webpackConfig, api, options)
 
    // load user devServer options with higher priority than devServer
    // in webpck config
    const projectDevServerOptions = Object.assign(
      webpackConfig.devServer || {},
      options.devServer
    )
 
    // expose advanced stats
    if (args.dashboard) {
      const DashboardPlugin = require('@vue/cli-service/lib/webpack/DashboardPlugin');
      (webpackConfig.plugins = webpackConfig.plugins || []).push(new DashboardPlugin({
        type: 'serve'
      }))
    }
 
    // entry arg
    const entry = args._[0]
    if (entry) {
      webpackConfig.entry = {
        app: api.resolve(entry)
      }
    }
 
    // resolve server options
    const useHttps = args.https || projectDevServerOptions.https || defaults.https
    const protocol = useHttps ? 'https' : 'http'
    const host = args.host || process.env.HOST || projectDevServerOptions.host || defaults.host
    portfinder.basePort = args.port || process.env.PORT || projectDevServerOptions.port || defaults
      .port
    const port = await portfinder.getPortPromise()
    const rawPublicUrl = args.public || projectDevServerOptions.public
    const publicUrl = rawPublicUrl
      ? /^[a-zA-Z]+:\/\//.test(rawPublicUrl)
        ? rawPublicUrl
        : `${protocol}://${rawPublicUrl}`
      : null
 
    const urls = prepareURLs(
      protocol,
      host,
      port,
      isAbsoluteUrl(options.publicPath) ? '/' : options.publicPath
    )
 
    const proxySettings = prepareProxy(
      projectDevServerOptions.proxy,
      api.resolve('public')
    )
 
    // inject dev & hot-reload middleware entries
    if (!isProduction) {
      const sockjsUrl = publicUrl
        // explicitly configured via devServer.public
        ? `?${publicUrl}/sockjs-node`
        : isInContainer
        // can't infer public netowrk url if inside a container...
        // use client-side inference (note this would break with non-root publicPath)
          ? ''
        // otherwise infer the url
          : '?' + url.format({
            protocol,
            port,
            hostname: urls.lanUrlForConfig || 'localhost',
            pathname: '/sockjs-node'
          })
      const devClients = [
        // dev server client
        require.resolve('webpack-dev-server/client') + sockjsUrl,
        // hmr client
        require.resolve(projectDevServerOptions.hotOnly
          ? 'webpack/hot/only-dev-server'
          : 'webpack/hot/dev-server')
        // TODO custom overlay client
        // `@vue/cli-overlay/dist/client`
      ]
      if (process.env.APPVEYOR) {
        devClients.push('webpack/hot/poll?500')
      }
      // inject dev/hot client
      addDevClientToEntry(webpackConfig, devClients)
    }
 
    // create compiler
    const compiler = webpack(webpackConfig)
 
    // create server
    let server
    if (webpack.version[0] > 4) {
      server = new WebpackDevServer(Object.assign({
        historyApiFallback: {
          disableDotRule: true,
          rewrites: [{
            from: /./,
            to: path.posix.join(options.publicPath, 'index.html')
          }]
        },
        hot: !isProduction,
        compress: isProduction,
        static: {
          directory: api.resolve('public'),
          publicPath: options.publicPath,
          watch: !isProduction,
          ...projectDevServerOptions.static
        },
        client: {
          logging: 'none',
          overlay: isProduction // TODO disable this
            ? false
            : { warnings: false, errors: true },
          progress: !process.env.VUE_CLI_TEST,
          ...projectDevServerOptions.client
        }
      }, projectDevServerOptions, {
        https: useHttps,
        proxy: proxySettings,
        setupMiddlewares (middlewares, devServer) {
          // launch editor support.
          // this works with vue-devtools & @vue/cli-overlay
          devServer.app.use('/__open-in-editor', launchEditorMiddleware(() => console.log(
            'To specify an editor, specify the EDITOR env variable or ' +
            'add "editor" field to your Vue project config.\n'
          )))
 
          // allow other plugins to register middlewares, e.g. PWA
          // todo: migrate to the new API interface
          api.service.devServerConfigFns.forEach(fn => fn(devServer.app, devServer))
 
          if (projectDevServerOptions.setupMiddlewares) {
            return projectDevServerOptions.setupMiddlewares(middlewares, devServer)
          }
 
          return middlewares
        }
      }), compiler)
    } else {
      server = new WebpackDevServer(compiler, Object.assign({
        clientLogLevel: 'none',
        historyApiFallback: {
          disableDotRule: true,
          rewrites: [{
            from: /./,
            to: path.posix.join(options.publicPath, 'index.html')
          }]
        },
        contentBase: api.resolve('public'),
        watchContentBase: !isProduction,
        hot: !isProduction,
        quiet: true,
        compress: isProduction,
        publicPath: options.publicPath,
        overlay: isProduction // TODO disable this
          ? false : {
            warnings: false,
            errors: true
          }
      }, projectDevServerOptions, {
        https: useHttps,
        proxy: proxySettings,
        before (app, server) {
        // launch editor support.
        // this works with vue-devtools & @vue/cli-overlay
          app.use('/__open-in-editor', launchEditorMiddleware(() => console.log(
            'To specify an editor, sepcify the EDITOR env variable or ' +
          'add "editor" field to your Vue project config.\n'
          )))
          // allow other plugins to register middlewares, e.g. PWA
          api.service.devServerConfigFns.forEach(fn => fn(app, server))
          // apply in project middlewares
          projectDevServerOptions.before && projectDevServerOptions.before(app,
            server)
        }
      }))
    }
 
    ;
    ['SIGINT', 'SIGTERM'].forEach(signal => {
      process.on(signal, () => {
        server[webpack.version[0] > 4 ? 'stopCallback' : 'close'](() => {
          process.exit(0)
        })
      })
    })
 
    // on appveyor, killing the process with SIGTERM causes execa to
    // throw error
    if (process.env.VUE_CLI_TEST) {
      process.stdin.on('data', data => {
        if (data.toString() === 'close') {
          console.log('got close signal!')
          server[webpack.version[0] > 4 ? 'stopCallback' : 'close'](() => {
            process.exit(0)
          })
        }
      })
    }
 
    return new Promise((resolve, reject) => {
      const {
        runByHBuilderX
      } = require('@dcloudio/uni-cli-shared')
      // log instructions & open browser on first compilation complete
      let isFirstCompile = true
      compiler.hooks.done.tap('vue-cli-service uni-serve', stats => {
        if (stats.hasErrors()) {
          return
        }
 
        let copied = ''
        if (isFirstCompile && args.copy) {
          require('clipboardy').write(urls.localUrlForBrowser)
          copied = chalk.dim('(copied to clipboard)')
        }
 
        const networkUrl = publicUrl
          ? publicUrl.replace(/([^/])$/, '$1/')
          : urls.lanUrlForTerminal
        const printRunningAt = !runByHBuilderX || (runByHBuilderX && isFirstCompile)
        printRunningAt && console.log()
        printRunningAt && console.log('  App running at:')
        printRunningAt && console.log(
          `  - Local:   ${chalk.cyan(urls.localUrlForTerminal)} ${copied}`
        )
        if (!printRunningAt) {
          console.log('Build complete. Watching for changes...')
        }
        if (!isInContainer) {
          printRunningAt && console.log(`  - Network: ${chalk.cyan(networkUrl)}`)
        } else {
          console.log()
          console.log(chalk.yellow(
            '  It seems you are running Vue CLI inside a container.'
          ))
          if (!publicUrl && options.publicPath && options.publicPath !== '/') {
            console.log()
            console.log(chalk.yellow(
              '  Since you are using a non-root publicPath, the hot-reload socket'
            ))
            console.log(chalk.yellow(
              '  will not be able to infer the correct URL to connect. You should'
            ))
            console.log(chalk.yellow(
              `  explicitly specify the URL via ${chalk.blue('devServer.public')}.`
            ))
            console.log()
          }
          console.log(chalk.yellow(
            `  Access the dev server via ${chalk.cyan(
              `${protocol}://localhost:<your container's external mapped port>${options.publicPath}`
            )}`
          ))
        }
        console.log()
 
        if (isFirstCompile) {
          isFirstCompile = false
 
          if (!isProduction) {
            // const buildCommand = hasProjectYarn(api.getCwd()) ? `yarn build` : `npm run build`
            // console.log(`  Note that the development build is not optimized.`)
            // console.log(`  To create a production build, run ${chalk.cyan(buildCommand)}.`)
          } else {
            console.log('  App is served in production mode.')
            console.log('  Note this is for preview or E2E testing only.')
          }
          console.log()
 
          if (args.open || projectDevServerOptions.open) {
            const pageUri = (projectDevServerOptions.openPage && typeof projectDevServerOptions
              .openPage === 'string')
              ? projectDevServerOptions.openPage
              : ''
            openBrowser(urls.localUrlForBrowser + pageUri)
          }
 
          // Send final app URL
          if (args.dashboard) {
            const ipc = new IpcMessenger()
            ipc.send({
              vueServe: {
                url: urls.localUrlForBrowser
              }
            })
          }
 
          // resolve returned Promise
          // so other commands can do api.service.run('serve').then(...)
          resolve({
            server,
            url: urls.localUrlForBrowser
          })
        } else if (process.env.VUE_CLI_TEST) {
          // signal for test to check HMR
          console.log('App updated')
        }
      })
 
      if (server.showStatus) {
        server.showStatus = function () {}
      }
 
      if (webpack.version[0] > 4) {
        server.start().catch(err => reject(err))
      } else {
        server.listen(port, host, err => {
          if (err) {
            reject(err)
          }
        })
      }
    })
  })
}
 
function addDevClientToEntry (config, devClient) {
  const {
    entry
  } = config
  if (typeof entry === 'object' && !Array.isArray(entry)) {
    Object.keys(entry).forEach((key) => {
      entry[key] = devClient.concat(entry[key])
    })
  } else if (typeof entry === 'function') {
    config.entry = entry(devClient)
  } else {
    config.entry = devClient.concat(entry)
  }
}
 
// https://stackoverflow.com/a/20012536
function checkInContainer () {
  const fs = require('fs')
  if (fs.existsSync('/proc/1/cgroup')) {
    const content = fs.readFileSync('/proc/1/cgroup', 'utf-8')
    return /:\/(lxc|docker|kubepods)\//.test(content)
  }
}
 
module.exports.defaultModes = {
  serve: 'development'
}