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({
|
2019-02-20 15:34:02 +08:00
|
|
|
browsers: ['ff > 61', 'Chrome > 63']
|
2018-01-22 02:01:44 +08:00
|
|
|
})
|
|
|
|
)
|
2017-12-04 02:02:18 +08:00
|
|
|
|
|
|
|
const compileJs = (entry, output) => {
|
2017-12-31 22:12:16 +08:00
|
|
|
log('编译JS: %s', chalk.green(entry))
|
2019-02-20 15:34:02 +08:00
|
|
|
let buf = fs.cat(entry).toString()
|
|
|
|
let code = buf
|
|
|
|
.replace(/\.scss/g, '.css')
|
|
|
|
.replace(/import '([a-z0-9\/\.\-_]*)(?<!\.css)'/g, 'import "$1.js"')
|
|
|
|
.replace(
|
|
|
|
/import ([\w]*) from '([a-z0-9\/\.\-_]*)'/g,
|
|
|
|
'import $1 from "$2.js"'
|
|
|
|
)
|
|
|
|
.replace(/import '([a-z0-9\/\.\-_]*\.css)'/g, 'importCss("/$1")')
|
|
|
|
|
|
|
|
fs.echo(code, output)
|
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 {
|
2019-02-20 15:34:02 +08:00
|
|
|
const { css } = scss.renderSync({ file: entry })
|
2018-08-04 21:55:37 +08:00
|
|
|
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框架文件完成...')
|
|
|
|
})
|