Skip to content

Commit 6059387

Browse files
committed
Add option to strip workspace: protocol from dependencies in zipped sources
1 parent 4f772fc commit 6059387

File tree

4 files changed

+63
-16
lines changed

4 files changed

+63
-16
lines changed

packages/wxt/src/core/resolve-config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ function resolveZipConfig(
326326
],
327327
downloadPackages: mergedConfig.zip?.downloadPackages ?? [],
328328
downloadedPackagesDir,
329+
stripWorkspaceProtocol: mergedConfig.zip?.stripWorkspaceProtocol ?? false,
329330
};
330331
}
331332

packages/wxt/src/core/utils/testing/fake-objects.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ export const fakeResolvedConfig = fakeObjectCreator<ResolvedConfig>(() => {
293293
name: faker.person.firstName().toLowerCase(),
294294
downloadedPackagesDir: fakeDir(),
295295
downloadPackages: [],
296+
stripWorkspaceProtocol: false,
296297
compressionLevel: 9,
297298
zipSources: false,
298299
},

packages/wxt/src/core/zip.ts

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

packages/wxt/src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@ export interface InlineConfig {
252252
* ["@scope/package-name@1.1.3", "package-name@^2"]
253253
*/
254254
downloadPackages?: string[];
255+
/**
256+
* Strips the `workspace:` protocol used by some package managers from dependencies.
257+
*
258+
* @default false
259+
*/
260+
stripWorkspaceProtocol?: boolean;
255261
/**
256262
* Compression level to use when zipping files.
257263
*
@@ -1336,6 +1342,7 @@ export interface ResolvedConfig {
13361342
sourcesRoot: string;
13371343
downloadedPackagesDir: string;
13381344
downloadPackages: string[];
1345+
stripWorkspaceProtocol: boolean;
13391346
compressionLevel: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
13401347
exclude: string[];
13411348
/**

0 commit comments

Comments
 (0)