niucloud/admin/scripts/strip-style-imports.cjs
wangchen14709853322 5ed9a0ba81 v2.0-beta-20260626
v2框架公测版测试流程请看v2.0-beta.md
2026-07-01 12:25:30 +08:00

49 lines
1.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 从已构建 JS 中移除 element-plus style/css side-effect import
*
* assemble 最后一步兜底vite 插件可能未覆盖到的 import 语句在此二次清理。
* 也可单独运行node scripts/strip-style-imports.cjs [dist-root]
*/
const fs = require('fs')
const path = require('path')
const STYLE_IMPORT_RE = /import\s*["']element-plus[^"']*\/style\/css["'];?/g
function stripFile(filePath) {
const text = fs.readFileSync(filePath, 'utf-8')
if (!STYLE_IMPORT_RE.test(text)) return false
STYLE_IMPORT_RE.lastIndex = 0
const next = text.replace(STYLE_IMPORT_RE, '')
if (next === text) return false
fs.writeFileSync(filePath, next, 'utf-8')
return true
}
function walkJs(dir, changed) {
if (!fs.existsSync(dir)) return
for (const name of fs.readdirSync(dir)) {
const full = path.join(dir, name)
if (fs.statSync(full).isDirectory()) {
walkJs(full, changed)
} else if (name.endsWith('.js')) {
if (stripFile(full)) changed.push(full)
}
}
}
/** @param rootDir dist 根目录,递归处理 assets 下所有 .js */
function stripBuiltAssets(rootDir) {
const assetsDir = path.join(rootDir, 'assets')
const changed = []
walkJs(assetsDir, changed)
return changed
}
if (require.main === module) {
const root = process.argv[2] || path.join(__dirname, '..', 'dist')
const changed = stripBuiltAssets(root)
console.log(`[strip-style-imports] cleaned ${changed.length} files under ${root}/assets`)
}
module.exports = { stripBuiltAssets, stripFile }