#Webpack es6
run with babel-node --presets es2015 webpack.js
webpack.js
'use strict'
import { spawn } from 'child_process'
import path from 'path'
import fs from 'fs'
import webpack from 'webpack'
import debug from 'debug'
debug.enable('bundle:*')
let success = debug('bundle:success'); success.color = 2
let info = debug('bundle:info'); info.color = 7
let error = debug('bundle:error'); error.color = 1
info('start bundle')
const DEVELOPMENT = process.env.NODE_ENV === 'development'
const PRODUCTION = process.env.NODE_ENV === 'production'
const config = {
target: 'node',
debug: DEVELOPMENT,
cache: DEVELOPMENT,
stats: {
colors: true,
reasons: DEVELOPMENT,
hash: DEVELOPMENT,
version: DEVELOPMENT,
timings: true,
chunks: DEVELOPMENT,
chunkModules: DEVELOPMENT,
cached: DEVELOPMENT,
cachedAssets: DEVELOPMENT
},
entry: {
server: [
'babel-polyfill',
path.join(__dirname, '../src/server/server.js')
]
},
output: {
path: path.join(__dirname, '../build'),
filename: 'api.js',
libraryTarget: "commonjs2"
},
watch: {
aggregateTimeout: 500,
poll: true
},
externals: /^[a-z\/\-0-9]+$/i,
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel'
},
{
test: /\.json$/,
exclude: /node_modules/,
loader: 'json'
}
],
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.BannerPlugin(
'require("source-map-support").install();',
{ raw: true, entryOnly: false }
),
new webpack.ProgressPlugin((percentage, message) => {
const MOVE = new Buffer("1b5b3130303044", "hex").toString()
const CLEAR = new Buffer("1b5b304b", "hex").toString()
process.stdout.write((CLEAR + Math.round(percentage * 100) + "% :" + message + MOVE))
}),
...(!DEVELOPMENT ? [
new webpack.NoErrorsPlugin()
] : []),
...(!PRODUCTION ? [
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.AggressiveMergingPlugin(),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
})
] : [])
],
devtool: 'sourcemap'
}
let bundler = webpack(config)
if (PRODUCTION) {
bundler.run((err, stats) => {
let jsonStats = stats.toJson(config.stats)
let stringStats = stats.toString(config.stats)
if (err || jsonStats.errors.length > 0 || jsonStats.warnings.length > 0) {
error(err)
error('server not builded')
} else {
info('bundle stats : \n' + stringStats)
success('server builded')
}
})
}
if (DEVELOPMENT) {
let child = null;
bundler.watch(config.watch, (err, stats) => {
let jsonStats = stats.toJson(config.stats)
let stringStats = stats.toString(config.stats)
if (child) {
child.kill()
}
info('stats : \n' + stringStats)
if (err || jsonStats.errors.length > 0 || jsonStats.warnings.length > 0) {
error(err)
error('server not builded')
} else {
success('server builded')
info('run server with file watch')
child = spawn('node', ['build/api.js'], {
stdio: 'inherit'
})
}
})
}