@@ -76,7 +76,10 @@ export async function zip(config?: InlineConfig): Promise<string[]> {
7676 exclude : excludeSources ,
7777 transform ( absolutePath , zipPath , content ) {
7878 if ( zipPath . endsWith ( 'package.json' ) ) {
79- return addOverridesToPackageJson ( absolutePath , content , overrides ) ;
79+ return transformPackageJson ( absolutePath , content , {
80+ overrides,
81+ stripWorkspaceProtocol : wxt . config . zip . stripWorkspaceProtocol ,
82+ } ) ;
8083 }
8184 } ,
8285 additionalFiles : downloadedPackages ,
@@ -193,22 +196,57 @@ async function downloadPrivatePackages() {
193196 return { overrides, files } ;
194197}
195198
196- function addOverridesToPackageJson (
199+ function transformPackageJson (
197200 absolutePackageJsonPath : string ,
198201 content : string ,
199- overrides : Record < string , string > ,
202+ {
203+ overrides,
204+ stripWorkspaceProtocol,
205+ } : {
206+ overrides : Record < string , string > ;
207+ stripWorkspaceProtocol : boolean ;
208+ } ,
200209) : string {
201- if ( Object . keys ( overrides ) . length === 0 ) return content ;
202-
203- const packageJsonDir = path . dirname ( absolutePackageJsonPath ) ;
204- const oldPackage = JSON . parse ( content ) ;
205- const newPackage = {
206- ...oldPackage ,
207- [ wxt . pm . overridesKey ] : { ...oldPackage [ wxt . pm . overridesKey ] } ,
208- } ;
209- Object . entries ( overrides ) . forEach ( ( [ key , absolutePath ] ) => {
210- newPackage [ wxt . pm . overridesKey ] [ key ] =
211- 'file://./' + normalizePath ( path . relative ( packageJsonDir , absolutePath ) ) ;
212- } ) ;
213- return JSON . stringify ( newPackage , null , 2 ) ;
210+ const packageJson = JSON . parse ( content ) ;
211+
212+ // add overrides to package.json
213+ if ( Object . keys ( overrides ) . length > 0 ) {
214+ const packageJsonDir = path . dirname ( absolutePackageJsonPath ) ;
215+ packageJson [ wxt . pm . overridesKey ] ??= { } ;
216+
217+ Object . entries ( overrides ) . forEach ( ( [ key , absolutePath ] ) => {
218+ packageJson [ wxt . pm . overridesKey ] [ key ] =
219+ 'file://./' +
220+ normalizePath ( path . relative ( packageJsonDir , absolutePath ) ) ;
221+ } ) ;
222+ }
223+
224+ // strip "workspace:" protocol in dependency versions (see https://pnpm.io/workspaces#workspace-protocol-workspace)
225+ if ( stripWorkspaceProtocol ) {
226+ for ( const depType of [
227+ 'dependencies' ,
228+ 'devDependencies' ,
229+ 'peerDependencies' ,
230+ 'optionalDependencies' ,
231+ ] as const ) {
232+ for ( const [ name , version ] of Object . entries < string > (
233+ packageJson [ depType ] ?? { } ,
234+ ) ) {
235+ if ( version . startsWith ( 'workspace:' ) ) {
236+ // TODO: for "workspace:*", "workspace:~" and "workspace:^" the version should ideally be replaced by the package version in the workspace
237+ // (see https://pnpm.io/workspaces#publishing-workspace-packages)
238+ const versionWithoutWorkspace = version . slice ( 'workspace:' . length ) ;
239+
240+ // if the version is an alias (`workspace:foo@1.0.0`) it has to be converted to a regular aliased dependency (`npm:foo@1.0.0`)
241+ const isAlias = versionWithoutWorkspace . includes ( '@' ) ;
242+
243+ packageJson [ depType ] [ name ] = isAlias
244+ ? `npm:${ versionWithoutWorkspace } `
245+ : versionWithoutWorkspace ;
246+ }
247+ }
248+ }
249+ }
250+
251+ return JSON . stringify ( packageJson , null , 2 ) ;
214252}
0 commit comments