Skip to content

fix: Correctly zip local (and aliased) packages #1685

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "local-package",
"version": "0.0.0"
}
70 changes: 70 additions & 0 deletions packages/wxt/e2e/tests/zip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,76 @@ describe('Zipping', () => {
`);
});

it('should download local packages and produce a valid build when zipping sources', async () => {
const project = new TestProject({
name: 'test',
version: '1.0.0',
dependencies: {
'@local/file': `file:../../tests/__fixtures__/local-package`,
'@local/link': `link:../../tests/__fixtures__/local-package`,
},
});
project.addFile(
'entrypoints/background.ts',
'export default defineBackground(() => {});',
);
const unzipDir = project.resolvePath('.output/test-1.0.0-sources');
const sourcesZip = project.resolvePath('.output/test-1.0.0-sources.zip');

await project.zip({
browser: 'firefox',
zip: { downloadPackages: ['@local/file', '@local/link'] },
});
expect(await project.fileExists('.output/')).toBe(true);

await extract(sourcesZip, { dir: unzipDir });
// Update package json wxt path
const packageJsonPath = project.resolvePath(unzipDir, 'package.json');
const packageJson = JSON.parse(await readFile(packageJsonPath, 'utf-8'));
packageJson.dependencies.wxt = '../../../../..';
await writeFile(
packageJsonPath,
JSON.stringify(packageJson, null, 2),
'utf-8',
);

// Build zipped extension
await expect(
spawn('pnpm', ['i', '--ignore-workspace', '--frozen-lockfile', 'false'], {
cwd: unzipDir,
}),
).resolves.not.toHaveProperty('exitCode');
await expect(
spawn('pnpm', ['wxt', 'build', '-b', 'firefox'], {
cwd: unzipDir,
}),
).resolves.not.toHaveProperty('exitCode');

await expect(project.fileExists(unzipDir, '.output')).resolves.toBe(true);
expect(
await project.serializeFile(
project.resolvePath(unzipDir, 'package.json'),
),
).toMatchInlineSnapshot(`
".output/test-1.0.0-sources/package.json
----------------------------------------
{
"name": "test",
"description": "Example description",
"version": "1.0.0",
"dependencies": {
"wxt": "../../../../..",
"@local/file": "file:../../tests/__fixtures__/local-package",
"@local/link": "link:../../tests/__fixtures__/local-package"
},
"resolutions": {
"@local/file@file:../../tests/__fixtures__/local-package": "file://./.wxt/local_modules/local-package-0.0.0.tgz",
"@local/link@link:../../tests/__fixtures__/local-package": "file://./.wxt/local_modules/local-package-0.0.0.tgz"
}
}"
`);
});

it('should correctly apply template variables for zip file names based on provided config', async () => {
const project = new TestProject({
name: 'test',
Expand Down
19 changes: 17 additions & 2 deletions packages/wxt/src/core/package-managers/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import spawn from 'nano-spawn';

export const npm: WxtPackageManagerImpl = {
overridesKey: 'overrides',
async downloadDependency(id, downloadDir) {
async downloadDependency(id, downloadDir, options) {
await ensureDir(downloadDir);
const res = await spawn('npm', ['pack', id, '--json'], {
const normalizedId = normalizeId(id, options?.cwd);
const res = await spawn('npm', ['pack', normalizedId, '--json'], {
cwd: downloadDir,
});
const packed: PackedDependency[] = JSON.parse(res.stdout);
Expand Down Expand Up @@ -82,3 +83,17 @@ interface PackedDependency {
version: string;
filename: string;
}

/** Normalizes `file:` or `link:` package ids to point to an absolute path so they can be resolved from downloadDir. */
function normalizeId(id: string, cwd?: string) {
// this regex matches file: and link: dependencies with optional alias
const match = id.match(/^(@?[^@]+)(?:@@?[^@]+)?@(?:file|link):(.+)$/);

if (!match) {
return id;
}

const [_, dependency, relativePath] = match;

return `${dependency}@file:${path.resolve(cwd ?? '', relativePath)}`;
}
8 changes: 7 additions & 1 deletion packages/wxt/src/core/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,15 @@ async function downloadPrivatePackages() {
const tgzPath = await wxt.pm.downloadDependency(
id,
wxt.config.zip.downloadedPackagesDir,
{ cwd: wxt.config.root },
);
files.push(tgzPath);
overrides[id] = tgzPath;

// make sure alias packages are correctly overridden
// alias@name@spec -> alias@spec
const normalizedId = id.replace(/^(.+?)(@.+)@(.+)$/, '$1@$3');

overrides[normalizedId] = tgzPath;
}
}

Expand Down
7 changes: 6 additions & 1 deletion packages/wxt/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1492,9 +1492,14 @@ export interface WxtPackageManager extends Nypm.PackageManager {
*
* @param id Name of the package to download, can include a version (like `[email protected]`)
* @param downloadDir Where to store the package.
* @param options Working directory used to resolve local packages
* @returns Absolute path to downloaded file.
*/
downloadDependency: (id: string, downloadDir: string) => Promise<string>;
downloadDependency: (
id: string,
downloadDir: string,
options?: { cwd?: string },
) => Promise<string>;
/**
* Run `npm ls`, `pnpm ls`, or `bun pm ls`, or `yarn list` and return the results.
*
Expand Down