Skip to content

Commit

Permalink
[Vite Plugin] Fix vite manifest location (#841)
Browse files Browse the repository at this point in the history
* [main] Fix vite manifest location According to documentation, vite manifest is in .vite/manifest.json. Fixing bug that prevented building

* [main] Added changeset

* [main] Determine vite manifest path based on vite version

* [main] Formatting issue

* [main] Update changelog to reflect new changes

---------

Co-authored-by: Jack Steam <jacksteamdev@gmail.com>
  • Loading branch information
ale-ben and jacksteamdev committed Jan 10, 2024
1 parent ce9fe1c commit 48d8c68
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
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

0 comments on commit 48d8c68

Please sign in to comment.