UNPKG

3.33 kBJavaScriptView Raw
1'use strict'
2
3// 确保在文件首部设置环境变量
4process.env.BABEL_ENV = 'production'
5process.env.NODE_ENV = 'production'
6
7process.on('unhandledRejection', err => {
8 throw err
9})
10
11const chalk = require('chalk')
12const config = require('../config')
13const { isObject, isNotEmptyArray } = require('../lib/utils')
14const paths = config.paths
15const vendorConf = config.vendor || []
16
17// 在 webpack.dll.conf 引入之前优先执行异常检查
18if (!Object.keys(vendorConf).length) {
19 console.log(
20 chalk.yellow(
21 'Build skip, vendor options is empty. Please check marauder.config.js'
22 )
23 )
24 process.exit(0)
25} else if (isObject(vendorConf) && !isNotEmptyArray(vendorConf.libs)) {
26 console.log(
27 chalk.yellow(
28 'Build skip, vendor.libs is empty. Please check marauder.config.js'
29 )
30 )
31 process.exit(0)
32}
33
34const fs = require('fs-extra')
35const ora = require('ora')
36const webpack = require('webpack')
37const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages')
38const printBuildError = require('../lib/printBuildError')
39const prehandleConfig = require('../lib/prehandleConfig')
40const input = config.argv._
41let webpackDllConfig = require('../webpack/webpack.dll.conf')()
42
43const spinner = ora('Building dll...')
44spinner.start()
45
46function build() {
47 webpackDllConfig = prehandleConfig({ command: 'dll', webpackDllConfig })
48 const compiler = webpack(webpackDllConfig)
49
50 return new Promise((resolve, reject) => {
51 compiler.run((err, stats) => {
52 spinner.stop()
53
54 if (err) return reject(err)
55
56 const messages = formatWebpackMessages(stats.toJson({}, true))
57 if (messages.errors.length) {
58 // Only keep the first error. Others are often indicative
59 // of the same problem, but confuse the reader with noise.
60 if (messages.errors.length > 1) {
61 messages.errors.length = 1
62 }
63 return reject(new Error(messages.errors.join('\n\n')))
64 }
65
66 return resolve({
67 stats,
68 warnings: messages.warnings
69 })
70 })
71 })
72}
73
74// 清空 dll 文件夹
75fs.emptyDirSync(paths.dll)
76
77// 为多页面准备,生成 xxx_vender 文件夹
78const namespace = vendorConf.name ? `${vendorConf.name}_` : ''
79const vendorDir = namespace + 'vendor'
80
81// 清空 vendor 文件
82fs.emptyDirSync(`${paths.dist}/${vendorDir}`)
83
84function ftp() {
85 if (!config.build.uploadFtp) return
86
87 const { name: projectName } = require(config.paths.packageJson)
88
89 require('../lib/ftp').uploadDir({
90 project: projectName,
91 view: vendorDir,
92 namespace: input[0]
93 })
94}
95
96function errorLog(err) {
97 spinner.stop()
98
99 console.log(chalk.red('Failed to compile.\n'))
100 printBuildError(err)
101 process.exit(1)
102}
103
104module.exports = function runDll() {
105 return build()
106 .then(output => {
107 // webpack 打包结果统计
108 process.stdout.write(
109 output.stats.toString({
110 colors: true,
111 modules: false,
112 children: false,
113 chunks: false,
114 chunkModules: false
115 }) + '\n\n'
116 )
117
118 console.log(chalk.green(' DLL Build complete.\n'))
119 console.log(
120 chalk.yellow(
121 ' Vender bundle has been generated, please rebuild your app:\n'
122 )
123 )
124 console.log(chalk.cyan(' $ yarn build <view_name>\n'))
125 })
126 .then(ftp)
127 .catch(errorLog)
128}