From 95442dd39bde22895c74bac0466da2a1da870136 Mon Sep 17 00:00:00 2001 From: Aaron Date: Fri, 13 Oct 2023 17:37:08 -0500 Subject: [PATCH] fix: More consistent `version_name` generation between browsers (#163) --- e2e/tests/manifest-content.test.ts | 82 ++++++++++++++++++++++++++---- e2e/tests/output-structure.test.ts | 14 ++--- e2e/tests/react.test.ts | 8 +-- e2e/tests/user-config.test.ts | 6 +-- e2e/utils.ts | 2 +- src/core/utils/manifest.ts | 13 +++-- 6 files changed, 95 insertions(+), 30 deletions(-) diff --git a/e2e/tests/manifest-content.test.ts b/e2e/tests/manifest-content.test.ts index b91d22d8..8720966b 100644 --- a/e2e/tests/manifest-content.test.ts +++ b/e2e/tests/manifest-content.test.ts @@ -333,7 +333,7 @@ describe('Manifest Content', () => { .toMatchInlineSnapshot(` ".output/chrome-mv3/manifest.json ---------------------------------------- - {\\"manifest_version\\":3,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"version_name\\":\\"0.0.0-test\\",\\"content_scripts\\":[{\\"matches\\":[\\"https://*.google.com/*\\"],\\"css\\":[\\"content-scripts/content.css\\"],\\"js\\":[\\"content-scripts/content.js\\"]}]}" + {\\"manifest_version\\":3,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"content_scripts\\":[{\\"matches\\":[\\"https://*.google.com/*\\"],\\"css\\":[\\"content-scripts/content.css\\"],\\"js\\":[\\"content-scripts/content.js\\"]}]}" `); }); @@ -360,7 +360,7 @@ describe('Manifest Content', () => { .toMatchInlineSnapshot(` ".output/chrome-mv3/manifest.json ---------------------------------------- - {\\"manifest_version\\":3,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"version_name\\":\\"0.0.0-test\\",\\"content_scripts\\":[{\\"matches\\":[\\"https://*.google.com/*\\"],\\"css\\":[\\"content-scripts/content.css\\"],\\"js\\":[\\"content-scripts/content.js\\"]}]}" + {\\"manifest_version\\":3,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"content_scripts\\":[{\\"matches\\":[\\"https://*.google.com/*\\"],\\"css\\":[\\"content-scripts/content.css\\"],\\"js\\":[\\"content-scripts/content.js\\"]}]}" `); }); @@ -385,10 +385,10 @@ describe('Manifest Content', () => { expect(await project.serializeFile('.output/chrome-mv3/manifest.json')) .toMatchInlineSnapshot(` - ".output/chrome-mv3/manifest.json - ---------------------------------------- - {\\"manifest_version\\":3,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"version_name\\":\\"0.0.0-test\\",\\"content_scripts\\":[{\\"matches\\":[\\"https://*.google.com/*\\"],\\"js\\":[\\"content-scripts/content.js\\"]}]}" - `); + ".output/chrome-mv3/manifest.json + ---------------------------------------- + {\\"manifest_version\\":3,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"content_scripts\\":[{\\"matches\\":[\\"https://*.google.com/*\\"],\\"js\\":[\\"content-scripts/content.js\\"]}]}" + `); }); it('should not add a content script entry for CSS when cssInjectionMode is "ui", but add a web_accessible_resources entry for MV2', async () => { @@ -416,7 +416,7 @@ describe('Manifest Content', () => { .toMatchInlineSnapshot(` ".output/chrome-mv2/manifest.json ---------------------------------------- - {\\"manifest_version\\":2,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"version_name\\":\\"0.0.0-test\\",\\"content_scripts\\":[{\\"matches\\":[\\"https://*.google.com/*\\"],\\"js\\":[\\"content-scripts/content.js\\"]}],\\"web_accessible_resources\\":[\\"content-scripts/content.css\\"]}" + {\\"manifest_version\\":2,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"content_scripts\\":[{\\"matches\\":[\\"https://*.google.com/*\\"],\\"js\\":[\\"content-scripts/content.js\\"]}],\\"web_accessible_resources\\":[\\"content-scripts/content.css\\"]}" `); }); @@ -445,7 +445,7 @@ describe('Manifest Content', () => { .toMatchInlineSnapshot(` ".output/chrome-mv3/manifest.json ---------------------------------------- - {\\"manifest_version\\":3,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"version_name\\":\\"0.0.0-test\\",\\"content_scripts\\":[{\\"matches\\":[\\"https://*.google.com/*\\"],\\"js\\":[\\"content-scripts/content.js\\"]}],\\"web_accessible_resources\\":[{\\"resources\\":[\\"content-scripts/content.css\\"],\\"matches\\":[\\"https://*.google.com/*\\"]}]}" + {\\"manifest_version\\":3,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"content_scripts\\":[{\\"matches\\":[\\"https://*.google.com/*\\"],\\"js\\":[\\"content-scripts/content.js\\"]}],\\"web_accessible_resources\\":[{\\"resources\\":[\\"content-scripts/content.css\\"],\\"matches\\":[\\"https://*.google.com/*\\"]}]}" `); }); }); @@ -480,7 +480,7 @@ describe('Manifest Content', () => { .toMatchInlineSnapshot(` ".output/chrome-mv3/manifest.json ---------------------------------------- - {\\"manifest_version\\":3,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"version_name\\":\\"0.0.0-test\\",\\"web_accessible_resources\\":[{\\"resources\\":[\\"one.png\\"],\\"matches\\":[\\"https://one.com/*\\"]},{\\"resources\\":[\\"content-scripts/content.css\\"],\\"matches\\":[\\"https://*.google.com/*\\"]}],\\"content_scripts\\":[{\\"matches\\":[\\"https://*.google.com/*\\"],\\"js\\":[\\"content-scripts/content.js\\"]}]}" + {\\"manifest_version\\":3,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"web_accessible_resources\\":[{\\"resources\\":[\\"one.png\\"],\\"matches\\":[\\"https://one.com/*\\"]},{\\"resources\\":[\\"content-scripts/content.css\\"],\\"matches\\":[\\"https://*.google.com/*\\"]}],\\"content_scripts\\":[{\\"matches\\":[\\"https://*.google.com/*\\"],\\"js\\":[\\"content-scripts/content.js\\"]}]}" `); }); @@ -505,7 +505,7 @@ describe('Manifest Content', () => { expect(output).toMatchInlineSnapshot(` ".output/chrome-mv3/manifest.json ---------------------------------------- - {\\"manifest_version\\":3,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"version_name\\":\\"0.0.0-test\\",\\"author\\":\\"Custom Author\\"}" + {\\"manifest_version\\":3,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"author\\":\\"Custom Author\\"}" `); }); @@ -537,4 +537,66 @@ describe('Manifest Content', () => { expect(safariManifest.background.persistent).toBe(expected); }, ); + + describe('versions', () => { + it.each([ + ['chrome', 3] as const, + ['safari', 2] as const, + ['edge', 3] as const, + ])( + 'should include version_name on %s when it needs simplified', + async (browser, manifestVersion) => { + const project = new TestProject({ + version: '1.0.0-alpha1', + }); + + await project.build({ browser, manifestVersion }); + const manifest = await project.getOutputManifest( + `.output/${browser}-mv${manifestVersion}/manifest.json`, + ); + + expect(manifest.version).toBe('1.0.0'); + expect(manifest.version_name).toBe('1.0.0-alpha1'); + }, + ); + + it.each([['firefox', 2] as const])( + "should not include a version_name on %s because the browser doesn't support it", + async (browser, manifestVersion) => { + const project = new TestProject({ + version: '1.0.0-alpha1', + }); + + await project.build({ browser, manifestVersion }); + const manifest = await project.getOutputManifest( + `.output/${browser}-mv${manifestVersion}/manifest.json`, + ); + + expect(manifest.version).toBe('1.0.0'); + expect(manifest.version_name).toBeUndefined(); + }, + ); + + it.each([ + ['chrome', 3] as const, + ['firefox', 2] as const, + ['safari', 3] as const, + ['edge', 3] as const, + ])( + 'should not include the version_name if it is equal to version', + async (browser, manifestVersion) => { + const project = new TestProject({ + version: '1.0.0.1', + }); + + await project.build({ browser, manifestVersion }); + const manifest = await project.getOutputManifest( + `.output/${browser}-mv${manifestVersion}/manifest.json`, + ); + + expect(manifest.version).toBe('1.0.0.1'); + expect(manifest.version_name).toBeUndefined(); + }, + ); + }); }); diff --git a/e2e/tests/output-structure.test.ts b/e2e/tests/output-structure.test.ts index 1e57ed79..5e3a04c3 100644 --- a/e2e/tests/output-structure.test.ts +++ b/e2e/tests/output-structure.test.ts @@ -13,7 +13,7 @@ describe('Output Directory Structure', () => { expect(await project.serializeOutput()).toMatchInlineSnapshot(` ".output/chrome-mv3/manifest.json ---------------------------------------- - {\\"manifest_version\\":3,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"version_name\\":\\"0.0.0-test\\"}" + {\\"manifest_version\\":3,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\"}" `); }); @@ -69,7 +69,7 @@ describe('Output Directory Structure', () => { ================================================================================ .output/chrome-mv3/manifest.json ---------------------------------------- - {\\"manifest_version\\":3,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"version_name\\":\\"0.0.0-test\\",\\"content_scripts\\":[{\\"matches\\":[\\"*://*/*\\"],\\"css\\":[\\"content-scripts/one.css\\",\\"content-scripts/two.css\\"],\\"js\\":[\\"content-scripts/one.js\\",\\"content-scripts/two.js\\"]}]}" + {\\"manifest_version\\":3,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"content_scripts\\":[{\\"matches\\":[\\"*://*/*\\"],\\"css\\":[\\"content-scripts/one.css\\",\\"content-scripts/two.css\\"],\\"js\\":[\\"content-scripts/one.js\\",\\"content-scripts/two.js\\"]}]}" `); }); @@ -96,7 +96,7 @@ describe('Output Directory Structure', () => { ================================================================================ .output/chrome-mv3/manifest.json ---------------------------------------- - {\\"manifest_version\\":3,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"version_name\\":\\"0.0.0-test\\",\\"content_scripts\\":[{\\"matches\\":[\\"*://*/*\\"],\\"js\\":[\\"content-scripts/overlay-one.js\\"]}]}" + {\\"manifest_version\\":3,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"content_scripts\\":[{\\"matches\\":[\\"*://*/*\\"],\\"js\\":[\\"content-scripts/overlay-one.js\\"]}]}" `); }); @@ -192,10 +192,10 @@ describe('Output Directory Structure', () => { expect(await project.serializeFile('.output/chrome-mv3/manifest.json')) .toMatchInlineSnapshot(` - ".output/chrome-mv3/manifest.json - ---------------------------------------- - {\\"manifest_version\\":3,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"version_name\\":\\"0.0.0-test\\",\\"background\\":{\\"service_worker\\":\\"background.js\\"},\\"content_scripts\\":[{\\"matches\\":[\\"*://*.google.com/*\\"],\\"js\\":[\\"content-scripts/content.js\\"]},{\\"matches\\":[\\"*://*.duckduckgo.com/*\\"],\\"js\\":[\\"content-scripts/named.js\\"]}]}" - `); + ".output/chrome-mv3/manifest.json + ---------------------------------------- + {\\"manifest_version\\":3,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"background\\":{\\"service_worker\\":\\"background.js\\"},\\"content_scripts\\":[{\\"matches\\":[\\"*://*.google.com/*\\"],\\"js\\":[\\"content-scripts/content.js\\"]},{\\"matches\\":[\\"*://*.duckduckgo.com/*\\"],\\"js\\":[\\"content-scripts/named.js\\"]}]}" + `); expect(await project.fileExists('.output/chrome-mv3/background.js')); expect( await project.fileExists('.output/chrome-mv3/content-scripts/content.js'), diff --git a/e2e/tests/react.test.ts b/e2e/tests/react.test.ts index bbb66e53..7a634848 100644 --- a/e2e/tests/react.test.ts +++ b/e2e/tests/react.test.ts @@ -35,9 +35,9 @@ describe('React', () => { ).toBe(true); expect(await project.serializeFile('.output/chrome-mv3/manifest.json')) .toMatchInlineSnapshot(` - ".output/chrome-mv3/manifest.json - ---------------------------------------- - {\\"manifest_version\\":3,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"version_name\\":\\"0.0.0-test\\",\\"content_scripts\\":[{\\"matches\\":\\"\\",\\"js\\":[\\"content-scripts/demo.js\\"]}]}" - `); + ".output/chrome-mv3/manifest.json + ---------------------------------------- + {\\"manifest_version\\":3,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"content_scripts\\":[{\\"matches\\":\\"\\",\\"js\\":[\\"content-scripts/demo.js\\"]}]}" + `); }); }); diff --git a/e2e/tests/user-config.test.ts b/e2e/tests/user-config.test.ts index 238cd5ab..409bce0f 100644 --- a/e2e/tests/user-config.test.ts +++ b/e2e/tests/user-config.test.ts @@ -28,7 +28,7 @@ describe('User Config', () => { ================================================================================ .output/chrome-mv3/manifest.json ---------------------------------------- - {\\"manifest_version\\":3,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"version_name\\":\\"0.0.0-test\\",\\"background\\":{\\"service_worker\\":\\"background.js\\"}}" + {\\"manifest_version\\":3,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"background\\":{\\"service_worker\\":\\"background.js\\"}}" `); }); @@ -54,7 +54,7 @@ describe('User Config', () => { ================================================================================ .output/chrome-mv3/manifest.json ---------------------------------------- - {\\"manifest_version\\":3,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"version_name\\":\\"0.0.0-test\\",\\"background\\":{\\"service_worker\\":\\"background.js\\"}}" + {\\"manifest_version\\":3,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"background\\":{\\"service_worker\\":\\"background.js\\"}}" `); }); @@ -82,7 +82,7 @@ describe('User Config', () => { expect(output).toMatchInlineSnapshot(` ".output/chrome-mv3/manifest.json ---------------------------------------- - {\\"manifest_version\\":3,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"version_name\\":\\"0.0.0-test\\",\\"example_customization\\":[\\"production\\",\\"chrome\\",\\"3\\",\\"build\\"]}" + {\\"manifest_version\\":3,\\"name\\":\\"E2E Extension\\",\\"description\\":\\"Example description\\",\\"version\\":\\"0.0.0\\",\\"example_customization\\":[\\"production\\",\\"chrome\\",\\"3\\",\\"build\\"]}" `); }); }); diff --git a/e2e/utils.ts b/e2e/utils.ts index ee8fe05a..6e0afc57 100644 --- a/e2e/utils.ts +++ b/e2e/utils.ts @@ -24,7 +24,7 @@ export class TestProject { { name: 'E2E Extension', description: 'Example description', - version: '0.0.0-test', + version: '0.0.0', dependencies: { wxt: '../../..', }, diff --git a/src/core/utils/manifest.ts b/src/core/utils/manifest.ts index 463c3125..0688d603 100644 --- a/src/core/utils/manifest.ts +++ b/src/core/utils/manifest.ts @@ -60,16 +60,19 @@ export async function generateMainfest( ): Promise { const pkg = await getPackageJson(config); + const versionName = config.manifest.version_name ?? pkg?.version; + const version = config.manifest.version ?? simplifyVersion(pkg?.version); + const baseManifest: Manifest.WebExtensionManifest = { manifest_version: config.manifestVersion, name: pkg?.name, description: pkg?.description, - version: pkg?.version && simplifyVersion(pkg.version), - // Only add the version name to chromium and if the user hasn't specified a custom version. + version, version_name: - config.browser !== 'firefox' && !config.manifest.version - ? pkg?.version - : undefined, + // Firefox doesn't support version_name + config.browser === 'firefox' || versionName === version + ? undefined + : versionName, short_name: pkg?.shortName, icons: discoverIcons(buildOutput), };