Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fa30de1

Browse files
committed
fix: use version import from vite instead of bundle-based detection
Reverts to the original approach of using the version import from vite to determine the manifest path and sourcemap handling. The bundle-based detection was incorrect. Keeps the new userWantsViteManifest feature to respect user's manifest setting.
1 parent 7cfcd04 commit fa30de1

2 files changed

Lines changed: 28 additions & 37 deletions

File tree

packages/vite-plugin/src/node/plugin-manifest.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import loadingPageHtml from 'client/html/loading-page.html'
33
import { existsSync, promises as fs } from 'fs'
44
import colors from 'picocolors'
55
import { OutputAsset, OutputChunk } from 'rollup'
6-
import { ResolvedConfig } from 'vite'
6+
import { ResolvedConfig, version as ViteVersion } from 'vite'
77
import { contentScripts, hashScriptId } from './contentScripts'
88
import { formatFileData, getFileName, prefix } from './fileWriter-utilities'
99
import { htmlFiles, manifestFiles } from './files'
@@ -355,9 +355,12 @@ export const pluginManifest: CrxPluginFn = () => {
355355
let filename = join(config.root, f)
356356
if (!existsSync(filename)) filename = join(config.publicDir, f)
357357
if (!existsSync(filename)) {
358-
// Some Vite versions don't write source map files until after this plugin is called.
359-
// If sourcemaps are enabled and this is a .map file, assume it will be written later.
358+
// Vite 3 doesn't write source map files until after this plugin is called.
359+
// To support Vite 3, check the file extension and assume the source map
360+
// files will be written to disk later.
361+
const viteMajorVersion = parseInt(ViteVersion.split('.')[0])
360362
if (
363+
viteMajorVersion < 4 &&
361364
filename.endsWith('.map') &&
362365
config.build.sourcemap === true
363366
) {

packages/vite-plugin/src/node/plugin-webAccessibleResources.ts

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { OutputBundle, OutputChunk } from 'rollup'
1+
import { OutputChunk } from 'rollup'
22
import {
33
Manifest as ViteManifest,
44
ResolvedConfig,
5+
version as ViteVersion,
56
} from 'vite'
67
import { compileFileResources } from './compileFileResources'
78
import { contentScripts } from './contentScripts'
@@ -12,18 +13,6 @@ import {
1213
parseJsonAsset,
1314
_debug,
1415
} from './helpers'
15-
16-
/**
17-
* Vite 5+ uses .vite/manifest.json, older versions use manifest.json.
18-
* We detect the path dynamically from the bundle rather than relying on
19-
* the imported Vite version, since the plugin's dependency version may
20-
* differ from the user's runtime Vite version.
21-
*/
22-
function getViteManifestPath(bundle: OutputBundle): string | undefined {
23-
if (bundle['.vite/manifest.json']) return '.vite/manifest.json'
24-
if (bundle['manifest.json']) return 'manifest.json'
25-
return undefined
26-
}
2716
import {
2817
WebAccessibleResourceById,
2918
WebAccessibleResourceByMatch,
@@ -122,13 +111,11 @@ export const pluginWebAccessibleResources: CrxPluginFn = () => {
122111

123112
// derive content script resources from vite file manifest
124113
if (contentScripts.size > 0) {
125-
// Detect the Vite manifest path dynamically from the bundle
126-
const manifestPath = getViteManifestPath(bundle)
127-
if (!manifestPath) {
128-
throw new Error(
129-
'Vite manifest not found in bundle. Expected .vite/manifest.json or manifest.json',
130-
)
131-
}
114+
// Vite 5 changed the manifest.json location to .vite/manifest.json.
115+
// In order to support both Vite <=4 and Vite 5, we need to check the Vite version and determine the path accordingly.
116+
const viteMajorVersion = parseInt(ViteVersion.split('.')[0])
117+
const manifestPath =
118+
viteMajorVersion > 4 ? '.vite/manifest.json' : 'manifest.json'
132119

133120
const viteManifest = parseJsonAsset<ViteManifest>(
134121
bundle,
@@ -255,11 +242,13 @@ export const pluginWebAccessibleResources: CrxPluginFn = () => {
255242
if (chunk.map) {
256243
const sourcemapFileName =
257244
chunk.sourcemapFileName || `${chunk.fileName}.map`
258-
// Include sourcemap if it exists in the bundle or if sourcemaps are enabled in config
259-
// (older Vite versions may not include sourcemap files in the bundle object)
245+
// Vite 3 doesn't include source map files in the bundle object.
246+
// To support both Vite 3 and Vite >=4, check the Vite version and fall back to checking the
247+
// Vite config.
248+
const viteMajorVersion = parseInt(ViteVersion.split('.')[0])
260249
if (
261250
sourcemapFileName in bundle ||
262-
config.build.sourcemap === true
251+
(viteMajorVersion < 4 && config.build.sourcemap == true)
263252
) {
264253
resourcesWithMaps.push(sourcemapFileName)
265254
}
@@ -279,17 +268,16 @@ export const pluginWebAccessibleResources: CrxPluginFn = () => {
279268
// or left at its default), remove the Vite manifest from the bundle to keep the distribution clean
280269
if (!userWantsViteManifest) {
281270
// Vite 5+ uses .vite/manifest.json, older versions use manifest.json
282-
// Check both paths since the imported version might not match the runtime version
283-
const manifestPaths = ['.vite/manifest.json', 'manifest.json']
284-
for (const manifestPath of manifestPaths) {
285-
if (bundle[manifestPath]) {
286-
debug(
287-
'Removing Vite manifest: %s (userWantsViteManifest=%s)',
288-
manifestPath,
289-
userWantsViteManifest,
290-
)
291-
delete bundle[manifestPath]
292-
}
271+
const viteMajorVersion = parseInt(ViteVersion.split('.')[0])
272+
const manifestPath =
273+
viteMajorVersion > 4 ? '.vite/manifest.json' : 'manifest.json'
274+
if (bundle[manifestPath]) {
275+
debug(
276+
'Removing Vite manifest: %s (userWantsViteManifest=%s)',
277+
manifestPath,
278+
userWantsViteManifest,
279+
)
280+
delete bundle[manifestPath]
293281
}
294282
}
295283

0 commit comments

Comments
 (0)