slush-laima
Version:
The generator of Laima https://github.com/indigofeather/laima
107 lines (89 loc) • 2.43 kB
JavaScript
import path from 'path'
import cp from 'child_process'
import webpack, { DefinePlugin } from 'webpack'
import webpackConfig from './webpack.config.js'
import AppcacheWebpackPlugin from 'appcache-webpack-plugin'
import ghPages from 'gh-pages'
import git from 'git-rev-sync'
import jsonminify from 'jsonminify'
const md = require('markdown-it')({
html: true,
linkify: true,
typographer: true
}).use(require('markdown-it-deflist'))
const paths = {
build: path.resolve('build')
}
const config = {
deploy: {
message: git.short() + ' - Update ' + (new Date()).toISOString(),
logger: message => console.log(message)
}
}
let Mock
let isMockStart = false
export function* clear() {
yield this
.clear(paths.build + '/**/*')
}
export function* compile() {
yield this
.source('src/**/*.md')
.filter((data) => md.render(`${data}`))
.minify({ ext: 'html' })
.target(paths.build)
yield this
.source('src/**/*.json')
.filter(data => jsonminify(`${data}`))
.target(paths.build)
}
export function* mock() {
if (isMockStart) {
this.log('Restarting development server.')
Mock.kill('SIGTERM')
}
Mock = (() => {
isMockStart = true
return cp.fork('./mock.js', {
env: Object.assign({ NODE_ENV: 'development' }, process.env)
})
})()
process.on('exit', () => Mock.kill('SIGTERM'))
}
export function* webpackBuild() {
webpackConfig.plugins = webpackConfig.plugins.concat(
new DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
}),
new AppcacheWebpackPlugin(),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false } })
)
yield this
.source('src/js/index.js')
.webpack(webpackConfig)
}
export function* release() {
config.deploy.branch = 'build/release'
ghPages.publish(paths.build, config.deploy)
}
export function* master() {
config.deploy.branch = 'build/master'
ghPages.publish(paths.build, config.deploy)
}
export default function* () {
yield this
.watch('src/**/*.+(md|json)', 'compile')
yield this
.watch(['./mock.js', './api.js'], 'mock')
const server = (() => cp.fork('./server.js', {
env: Object.assign({ NODE_ENV: 'development' }, process.env)
}))()
process.on('exit', () => server.kill('SIGTERM'))
}
export function* build() {
yield this
.start(['compile', 'webpackBuild'])
}