Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/wxt/src/core/resolve-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ function resolveZipConfig(
],
downloadPackages: mergedConfig.zip?.downloadPackages ?? [],
downloadedPackagesDir,
stripWorkspaceProtocol: mergedConfig.zip?.stripWorkspaceProtocol ?? false,
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/wxt/src/core/utils/testing/fake-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ export const fakeResolvedConfig = fakeObjectCreator<ResolvedConfig>(() => {
name: faker.person.firstName().toLowerCase(),
downloadedPackagesDir: fakeDir(),
downloadPackages: [],
stripWorkspaceProtocol: false,
compressionLevel: 9,
zipSources: false,
},
Expand Down
70 changes: 54 additions & 16 deletions packages/wxt/src/core/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ export async function zip(config?: InlineConfig): Promise<string[]> {
exclude: excludeSources,
transform(absolutePath, zipPath, content) {
if (zipPath.endsWith('package.json')) {
return addOverridesToPackageJson(absolutePath, content, overrides);
return transformPackageJson(absolutePath, content, {
overrides,
stripWorkspaceProtocol: wxt.config.zip.stripWorkspaceProtocol,
});
}
},
additionalFiles: downloadedPackages,
Expand Down Expand Up @@ -193,22 +196,57 @@ async function downloadPrivatePackages() {
return { overrides, files };
}

function addOverridesToPackageJson(
function transformPackageJson(
absolutePackageJsonPath: string,
content: string,
overrides: Record<string, string>,
{
overrides,
stripWorkspaceProtocol,
}: {
overrides: Record<string, string>;
stripWorkspaceProtocol: boolean;
},
): string {
if (Object.keys(overrides).length === 0) return content;

const packageJsonDir = path.dirname(absolutePackageJsonPath);
const oldPackage = JSON.parse(content);
const newPackage = {
...oldPackage,
[wxt.pm.overridesKey]: { ...oldPackage[wxt.pm.overridesKey] },
};
Object.entries(overrides).forEach(([key, absolutePath]) => {
newPackage[wxt.pm.overridesKey][key] =
'file://./' + normalizePath(path.relative(packageJsonDir, absolutePath));
});
return JSON.stringify(newPackage, null, 2);
const packageJson = JSON.parse(content);

// add overrides to package.json
if (Object.keys(overrides).length > 0) {
const packageJsonDir = path.dirname(absolutePackageJsonPath);
packageJson[wxt.pm.overridesKey] ??= {};

Object.entries(overrides).forEach(([key, absolutePath]) => {
packageJson[wxt.pm.overridesKey][key] =
'file://./' +
normalizePath(path.relative(packageJsonDir, absolutePath));
});
}

// strip "workspace:" protocol in dependency versions (see https://pnpm.io/workspaces#workspace-protocol-workspace)
if (stripWorkspaceProtocol) {
for (const depType of [
'dependencies',
'devDependencies',
'peerDependencies',
'optionalDependencies',
] as const) {
for (const [name, version] of Object.entries<string>(
packageJson[depType] ?? {},
)) {
if (version.startsWith('workspace:')) {
// TODO: for "workspace:*", "workspace:~" and "workspace:^" the version should ideally be replaced by the package version in the workspace
// (see https://pnpm.io/workspaces#publishing-workspace-packages)
const versionWithoutWorkspace = version.slice('workspace:'.length);

// if the version is an alias (`workspace:[email protected]`) it has to be converted to a regular aliased dependency (`npm:[email protected]`)
const isAlias = versionWithoutWorkspace.includes('@');

packageJson[depType][name] = isAlias
? `npm:${versionWithoutWorkspace}`
: versionWithoutWorkspace;
}
}
}
}

return JSON.stringify(packageJson, null, 2);
}
7 changes: 7 additions & 0 deletions packages/wxt/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ export interface InlineConfig {
* ["@scope/[email protected]", "package-name@^2"]
*/
downloadPackages?: string[];
/**
* Strips the `workspace:` protocol used by some package managers from dependencies.
*
* @default false
*/
stripWorkspaceProtocol?: boolean;
/**
* Compression level to use when zipping files.
*
Expand Down Expand Up @@ -1336,6 +1342,7 @@ export interface ResolvedConfig {
sourcesRoot: string;
downloadedPackagesDir: string;
downloadPackages: string[];
stripWorkspaceProtocol: boolean;
compressionLevel: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
exclude: string[];
/**
Expand Down