|
13 | 13 | // SPDX-License-Identifier: MulanPSL-2.0
|
14 | 14 | // *****************************************************************************
|
15 | 15 |
|
| 16 | +import MagicString from 'magic-string' |
16 | 17 | import type { PluginHooks } from '../../types/pluginHooks'
|
17 | 18 | import { NAME_CHAR_REG, parseSharedOptions, removeNonRegLetter } from '../utils'
|
18 |
| -import { parsedOptions } from '../public' |
| 19 | +import { parsedOptions, builderInfo } from '../public' |
19 | 20 | import type { ConfigTypeSet, VitePluginFederationOptions } from 'types'
|
20 |
| -import { basename, join, resolve } from 'path' |
| 21 | +import type { RenderedChunk } from 'rollup' |
| 22 | +import type { ChunkMetadata } from 'vite' |
| 23 | +import { basename, join, resolve, relative, dirname } from 'path' |
21 | 24 | import { readdirSync, readFileSync, statSync } from 'fs'
|
22 | 25 | const sharedFilePathReg = /__federation_shared_(.+)-.{8}\.js$/
|
23 | 26 | import federation_fn_import from './federation_fn_import.js?raw'
|
@@ -151,6 +154,38 @@ export function prodSharedPlugin(
|
151 | 154 | }
|
152 | 155 | },
|
153 | 156 |
|
| 157 | + renderChunk(this, code, _chunk) { |
| 158 | + if (builderInfo.isShared) { |
| 159 | + const chunk = _chunk as RenderedChunk & { viteMetadata?: ChunkMetadata } |
| 160 | + const regRst = sharedFilePathReg.exec(chunk.fileName) |
| 161 | + if ( |
| 162 | + regRst && |
| 163 | + shareName2Prop.get(removeNonRegLetter(regRst[1], NAME_CHAR_REG)) |
| 164 | + ?.generate !== false && |
| 165 | + chunk.type === 'chunk' && |
| 166 | + chunk.viteMetadata?.importedCss.size |
| 167 | + ) { |
| 168 | + /** |
| 169 | + * Inject the referenced style files at the top of the chunk. |
| 170 | + */ |
| 171 | + const magicString = new MagicString(code) |
| 172 | + for (const cssFileName of chunk.viteMetadata.importedCss) { |
| 173 | + let cssFilePath = relative(dirname(chunk.fileName), cssFileName) |
| 174 | + cssFilePath = cssFilePath.startsWith('.') |
| 175 | + ? cssFilePath |
| 176 | + : `./${cssFilePath}` |
| 177 | + |
| 178 | + magicString.prepend(`import '${cssFilePath}';`) |
| 179 | + } |
| 180 | + |
| 181 | + return { |
| 182 | + code: magicString.toString(), |
| 183 | + map: magicString.generateMap({ hires: true }) |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + }, |
| 188 | + |
154 | 189 | outputOptions: function (outputOption) {
|
155 | 190 | // remove rollup generated empty imports,like import './filename.js'
|
156 | 191 | outputOption.hoistTransitiveImports = false
|
|
0 commit comments