1 | 'use strict'
|
2 |
|
3 | process.env.BABEL_ENV = 'production'
|
4 | process.env.NODE_ENV = 'production'
|
5 |
|
6 | process.on('unhandledRejection', err => {
|
7 | throw err
|
8 | });
|
9 |
|
10 | var ora = require('ora')
|
11 | var fs = require('fs-extra')
|
12 | var path = require('path')
|
13 | var formatWebpackMessages = require('react-dev-utils/formatWebpackMessages')
|
14 | var buildReporter = require('../libs/buildReporter')
|
15 | var config = require('../config')
|
16 | var paths = config.paths
|
17 | var chalk = require('chalk')
|
18 | var webpack = require('webpack')
|
19 | var getEntry = require('../libs/wx-entry')
|
20 | var printBuildError = require('../libs/printBuildError')
|
21 | var getWebpackConfig = require('../webpack/webpack.wx-prod.conf')
|
22 | var prehandleConfig = require('../libs/prehandleConfig')
|
23 |
|
24 | const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024
|
25 | const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024
|
26 |
|
27 | var spinner = ora('building for production...')
|
28 | let entryInput = null
|
29 |
|
30 | function setup(entry) {
|
31 | entryInput = entry
|
32 | spinner.start()
|
33 | }
|
34 |
|
35 | function clean() {
|
36 | const dist = paths['wx-dist'];
|
37 | return fs.emptyDir(dist).then(() => dist)
|
38 | }
|
39 |
|
40 | function build(dist) {
|
41 | let webpackConfig = getWebpackConfig(entryInput);
|
42 | webpackConfig = prehandleConfig('wx-build', webpackConfig)
|
43 | const compiler = webpack(webpackConfig)
|
44 |
|
45 | return new Promise((resolve, reject) => {
|
46 | compiler.run((err, stats) => {
|
47 | spinner.stop()
|
48 |
|
49 | if (err) return reject(err)
|
50 |
|
51 | const messages = formatWebpackMessages(stats.toJson({}, true))
|
52 | if (messages.errors.length) {
|
53 |
|
54 |
|
55 | if (messages.errors.length > 1) {
|
56 | messages.errors.length = 1
|
57 | }
|
58 | return reject(new Error(messages.errors.join('\n\n')))
|
59 | }
|
60 |
|
61 | return resolve({
|
62 | stats,
|
63 | publicPath: webpackConfig.output.publicPath,
|
64 | path: webpackConfig.output.path,
|
65 | warnings: messages.warnings
|
66 | })
|
67 | })
|
68 | })
|
69 | }
|
70 |
|
71 | function success(output){
|
72 | const stats = output.stats.toJson({
|
73 | hash: false,
|
74 | chunks: false,
|
75 | modules: false,
|
76 | chunkModules: false
|
77 | })
|
78 |
|
79 | console.log(chalk.green(`Build complete in ${stats.time}ms\n`))
|
80 | console.log('File sizes after gzip:\n')
|
81 |
|
82 | stats.assets['__dist'] = output.path
|
83 |
|
84 | buildReporter(
|
85 |
|
86 | { page: [stats.assets] },
|
87 | WARN_AFTER_BUNDLE_GZIP_SIZE,
|
88 | WARN_AFTER_CHUNK_GZIP_SIZE
|
89 | )
|
90 |
|
91 | console.log()
|
92 |
|
93 | if (output.publicPath === '/') {
|
94 | console.log(
|
95 | chalk.yellow(
|
96 | `The app is built assuming that it will be deployed at the root of a domain.`
|
97 | )
|
98 | )
|
99 | console.log(
|
100 | chalk.yellow(
|
101 | `If you intend to deploy it under a subpath, update the ${chalk.green(
|
102 | 'publicPath'
|
103 | )} option in your project config (${chalk.cyan(
|
104 | `marauder.config.js`
|
105 | )}).\n`
|
106 | )
|
107 | )
|
108 | }
|
109 | }
|
110 |
|
111 | function error(err){
|
112 | console.log(err);
|
113 | console.log(chalk.red('Failed to compile1.\n'))
|
114 | printBuildError(err)
|
115 | process.exit(1)
|
116 | }
|
117 |
|
118 | getEntry()
|
119 | .then(setup)
|
120 | .then(clean)
|
121 | .then(build)
|
122 | .then(success)
|
123 | .catch(error)
|