1- import { OutputBundle , OutputChunk } from 'rollup'
1+ import { OutputChunk } from 'rollup'
22import {
33 Manifest as ViteManifest ,
44 ResolvedConfig ,
5+ version as ViteVersion ,
56} from 'vite'
67import { compileFileResources } from './compileFileResources'
78import { 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- }
2716import {
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