@meethive/vite 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{src/federation/src/runtime/dynamic-remote.d.ts → dynamic-remote.d.ts} +25 -19
- package/dist/dynamic-remote.js +185 -0
- package/dist/dynamic-remote.mjs +163 -0
- package/dist/index.d.ts +51 -4
- package/dist/index.js +1 -20
- package/dist/index.mjs +1 -20
- package/dist/{src/federation/src/utils/semver/satisfy.d.ts → satisfy.d.ts} +2 -1
- package/dist/satisfy.js +381 -0
- package/dist/satisfy.mjs +381 -0
- package/package.json +20 -4
- package/dist/index.d.ts.map +0 -1
- package/dist/src/federation/src/dev/expose-development.d.ts +0 -5
- package/dist/src/federation/src/dev/expose-development.d.ts.map +0 -1
- package/dist/src/federation/src/dev/remote-development.d.ts +0 -5
- package/dist/src/federation/src/dev/remote-development.d.ts.map +0 -1
- package/dist/src/federation/src/dev/shared-development.d.ts +0 -5
- package/dist/src/federation/src/dev/shared-development.d.ts.map +0 -1
- package/dist/src/federation/src/index.d.ts +0 -7
- package/dist/src/federation/src/index.d.ts.map +0 -1
- package/dist/src/federation/src/prod/expose-production.d.ts +0 -5
- package/dist/src/federation/src/prod/expose-production.d.ts.map +0 -1
- package/dist/src/federation/src/prod/remote-production.d.ts +0 -7
- package/dist/src/federation/src/prod/remote-production.d.ts.map +0 -1
- package/dist/src/federation/src/prod/shared-production.d.ts +0 -5
- package/dist/src/federation/src/prod/shared-production.d.ts.map +0 -1
- package/dist/src/federation/src/public.d.ts +0 -40
- package/dist/src/federation/src/public.d.ts.map +0 -1
- package/dist/src/federation/src/runtime/dynamic-remote.d.ts.map +0 -1
- package/dist/src/federation/src/utils/html.d.ts +0 -12
- package/dist/src/federation/src/utils/html.d.ts.map +0 -1
- package/dist/src/federation/src/utils/index.d.ts +0 -29
- package/dist/src/federation/src/utils/index.d.ts.map +0 -1
- package/dist/src/federation/src/utils/semver/compare.d.ts +0 -10
- package/dist/src/federation/src/utils/semver/compare.d.ts.map +0 -1
- package/dist/src/federation/src/utils/semver/constants.d.ts +0 -11
- package/dist/src/federation/src/utils/semver/constants.d.ts.map +0 -1
- package/dist/src/federation/src/utils/semver/parser.d.ts +0 -10
- package/dist/src/federation/src/utils/semver/parser.d.ts.map +0 -1
- package/dist/src/federation/src/utils/semver/satisfy.d.ts.map +0 -1
- package/dist/src/federation/src/utils/semver/utils.d.ts +0 -12
- package/dist/src/federation/src/utils/semver/utils.d.ts.map +0 -1
- package/dist/src/monaco-editor/index.d.ts +0 -35
- package/dist/src/monaco-editor/index.d.ts.map +0 -1
- package/dist/src/monaco-editor/languageWork.d.ts +0 -10
- package/dist/src/monaco-editor/languageWork.d.ts.map +0 -1
- package/dist/src/monaco-editor/workerMiddleware.d.ts +0 -9
- package/dist/src/monaco-editor/workerMiddleware.d.ts.map +0 -1
- package/dist/src/sharp/index.d.ts +0 -12
- package/dist/src/sharp/index.d.ts.map +0 -1
- package/index.ts +0 -3
- package/src/federation/src/dev/expose-development.ts +0 -29
- package/src/federation/src/dev/remote-development.ts +0 -435
- package/src/federation/src/dev/shared-development.ts +0 -29
- package/src/federation/src/index.ts +0 -242
- package/src/federation/src/prod/expose-production.ts +0 -333
- package/src/federation/src/prod/federation_fn_import.js +0 -75
- package/src/federation/src/prod/remote-production.ts +0 -658
- package/src/federation/src/prod/shared-production.ts +0 -268
- package/src/federation/src/public.ts +0 -54
- package/src/federation/src/runtime/dynamic-remote.ts +0 -247
- package/src/federation/src/utils/html.ts +0 -165
- package/src/federation/src/utils/index.ts +0 -255
- package/src/federation/src/utils/semver/compare.ts +0 -131
- package/src/federation/src/utils/semver/constants.ts +0 -46
- package/src/federation/src/utils/semver/parser.ts +0 -253
- package/src/federation/src/utils/semver/satisfy.ts +0 -151
- package/src/federation/src/utils/semver/utils.ts +0 -93
- package/src/federation/types/dynamic-remote.d.ts +0 -105
- package/src/federation/types/index.d.ts +0 -344
- package/src/federation/types/pluginHooks.d.ts +0 -4
- package/src/federation/types/virtual-modules.d.ts +0 -48
- package/src/federation/types/viteDevServer.d.ts +0 -22
- package/src/monaco-editor/index.ts +0 -205
- package/src/monaco-editor/languageWork.ts +0 -36
- package/src/monaco-editor/workerMiddleware.ts +0 -78
- package/src/sharp/index.ts +0 -93
|
@@ -1,242 +0,0 @@
|
|
|
1
|
-
// *****************************************************************************
|
|
2
|
-
// Copyright (C) 2022 Origin.js and others.
|
|
3
|
-
//
|
|
4
|
-
// This program and the accompanying materials are licensed under Mulan PSL v2.
|
|
5
|
-
// You can use this software according to the terms and conditions of the Mulan PSL v2.
|
|
6
|
-
// You may obtain a copy of Mulan PSL v2 at:
|
|
7
|
-
// http://license.coscl.org.cn/MulanPSL2
|
|
8
|
-
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
|
9
|
-
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
|
10
|
-
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
|
11
|
-
// See the Mulan PSL v2 for more details.
|
|
12
|
-
//
|
|
13
|
-
// SPDX-License-Identifier: MulanPSL-2.0
|
|
14
|
-
// *****************************************************************************
|
|
15
|
-
|
|
16
|
-
import type {
|
|
17
|
-
ConfigEnv,
|
|
18
|
-
Plugin,
|
|
19
|
-
UserConfig,
|
|
20
|
-
ViteDevServer,
|
|
21
|
-
ResolvedConfig
|
|
22
|
-
} from 'vite'
|
|
23
|
-
import virtual from '@rollup/plugin-virtual'
|
|
24
|
-
import { dirname, isAbsolute, relative, resolve } from 'path'
|
|
25
|
-
import { prodRemotePlugin } from './prod/remote-production'
|
|
26
|
-
import type { VitePluginFederationOptions } from '../types'
|
|
27
|
-
import { builderInfo, DEFAULT_ENTRY_FILENAME, parsedOptions } from './public'
|
|
28
|
-
import type { PluginHooks } from '../types/pluginHooks'
|
|
29
|
-
import type { ModuleInfo } from 'rollup'
|
|
30
|
-
import { prodSharedPlugin } from './prod/shared-production'
|
|
31
|
-
import { prodExposePlugin } from './prod/expose-production'
|
|
32
|
-
import { devSharedPlugin } from './dev/shared-development'
|
|
33
|
-
import { devRemotePlugin } from './dev/remote-development'
|
|
34
|
-
import { devExposePlugin } from './dev/expose-development'
|
|
35
|
-
|
|
36
|
-
const normalizeAssetsDir = (config?: UserConfig) => {
|
|
37
|
-
const configured = config?.build?.assetsDir ?? 'assets'
|
|
38
|
-
if (!configured) return ''
|
|
39
|
-
if (!isAbsolute(configured)) return configured
|
|
40
|
-
const root = config?.root ? resolve(config.root) : process.cwd()
|
|
41
|
-
const resolvedOutDir = config?.build?.outDir
|
|
42
|
-
? (isAbsolute(config.build.outDir)
|
|
43
|
-
? config.build.outDir
|
|
44
|
-
: resolve(root, config.build.outDir))
|
|
45
|
-
: resolve(root, 'dist')
|
|
46
|
-
const normalized = relative(resolvedOutDir, configured).replace(/\\/g, '/')
|
|
47
|
-
return normalized.startsWith('..') ? '' : normalized || 'assets'
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export { VitePluginFederationVersion } from './public'
|
|
51
|
-
export * from './runtime/dynamic-remote'
|
|
52
|
-
export default function federation(
|
|
53
|
-
options: VitePluginFederationOptions
|
|
54
|
-
): Plugin {
|
|
55
|
-
options.filename = options.filename
|
|
56
|
-
? options.filename
|
|
57
|
-
: DEFAULT_ENTRY_FILENAME
|
|
58
|
-
|
|
59
|
-
let pluginList: PluginHooks[] = []
|
|
60
|
-
let virtualMod
|
|
61
|
-
let registerCount = 0
|
|
62
|
-
|
|
63
|
-
function registerPlugins(mode: string, command: string) {
|
|
64
|
-
if (mode === 'production' || command === 'build') {
|
|
65
|
-
pluginList = [
|
|
66
|
-
prodSharedPlugin(options),
|
|
67
|
-
prodExposePlugin(options),
|
|
68
|
-
prodRemotePlugin(options)
|
|
69
|
-
]
|
|
70
|
-
} else if (mode === 'development' || command === 'serve') {
|
|
71
|
-
pluginList = [
|
|
72
|
-
devSharedPlugin(options),
|
|
73
|
-
devExposePlugin(options),
|
|
74
|
-
devRemotePlugin(options)
|
|
75
|
-
]
|
|
76
|
-
} else {
|
|
77
|
-
pluginList = []
|
|
78
|
-
}
|
|
79
|
-
builderInfo.isHost = options.isHost || !!(
|
|
80
|
-
parsedOptions.prodRemote.length || parsedOptions.devRemote.length
|
|
81
|
-
)
|
|
82
|
-
builderInfo.isRemote = !!(
|
|
83
|
-
parsedOptions.prodExpose.length || parsedOptions.devExpose.length
|
|
84
|
-
)
|
|
85
|
-
builderInfo.isShared = !!(
|
|
86
|
-
parsedOptions.prodShared.length || parsedOptions.devShared.length
|
|
87
|
-
)
|
|
88
|
-
|
|
89
|
-
let virtualFiles = {}
|
|
90
|
-
pluginList.forEach((plugin) => {
|
|
91
|
-
if (plugin.virtualFile) {
|
|
92
|
-
virtualFiles = Object.assign(virtualFiles, plugin.virtualFile)
|
|
93
|
-
}
|
|
94
|
-
})
|
|
95
|
-
virtualMod = virtual(virtualFiles)
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
return {
|
|
99
|
-
name: 'originjs:federation',
|
|
100
|
-
// for scenario vite.config.js build.cssCodeSplit: false
|
|
101
|
-
// vite:css-post plugin will summarize all the styles in the style.xxxxxx.css file
|
|
102
|
-
// so, this plugin need run after vite:css-post in post plugin list
|
|
103
|
-
enforce: 'post',
|
|
104
|
-
// apply:'build',
|
|
105
|
-
options(_options) {
|
|
106
|
-
// rollup doesnt has options.mode and options.command
|
|
107
|
-
if (!registerCount++) {
|
|
108
|
-
registerPlugins((options.mode = options.mode ?? 'production'), '')
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
if (typeof _options.input === 'string') {
|
|
112
|
-
_options.input = { index: _options.input }
|
|
113
|
-
}
|
|
114
|
-
_options.external = _options.external || []
|
|
115
|
-
if (!Array.isArray(_options.external)) {
|
|
116
|
-
_options.external = [_options.external as string]
|
|
117
|
-
}
|
|
118
|
-
for (const pluginHook of pluginList) {
|
|
119
|
-
// @ts-ignore
|
|
120
|
-
pluginHook.options?.call(this, _options)
|
|
121
|
-
}
|
|
122
|
-
return _options
|
|
123
|
-
},
|
|
124
|
-
config(config: UserConfig, env: ConfigEnv) {
|
|
125
|
-
options.mode = options.mode ?? env.mode
|
|
126
|
-
registerPlugins(options.mode, env.command)
|
|
127
|
-
registerCount++
|
|
128
|
-
for (const pluginHook of pluginList) {
|
|
129
|
-
// @ts-ignores
|
|
130
|
-
pluginHook.config?.call(this, config, env)
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// only run when builder is vite,rollup doesnt has hook named `config`
|
|
134
|
-
builderInfo.builder = 'vite'
|
|
135
|
-
builderInfo.assetsDir = normalizeAssetsDir(config) ?? 'assets'
|
|
136
|
-
},
|
|
137
|
-
configureServer(server: ViteDevServer) {
|
|
138
|
-
for (const pluginHook of pluginList) {
|
|
139
|
-
// @ts-ignore
|
|
140
|
-
pluginHook.configureServer?.call(this, server)
|
|
141
|
-
}
|
|
142
|
-
},
|
|
143
|
-
configResolved(config: ResolvedConfig) {
|
|
144
|
-
for (const pluginHook of pluginList) {
|
|
145
|
-
// @ts-ignore
|
|
146
|
-
pluginHook.configResolved?.call(this, config)
|
|
147
|
-
}
|
|
148
|
-
},
|
|
149
|
-
buildStart(inputOptions) {
|
|
150
|
-
for (const pluginHook of pluginList) {
|
|
151
|
-
// @ts-ignore
|
|
152
|
-
pluginHook.buildStart?.call(this, inputOptions)
|
|
153
|
-
}
|
|
154
|
-
},
|
|
155
|
-
|
|
156
|
-
async resolveId(...args) {
|
|
157
|
-
const v = virtualMod.resolveId.call(this, ...args)
|
|
158
|
-
if (v) {
|
|
159
|
-
return v
|
|
160
|
-
}
|
|
161
|
-
if (args[0] === '\0virtual:__federation_fn_import') {
|
|
162
|
-
return {
|
|
163
|
-
id: '\0virtual:__federation_fn_import',
|
|
164
|
-
moduleSideEffects: true
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
if (args[0] === '__federation_fn_satisfy') {
|
|
168
|
-
const federationId = (
|
|
169
|
-
await this.resolve('@meethive/vite')
|
|
170
|
-
)?.id
|
|
171
|
-
if (federationId) {
|
|
172
|
-
return await this.resolve(`${dirname(federationId)}/satisfy.mjs`)
|
|
173
|
-
}
|
|
174
|
-
return null
|
|
175
|
-
}
|
|
176
|
-
if (args[0] === 'virtual:__federation__') {
|
|
177
|
-
return {
|
|
178
|
-
id: '\0virtual:__federation__',
|
|
179
|
-
moduleSideEffects: true
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
return null
|
|
183
|
-
},
|
|
184
|
-
|
|
185
|
-
load(...args) {
|
|
186
|
-
const v = virtualMod.load.call(this, ...args)
|
|
187
|
-
if (v) {
|
|
188
|
-
return v
|
|
189
|
-
}
|
|
190
|
-
return null
|
|
191
|
-
},
|
|
192
|
-
|
|
193
|
-
transform(code: string, id: string) {
|
|
194
|
-
for (const pluginHook of pluginList) {
|
|
195
|
-
// @ts-ignore
|
|
196
|
-
const result = pluginHook.transform?.call(this, code, id)
|
|
197
|
-
if (result) {
|
|
198
|
-
return result
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
return code
|
|
202
|
-
},
|
|
203
|
-
moduleParsed(moduleInfo: ModuleInfo): void {
|
|
204
|
-
for (const pluginHook of pluginList) {
|
|
205
|
-
// @ts-ignore
|
|
206
|
-
pluginHook.moduleParsed?.call(this, moduleInfo)
|
|
207
|
-
}
|
|
208
|
-
},
|
|
209
|
-
|
|
210
|
-
outputOptions(outputOptions) {
|
|
211
|
-
// @ts-ignore
|
|
212
|
-
for (const pluginHook of pluginList) {
|
|
213
|
-
// @ts-ignore
|
|
214
|
-
pluginHook.outputOptions?.call(this, outputOptions)
|
|
215
|
-
}
|
|
216
|
-
return outputOptions
|
|
217
|
-
},
|
|
218
|
-
|
|
219
|
-
renderChunk(code, chunkInfo, _options) {
|
|
220
|
-
for (const pluginHook of pluginList) {
|
|
221
|
-
// @ts-ignore
|
|
222
|
-
const result = pluginHook.renderChunk?.call(
|
|
223
|
-
this,
|
|
224
|
-
code,
|
|
225
|
-
chunkInfo,
|
|
226
|
-
_options
|
|
227
|
-
)
|
|
228
|
-
if (result) {
|
|
229
|
-
return result
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
return null
|
|
233
|
-
},
|
|
234
|
-
|
|
235
|
-
generateBundle: function (_options, bundle, isWrite) {
|
|
236
|
-
for (const pluginHook of pluginList) {
|
|
237
|
-
// @ts-ignore
|
|
238
|
-
pluginHook.generateBundle?.call(this, _options, bundle, isWrite)
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
}
|
|
@@ -1,333 +0,0 @@
|
|
|
1
|
-
// *****************************************************************************
|
|
2
|
-
// Copyright (C) 2022 Origin.js and others.
|
|
3
|
-
//
|
|
4
|
-
// This program and the accompanying materials are licensed under Mulan PSL v2.
|
|
5
|
-
// You can use this software according to the terms and conditions of the Mulan PSL v2.
|
|
6
|
-
// You may obtain a copy of Mulan PSL v2 at:
|
|
7
|
-
// http://license.coscl.org.cn/MulanPSL2
|
|
8
|
-
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
|
9
|
-
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
|
10
|
-
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
|
11
|
-
// See the Mulan PSL v2 for more details.
|
|
12
|
-
//
|
|
13
|
-
// SPDX-License-Identifier: MulanPSL-2.0
|
|
14
|
-
// *****************************************************************************
|
|
15
|
-
|
|
16
|
-
import { resolve, parse, basename, extname, relative, dirname } from 'path'
|
|
17
|
-
import {
|
|
18
|
-
getModuleMarker,
|
|
19
|
-
normalizePath,
|
|
20
|
-
parseExposeOptions,
|
|
21
|
-
removeNonRegLetter,
|
|
22
|
-
NAME_CHAR_REG
|
|
23
|
-
} from '../utils'
|
|
24
|
-
import {
|
|
25
|
-
builderInfo,
|
|
26
|
-
DYNAMIC_LOADING_CSS,
|
|
27
|
-
DYNAMIC_LOADING_CSS_PREFIX,
|
|
28
|
-
EXPOSES_MAP,
|
|
29
|
-
EXPOSES_KEY_MAP,
|
|
30
|
-
EXTERNALS,
|
|
31
|
-
parsedOptions,
|
|
32
|
-
SHARED,
|
|
33
|
-
viteConfigResolved
|
|
34
|
-
} from '../public'
|
|
35
|
-
// @ts-ignore
|
|
36
|
-
import type { AcornNode, OutputAsset, OutputChunk } from 'rollup'
|
|
37
|
-
import type { VitePluginFederationOptions } from '../../types'
|
|
38
|
-
import type { PluginHooks } from '../../types/pluginHooks'
|
|
39
|
-
import MagicString from 'magic-string'
|
|
40
|
-
import { walk } from 'estree-walker'
|
|
41
|
-
import type { ResolvedConfig } from 'vite'
|
|
42
|
-
|
|
43
|
-
export function prodExposePlugin(
|
|
44
|
-
options: VitePluginFederationOptions
|
|
45
|
-
): PluginHooks {
|
|
46
|
-
let moduleMap = ''
|
|
47
|
-
const hasOptions = parsedOptions.prodExpose.some((expose) => {
|
|
48
|
-
return expose[0] === parseExposeOptions(options)[0]?.[0]
|
|
49
|
-
})
|
|
50
|
-
if (!hasOptions) {
|
|
51
|
-
parsedOptions.prodExpose = Array.prototype.concat(
|
|
52
|
-
parsedOptions.prodExpose,
|
|
53
|
-
parseExposeOptions(options)
|
|
54
|
-
)
|
|
55
|
-
}
|
|
56
|
-
// exposes module
|
|
57
|
-
for (const item of parseExposeOptions(options)) {
|
|
58
|
-
const moduleName = getModuleMarker(`\${${item[0]}}`, SHARED)
|
|
59
|
-
EXTERNALS.push(moduleName)
|
|
60
|
-
const exposeFilepath = normalizePath(resolve(item[1].import))
|
|
61
|
-
EXPOSES_MAP.set(item[0], exposeFilepath)
|
|
62
|
-
EXPOSES_KEY_MAP.set(
|
|
63
|
-
item[0],
|
|
64
|
-
`__federation_expose_${removeNonRegLetter(item[0], NAME_CHAR_REG)}`
|
|
65
|
-
)
|
|
66
|
-
moduleMap += `\n"${item[0]}":()=>{
|
|
67
|
-
${DYNAMIC_LOADING_CSS}('${DYNAMIC_LOADING_CSS_PREFIX}${exposeFilepath}', ${item[1].dontAppendStylesToHead}, '${item[0]}')
|
|
68
|
-
return __federation_import('\${__federation_expose_${item[0]}}').then(module =>Object.keys(module).every(item => exportSet.has(item)) ? () => module.default : () => module)},`
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// let viteConfigResolved: ResolvedConfig
|
|
72
|
-
|
|
73
|
-
return {
|
|
74
|
-
name: 'originjs:expose-production',
|
|
75
|
-
virtualFile: {
|
|
76
|
-
// code generated for remote
|
|
77
|
-
// language=JS
|
|
78
|
-
[`__remoteEntryHelper__${options.filename}`]: `
|
|
79
|
-
const currentImports = {}
|
|
80
|
-
const exportSet = new Set(['Module', '__esModule', 'default', '_export_sfc']);
|
|
81
|
-
let moduleMap = {${moduleMap}}
|
|
82
|
-
const seen = {}
|
|
83
|
-
export const ${DYNAMIC_LOADING_CSS} = (cssFilePaths, dontAppendStylesToHead, exposeItemName) => {
|
|
84
|
-
const metaUrl = import.meta.url;
|
|
85
|
-
if (typeof metaUrl === 'undefined') {
|
|
86
|
-
console.warn('The remote style takes effect only when the build.target option in the vite.config.ts file is higher than that of "es2020".');
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const curUrl = metaUrl.substring(0, metaUrl.lastIndexOf('${options.filename}'));
|
|
91
|
-
const base = __VITE_BASE_PLACEHOLDER__;
|
|
92
|
-
const assetsDir = __VITE_ASSETS_DIR_PLACEHOLDER__;
|
|
93
|
-
|
|
94
|
-
cssFilePaths.forEach(cssPath => {
|
|
95
|
-
let href = '';
|
|
96
|
-
const baseUrl = base || curUrl;
|
|
97
|
-
if (baseUrl) {
|
|
98
|
-
const trimmer = {
|
|
99
|
-
trailing: (path) => (path.endsWith('/') ? path.slice(0, -1) : path),
|
|
100
|
-
leading: (path) => (path.startsWith('/') ? path.slice(1) : path)
|
|
101
|
-
}
|
|
102
|
-
const isAbsoluteUrl = (url) => url.startsWith('http') || url.startsWith('//');
|
|
103
|
-
|
|
104
|
-
const cleanBaseUrl = trimmer.trailing(baseUrl).replace('.', '');
|
|
105
|
-
const cleanAssetsDir = trimmer.leading(assetsDir);
|
|
106
|
-
const cleanCssPath = trimmer.leading(cssPath);
|
|
107
|
-
const cleanCurUrl = trimmer.trailing(curUrl);
|
|
108
|
-
|
|
109
|
-
if (isAbsoluteUrl(baseUrl)) {
|
|
110
|
-
href = [cleanBaseUrl, cleanAssetsDir, cleanCssPath].filter(Boolean).join('/');
|
|
111
|
-
} else {
|
|
112
|
-
// 当remoteEntry.js和CSS都在assets目录下时,使用相对路径
|
|
113
|
-
const remoteEntryInAssets = cleanCurUrl.endsWith('/assets/') || cleanCurUrl.endsWith('/assets');
|
|
114
|
-
|
|
115
|
-
if (remoteEntryInAssets) {
|
|
116
|
-
// 都在assets目录下,直接使用CSS文件名
|
|
117
|
-
href = [cleanCurUrl, cleanCssPath].filter(Boolean).join('/');
|
|
118
|
-
} else if (cleanCurUrl.includes(cleanBaseUrl)) {
|
|
119
|
-
href = [cleanCurUrl, cleanAssetsDir, cleanCssPath].filter(Boolean).join('/');
|
|
120
|
-
} else {
|
|
121
|
-
href = [cleanCurUrl + cleanBaseUrl, cleanAssetsDir, cleanCssPath].filter(Boolean).join('/');
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
} else {
|
|
125
|
-
href = cssPath;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
if (dontAppendStylesToHead) {
|
|
129
|
-
const key = 'css__${options.name}__' + exposeItemName;
|
|
130
|
-
window[key] = window[key] || [];
|
|
131
|
-
window[key].push(href);
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
if (href in seen) return;
|
|
136
|
-
seen[href] = true;
|
|
137
|
-
|
|
138
|
-
const element = document.createElement('link');
|
|
139
|
-
element.rel = 'stylesheet';
|
|
140
|
-
element.href = href;
|
|
141
|
-
document.head.appendChild(element);
|
|
142
|
-
});
|
|
143
|
-
};
|
|
144
|
-
async function __federation_import(name) {
|
|
145
|
-
currentImports[name] ??= import(name)
|
|
146
|
-
return currentImports[name]
|
|
147
|
-
};
|
|
148
|
-
export const get =(module) => {
|
|
149
|
-
if(!moduleMap[module]) throw new Error('Can not find remote module ' + module)
|
|
150
|
-
return moduleMap[module]();
|
|
151
|
-
};
|
|
152
|
-
export const init =(shareScope) => {
|
|
153
|
-
globalThis.__federation_shared__= globalThis.__federation_shared__|| {};
|
|
154
|
-
Object.entries(shareScope).forEach(([key, value]) => {
|
|
155
|
-
for (const [versionKey, versionValue] of Object.entries(value)) {
|
|
156
|
-
const scope = versionValue.scope || 'default'
|
|
157
|
-
globalThis.__federation_shared__[scope] = globalThis.__federation_shared__[scope] || {};
|
|
158
|
-
const shared= globalThis.__federation_shared__[scope];
|
|
159
|
-
(shared[key] = shared[key]||{})[versionKey] = versionValue;
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
}`
|
|
163
|
-
},
|
|
164
|
-
|
|
165
|
-
configResolved(config: ResolvedConfig) {
|
|
166
|
-
if (config) {
|
|
167
|
-
viteConfigResolved.config = config
|
|
168
|
-
}
|
|
169
|
-
},
|
|
170
|
-
|
|
171
|
-
buildStart() {
|
|
172
|
-
// if we don't expose any modules, there is no need to emit file
|
|
173
|
-
if (parsedOptions.prodExpose.length > 0) {
|
|
174
|
-
const assetsDir = builderInfo.assetsDir
|
|
175
|
-
this.emitFile({
|
|
176
|
-
fileName: assetsDir ? `${assetsDir}/${options.filename}` : options.filename ,
|
|
177
|
-
type: 'chunk',
|
|
178
|
-
id: `__remoteEntryHelper__${options.filename}`,
|
|
179
|
-
preserveSignature: 'strict'
|
|
180
|
-
})
|
|
181
|
-
}
|
|
182
|
-
},
|
|
183
|
-
|
|
184
|
-
generateBundle(_options, bundle) {
|
|
185
|
-
// replace import absolute path to chunk's fileName in remoteEntry.js
|
|
186
|
-
let remoteEntryChunk
|
|
187
|
-
for (const file in bundle) {
|
|
188
|
-
const chunk = bundle[file] as OutputChunk
|
|
189
|
-
if (
|
|
190
|
-
chunk?.facadeModuleId ===
|
|
191
|
-
`\0virtual:__remoteEntryHelper__${options.filename}`
|
|
192
|
-
) {
|
|
193
|
-
remoteEntryChunk = chunk
|
|
194
|
-
break
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
// placeholder replace
|
|
198
|
-
if (remoteEntryChunk) {
|
|
199
|
-
// 替换 base 和 assetsDir 占位符
|
|
200
|
-
remoteEntryChunk.code = remoteEntryChunk.code
|
|
201
|
-
.replace(
|
|
202
|
-
'__VITE_BASE_PLACEHOLDER__',
|
|
203
|
-
`'${viteConfigResolved.config?.base || ''}'`
|
|
204
|
-
)
|
|
205
|
-
.replace(
|
|
206
|
-
'__VITE_ASSETS_DIR_PLACEHOLDER__',
|
|
207
|
-
`'${builderInfo.assetsDir || ''}'`
|
|
208
|
-
)
|
|
209
|
-
|
|
210
|
-
const filepathMap = new Map()
|
|
211
|
-
const getFilename = (name) => parse(parse(name).name).name
|
|
212
|
-
const cssBundlesMap: Map<string, OutputAsset | OutputChunk> =
|
|
213
|
-
Object.keys(bundle)
|
|
214
|
-
.filter((name) => extname(name) === '.css')
|
|
215
|
-
.reduce((res, name) => {
|
|
216
|
-
const filename = getFilename(name)
|
|
217
|
-
res.set(filename, bundle[name])
|
|
218
|
-
return res
|
|
219
|
-
}, new Map())
|
|
220
|
-
remoteEntryChunk.code = remoteEntryChunk.code.replace(
|
|
221
|
-
new RegExp(`(["'])${DYNAMIC_LOADING_CSS_PREFIX}.*?\\1`, 'g'),
|
|
222
|
-
(str) => {
|
|
223
|
-
// when build.cssCodeSplit: false, all files are aggregated into style.xxxxxxxx.css
|
|
224
|
-
if (
|
|
225
|
-
viteConfigResolved.config &&
|
|
226
|
-
!viteConfigResolved.config.build.cssCodeSplit
|
|
227
|
-
) {
|
|
228
|
-
if (cssBundlesMap.size) {
|
|
229
|
-
return `[${[...cssBundlesMap.values()]
|
|
230
|
-
.map((cssBundle) =>
|
|
231
|
-
JSON.stringify(basename(cssBundle.fileName))
|
|
232
|
-
)
|
|
233
|
-
.join(',')}]`
|
|
234
|
-
} else {
|
|
235
|
-
return '[]'
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
const filepath = str.slice(
|
|
239
|
-
(`'` + DYNAMIC_LOADING_CSS_PREFIX).length,
|
|
240
|
-
-1
|
|
241
|
-
)
|
|
242
|
-
if (!filepath || !filepath.length) return str
|
|
243
|
-
let fileBundle = filepathMap.get(filepath)
|
|
244
|
-
if (!fileBundle) {
|
|
245
|
-
fileBundle = Object.values(bundle).find(
|
|
246
|
-
(b) => 'facadeModuleId' in b && b.facadeModuleId === filepath
|
|
247
|
-
)
|
|
248
|
-
if (fileBundle) filepathMap.set(filepath, fileBundle)
|
|
249
|
-
else return str
|
|
250
|
-
}
|
|
251
|
-
const depCssFiles: Set<string> = new Set()
|
|
252
|
-
const addDepCss = (bundleName) => {
|
|
253
|
-
const theBundle = bundle[bundleName] as any
|
|
254
|
-
if (theBundle && theBundle.viteMetadata) {
|
|
255
|
-
for (const cssFileName of theBundle.viteMetadata.importedCss.values()) {
|
|
256
|
-
const cssBundle = cssBundlesMap.get(getFilename(cssFileName))
|
|
257
|
-
if (cssBundle) {
|
|
258
|
-
depCssFiles.add(cssBundle.fileName)
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
if (theBundle && theBundle.imports && theBundle.imports.length) {
|
|
263
|
-
theBundle.imports.forEach((name) => addDepCss(name))
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
;[fileBundle.fileName, ...fileBundle.imports].forEach(addDepCss)
|
|
268
|
-
|
|
269
|
-
return `[${[...depCssFiles]
|
|
270
|
-
.map((d) => JSON.stringify(basename(d)))
|
|
271
|
-
.join(',')}]`
|
|
272
|
-
}
|
|
273
|
-
)
|
|
274
|
-
|
|
275
|
-
// replace the export file placeholder path to final chunk path
|
|
276
|
-
for (const expose of parseExposeOptions(options)) {
|
|
277
|
-
const module = Object.keys(bundle).find((module) => {
|
|
278
|
-
const chunk = bundle[module]
|
|
279
|
-
return chunk.name === EXPOSES_KEY_MAP.get(expose[0])
|
|
280
|
-
})
|
|
281
|
-
|
|
282
|
-
if (module) {
|
|
283
|
-
const chunk = bundle[module]
|
|
284
|
-
const fileRelativePath = relative(
|
|
285
|
-
dirname(remoteEntryChunk.fileName),
|
|
286
|
-
chunk.fileName
|
|
287
|
-
)
|
|
288
|
-
const slashPath = fileRelativePath.replace(/\\/g, '/')
|
|
289
|
-
remoteEntryChunk.code = remoteEntryChunk.code.replace(
|
|
290
|
-
`\${__federation_expose_${expose[0]}}`,
|
|
291
|
-
viteConfigResolved.config?.base?.replace(/\/+$/, '')
|
|
292
|
-
? [
|
|
293
|
-
viteConfigResolved.config.base.replace(/\/+$/, ''),
|
|
294
|
-
slashPath
|
|
295
|
-
]
|
|
296
|
-
.filter(Boolean)
|
|
297
|
-
.join('/')
|
|
298
|
-
: `./${slashPath}`
|
|
299
|
-
)
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
// remove all __f__dynamic_loading_css__ after replace
|
|
304
|
-
let ast: AcornNode | null = null
|
|
305
|
-
try {
|
|
306
|
-
ast = this.parse(remoteEntryChunk.code)
|
|
307
|
-
} catch (err) {
|
|
308
|
-
console.error(err)
|
|
309
|
-
}
|
|
310
|
-
if (!ast) {
|
|
311
|
-
return
|
|
312
|
-
}
|
|
313
|
-
const magicString = new MagicString(remoteEntryChunk.code)
|
|
314
|
-
// let cssFunctionName: string = DYNAMIC_LOADING_CSS
|
|
315
|
-
walk(ast, {
|
|
316
|
-
enter(node: any) {
|
|
317
|
-
if (
|
|
318
|
-
node &&
|
|
319
|
-
node.type === 'CallExpression' &&
|
|
320
|
-
typeof node.arguments[0]?.value === 'string' &&
|
|
321
|
-
node.arguments[0]?.value.indexOf(
|
|
322
|
-
`${DYNAMIC_LOADING_CSS_PREFIX}`
|
|
323
|
-
) > -1
|
|
324
|
-
) {
|
|
325
|
-
magicString.remove(node.start, node.end + 1)
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
})
|
|
329
|
-
remoteEntryChunk.code = magicString.toString()
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
}
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { satisfy } from '__federation_fn_satisfy'
|
|
2
|
-
|
|
3
|
-
const currentImports = {}
|
|
4
|
-
|
|
5
|
-
// eslint-disable-next-line no-undef
|
|
6
|
-
const moduleMap = __rf_var__moduleMap
|
|
7
|
-
const moduleCache = Object.create(null)
|
|
8
|
-
async function importShared(name, shareScope = 'default') {
|
|
9
|
-
return moduleCache[name]
|
|
10
|
-
? new Promise((r) => r(moduleCache[name]))
|
|
11
|
-
: (await getSharedFromRuntime(name, shareScope)) || getSharedFromLocal(name)
|
|
12
|
-
}
|
|
13
|
-
// eslint-disable-next-line
|
|
14
|
-
async function __federation_import(name) {
|
|
15
|
-
currentImports[name] ??= import(name)
|
|
16
|
-
return currentImports[name]
|
|
17
|
-
}
|
|
18
|
-
async function getSharedFromRuntime(name, shareScope) {
|
|
19
|
-
let module = null
|
|
20
|
-
if (globalThis?.__federation_shared__?.[shareScope]?.[name]) {
|
|
21
|
-
const versionObj = globalThis.__federation_shared__[shareScope][name]
|
|
22
|
-
const requiredVersion = moduleMap[name]?.requiredVersion
|
|
23
|
-
const hasRequiredVersion = !!requiredVersion
|
|
24
|
-
if (hasRequiredVersion) {
|
|
25
|
-
const versionKey = Object.keys(versionObj).find((version) =>
|
|
26
|
-
satisfy(version, requiredVersion)
|
|
27
|
-
)
|
|
28
|
-
if (versionKey) {
|
|
29
|
-
const versionValue = versionObj[versionKey]
|
|
30
|
-
module = await (await versionValue.get())()
|
|
31
|
-
} else {
|
|
32
|
-
console.log(
|
|
33
|
-
`provider support ${name}(${versionKey}) is not satisfied requiredVersion(\${moduleMap[name].requiredVersion})`
|
|
34
|
-
)
|
|
35
|
-
}
|
|
36
|
-
} else {
|
|
37
|
-
const versionKey = Object.keys(versionObj)[0]
|
|
38
|
-
const versionValue = versionObj[versionKey]
|
|
39
|
-
module = await (await versionValue.get())()
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
if (module) {
|
|
43
|
-
return flattenModule(module, name)
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
async function getSharedFromLocal(name) {
|
|
47
|
-
if (moduleMap[name]?.import) {
|
|
48
|
-
let module = await (await moduleMap[name].get())()
|
|
49
|
-
return flattenModule(module, name)
|
|
50
|
-
} else {
|
|
51
|
-
console.error(
|
|
52
|
-
`consumer config import=false,so cant use callback shared module`
|
|
53
|
-
)
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
function flattenModule(module, name) {
|
|
57
|
-
// use a shared module which export default a function will getting error 'TypeError: xxx is not a function'
|
|
58
|
-
if (typeof module.default === 'function') {
|
|
59
|
-
Object.keys(module).forEach((key) => {
|
|
60
|
-
if (key !== 'default') {
|
|
61
|
-
module.default[key] = module[key]
|
|
62
|
-
}
|
|
63
|
-
})
|
|
64
|
-
moduleCache[name] = module.default
|
|
65
|
-
return module.default
|
|
66
|
-
}
|
|
67
|
-
if (module.default) module = Object.assign({}, module.default, module)
|
|
68
|
-
moduleCache[name] = module
|
|
69
|
-
return module
|
|
70
|
-
}
|
|
71
|
-
export {
|
|
72
|
-
importShared,
|
|
73
|
-
getSharedFromRuntime as importSharedRuntime,
|
|
74
|
-
getSharedFromLocal as importSharedLocal
|
|
75
|
-
}
|