1 | 'use strict'
|
2 |
|
3 |
|
4 | process.env.BABEL_ENV = 'production'
|
5 | process.env.NODE_ENV = 'production'
|
6 |
|
7 | process.on('unhandledRejection', err => {
|
8 | throw err
|
9 | })
|
10 |
|
11 | const chalk = require('chalk')
|
12 | const config = require('../config')
|
13 | const { isObject, isNotEmptyArray } = require('../lib/utils')
|
14 | const paths = config.paths
|
15 | const vendorConf = config.vendor || []
|
16 |
|
17 |
|
18 | if (!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 |
|
34 | const fs = require('fs-extra')
|
35 | const ora = require('ora')
|
36 | const webpack = require('webpack')
|
37 | const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages')
|
38 | const printBuildError = require('../lib/printBuildError')
|
39 | const prehandleConfig = require('../lib/prehandleConfig')
|
40 | const input = config.argv._
|
41 | let webpackDllConfig = require('../webpack/webpack.dll.conf')()
|
42 |
|
43 | const spinner = ora('Building dll...')
|
44 | spinner.start()
|
45 |
|
46 | function 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 |
|
59 |
|
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 |
|
75 | fs.emptyDirSync(paths.dll)
|
76 |
|
77 |
|
78 | const namespace = vendorConf.name ? `${vendorConf.name}_` : ''
|
79 | const vendorDir = namespace + 'vendor'
|
80 |
|
81 |
|
82 | fs.emptyDirSync(`${paths.dist}/${vendorDir}`)
|
83 |
|
84 | function 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 |
|
96 | function errorLog(err) {
|
97 | spinner.stop()
|
98 |
|
99 | console.log(chalk.red('Failed to compile.\n'))
|
100 | printBuildError(err)
|
101 | process.exit(1)
|
102 | }
|
103 |
|
104 | module.exports = function runDll() {
|
105 | return build()
|
106 | .then(output => {
|
107 |
|
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 | }
|