custom-app
Version:
ITIMS��Ʒ�鿪��ר��React���,�Dz��ý��ּ�dhcc-app���������
103 lines (92 loc) • 3.67 kB
JavaScript
let exec = require('child_process').exec;
const webpack = require('webpack')
const fs = require('fs-extra')
const path = require('path')
const chalk = require('chalk')
const util = require('../../util/util')
const spinner = require('../../util/spinner');
function build (projectName, options) {
projectName = projectName === "all" ? "." : projectName;
const cwd = process.cwd()
const reactAppPath = util.getReactAppPath(cwd);
const inCurrent = projectName === '.'
const name = inCurrent ? path.relative('../', cwd) : projectName
const targetDir = path.resolve(cwd, projectName || '.')
console.log(util.getReactAppPath(cwd) + "-----------")
if (inCurrent) {
//顶级目录全部打包
let list = [];
fs.readdir(path.join(reactAppPath, "src"), function (err, files) {
files.forEach(function (v, i, array) {
// webpackSPA(reactAppPath, v)
let promise = function () {
return new Promise(function (resolve, reject) {
delete require.cache[require.resolve(path.join(reactAppPath, 'config', 'webpack.base.conf.js'))]
delete require.cache[require.resolve(path.join(reactAppPath, 'config', 'webpack.prod.conf.js'))]
delete require.cache[require.resolve(path.join(reactAppPath, 'config', 'project.js'))]
delete require.cache[require.resolve(path.join(reactAppPath, 'config', 'rootpath.js'))]
webpackSPA(reactAppPath, v, function () {
resolve();
})
})
}
list.push(promise)
})
queue(list)
.then(data => {
require("./publish-zip")(reactAppPath, projectName, options)
})
})
} else {
webpackSPA(reactAppPath, projectName, function () {
require("./publish-zip")(reactAppPath, projectName, options)
})
}
}
async function queue (arr) {
let res = null
for (let promise of arr) {
res = await promise(res)
}
return await res
}
function webpackSPA (reactAppPath, projectName, callback) {
fs.writeFileSync(path.join(reactAppPath, 'config', 'rootpath.js'), `exports.path = '${reactAppPath.split("\\")}'`)
fs.writeFileSync(path.join(reactAppPath, 'config', 'project.js'), `exports.name = '${projectName}'`)
spinner.logWithSpinner('', chalk.cyan('开始打包' + projectName));
exec(`webpack --config ${path.join(reactAppPath, 'config', 'webpack.prod.conf.js')}`, { cwd: reactAppPath }, function (err, stdout, stderr) {
if (err) {
console.error(chalk.red(`打包${projectName}失败:` + stderr));
console.log(stdout);
spinner.stopSpinner();
process.exit(1)
} else {
spinner.logWithSpinner('', stdout)
spinner.logWithSpinner(chalk.cyan(`打包${projectName}成功`))
spinner.logWithSpinner(' ', ' ');
spinner.stopSpinner();
callback && callback.call();
}
});
// webpack(confFile, (err, stats) => {
// spinner.stopSpinner();
// if (err) throw err
// process.stdout.write(stats.toString({
// colors: true,
// modules: false,
// children: false, // If you are using ts-loader, setting this to true will make TypeScript errors show up during build.
// chunks: false,
// chunkModules: false
// }) + '\n\n')
// if (stats.hasErrors()) {
// console.log(chalk.red(' Build failed with errors.\n'))
// process.exit(1)
// }
// console.log(chalk.cyan(projectName + ' 打包成功\n'))
// callback && callback.call();
// })
}
module.exports = (...args) => {
build(...args)
}