UNPKG

3.09 kBJavaScriptView Raw
1'use strict'
2// 确保在文件首部设置环境变量
3process.env.BABEL_ENV = 'production'
4process.env.NODE_ENV = 'production'
5
6process.on('unhandledRejection', err => {
7 throw err
8});
9
10var ora = require('ora')
11var fs = require('fs-extra')
12var path = require('path')
13var formatWebpackMessages = require('react-dev-utils/formatWebpackMessages')
14var buildReporter = require('../libs/buildReporter')
15var config = require('../config')
16var paths = config.paths
17var chalk = require('chalk')
18var webpack = require('webpack')
19var getEntry = require('../libs/wx-entry')
20var printBuildError = require('../libs/printBuildError')
21var getWebpackConfig = require('../webpack/webpack.wx-prod.conf')
22var prehandleConfig = require('../libs/prehandleConfig')
23
24const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024
25const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024
26
27var spinner = ora('building for production...')
28let entryInput = null
29
30function setup(entry) {
31 entryInput = entry
32 spinner.start()
33}
34
35function clean() {
36 const dist = paths['wx-dist'];
37 return fs.emptyDir(dist).then(() => dist)
38}
39
40function 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 // Only keep the first error. Others are often indicative
54 // of the same problem, but confuse the reader with noise.
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
71function 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 // page 为数组
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
111function error(err){
112 console.log(err);
113 console.log(chalk.red('Failed to compile1.\n'))
114 printBuildError(err)
115 process.exit(1)
116}
117
118getEntry()
119 .then(setup)
120 .then(clean)
121 .then(build)
122 .then(success)
123 .catch(error)