3
3
fs,
4
4
chalk,
5
5
logger,
6
- codegen : { pathsToModuleCode }
6
+ codegen : { pathsToModuleCode } ,
7
+ datatypes : { isPlainObject }
7
8
} = require ( '@vuepress/shared-utils' )
8
9
9
10
module . exports = class EnhanceAppFilesOption extends Option {
@@ -36,30 +37,55 @@ module.exports = class EnhanceAppFilesOption extends Option {
36
37
const manifest = [ ]
37
38
let moduleId = 0
38
39
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
+
39
49
// 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 ) {
41
51
let destPath
42
52
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 ( / .j s $ / , '' )
58
+
59
+ if ( hasDefaultExport ( content ) ) {
60
+ destPath = await writeEnhancer ( name , content )
47
61
} else {
48
- destPath = await context . writeTemp ( `app-enhancers/ ${ name } ` , content + '\nexport default {}' )
62
+ destPath = await writeEnhancer ( name , content , false /* do not contain default export*/ )
49
63
}
64
+ // 1.2 local file
50
65
} 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
+ }
56
81
} else {
57
82
logger . debug (
58
83
chalk . gray ( `[${ pluginName } ] ` ) +
59
- `${ chalk . cyan ( filePath ) } Not Found.`
84
+ `${ chalk . cyan ( enhanceAppFile ) } Not Found.`
60
85
)
61
86
}
62
87
}
88
+
63
89
if ( destPath ) {
64
90
manifest . push ( destPath )
65
91
}
@@ -69,3 +95,7 @@ module.exports = class EnhanceAppFilesOption extends Option {
69
95
await context . writeTemp ( 'internal/app-enhancers.js' , pathsToModuleCode ( manifest ) )
70
96
}
71
97
}
98
+
99
+ function hasDefaultExport ( content ) {
100
+ return content . includes ( 'export default' ) || content . includes ( 'module.exports' )
101
+ }
0 commit comments