Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Vite Plugin] Fix vite manifest location #841

Merged
merged 9 commits into from
Jan 10, 2024
6 changes: 6 additions & 0 deletions .changeset/yellow-paws-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@crxjs/vite-plugin': patch
---

Vite 5 moved vite manifest from 'manifest.json' to '.vite/manifest.json'.
This change updates the plugin to use the new location if Vite major version is >4, old location otherwise.
14 changes: 12 additions & 2 deletions packages/vite-plugin/src/node/plugin-webAccessibleResources.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { OutputChunk } from 'rollup'
import { Manifest as ViteManifest, ResolvedConfig } from 'vite'
import {
Manifest as ViteManifest,
ResolvedConfig,
version as ViteVersion,
} from 'vite'
import { compileFileResources } from './compileFileResources'
import { contentScripts } from './contentScripts'
import { DYNAMIC_RESOURCE } from './defineManifest'
Expand Down Expand Up @@ -102,10 +106,16 @@ export const pluginWebAccessibleResources: CrxPluginFn = () => {

// derive content script resources from vite file manifest
if (contentScripts.size > 0) {
// Vite 5 changed the manifest.json location to .vite/manifest.json.
// In order to support both Vite <=4 and Vite 5, we need to check the Vite version and determine the path accordingly.
const viteMajorVersion = parseInt(ViteVersion.split('.')[0])
const manifestPath = viteMajorVersion > 4 ? '.vite/manifest.json' : 'manifest.json'

const viteManifest = parseJsonAsset<ViteManifest>(
bundle,
'manifest.json',
manifestPath,
)

const viteFiles = new Map()
for (const [, file] of Object.entries(viteManifest))
viteFiles.set(file.file, file)
Expand Down