This repository has been archived on 2023-08-29. You can view files and clone it, but cannot push or open issues/pull-requests.
yutent
/
anot.js
Archived
1
0
Fork 0
anot.js/pack.config.js

190 lines
4.8 KiB
JavaScript
Raw Normal View History

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
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(`
const _Anot = (function() {
2018-08-04 16:26:50 +08:00
`)
const PAD_END = Buffer.from(`
2018-08-04 16:26:50 +08:00
/*********************************************************************
* DOMReady *
**********************************************************************/
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)
}
}
window.Anot = Anot
2018-08-04 16:26:50 +08:00
return Anot
})()
export default _Anot
`)
function comment({ touch } = {}) {
2018-08-04 16:26:50 +08:00
return `/*==================================================
* 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')
let normalVer = Buffer.concat(libs)
let touchVer = Buffer.concat([normalVer, touchModule])
2018-08-04 16:26:50 +08:00
/**
* --------------------------------------------------------
* 打包未来版的 anot
* --------------------------------------------------------
*/
fs.echo(Buffer.concat([PAD_START, normalVer, PAD_END]), './dist/anot.js')
log('%s 打包完成...', chalk.green('anot.js'))
/**
* --------------------------------------------------------
* 打包带触摸事件的未来版的 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')
let normalVer = Buffer.concat(libs)
let touchVer = Buffer.concat([normalVer, touchModule])
2018-08-04 16:26:50 +08:00
/**
* --------------------------------------------------------
* 打包未来版的 anot
2018-08-04 16:26:50 +08:00
* --------------------------------------------------------
*/
log('正在打包 anot.js...')
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 打包压缩完成!'))
/**
* --------------------------------------------------------
* 打包带触摸事件的未来版的 anot
2018-08-04 16:26:50 +08:00
* --------------------------------------------------------
*/
2020-07-27 15:20:22 +08:00
log('正在打包 anot.touch.js...')
let touchVerPack = Buffer.concat([PAD_START, touchVer, PAD_END]).toString()
2018-08-04 16:26:50 +08:00
fs.echo(
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
}