build.js 1.5 KB
Newer Older
B
BingBlog 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
const webpack = require('webpack');
const rm = require('rimraf');
const ora = require('ora');
const chalk = require('chalk');
const HtmlReplacePlugin = require('./HtmlReplacePlugin');

// env 'production'
process.env.WEBPACK_ENV = 'production';

let webpackConfig = require('./webpack.prod.config');

let spinner = ora('building for production...');
spinner.start();

let feRoots = {
B
BingBlog 已提交
16
    'index': './'
B
BingBlog 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
};

webpackConfig.plugins = webpackConfig.plugins.concat([

    new HtmlReplacePlugin({
        replacer: function(html, opt) {

            var name = opt.outputName.replace(/\.html$/, '');

            var feRoot = feRoots[name];

            if (feRoot) {
                html = html
                    .replace(/href="/g, 'href="' + feRoot)
                    .replace(/src="/g, 'src="' + feRoot);
            }

            return html;

        }
    })

]);

rm(webpackConfig.output.path, err => {

    if (err) throw err;

    webpack(webpackConfig, function(err, stats) {
        spinner.stop()
        if (err) throw err

        process.stdout.write(stats.toString({
            colors: true,
            modules: false,
            children: false,
            chunks: false,
            chunkModules: false
        }) + '\n\n');

        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'
        ));
    })

});