2017-12-04 02:02:18 +08:00
|
|
|
#! /usr/bin/env node
|
|
|
|
|
2018-01-22 02:01:44 +08:00
|
|
|
const log = console.log
|
2017-12-04 02:02:18 +08:00
|
|
|
const fs = require('iofs')
|
|
|
|
const path = require('path')
|
|
|
|
const babel = require('babel-core')
|
|
|
|
const scss = require('node-sass')
|
|
|
|
const chokidar = require('chokidar')
|
2018-01-22 02:01:44 +08:00
|
|
|
const postcss = require('postcss')
|
|
|
|
const autoprefixer = require('autoprefixer')
|
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')
|
2018-01-22 02:01:44 +08:00
|
|
|
const prefixer = postcss().use(
|
|
|
|
autoprefixer({
|
|
|
|
browsers: ['ie > 9', 'iOS > 8', 'Android >= 4.4', 'ff > 38', 'Chrome > 38']
|
|
|
|
})
|
|
|
|
)
|
2017-12-04 02:02:18 +08:00
|
|
|
const jsOpt = {
|
|
|
|
presets: ['es2015'],
|
2018-02-03 02:10:34 +08:00
|
|
|
plugins: [
|
|
|
|
'transform-es2015-modules-amd',
|
|
|
|
'transform-decorators-legacy',
|
2018-05-20 05:32:51 +08:00
|
|
|
'transform-object-rest-spread',
|
|
|
|
['transform-es2015-classes', { loose: true }],
|
|
|
|
['transform-es2015-for-of', { loose: true }]
|
2018-02-03 02:10:34 +08:00
|
|
|
]
|
2017-12-04 02:02:18 +08:00
|
|
|
}
|
|
|
|
const cssOpt = {
|
|
|
|
outputStyle: 'compressed'
|
|
|
|
}
|
|
|
|
|
|
|
|
const compileJs = (entry, output) => {
|
2017-12-31 22:12:16 +08:00
|
|
|
log('编译JS: %s', chalk.green(entry))
|
2018-08-04 21:55:37 +08:00
|
|
|
try {
|
|
|
|
let { code } = babel.transformFileSync(entry, jsOpt)
|
|
|
|
code = code.replace(/\.scss/g, '.css')
|
|
|
|
fs.echo(code, output)
|
|
|
|
} catch (err) {
|
|
|
|
return log(err)
|
|
|
|
}
|
2017-12-04 02:02:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const compileCss = (entry, output) => {
|
2017-12-31 22:12:16 +08:00
|
|
|
log('编译scss: %s', chalk.green(entry))
|
2018-08-04 21:55:37 +08:00
|
|
|
try {
|
|
|
|
const { css } = scss.renderSync({ ...cssOpt, file: entry })
|
|
|
|
prefixer.process(css, { from: '', to: '' }).then(result => {
|
|
|
|
fs.echo(result.css, output)
|
|
|
|
})
|
|
|
|
} catch (err) {
|
|
|
|
log(err)
|
|
|
|
}
|
2017-12-04 02:02:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*=======================================================*/
|
|
|
|
/*===== ===*/
|
|
|
|
/*=======================================================*/
|
|
|
|
|
|
|
|
chokidar
|
2018-08-04 21:55:37 +08:00
|
|
|
.watch(sourceDir)
|
2017-12-04 02:02:18 +08:00
|
|
|
.on('all', (act, file) => {
|
|
|
|
if (act === 'add' || act === 'change') {
|
2018-08-04 21:55:37 +08:00
|
|
|
let entry = file
|
|
|
|
let output = file.replace('src/', 'dist/')
|
|
|
|
|
|
|
|
file = path.parse(entry)
|
|
|
|
if (!file.ext || file.base === '.DS_Store' || file.base === 'var.scss') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (file.ext) {
|
|
|
|
case '.js':
|
|
|
|
compileJs(entry, output)
|
2017-12-04 02:02:18 +08:00
|
|
|
break
|
2018-08-04 21:55:37 +08:00
|
|
|
case '.scss':
|
|
|
|
output = output.replace(/\.scss$/, '.css')
|
|
|
|
compileCss(entry, output)
|
2017-12-04 02:02:18 +08:00
|
|
|
break
|
|
|
|
default:
|
2018-08-04 21:55:37 +08:00
|
|
|
fs.cp(entry, output)
|
2017-12-04 02:02:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.on('ready', () => {
|
2017-12-21 22:39:45 +08:00
|
|
|
log(chalk.red('预处理完成,监听文件变化中,请勿关闭本窗口...'))
|
2017-12-04 02:02:18 +08:00
|
|
|
})
|
2018-08-04 21:55:37 +08:00
|
|
|
|
|
|
|
chokidar
|
|
|
|
.watch(path.resolve('./node_modules/anot/dist/'))
|
|
|
|
.on('all', (act, file) => {
|
|
|
|
if (act === 'add' || act === 'change') {
|
|
|
|
log('复制: %s', chalk.green(file))
|
|
|
|
fs.cp(file, path.resolve(buildDir, path.parse(file).base))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.on('ready', () => {
|
|
|
|
log('复制anot框架文件完成...')
|
|
|
|
})
|