6
6
7
7
const { EventEmitter } = require ( 'events' )
8
8
const { getOptions } = require ( 'loader-utils' )
9
- const { fs, path, parseFrontmatter, inferTitle, extractHeaders } = require ( '@vuepress/shared-utils' )
9
+ const { fs, path, hash , parseFrontmatter, inferTitle, extractHeaders } = require ( '@vuepress/shared-utils' )
10
10
const LRU = require ( 'lru-cache' )
11
11
const md = require ( '@vuepress/markdown' )
12
12
13
+ const cache = new LRU ( { max : 1000 } )
13
14
const devCache = new LRU ( { max : 1000 } )
14
15
15
16
/**
@@ -31,18 +32,26 @@ module.exports = function (src) {
31
32
// vue-loader, and will be applied on the same file multiple times when
32
33
// selecting the individual blocks.
33
34
const file = this . resourcePath
34
- const { content, data } = parseFrontmatter ( src )
35
+ const key = hash ( file + src )
36
+ const cached = cache . get ( key )
37
+ if ( cached && ( isProd || / \? v u e / . test ( this . resourceQuery ) ) ) {
38
+ return cached
39
+ }
40
+
41
+ const frontmatter = parseFrontmatter ( src )
42
+ const content = frontmatter . content
35
43
36
44
if ( ! isProd && ! isServer ) {
37
- const inferredTitle = inferTitle ( data , content )
45
+ const inferredTitle = inferTitle ( frontmatter . data , frontmatter . content )
38
46
const headers = extractHeaders ( content , [ 'h2' , 'h3' ] , markdown )
47
+ delete frontmatter . content
39
48
40
49
// diff frontmatter and title, since they are not going to be part of the
41
50
// returned component, changes in frontmatter do not trigger proper updates
42
51
const cachedData = devCache . get ( file )
43
52
if ( cachedData && (
44
53
cachedData . inferredTitle !== inferredTitle
45
- || JSON . stringify ( cachedData . frontmatterData ) !== JSON . stringify ( data )
54
+ || JSON . stringify ( cachedData . frontmatterData ) !== JSON . stringify ( frontmatter . data )
46
55
|| headersChanged ( cachedData . headers , headers )
47
56
) ) {
48
57
// frontmatter changed... need to do a full reload
@@ -51,7 +60,7 @@ module.exports = function (src) {
51
60
52
61
devCache . set ( file , {
53
62
headers,
54
- frontmatterData : data ,
63
+ frontmatterData : frontmatter . data ,
55
64
inferredTitle
56
65
} )
57
66
}
@@ -64,7 +73,7 @@ module.exports = function (src) {
64
73
dataBlockString
65
74
} = markdown . render ( content , {
66
75
loader,
67
- frontmatter : data ,
76
+ frontmatter : frontmatter . data ,
68
77
relativePath : path . relative ( sourceDir , file ) . replace ( / \\ / g, '/' )
69
78
} )
70
79
@@ -105,6 +114,7 @@ module.exports = function (src) {
105
114
+ ( hoistedTags || [ ] ) . join ( '\n' )
106
115
+ `\n${ dataBlockString } \n`
107
116
)
117
+ cache . set ( key , res )
108
118
return res
109
119
}
110
120
0 commit comments