22
schangxiang@126.com
2025-05-19 9a8168790e0d6b8601b0f7f5557976358677eeb1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
 * 通过element plus自动的namespace进行样式分离
 * 由于目前element plus自动的namespace,无法支持treeshaking
 * 因此打包之后,再遍历dist文件夹,替换打包之后的js、css值,实现namespace最终效果
 */
/* eslint-disable no-undef */
const fs = require('fs')
const path = require('path')
 
const distPath = path.resolve(__dirname, '../dist/information-ui/es')
 
fs.readdir(distPath, function (err, files) {
  if (err) return console.error('Error:(spec)', err)
  for (const filename of files) {
    if (!filename.endsWith('js') && !filename.endsWith('.css')) continue
    const filePath = distPath + '/' + filename
 
    fs.readFile(filePath, function (err, data) {
      if (err) {
        return err
      }
      let str = data.toString()
 
      // js仅替换namespace key值即可
      if (filename.endsWith('js')) {
        str = str.replace('"el"', '"cs"')
      }
      // css需要替换所有的el-
      if (filename.endsWith('.css')) {
        str = str.replace(/el-/g, 'cs-')
      }
 
      fs.writeFile(filePath, str, function (err) {
        if (err) return err
      })
    })
  }
})