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')
|
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')
|
2018-03-25 15:45:11 +08:00
|
|
|
const uglify = require('uglify-es')
|
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 = {
|
2018-03-16 18:04:41 +08:00
|
|
|
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) => {
|
|
|
|
let t1 = Date.now()
|
2018-08-04 21:55:37 +08:00
|
|
|
try {
|
|
|
|
let { code } = babel.transformFileSync(entry, jsOpt)
|
|
|
|
code = uglify.minify(code).code.replace(/\.scss/g, '.css')
|
|
|
|
|
|
|
|
log(
|
|
|
|
'编译JS: %s, 耗时 %s ms',
|
|
|
|
chalk.green(entry),
|
|
|
|
chalk.yellow(Date.now() - t1)
|
|
|
|
)
|
|
|
|
fs.echo(code, output)
|
|
|
|
} catch (err) {
|
|
|
|
return log(err)
|
2017-12-19 10:41:13 +08:00
|
|
|
}
|
2017-12-04 02:02:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const compileCss = (entry, output) => {
|
|
|
|
let t1 = Date.now()
|
|
|
|
const { css } = scss.renderSync({ ...cssOpt, file: entry })
|
2018-01-22 02:01:44 +08:00
|
|
|
prefixer.process(css, { from: '', to: '' }).then(result => {
|
|
|
|
log(
|
|
|
|
'编译scss: %s, 耗时 %s ms',
|
|
|
|
chalk.green(entry),
|
|
|
|
chalk.yellow(Date.now() - t1)
|
|
|
|
)
|
|
|
|
fs.echo(result.css, output)
|
|
|
|
})
|
2017-12-04 02:02:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*=======================================================*/
|
|
|
|
/*===== ===*/
|
|
|
|
/*=======================================================*/
|
|
|
|
|
|
|
|
if (fs.isdir(buildDir)) {
|
2018-03-25 15:45:11 +08:00
|
|
|
fs.rm(buildDir, true)
|
2017-12-21 22:39:45 +08:00
|
|
|
log(chalk.cyan('清除旧目录 dist/'))
|
2017-12-04 02:02:18 +08:00
|
|
|
}
|
2018-08-04 21:55:37 +08:00
|
|
|
fs.mkdir(buildDir)
|
|
|
|
|
|
|
|
let list = fs.ls('./node_modules/anot/dist/')
|
|
|
|
list.forEach(it => {
|
|
|
|
fs.cp(it, path.resolve(buildDir, path.parse(it).base))
|
|
|
|
})
|
2017-12-04 02:02:18 +08:00
|
|
|
|
2018-08-04 21:55:37 +08:00
|
|
|
log('复制anot框架文件完成...')
|
2017-12-04 02:02:18 +08:00
|
|
|
|
2018-08-04 21:55:37 +08:00
|
|
|
/*----------------------------------------------*/
|
|
|
|
/*----------------------------------------------*/
|
|
|
|
/*----------------------------------------------*/
|
|
|
|
|
|
|
|
let files = fs.ls(sourceDir, true)
|
|
|
|
files = files.map(it => {
|
|
|
|
let file = path.parse(it)
|
|
|
|
if (!file.ext || file.base === '.DS_Store' || file.base === 'var.scss') {
|
|
|
|
return null
|
2017-12-04 02:02:18 +08:00
|
|
|
}
|
2018-08-04 21:55:37 +08:00
|
|
|
return { path: it, ext: file.ext, name: file.base }
|
2017-12-04 02:02:18 +08:00
|
|
|
})
|
|
|
|
|
2018-08-04 21:55:37 +08:00
|
|
|
files.forEach(file => {
|
|
|
|
if (!file) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let entry = file.path
|
|
|
|
let output = file.path.replace('src/', 'dist/')
|
2017-12-04 02:02:18 +08:00
|
|
|
|
2018-08-04 21:55:37 +08:00
|
|
|
switch (file.ext) {
|
|
|
|
case '.js':
|
2017-12-04 02:02:18 +08:00
|
|
|
compileJs(entry, output)
|
|
|
|
break
|
2018-08-04 21:55:37 +08:00
|
|
|
case '.scss':
|
|
|
|
output = output.replace(/\.scss$/, '.css')
|
2017-12-04 02:02:18 +08:00
|
|
|
compileCss(entry, output)
|
|
|
|
break
|
|
|
|
default:
|
2018-08-04 21:55:37 +08:00
|
|
|
fs.cp(entry, output)
|
2017-12-04 02:02:18 +08:00
|
|
|
}
|
|
|
|
})
|