wox-cli
Version:
scaffold for create component, toolkit and so on
90 lines (81 loc) • 2.81 kB
JavaScript
'use strict';
process.env.NODE_ENV = 'production';
const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const baseWebpackConfig = require('./webpack.base.conf');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const ora = require('ora');
const rm = require('rimraf');
const chalk = require('chalk');
const { publicPath, gitVersion } = require('./publicPath.config');
if (gitVersion === 'master') {
console.log(chalk.red.bold('\n🚨 严禁在 master 分支上构建!请拉取日常分支 `daily/x.y.z`!\n'));
process.exit(0);
} else if (!(/^daily\/\d*\.\d*\.\d*$/.test(gitVersion))) {
console.log(chalk.red.bold('\n🚨 当前分支不符合 `daily/x.y.z` 的版本规范!如果是开发分支,则不需要构建,请提 MR 到 `daily` 分支!\n'));
process.exit(0);
}
const webpackConfig = merge(baseWebpackConfig, {
devtool: '#source-map',
output: {
path: path.resolve(__dirname, './dist'),
filename: 'js/[name].js',
publicPath
},
plugins: [
new webpack.DefinePlugin({
'process.env': { NODE_ENV: '"production"' }
}),
new UglifyJsPlugin({
uglifyOptions: {
compress: {
warnings: false
}
},
sourceMap: true,
parallel: true
}),
new ExtractTextPlugin({
filename: 'css/[name].css',
allChunks: true,
}),
new OptimizeCSSPlugin({
cssProcessorOptions: { safe: true, map: { inline: false } ,autoprefixer: false}
}),
new webpack.HashedModuleIdsPlugin(),
new webpack.optimize.ModuleConcatenationPlugin(),
]
});
if (process.env.npm_config_report) {
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
webpackConfig.plugins.push(new BundleAnalyzerPlugin());
}
// build
const spinner = ora('building for production...');
spinner.start();
rm(path.resolve(__dirname, './dist'), err => {
if (err) throw err;
webpack(webpackConfig, (err, stats) => {
spinner.stop();
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(' Build complete.\n'));
console.log(chalk.yellow(
' Tip: built files are meant to be served over an HTTP server.\n' +
' Opening index.html over file:// won\'t work.\n'
));
});
});