Skip to content

Commit eff7949

Browse files
committed
feat($core): enhanceAppFile doesn't need export default manually.
1 parent ea64033 commit eff7949

File tree

1 file changed

+43
-13
lines changed

1 file changed

+43
-13
lines changed

packages/@vuepress/core/lib/plugin-api/option/EnhanceAppFilesOption.js

+43-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ const {
33
fs,
44
chalk,
55
logger,
6-
codegen: { pathsToModuleCode }
6+
codegen: { pathsToModuleCode },
7+
datatypes: { isPlainObject }
78
} = require('@vuepress/shared-utils')
89

910
module.exports = class EnhanceAppFilesOption extends Option {
@@ -36,30 +37,55 @@ module.exports = class EnhanceAppFilesOption extends Option {
3637
const manifest = []
3738
let moduleId = 0
3839

40+
async function writeEnhancer (name, content, hasDefaultExport = true) {
41+
return await context.writeTemp(
42+
`app-enhancers/${name}.js`,
43+
hasDefaultExport
44+
? content
45+
: content + '\nexport default {}'
46+
)
47+
}
48+
3949
// 1. write enhance app files.
40-
for (const { value: filePath, name: pluginName } of this.items) {
50+
for (const { value: enhanceAppFile, name: pluginName } of this.items) {
4151
let destPath
4252

43-
if (typeof filePath === 'object') {
44-
const { name, content } = filePath
45-
if (content.includes('export default') || content.includes('module.exports')) {
46-
destPath = await context.writeTemp(`app-enhancers/${name}`, content)
53+
// 1.1 dynamic code
54+
if (isPlainObject(enhanceAppFile)) {
55+
const { content } = enhanceAppFile
56+
let { name } = enhanceAppFile
57+
name = name.replace(/.js$/, '')
58+
59+
if (hasDefaultExport(content)) {
60+
destPath = await writeEnhancer(name, content)
4761
} else {
48-
destPath = await context.writeTemp(`app-enhancers/${name}`, content + '\nexport default {}')
62+
destPath = await writeEnhancer(name, content, false /* do not contain default export*/)
4963
}
64+
// 1.2 local file
5065
} else {
51-
if (fs.existsSync(filePath)) {
52-
destPath = await context.writeTemp(
53-
`app-enhancers/enhancer-${moduleId++}.js`,
54-
`export { default } from ${JSON.stringify(filePath)}`
55-
)
66+
if (fs.existsSync(enhanceAppFile)) {
67+
const content = await fs.readFile(enhanceAppFile, 'utf-8')
68+
69+
if (hasDefaultExport(content)) {
70+
destPath = await writeEnhancer(
71+
moduleId++,
72+
`export { default } from ${JSON.stringify(enhanceAppFile)}`
73+
)
74+
} else {
75+
destPath = await writeEnhancer(
76+
moduleId++,
77+
`import ${JSON.stringify(enhanceAppFile)}`,
78+
false /* do not contain default export*/
79+
)
80+
}
5681
} else {
5782
logger.debug(
5883
chalk.gray(`[${pluginName}] `) +
59-
`${chalk.cyan(filePath)} Not Found.`
84+
`${chalk.cyan(enhanceAppFile)} Not Found.`
6085
)
6186
}
6287
}
88+
6389
if (destPath) {
6490
manifest.push(destPath)
6591
}
@@ -69,3 +95,7 @@ module.exports = class EnhanceAppFilesOption extends Option {
6995
await context.writeTemp('internal/app-enhancers.js', pathsToModuleCode(manifest))
7096
}
7197
}
98+
99+
function hasDefaultExport (content) {
100+
return content.includes('export default') || content.includes('module.exports')
101+
}

0 commit comments

Comments
 (0)