@hyext/builder-zabin
Version:
builder of hyext
103 lines (78 loc) • 3.1 kB
JavaScript
const path = require('path')
const fse = require('fs-extra')
const chalk = require('chalk')
const spawn = require('cross-spawn')
const webpack = require('webpack')
const makeBuildResult = require('./lib/makeBuildResult')
async function build ({ mode, config, inputPath, outputPath, publicPath }) {
// process.env.NODE_ENV = mode;
// Makes the script crash on unhandled rejections instead of silently
// ignoring them. In the future, promise rejections that are not handled will
// terminate the Node.js process with a non-zero exit code.
if (mode != 'production') {
process.on('unhandledRejection', err => {throw err})
}
const BWD = path.resolve(__dirname, `scripts/${config.lib}`)
process.chdir(BWD) // side effect!!! side effect!!! side effect!!!
const webpackCfgFactory = require( path.join(BWD, `webpack.prod.js`) )
const compiler = webpack( webpackCfgFactory({config, inputPath, outputPath, publicPath}) )
console.log(
chalk.cyan('start building...')
)
// fse.removeSync(outputPath)
await new Promise((resolve, reject) => {
compiler.run((err, stats) => {
if (err) {
console.log(err.stack || err)
let msg = (mode == 'production') ? 'compiler error' : (err.stack || err)
if (err.details) {
msg = err.details
}
reject(msg)
return
}
const info = stats.toJson(mode == 'production' ? 'errors-only' : 'minimal')
if ( stats.hasErrors() ) {
reject(info.errors)
return
}
if (mode != 'production' && stats.hasWarnings()) {
console.warn(info.warnings)
}
resolve(stats)
console.log(
chalk.cyan('build content in bellow place:')
)
console.log(
chalk.green(`${outputPath}`)
)
console.log(
chalk.cyan('build finished.')
)
})
})
return makeBuildResult(config.endpoints, publicPath)
}
async function run (options) {
const { inputPath } = options
const babelRumtime = paths => path.join.apply(path, [...paths, '@babel', 'runtime'])
const regeneratorRuntime = paths => path.join.apply(path, [...paths, 'regenerator-runtime'])
await fse.copy(
babelRumtime([__dirname, 'providedProjectSourceCode']),
babelRumtime([inputPath, 'node_modules'])
).catch(err => {
console.log('copy @babel/runtime error')
console.log(err)
throw new Error('构建初始化失败')
})
await fse.copy(
regeneratorRuntime([__dirname, 'providedProjectSourceCode']),
regeneratorRuntime([inputPath, 'node_modules'])
).catch(err => {
console.log('copy regenerator-runtime error')
console.log(err)
throw new Error('构建初始化失败')
})
return await build(options)
}
module.exports = {build: run}