@@ -12,13 +12,18 @@ const checkVersion = (nuxtVersion = '', skip = false) => {
12
12
return skip || enableVersion . includes ( nuxtVersion )
13
13
}
14
14
15
+ const CORSHeaders = {
16
+ 'Access-Control-Allow-Origin' : '*'
17
+ }
18
+
15
19
/**
16
20
* Nuxt module for micro frontend using such as single-SPA or qiankun
17
21
*
18
22
* @param {Object } moduleOptions
19
23
* @param {string } [moduleOptions.path = 'mfe.js'] - the MFE lifecycle hook file path relative to rootDir
20
24
* @param {boolean } [moduleOptions.force = false] - skip version check and force to use this module
21
25
* @param {boolean } [moduleOptions.unique = false] - create a unique name scope under window in umd library
26
+ * @param {object } [moduleOptions.headers = CORSHeaders] - develop environment allow CORS for qiankun fetch resource
22
27
*/
23
28
module . exports = function ( moduleOptions ) {
24
29
const { rootDir, MFE , mode, buildDir } = this . options
@@ -31,7 +36,7 @@ module.exports = function (moduleOptions) {
31
36
32
37
const usingMFE = ! isEmpty ( options ) || MFE === true
33
38
34
- const { path = 'mfe.js' , force = false } = options
39
+ const { path = 'mfe.js' , force = false , headers = { } } = options
35
40
36
41
options . path = relativeTo ( buildDir , path )
37
42
@@ -63,6 +68,28 @@ module.exports = function (moduleOptions) {
63
68
options
64
69
} )
65
70
71
+ // set absolute resource for html entry to fetch sub application correctly
72
+ this . nuxt . hook ( 'listen' , ( server , { url = '' } ) => {
73
+ const { dev, build : { publicPath } } = this . options
74
+ url = url . endsWith ( '/' ) ? url . slice ( 0 , - 1 ) : url
75
+ if ( dev ) {
76
+ this . options . build . publicPath = `${ url } ${ publicPath } `
77
+ }
78
+ } )
79
+
80
+ // Access-Control-Allow-Origin for builded files
81
+ const devMiddlewareHeaders = this . options . build . devMiddleware . headers || { }
82
+ const fullHeaders = this . options . build . devMiddleware . headers = { ...CORSHeaders , ...headers , ...devMiddlewareHeaders }
83
+
84
+ // Access-Control-Allow-Origin specify for HOST:PORT/index.html
85
+ this . nuxt . hook ( 'render:route' , ( url , result , context ) => {
86
+ for ( const header in fullHeaders ) {
87
+ if ( Object . prototype . hasOwnProperty . call ( fullHeaders , header ) ) {
88
+ context . res . setHeader ( header , fullHeaders [ header ] )
89
+ }
90
+ }
91
+ } )
92
+
66
93
this . nuxt . hook ( 'build:templates' , ( { templatesFiles, templateVars, resolve } ) => {
67
94
templateVars = Object . assign ( templateVars , { MFE : options } )
68
95
} )
0 commit comments