Skip to content

Commit

Permalink
feat(zip): wxt zip --sources and auto sources for opera (#1014)
Browse files Browse the repository at this point in the history
Co-authored-by: Timeraa <[email protected]>
Co-authored-by: Aaron <[email protected]>
  • Loading branch information
3 people authored Oct 2, 2024
1 parent 5faa5d7 commit d75f64d
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 4 deletions.
69 changes: 69 additions & 0 deletions packages/wxt/e2e/tests/zip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,73 @@ describe('Zipping', () => {
true,
);
});

it.each(['firefox', 'opera'])(
'should create sources zip for "%s" browser when sourcesZip is undefined',
async (browser) => {
const project = new TestProject({
name: 'test',
version: '1.0.0',
});
project.addFile(
'entrypoints/background.ts',
'export default defineBackground(() => {});',
);
const sourcesZip = project.resolvePath('.output/test-1.0.0-sources.zip');

await project.zip({
browser,
});

expect(await project.fileExists(sourcesZip)).toBe(true);
},
);

it.each(['firefox', 'chrome'])(
'should create sources zip for "%s" when sourcesZip is true',
async (browser) => {
const project = new TestProject({
name: 'test',
version: '1.0.0',
});
project.addFile(
'entrypoints/background.ts',
'export default defineBackground(() => {});',
);
const sourcesZip = project.resolvePath('.output/test-1.0.0-sources.zip');

await project.zip({
browser,
zip: {
zipSources: true,
},
});

expect(await project.fileExists(sourcesZip)).toBe(true);
},
);

it.each(['firefox', 'chrome'])(
'should not create sources zip for "%s" when sourcesZip is false',
async (browser) => {
const project = new TestProject({
name: 'test',
version: '1.0.0',
});
project.addFile(
'entrypoints/background.ts',
'export default defineBackground(() => {});',
);
const sourcesZip = project.resolvePath('.output/test-1.0.0-sources.zip');

await project.zip({
browser,
zip: {
zipSources: false,
},
});

expect(await project.fileExists(sourcesZip)).toBe(false);
},
);
});
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
4 changes: 4 additions & 0 deletions packages/wxt/src/cli/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ cli
.option('-b, --browser <browser>', 'specify a browser')
.option('--mv3', 'target manifest v3')
.option('--mv2', 'target manifest v2')
.option('--sources', 'always create sources zip')
.action(
wrapAction(async (root, flags) => {
await zip({
Expand All @@ -104,6 +105,9 @@ cli
manifestVersion: flags.mv3 ? 3 : flags.mv2 ? 2 : undefined,
configFile: flags.config,
debug: flags.debug,
zip: {
zipSources: flags.sources,
},
});
}),
);
Expand Down
5 changes: 4 additions & 1 deletion 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 @@ -270,6 +271,8 @@ function resolveZipConfig(
includeSources: [],
compressionLevel: 9,
...mergedConfig.zip,
zipSources:
mergedConfig.zip?.zipSources ?? ['firefox', 'opera'].includes(browser),
exclude: mergedConfig.zip?.exclude ?? [],
excludeSources: [
'**/node_modules',
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>(() => {
downloadedPackagesDir: fakeDir(),
downloadPackages: [],
compressionLevel: 9,
zipSources: false,
},
transformManifest: () => {},
userConfigMetadata: {},
Expand Down
3 changes: 1 addition & 2 deletions packages/wxt/src/core/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ export async function zip(config?: InlineConfig): Promise<string[]> {
zipFiles.push(outZipPath);
await wxt.hooks.callHook('zip:extension:done', wxt, outZipPath);

// ZIP sources for Firefox
if (wxt.config.browser === 'firefox') {
if (wxt.config.zip.zipSources) {
await wxt.hooks.callHook('zip:sources:start', wxt);
const { overrides, files: downloadedPackages } =
await downloadPrivatePackages();
Expand Down
14 changes: 14 additions & 0 deletions packages/wxt/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ export interface InlineConfig {
* @default "{{name}}-{{version}}-{{browser}}.zip"
*/
artifactTemplate?: string;
/**
* When zipping the extension, also zip sources.
*
* - `undefined`: zip sources if the target browser is "firefox" or "opera"
* - `true`: always zip sources
* - `false`: never zip sources
*
* @default undefined
*/
zipSources?: boolean;
/**
* Configure the filename output when zipping files.
*
Expand Down Expand Up @@ -1285,6 +1295,10 @@ 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;
};
/**
* @deprecated Use `build:manifestGenerated` hook instead.
Expand Down

0 comments on commit d75f64d

Please sign in to comment.