1 | #!/usr/bin/env node
|
2 | /* eslint-disable no-console */
|
3 |
|
4 | // Show logs
|
5 | process.env.DEBUG = process.env.DEBUG || 'nuxt:*'
|
6 |
|
7 | const fs = require('fs')
|
8 | const parseArgs = require('minimist')
|
9 | const { Nuxt, Builder, Generator } = require('../')
|
10 | const resolve = require('path').resolve
|
11 | const debug = require('debug')('nuxt:build')
|
12 | debug.color = 2 // Force green color
|
13 |
|
14 | const argv = parseArgs(process.argv.slice(2), {
|
15 | alias: {
|
16 | h: 'help',
|
17 | c: 'config-file',
|
18 | a: 'analyze',
|
19 | s: 'spa',
|
20 | u: 'universal'
|
21 | },
|
22 | boolean: ['h', 'a', 's', 'u'],
|
23 | string: ['c'],
|
24 | default: {
|
25 | c: 'nuxt.config.js'
|
26 | }
|
27 | })
|
28 |
|
29 | if (argv.help) {
|
30 | console.log(`
|
31 | Description
|
32 | Compiles the application for production deployment
|
33 | Usage
|
34 | $ nuxt build <dir>
|
35 | Options
|
36 | --analyze, -a Launch webpack-bundle-analyzer to optimize your bundles.
|
37 | --spa Launch in SPA mode
|
38 | --universal Launch in Universal mode (default)
|
39 | --config-file, -c Path to Nuxt.js config file (default: nuxt.config.js)
|
40 | --help, -h Displays this message
|
41 | `)
|
42 | process.exit(0)
|
43 | }
|
44 |
|
45 | const rootDir = resolve(argv._[0] || '.')
|
46 | const nuxtConfigFile = resolve(rootDir, argv['config-file'])
|
47 |
|
48 | var options = {}
|
49 | if (fs.existsSync(nuxtConfigFile)) {
|
50 | options = require(nuxtConfigFile)
|
51 | } else if (argv['config-file'] !== 'nuxt.config.js') {
|
52 | console.error(`> Could not load config file ${argv['config-file']}`)
|
53 | process.exit(1)
|
54 | }
|
55 | if (typeof options.rootDir !== 'string') {
|
56 | options.rootDir = rootDir
|
57 | }
|
58 | // Create production build when calling `nuxt build`
|
59 | options.dev = false
|
60 |
|
61 | // Nuxt Mode
|
62 | options.mode = (argv['spa'] && 'spa') || (argv['universal'] && 'universal') || options.mode
|
63 |
|
64 | // Analyze option
|
65 | options.build = options.build || {}
|
66 | if (argv.analyze) {
|
67 | options.build.analyze = true
|
68 | }
|
69 |
|
70 | debug('Building...')
|
71 | const nuxt = new Nuxt(options)
|
72 | const builder = new Builder(nuxt)
|
73 |
|
74 | if (options.mode !== 'spa') {
|
75 | // Build for SSR app
|
76 | builder.build()
|
77 | .then(() => debug('Building done'))
|
78 | .catch((err) => {
|
79 | console.error(err)
|
80 | process.exit(1)
|
81 | })
|
82 | } else {
|
83 | const s = Date.now()
|
84 |
|
85 | nuxt.hook('generate:distRemoved', function () {
|
86 | debug('Destination folder cleaned')
|
87 | })
|
88 |
|
89 | nuxt.hook('generate:distCopied', function () {
|
90 | debug('Static & build files copied')
|
91 | })
|
92 |
|
93 | nuxt.hook('generate:page', function (page) {
|
94 | debug('Generate file: ' + page.path)
|
95 | })
|
96 |
|
97 | nuxt.hook('generate:done', function (generator, errors) {
|
98 | const duration = Math.round((Date.now() - s) / 100) / 10
|
99 |
|
100 | debug(`HTML Files generated in ${duration}s`)
|
101 |
|
102 | if (errors.length) {
|
103 | const report = errors.map(({ type, route, error }) => {
|
104 | /* istanbul ignore if */
|
105 | if (type === 'unhandled') {
|
106 | return `Route: '${route}'\n${error.stack}`
|
107 | } else {
|
108 | return `Route: '${route}' thrown an error: \n` + JSON.stringify(error)
|
109 | }
|
110 | })
|
111 | console.error('==== Error report ==== \n' + report.join('\n\n')) // eslint-disable-line no-console
|
112 | }
|
113 | })
|
114 |
|
115 | // Disable minify to get exact results of nuxt start
|
116 | nuxt.options.generate.minify = false
|
117 | // Generate on spa mode
|
118 | new Generator(nuxt, builder).generate({ build: true }).then(() => {
|
119 | if (!nuxt.options.dev) {
|
120 | console.log(`✓ You can now directly upload ${nuxt.options.generate.dir}/ or start server using "nuxt start"`)
|
121 | }
|
122 | })
|
123 | }
|