2018-08-04 16:26:50 +08:00
|
|
|
/**
|
|
|
|
*
|
2020-07-27 15:20:22 +08:00
|
|
|
* @authors yutent (yutent.io@gmail.com)
|
2018-08-04 16:26:50 +08:00
|
|
|
* @date 2018-08-04 01:00:06
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
require('es.shim')
|
|
|
|
const fs = require('iofs')
|
|
|
|
const path = require('path')
|
|
|
|
const chokidar = require('chokidar')
|
2021-01-08 10:14:44 +08:00
|
|
|
const uglify = require('terser')
|
2018-08-04 16:26:50 +08:00
|
|
|
const chalk = require('chalk')
|
2020-07-27 15:20:22 +08:00
|
|
|
const config = require('./package.json')
|
2018-08-04 16:26:50 +08:00
|
|
|
const log = console.log
|
2020-07-27 15:20:22 +08:00
|
|
|
const VERSION = config.version
|
2019-01-28 15:55:33 +08:00
|
|
|
|
2018-08-04 16:26:50 +08:00
|
|
|
const PACK_DIR = path.resolve('./dist')
|
|
|
|
const SOURCE_DIR = path.resolve('./src/')
|
|
|
|
|
|
|
|
const PAD_START = Buffer.from(`
|
2019-01-28 15:55:33 +08:00
|
|
|
const _Anot = (function() {
|
2018-08-04 16:26:50 +08:00
|
|
|
`)
|
|
|
|
|
2019-01-28 15:55:33 +08:00
|
|
|
const PAD_END = Buffer.from(`
|
2018-08-04 16:26:50 +08:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* DOMReady *
|
|
|
|
**********************************************************************/
|
|
|
|
|
2019-01-28 15:55:33 +08:00
|
|
|
let readyList = []
|
|
|
|
let isReady
|
|
|
|
let fireReady = function(fn) {
|
2018-08-04 16:26:50 +08:00
|
|
|
isReady = true
|
|
|
|
while ((fn = readyList.shift())) {
|
|
|
|
fn(Anot)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DOC.readyState === 'complete') {
|
|
|
|
setTimeout(fireReady) //如果在domReady之外加载
|
|
|
|
} else {
|
|
|
|
DOC.addEventListener('DOMContentLoaded', fireReady)
|
|
|
|
}
|
|
|
|
window.addEventListener('load', fireReady)
|
|
|
|
Anot.ready = function(fn) {
|
|
|
|
if (!isReady) {
|
|
|
|
readyList.push(fn)
|
|
|
|
} else {
|
|
|
|
fn(Anot)
|
|
|
|
}
|
|
|
|
}
|
2018-08-04 21:37:59 +08:00
|
|
|
window.Anot = Anot
|
2018-08-04 16:26:50 +08:00
|
|
|
return Anot
|
|
|
|
})()
|
|
|
|
export default _Anot
|
|
|
|
`)
|
|
|
|
|
2019-01-28 15:55:33 +08:00
|
|
|
function comment({ touch } = {}) {
|
2018-08-04 16:26:50 +08:00
|
|
|
return `/*==================================================
|
2019-01-28 15:55:33 +08:00
|
|
|
* Anot ${touch ? 'touch' : 'normal'} version for future browsers
|
2020-07-27 15:20:22 +08:00
|
|
|
* @authors yutent<yutent.io@gmail.com>
|
|
|
|
* @date ${new Date().format()}
|
|
|
|
* @version v${VERSION}
|
2018-08-04 16:26:50 +08:00
|
|
|
*
|
|
|
|
==================================================*/
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************************/
|
|
|
|
/********************* 华丽丽的分割线 ****************************/
|
|
|
|
/***************************************************************************/
|
|
|
|
|
|
|
|
const BUFFER_CACHE = {}
|
|
|
|
const LIB_QUEUE = []
|
|
|
|
|
|
|
|
function loadFiles() {
|
|
|
|
let files = fs.ls('./src/')
|
|
|
|
for (let it of files) {
|
|
|
|
if (fs.isdir(it)) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
BUFFER_CACHE[it] = fs.cat(it)
|
|
|
|
LIB_QUEUE.push(it)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateBuffer(file) {
|
|
|
|
BUFFER_CACHE[file] = fs.cat(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 打包,但不压缩
|
|
|
|
function packNoCompress(file) {
|
|
|
|
if (file) {
|
|
|
|
updateBuffer(file)
|
|
|
|
}
|
|
|
|
let libs = LIB_QUEUE.map(it => {
|
|
|
|
return BUFFER_CACHE[it]
|
|
|
|
})
|
|
|
|
let touchModule = fs.cat('./src/lib/touch.js')
|
|
|
|
|
2019-01-28 15:55:33 +08:00
|
|
|
let normalVer = Buffer.concat(libs)
|
|
|
|
let touchVer = Buffer.concat([normalVer, touchModule])
|
2018-08-04 16:26:50 +08:00
|
|
|
|
2018-08-04 21:37:59 +08:00
|
|
|
/**
|
|
|
|
* --------------------------------------------------------
|
|
|
|
* 打包未来版的 anot
|
|
|
|
* --------------------------------------------------------
|
|
|
|
*/
|
2019-01-28 15:55:33 +08:00
|
|
|
fs.echo(Buffer.concat([PAD_START, normalVer, PAD_END]), './dist/anot.js')
|
|
|
|
log('%s 打包完成...', chalk.green('anot.js'))
|
2018-08-04 21:37:59 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* --------------------------------------------------------
|
|
|
|
* 打包带触摸事件的未来版的 anot
|
|
|
|
* --------------------------------------------------------
|
|
|
|
*/
|
2020-07-27 15:20:22 +08:00
|
|
|
fs.echo(Buffer.concat([PAD_START, touchVer, PAD_END]), './dist/anot.touch.js')
|
|
|
|
log('%s 打包完成...', chalk.green('anot.touch.js'))
|
2018-08-04 16:26:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 打包并压缩
|
|
|
|
function packAndCompress() {
|
|
|
|
let libs = LIB_QUEUE.map(it => {
|
|
|
|
return BUFFER_CACHE[it]
|
|
|
|
})
|
|
|
|
let touchModule = fs.cat('./src/lib/touch.js')
|
|
|
|
|
2019-01-28 15:55:33 +08:00
|
|
|
let normalVer = Buffer.concat(libs)
|
|
|
|
let touchVer = Buffer.concat([normalVer, touchModule])
|
2018-08-04 16:26:50 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* --------------------------------------------------------
|
2019-01-28 15:55:33 +08:00
|
|
|
* 打包未来版的 anot
|
2018-08-04 16:26:50 +08:00
|
|
|
* --------------------------------------------------------
|
|
|
|
*/
|
|
|
|
log('正在打包 anot.js...')
|
2019-01-28 15:55:33 +08:00
|
|
|
let normalVerPack = Buffer.concat([PAD_START, normalVer, PAD_END]).toString()
|
|
|
|
fs.echo(comment() + uglify.minify(normalVerPack).code, './dist/anot.js')
|
2018-08-04 16:26:50 +08:00
|
|
|
log(chalk.green('anot.js 打包压缩完成!'))
|
|
|
|
|
|
|
|
/**
|
|
|
|
* --------------------------------------------------------
|
2019-01-28 15:55:33 +08:00
|
|
|
* 打包带触摸事件的未来版的 anot
|
2018-08-04 16:26:50 +08:00
|
|
|
* --------------------------------------------------------
|
|
|
|
*/
|
2020-07-27 15:20:22 +08:00
|
|
|
log('正在打包 anot.touch.js...')
|
2019-01-28 15:55:33 +08:00
|
|
|
let touchVerPack = Buffer.concat([PAD_START, touchVer, PAD_END]).toString()
|
2018-08-04 16:26:50 +08:00
|
|
|
|
|
|
|
fs.echo(
|
2019-01-28 15:55:33 +08:00
|
|
|
comment({ touch: true }) + uglify.minify(touchVerPack).code,
|
2020-07-27 15:20:22 +08:00
|
|
|
'./dist/anot.touch.js'
|
2018-08-04 16:26:50 +08:00
|
|
|
)
|
2020-07-27 15:20:22 +08:00
|
|
|
log(chalk.green('anot.touch.js 打包压缩完成!'))
|
2018-08-04 16:26:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
let args = process.argv.slice(2)
|
|
|
|
let mode = args.shift()
|
|
|
|
let ready = false
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case 'dev':
|
|
|
|
chokidar
|
|
|
|
.watch(path.resolve('./src/'))
|
|
|
|
.on('all', (act, file) => {
|
|
|
|
if (!ready) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (act === 'add' || act === 'change') {
|
|
|
|
packNoCompress(file)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.on('ready', () => {
|
|
|
|
log('正在执行首次打包...')
|
|
|
|
loadFiles()
|
|
|
|
packNoCompress()
|
|
|
|
log(chalk.red('预处理完成,监听文件变化中,请勿关闭本窗口...'))
|
|
|
|
ready = true
|
|
|
|
})
|
|
|
|
break
|
|
|
|
case 'prod':
|
|
|
|
loadFiles()
|
|
|
|
packAndCompress()
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
log(chalk.red('无效编译参数!'))
|
|
|
|
break
|
|
|
|
}
|