Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@DanielFGray
Last active September 9, 2018 00:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanielFGray/8ef3a1f104d70808f416ab2ae856aa85 to your computer and use it in GitHub Desktop.
Save DanielFGray/8ef3a1f104d70808f416ab2ae856aa85 to your computer and use it in GitHub Desktop.
webpack in gulp
/* eslint-disable no-console,import/no-dynamic-require,global-require */
const path = require('path')
const gulp = require('gulp')
const R = require('ramda')
const watch = require('gulp-watch')
const webpack = require('webpack')
const { terminal } = require('terminal-kit')
const { ProgressPlugin } = webpack
const webpackPath = './webpack.config.js'
let webpackConfig = require(webpackPath)
const progressBar = terminal.progressBar({
width: process.env.COLUMNS,
title: 'starting...',
percent: true,
inline: true,
titleSize: 75,
})
const progressPlugin = new ProgressPlugin((percentage, message, ...args) => {
progressBar.update({ progress: percentage, title: `${message} ${args.join(' ')}` })
})
const statConfig = {
chunks: true,
chunkModules: false,
colors: true,
modules: false,
children: true,
}
const watcher = from => {
from.watch({
ignored: /node_modules/,
}, (err, stats) => {
if (err) {
console.error(err.stack || err)
if (err.details) {
console.error(err.details)
}
return
}
console.log(stats.toString(statConfig))
const info = stats.toJson()
if (stats.hasWarnings()) {
console.warn(info.warnings)
}
if (stats.hasErrors()) {
console.error(info.errors)
}
})
}
const compiler = config => {
const c = [].concat(config)
return webpack(R.map(R.over(R.lensPath(['plugins']), R.concat([progressPlugin])), c))
}
gulp.task('start', (cb) => {
let instance = watcher(compiler(webpackConfig))
// watch('./dist', () => { /* node ./dist/index.js */ })
watch([webpackPath], () => {
console.log('loading new webpack config')
delete require.cache[path.resolve(webpackPath)]
webpackConfig = require(webpackPath).default
instance.close(() => {
instance = watcher(compiler(webpackConfig))
})
})
})
gulp.task('build', cb => {
compiler(webpackConfig).run((err, stats) => {
if (err) console.error(err)
console.log(stats.toString(statConfig))
cb()
})
})
gulp.task('default', ['start'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment