Skip to content

Commit

Permalink
Cleanup and more testing
Browse files Browse the repository at this point in the history
  • Loading branch information
aklinker1 committed Oct 2, 2024
1 parent 7adadd8 commit 1884eea
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 19 deletions.
6 changes: 3 additions & 3 deletions packages/wxt/e2e/tests/zip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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,
},
Expand Down
44 changes: 43 additions & 1 deletion packages/wxt/src/cli/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -246,6 +248,7 @@ describe('CLI', () => {

expect(zipMock).toBeCalledWith({
root: 'path/to/root',
zip: {},
});
});

Expand All @@ -255,6 +258,7 @@ describe('CLI', () => {

expect(zipMock).toBeCalledWith({
configFile: './path/to/config.ts',
zip: {},
});
});

Expand All @@ -264,6 +268,7 @@ describe('CLI', () => {

expect(zipMock).toBeCalledWith({
mode: 'development',
zip: {},
});
});

Expand All @@ -273,6 +278,7 @@ describe('CLI', () => {

expect(zipMock).toBeCalledWith({
browser: 'firefox',
zip: {},
});
});

Expand All @@ -282,6 +288,7 @@ describe('CLI', () => {

expect(zipMock).toBeCalledWith({
manifestVersion: 2,
zip: {},
});
});

Expand All @@ -291,6 +298,7 @@ describe('CLI', () => {

expect(zipMock).toBeCalledWith({
manifestVersion: 3,
zip: {},
});
});

Expand All @@ -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,
},
});
});
});
Expand Down
13 changes: 5 additions & 8 deletions packages/wxt/src/cli/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,20 @@ cli
.option('-b, --browser <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,
browser: flags.browser,
manifestVersion: flags.mv3 ? 3 : flags.mv2 ? 2 : undefined,
configFile: flags.config,
debug: flags.debug,
...(flags.sources
? {
zip: {
zipSources: true,
},
}
: {}),
zip: {
zipSources: flags.sources,
},
});
}),
);
Expand Down
8 changes: 4 additions & 4 deletions packages/wxt/src/core/resolve-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? {},
Expand Down Expand Up @@ -258,6 +258,7 @@ async function mergeInlineConfig(

function resolveZipConfig(
root: string,
browser: string,
outBaseDir: string,
mergedConfig: InlineConfig,
): NullablyRequired<ResolvedConfig['zip']> {
Expand All @@ -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',
Expand Down
1 change: 0 additions & 1 deletion packages/wxt/src/core/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export async function zip(config?: InlineConfig): Promise<string[]> {
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 } =
Expand Down
11 changes: 9 additions & 2 deletions packages/wxt/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
/**
Expand Down Expand Up @@ -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;
};
/**
Expand Down

0 comments on commit 1884eea

Please sign in to comment.