@@ -7,6 +7,7 @@ import type {
7
7
} from '@docusaurus/types' ;
8
8
import { normalizeUrl } from '@docusaurus/utils' ;
9
9
import { loadAndBundleSpec } from 'redoc' ;
10
+ import type { OpenAPISpec } from 'redoc/typings/types' ;
10
11
import {
11
12
formatProblems ,
12
13
getTotals ,
@@ -31,7 +32,10 @@ export { PluginOptions };
31
32
export default function redocPlugin (
32
33
context : LoadContext ,
33
34
opts : PluginOptions ,
34
- ) : Plugin < Record < string , unknown > > {
35
+ ) : Plugin < {
36
+ converted : OpenAPISpec ;
37
+ bundle ?: Record < string , unknown > ;
38
+ } > {
35
39
const { baseUrl } = context . siteConfig ;
36
40
const options : PluginOptionsWithDefault = { ...DEFAULT_OPTIONS , ...opts } ;
37
41
const { debug, spec, url : downloadUrl , config } = options ;
@@ -56,7 +60,10 @@ export default function redocPlugin(
56
60
if ( debug ) {
57
61
console . log ( '[REDOCUSAURUS_PLUGIN] bundling spec from url' , spec ) ;
58
62
}
59
- return loadAndBundleSpec ( spec ! ) ;
63
+ const converted = await loadAndBundleSpec ( spec ! ) ;
64
+ return {
65
+ converted,
66
+ } ;
60
67
}
61
68
62
69
// If local file
@@ -93,21 +100,26 @@ export default function redocPlugin(
93
100
if ( debug ) {
94
101
console . log ( '[REDOCUSAURUS_PLUGIN] File Bundled' ) ;
95
102
}
103
+ const converted = await loadAndBundleSpec ( bundledSpec . parsed ) ;
96
104
97
105
// If download url is not provided then use bundled yaml as a static file (see `postBuild`)
98
106
url = url || fileName ;
99
- return bundledSpec . parsed ;
107
+
108
+ return {
109
+ converted,
110
+ bundle : bundledSpec . parsed ,
111
+ } ;
100
112
} ,
101
113
async contentLoaded ( { content, actions } ) {
102
114
const { createData, addRoute, setGlobalData } = actions ;
103
- if ( ! content ) {
115
+ if ( ! content ?. converted ) {
104
116
throw new Error ( `[Redocusaurus] Spec could not be parsed: ${ spec } ` ) ;
105
117
}
106
118
107
119
const data : SpecProps = {
108
120
url,
109
121
// eslint-disable-next-line @typescript-eslint/no-explicit-any
110
- spec : content as any ,
122
+ spec : content . converted as any ,
111
123
} ;
112
124
setGlobalData ( data ) ;
113
125
@@ -142,19 +154,24 @@ export default function redocPlugin(
142
154
addRoute ( routeOptions ) ;
143
155
}
144
156
} ,
145
- async postBuild ( props ) {
157
+ async postBuild ( { content } ) {
146
158
if ( ! isSpecFile || downloadUrl ) {
147
159
return ;
148
160
}
149
161
// Create a static file from bundled spec
150
162
const staticFile = path . join ( context . outDir , fileName ) ;
151
- fs . mkdirSync ( path . dirname ( staticFile ) ) ;
152
- console . error (
153
- '[REDOCUSAURUS_PLUGIN] creating static bundle copy for download' ,
154
- staticFile ,
155
- ) ;
163
+ const dir = path . dirname ( staticFile ) ;
164
+ if ( ! fs . existsSync ( dir ) ) {
165
+ fs . mkdirSync ( dir ) ;
166
+ }
167
+ if ( debug ) {
168
+ console . error (
169
+ '[REDOCUSAURUS_PLUGIN] creating static bundle copy for download' ,
170
+ staticFile ,
171
+ ) ;
172
+ }
156
173
// create bundled url
157
- const bundledYaml = stringifyYaml ( props . content ) ;
174
+ const bundledYaml = stringifyYaml ( content . bundle || content . converted ) ;
158
175
fs . writeFileSync ( staticFile , bundledYaml ) ;
159
176
} ,
160
177
getPathsToWatch ( ) {
0 commit comments