2017-12-04 02:02:18 +08:00
|
|
|
#! /usr/bin/env node
|
|
|
|
|
|
|
|
const fs = require('iofs')
|
|
|
|
const path = require('path')
|
|
|
|
const babel = require('babel-core')
|
|
|
|
const scss = require('node-sass')
|
|
|
|
const chokidar = require('chokidar')
|
|
|
|
const log = console.log
|
2017-12-21 22:39:45 +08:00
|
|
|
const chalk = require('chalk')
|
2017-12-04 02:02:18 +08:00
|
|
|
|
|
|
|
const sourceDir = path.resolve(__dirname, 'src')
|
|
|
|
const buildDir = path.resolve(__dirname, 'dist')
|
|
|
|
const jsOpt = {
|
|
|
|
presets: ['es2015'],
|
2017-12-23 18:41:36 +08:00
|
|
|
plugins: ['transform-es2015-modules-amd']
|
2017-12-04 02:02:18 +08:00
|
|
|
}
|
|
|
|
const cssOpt = {
|
|
|
|
includePaths: ['src/css/'],
|
|
|
|
outputStyle: 'compressed'
|
|
|
|
}
|
|
|
|
|
|
|
|
const compileJs = (entry, output) => {
|
2017-12-19 10:41:13 +08:00
|
|
|
if (/touch\.patch/.test(entry)) {
|
|
|
|
return
|
|
|
|
}
|
2017-12-31 22:12:16 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
if (/anot/.test(entry)) {
|
2017-12-19 10:41:13 +08:00
|
|
|
fs.cp(entry, output)
|
2017-12-31 22:12:16 +08:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
const { code } = babel.transformFileSync(entry, jsOpt)
|
|
|
|
fs.echo(code, output)
|
|
|
|
} catch (err) {
|
|
|
|
return log(err)
|
|
|
|
}
|
2017-12-21 22:39:45 +08:00
|
|
|
}
|
2017-12-31 22:12:16 +08:00
|
|
|
}, 100)
|
|
|
|
log('编译JS: %s', chalk.green(entry))
|
2017-12-04 02:02:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const compileCss = (entry, output) => {
|
2017-12-31 22:12:16 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
try {
|
|
|
|
const { css } = scss.renderSync({ ...cssOpt, file: entry })
|
|
|
|
fs.echo(css, output)
|
|
|
|
} catch (err) {
|
|
|
|
log(err)
|
|
|
|
}
|
|
|
|
}, 100)
|
|
|
|
log('编译scss: %s', chalk.green(entry))
|
2017-12-04 02:02:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const compileHtm = (entry, output) => {
|
2017-12-31 22:12:16 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
let htm = fs.cat(entry).toString('utf8')
|
|
|
|
htm = htm.replace(/[\r\n\t]+/g, ' ').replace(/\s{2,}/g, ' ')
|
|
|
|
fs.echo(htm, output)
|
|
|
|
}, 100)
|
|
|
|
log('压缩HTML: %s', chalk.green(entry))
|
2017-12-04 02:02:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*=======================================================*/
|
|
|
|
/*===== ===*/
|
|
|
|
/*=======================================================*/
|
|
|
|
|
|
|
|
const fontFiles = fs.ls('./src/font/', true)
|
|
|
|
const jsFiles = fs.ls('./src/js/', true)
|
|
|
|
const cssFiles = fs.ls('./src/css/', true)
|
|
|
|
|
|
|
|
// 字体文件直接复制
|
|
|
|
chokidar.watch(path.join(sourceDir, 'font/')).on('all', (act, file) => {
|
|
|
|
if (act === 'add' || act === 'change') {
|
|
|
|
let output = file.replace('src/font/', 'dist/font/')
|
|
|
|
fs.cp(file, output)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// css目录
|
|
|
|
chokidar.watch(path.resolve(sourceDir, 'css/')).on('all', (act, file) => {
|
|
|
|
if (act === 'add' || act === 'change') {
|
|
|
|
if (/\.scss$/.test(file)) {
|
2017-12-04 10:07:58 +08:00
|
|
|
let output = file
|
|
|
|
.replace('src/css/', 'dist/css/')
|
|
|
|
.replace('.scss', '.css')
|
2017-12-04 02:02:18 +08:00
|
|
|
|
|
|
|
compileCss(file, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// js目录的处理要复杂一点
|
|
|
|
chokidar
|
|
|
|
.watch(path.resolve(sourceDir, 'js/'))
|
|
|
|
.on('all', (act, file) => {
|
|
|
|
if (act === 'add' || act === 'change') {
|
|
|
|
let output = file.replace('src/js/', 'dist/js/')
|
|
|
|
let ext = file.slice(file.lastIndexOf('.') + 1)
|
|
|
|
switch (ext) {
|
|
|
|
case 'js':
|
|
|
|
compileJs(file, output)
|
|
|
|
break
|
|
|
|
case 'scss':
|
|
|
|
output = output.replace(/scss$/, 'css')
|
|
|
|
compileCss(file, output)
|
|
|
|
break
|
|
|
|
case 'htm':
|
|
|
|
compileHtm(file, output)
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
fs.cp(file, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.on('ready', () => {
|
2017-12-21 22:39:45 +08:00
|
|
|
log(chalk.red('预处理完成,监听文件变化中,请勿关闭本窗口...'))
|
2017-12-04 02:02:18 +08:00
|
|
|
})
|