EnhanceAppFilesOption.js 1.2 KB
Newer Older
U
ULIVZ 已提交
1
const fs = require('fs-extra')
2
const chalk = require('chalk')
U
ULIVZ 已提交
3
const Tapable = require('../core/Tapable')
U
ULIVZ 已提交
4
const { writeTemp } = require('../../prepare/util')
5
const { pathsToModuleCode } = require('../../prepare/codegen')
6
const logger = require('../../util/logger')
U
ULIVZ 已提交
7

U
ULIVZ 已提交
8
module.exports = class EnhanceAppFilesOption extends Tapable {
9
  tap (pluginName, value) {
10 11 12
    if (typeof value === 'function') {
      value = value()
    }
13
    super.tap(pluginName, value)
14 15
  }

U
ULIVZ 已提交
16 17 18
  async run (...args) {
    const manifest = []

19 20
    console.log(this.items)

U
ULIVZ 已提交
21 22
    // 1. write enhance app files.
    for (const [index, value] of this.items.entries()) {
23
      const { value: filepath, name: pluginName } = value
24 25
      if (fs.existsSync(filepath)) {
        const destPath = await writeTemp(
26
          `app-enhancers/enhancer-${index}.js`,
27 28 29
          `export { default } from ${JSON.stringify(filepath)}`
        )
        manifest.push(destPath)
30 31
      } else {
        logger.debug(
32
          chalk.gray(`[vuepress-plugin-${pluginName}] `) +
33 34
          `${chalk.cyan(filepath)} Not Found.`
        )
35
      }
U
ULIVZ 已提交
36 37
    }

U
ULIVZ 已提交
38
    // 2. write entry file.
39
    await writeTemp('app-enhancers.js', pathsToModuleCode(manifest))
U
ULIVZ 已提交
40 41
  }
}