This repository has been archived on 2023-08-30. You can view files and clone it, but cannot push or open issues/pull-requests.
bytedo
/
wcui
Archived
1
0
Fork 0
wcui/build.prod.js

125 lines
3.0 KiB
JavaScript
Raw Normal View History

#! /usr/bin/env node
2018-01-22 02:01:44 +08:00
const log = console.log
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')
const uglify = require('uglify-es')
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']
})
)
const jsOpt = {
presets: ['es2015'],
plugins: [
'transform-es2015-modules-amd',
'transform-decorators-legacy',
'transform-object-rest-spread',
['transform-es2015-classes', { loose: true }],
['transform-es2015-for-of', { loose: true }]
]
}
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
}
let t1 = Date.now()
2017-12-19 10:41:13 +08:00
let tmpOpt = jsOpt
let code = ''
if (!/anot/.test(entry)) {
code = babel.transformFileSync(entry, jsOpt).code
} else {
code = fs.cat(entry).toString()
2017-12-19 10:41:13 +08:00
}
code = uglify.minify(code).code.replace(/\.scss/g, '.css')
2017-12-21 22:39:45 +08:00
log(
'编译JS: %s, 耗时 %s ms',
chalk.green(entry),
chalk.yellow(Date.now() - t1)
)
fs.echo(code, output)
}
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)
})
}
const compileHtm = (entry, output) => {
let t1 = Date.now()
let htm = fs.cat(entry).toString('utf8')
htm = htm.replace(/[\r\n\t]+/g, ' ').replace(/\s{2,}/g, ' ')
2017-12-21 22:39:45 +08:00
log(
'压缩HTML: %s, 耗时 %s ms',
chalk.green(entry),
chalk.yellow(Date.now() - t1)
)
fs.echo(htm, output)
}
/*=======================================================*/
/*===== ===*/
/*=======================================================*/
const jsFiles = fs.ls('./src/js/', true)
const cssFiles = fs.ls('./src/css/', true)
if (fs.isdir(buildDir)) {
fs.rm(buildDir, true)
2017-12-21 22:39:45 +08:00
log(chalk.cyan('清除旧目录 dist/'))
}
// css目录
cssFiles.forEach(file => {
if (/\.scss$/.test(file)) {
let entry = file
let output = file.replace('src/css', 'dist/css').replace(/scss$/, 'css')
compileCss(entry, output)
}
})
// js目录的处理要复杂一点
jsFiles.forEach(file => {
let entry = file
let output = file.replace(/src\/js/, 'dist/js').replace(/scss$/, 'css')
let ext = file.slice(file.lastIndexOf('.') + 1)
switch (ext) {
case 'js':
compileJs(entry, output)
break
case 'scss':
compileCss(entry, output)
break
case 'htm':
compileHtm(entry, output)
break
default:
if (!fs.isdir(entry)) {
fs.cp(entry, output)
}
}
})