12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- var UglifyJS = require('uglify-es')
- var fs = require('fs')
- const { resolve } = require('path')
- // console.log(fs.existsSync)
- let ysNameArr = [
- 'marker-direction',
- 'map-easy-insert',
- 'map-easy-client',
- 'leaflet-src',
- 'layer.yj',
- 'FormatTime',
- 'hnCityTree',
- 'MovingMarker.zt',
- 'MovingMarker',
- 'poi',
- 'realtime',
- 'tileLayer.wmts',
- 'tileLayer.wmts.poi',
- 'tileLayer.wmts.realtime',
- 'titleLayer.wmts.realtime.zt'
- ]// 需要加载的文件名
- let ysJsArr = []// 要压缩的JS文件路径数组
- let ysOutJsArr = []// 输出的JS文件路径数组
- let codeArray = []// 压缩后的代码集合
- const options = {
- //* ******配置输入输出口*********/
- Entrypath: '../public/map-easy-client', // 需要批量压缩js的文件夹入口
- Outpath: '../dist/map-easy-client' // 批量压缩后js的文件夹
- }
- init()// 开始
- //* *********函数部分***************
- // 判断输出路径是否存在
- // 遍历入口文件夹
- function startLoop () {
- ysNameArr.forEach(itemPath => {
- ysJsArr.push(resolve(__dirname, options.Entrypath, itemPath + '.js'))
- ysOutJsArr.push(resolve(__dirname, options.Outpath, itemPath + '.js'))
- })
- toUglify()
- }
- // 导入uglifyCode
- function toUglify () {
- // console.log(ysJsArr)
- ysJsArr.forEach((itemPath) => {
- let code = fs.readFileSync(itemPath, 'utf8')
- let uglifyCode = UglifyJS.minify(code).code
- codeArray.push(uglifyCode)
- })
- }
- function judgeDir () {
- let isExists = fs.existsSync(resolve(__dirname, options.Outpath))
- if (isExists) {
- console.log(`${options.Outpath}目录已存在,进入下一步`)
- } else {
- console.log('指定目录不存在,进入下一步')
- creatFolder(options.Outpath)
- }
- ysOutJsArr.forEach((itemPath, i) => {
- startCompress(itemPath, codeArray[i], i + 1)
- })
- }
- // 创建文件夹,默认dist
- function creatFolder () {
- fs.mkdir(resolve(__dirname, options.Outpath))
- console.log(`创建${options.Outpath}成功`)
- }
- // 执行写入压缩后的代码
- function startCompress (itemPath, item, n = 0) {
- console.log(itemPath)
- console.log(`开始写入第${n}条压缩后代码`)
- fs.writeFileSync(itemPath, item)
- console.log('写入代码成功')
- }
- // 初始化
- function init () {
- console.log('初始化开始')
- startLoop()
- console.log('判断输出路径是否存在')
- judgeDir()
- }
|