@honor-minigame/cli
Version:
honor minigame pack cli
141 lines (129 loc) • 4.52 kB
JavaScript
import { info } from '../../utils/logger.js'
import { get } from '../lib/manifest.js'
import * as paths from '../lib/paths.js'
import { format } from 'prettier'
import { fileURLToPath } from 'url'
import fs from 'fs-extra'
import path from 'path'
import * as babel from '@babel/core'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
// 使用babel & prettier处理代码
export default async function prettierCode(dir) {
// 默认Unity的文件以及laya的main文件进行处理,微小转换的文件进行处理
const manifest = get()
let option = {
include: /(engine\/js\/main\.js)|(wxapi-adapter\.js)|(unity_plugin(?:_\d+\.\d+\.\d+)?\.js)|(webgl\.wasm\.framework\.unityweb\.js)/
}
if ((manifest.engine && manifest.engine.includes('wx')) || manifest.isUnity) {
option = {
include: /\.js$/,
exclude: {
test: (filePath) => {
return (
filePath.includes('node_modules') ||
filePath.includes('@babel') ||
filePath.includes('laya') ||
filePath.includes('webgl.framework.js') ||
filePath.includes('unionSdk') ||
filePath.includes('unityAdapter.js') ||
filePath.includes('qgame-adapter.js') ||
filePath.includes('store.js')
)
}
}
}
}
const files = []
traverseDirSync(dir, files, option)
for (const file of files) {
// 统一使用babel-loader处理
babelCode(file, file)
// 统一使用prettier格式化
const content = fs.readFileSync(file, {
encoding: 'utf-8'
})
if (content) {
const formattedCode = await format(content, {
semi: true,
singleQuote: true,
parser: 'babel'
})
fs.outputFileSync(file, formattedCode, 'utf8')
}
}
}
/**
* 遍历目录文件 同步方法
* @param dir
* @param files 收集的文件列表
*/
const traverseDirSync = function (dir, files, options) {
const st = fs.statSync(dir)
if (st && st.isFile()) {
const isExclude = options && options.exclude && options.exclude.test(dir.replace(/\\/g, '/'))
if (!isExclude && options && options.include) {
if (options.include.test(dir.replace(/\\/g, '/'))) {
files.push(dir)
}
return
}
return
}
const list = fs.readdirSync(dir)
list.forEach(function (file) {
const isExclude = options && options.exclude && options.exclude.test(file.replace(/\\/g, '/'))
if (!isExclude) {
file = path.join(dir, file)
const stat = fs.statSync(file)
if (stat && stat.isDirectory()) {
traverseDirSync(file, files, options)
} else {
if (options && options.include) {
if (options.include.test(file.replace(/\\/g, '/'))) {
files.push(file)
}
return
}
files.push(file)
}
}
})
}
function babelCode(filePath) {
const code = fs.readFileSync(filePath, 'utf8')
try {
// 检查代码中是否有严苛模式以及@babel/的标记,如果有则不进行babel转换
if (code.includes('use strict') && code.includes('@babel/')) {
info(`[babel] babel not effective, ${filePath.replace(paths.PROJECT_PATH, '')}`)
return
}
// 将文件缓存到build_temp目录下
if (filePath.startsWith(paths.PROJECT_PATH)) {
const savePath = filePath.replace(paths.PROJECT_PATH, paths.BUILDTEMP)
if (!fs.existsSync(path.dirname(savePath))) {
fs.mkdirSync(path.dirname(savePath), { recursive: true })
}
fs.outputFileSync(savePath, code)
}
const result = babel.transformSync(code, {
filename: path.basename(filePath),
cwd: __dirname, // 设置工作目录
presets: ['@babel/preset-env'],
plugins: [
'@babel/plugin-transform-modules-commonjs',
'@babel/plugin-transform-optional-chaining',
'@babel/plugin-transform-nullish-coalescing-operator'
],
compact: false
})
// babel将src下内容转换下,到dist下
if (!fs.existsSync(path.dirname(filePath))) {
fs.mkdirSync(path.dirname(filePath), { recursive: true })
}
fs.outputFileSync(filePath, result.code)
info(`[babel] success ${filePath.replace(paths.PROJECT_PATH, '')}`)
} catch (e) {
info(`[babel] error ${filePath.replace(paths.PROJECT_PATH, '')}`)
}
}