From 1884eea53fcf70a8fa5f189325debb3baa3872ff Mon Sep 17 00:00:00 2001 From: Aaron Date: Wed, 2 Oct 2024 08:44:56 -0500 Subject: [PATCH] Cleanup and more testing --- packages/wxt/e2e/tests/zip.test.ts | 6 +-- packages/wxt/src/cli/__tests__/index.test.ts | 44 +++++++++++++++++++- packages/wxt/src/cli/commands.ts | 13 +++--- packages/wxt/src/core/resolve-config.ts | 8 ++-- packages/wxt/src/core/zip.ts | 1 - packages/wxt/src/types.ts | 11 ++++- 6 files changed, 64 insertions(+), 19 deletions(-) diff --git a/packages/wxt/e2e/tests/zip.test.ts b/packages/wxt/e2e/tests/zip.test.ts index d3fc38a0e..818c1b636 100644 --- a/packages/wxt/e2e/tests/zip.test.ts +++ b/packages/wxt/e2e/tests/zip.test.ts @@ -197,7 +197,7 @@ describe('Zipping', () => { expect(await project.fileExists(sourcesZip)).toBe(true); }); - it('should create sources zip when alwaysBuildSourcesZip is true, regardless of browser', async () => { + it('should create sources zip when sourcesZip is true, regardless of browser', async () => { const project = new TestProject({ name: 'test', version: '1.0.0', @@ -218,7 +218,7 @@ describe('Zipping', () => { expect(await project.fileExists(sourcesZip)).toBe(true); }); - it('should not create sources zip for Chrome when alwaysBuildSourcesZip is false', async () => { + it('should not create sources zip for Firefox when sourcesZip is false', async () => { const project = new TestProject({ name: 'test', version: '1.0.0', @@ -230,7 +230,7 @@ describe('Zipping', () => { const sourcesZip = project.resolvePath('.output/test-1.0.0-sources.zip'); await project.zip({ - browser: 'chrome', + browser: 'firefox', zip: { zipSources: false, }, diff --git a/packages/wxt/src/cli/__tests__/index.test.ts b/packages/wxt/src/cli/__tests__/index.test.ts index 6419163b9..074934338 100644 --- a/packages/wxt/src/cli/__tests__/index.test.ts +++ b/packages/wxt/src/cli/__tests__/index.test.ts @@ -237,7 +237,9 @@ describe('CLI', () => { mockArgv('zip'); await importCli(); - expect(zipMock).toBeCalledWith({}); + expect(zipMock).toBeCalledWith({ + zip: {}, + }); }); it('should respect passing a custom root', async () => { @@ -246,6 +248,7 @@ describe('CLI', () => { expect(zipMock).toBeCalledWith({ root: 'path/to/root', + zip: {}, }); }); @@ -255,6 +258,7 @@ describe('CLI', () => { expect(zipMock).toBeCalledWith({ configFile: './path/to/config.ts', + zip: {}, }); }); @@ -264,6 +268,7 @@ describe('CLI', () => { expect(zipMock).toBeCalledWith({ mode: 'development', + zip: {}, }); }); @@ -273,6 +278,7 @@ describe('CLI', () => { expect(zipMock).toBeCalledWith({ browser: 'firefox', + zip: {}, }); }); @@ -282,6 +288,7 @@ describe('CLI', () => { expect(zipMock).toBeCalledWith({ manifestVersion: 2, + zip: {}, }); }); @@ -291,6 +298,7 @@ describe('CLI', () => { expect(zipMock).toBeCalledWith({ manifestVersion: 3, + zip: {}, }); }); @@ -300,6 +308,40 @@ describe('CLI', () => { expect(zipMock).toBeCalledWith({ debug: true, + zip: {}, + }); + }); + + it('should pass undefined for zipSources when --sources is not passed', async () => { + mockArgv('zip'); + await importCli(); + + expect(zipMock).toBeCalledWith({ + zip: { + zipSources: undefined, + }, + }); + }); + + it('should pass true for zipSources when --sources is passed', async () => { + mockArgv('zip', '--sources'); + await importCli(); + + expect(zipMock).toBeCalledWith({ + zip: { + zipSources: true, + }, + }); + }); + + it('should pass false for zipSources when --sources=false is passed', async () => { + mockArgv('zip', '--sources=false'); + await importCli(); + + expect(zipMock).toBeCalledWith({ + zip: { + zipSources: false, + }, }); }); }); diff --git a/packages/wxt/src/cli/commands.ts b/packages/wxt/src/cli/commands.ts index 791c2ce52..2d8c52fc2 100644 --- a/packages/wxt/src/cli/commands.ts +++ b/packages/wxt/src/cli/commands.ts @@ -95,9 +95,10 @@ cli .option('-b, --browser ', 'specify a browser') .option('--mv3', 'target manifest v3') .option('--mv2', 'target manifest v2') - .option('--sources', 'always create sources zip instead of auto detecting') + .option('--sources', 'always create sources zip') .action( wrapAction(async (root, flags) => { + console.log(flags); await zip({ root, mode: flags.mode, @@ -105,13 +106,9 @@ cli manifestVersion: flags.mv3 ? 3 : flags.mv2 ? 2 : undefined, configFile: flags.config, debug: flags.debug, - ...(flags.sources - ? { - zip: { - zipSources: true, - }, - } - : {}), + zip: { + zipSources: flags.sources, + }, }); }), ); diff --git a/packages/wxt/src/core/resolve-config.ts b/packages/wxt/src/core/resolve-config.ts index 245943bd3..8269179ca 100644 --- a/packages/wxt/src/core/resolve-config.ts +++ b/packages/wxt/src/core/resolve-config.ts @@ -179,7 +179,7 @@ export async function resolveConfig( srcDir, typesDir, wxtDir, - zip: resolveZipConfig(root, outBaseDir, mergedConfig), + zip: resolveZipConfig(root, browser, outBaseDir, mergedConfig), transformManifest: mergedConfig.transformManifest, analysis: resolveAnalysisConfig(root, mergedConfig), userConfigMetadata: userConfigMetadata ?? {}, @@ -258,6 +258,7 @@ async function mergeInlineConfig( function resolveZipConfig( root: string, + browser: string, outBaseDir: string, mergedConfig: InlineConfig, ): NullablyRequired { @@ -269,10 +270,9 @@ function resolveZipConfig( sourcesRoot: root, includeSources: [], compressionLevel: 9, - zipSources: ['firefox', 'opera'].includes(mergedConfig.browser ?? '') - ? true - : false, ...mergedConfig.zip, + zipSources: + mergedConfig.zip?.zipSources ?? ['firefox', 'opera'].includes(browser), exclude: mergedConfig.zip?.exclude ?? [], excludeSources: [ '**/node_modules', diff --git a/packages/wxt/src/core/zip.ts b/packages/wxt/src/core/zip.ts index 244e0e830..de7b17b6f 100644 --- a/packages/wxt/src/core/zip.ts +++ b/packages/wxt/src/core/zip.ts @@ -52,7 +52,6 @@ export async function zip(config?: InlineConfig): Promise { zipFiles.push(outZipPath); await wxt.hooks.callHook('zip:extension:done', wxt, outZipPath); - // ZIP sources for Firefox or Opera, or when explicitly requested if (wxt.config.zip.zipSources) { await wxt.hooks.callHook('zip:sources:start', wxt); const { overrides, files: downloadedPackages } = diff --git a/packages/wxt/src/types.ts b/packages/wxt/src/types.ts index d218d24cb..445f04476 100644 --- a/packages/wxt/src/types.ts +++ b/packages/wxt/src/types.ts @@ -131,9 +131,13 @@ export interface InlineConfig { */ artifactTemplate?: string; /** - * Set to `true` to always create a sources zip, even when the browser is not Firefox or Opera. + * When zipping the extension, also zip sources. * - * @default false + * - `undefined`: zip sources if the target browser is "firefox" or "opera" + * - `true`: always zip sources + * - `false`: never zip sources + * + * @default undefined */ zipSources?: boolean; /** @@ -1291,6 +1295,9 @@ export interface ResolvedConfig { downloadPackages: string[]; compressionLevel: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9; exclude: string[]; + /** + * If true, when zipping the extension, also zip the sources. + */ zipSources: boolean; }; /**