Skip to content

Commit a4ab128

Browse files
committed
Merge branch 'dev'
2 parents ea36703 + a443d34 commit a4ab128

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

README-zh.md

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ yarn add @femessage/nuxt-micro-frontend -D
4141

4242
[文档](https://github.com/lianghx-319/micro-nuxt/blob/master/lib/module.js)
4343

44+
> 推荐使用 `build.devMiddleware.headers` 属性来设置开发环境资源 http header, 可以查看 Nuxt API: The build Property > [devMiddleware](https://nuxtjs.org/api/configuration-build#devmiddleware)
45+
4446
**path**: 微前端生命周期函数定义文件,相对 rootDir 的路径
4547

4648
**force**: 强制使用模块功能,忽略 Nuxt 版本校验

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ yarn add @femessage/nuxt-micro-frontend -D
4141

4242
[Documents](https://github.com/lianghx-319/micro-nuxt/blob/master/lib/module.js)
4343

44+
> If want to set headers recommend to set `build.devMiddleware.headers`, see Nuxt API: The build Property > [devMiddleware](https://nuxtjs.org/api/configuration-build#devmiddleware)
45+
4446
**path**: the MFE lifecycle hook file path relative to rootDir
4547

4648
**force**: skip version check and force to use this module

example/nuxt.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ const { resolve } = require('path')
33
module.exports = {
44
mode: 'spa',
55
MFE: {
6-
path: 'example/mfe.js'
6+
path: 'example/mfe.js',
7+
force: true
78
},
89
rootDir: resolve(__dirname, '..'),
910
buildDir: resolve(__dirname, '.nuxt'),

lib/module.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@ const checkVersion = (nuxtVersion = '', skip = false) => {
1212
return skip || enableVersion.includes(nuxtVersion)
1313
}
1414

15+
const CORSHeaders = {
16+
'Access-Control-Allow-Origin': '*'
17+
}
18+
1519
/**
1620
* Nuxt module for micro frontend using such as single-SPA or qiankun
1721
*
1822
* @param {Object} moduleOptions
1923
* @param {string} [moduleOptions.path = 'mfe.js'] - the MFE lifecycle hook file path relative to rootDir
2024
* @param {boolean} [moduleOptions.force = false] - skip version check and force to use this module
2125
* @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
2227
*/
2328
module.exports = function (moduleOptions) {
2429
const { rootDir, MFE, mode, buildDir } = this.options
@@ -31,7 +36,7 @@ module.exports = function (moduleOptions) {
3136

3237
const usingMFE = !isEmpty(options) || MFE === true
3338

34-
const { path = 'mfe.js', force = false } = options
39+
const { path = 'mfe.js', force = false, headers = {} } = options
3540

3641
options.path = relativeTo(buildDir, path)
3742

@@ -63,6 +68,28 @@ module.exports = function (moduleOptions) {
6368
options
6469
})
6570

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+
6693
this.nuxt.hook('build:templates', ({ templatesFiles, templateVars, resolve }) => {
6794
templateVars = Object.assign(templateVars, { MFE: options })
6895
})

0 commit comments

Comments
 (0)