From 1a39f856afb8837dc3f9e5b928bbe8a2f10f435f Mon Sep 17 00:00:00 2001 From: Aaron Date: Fri, 19 Apr 2024 20:31:32 -0500 Subject: [PATCH] get it working --- pnpm-lock.yaml | 6 +++--- src/core/builders/vite/index.ts | 11 ++++++++++- src/core/utils/strings.ts | 8 ++++++++ .../content-script-isolated-world-entrypoint.ts | 5 +++-- src/virtual/content-script-main-world-entrypoint.ts | 5 +++-- src/virtual/unlisted-script-entrypoint.ts | 5 +++-- 6 files changed, 30 insertions(+), 10 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0b103ef3..ac6ecab2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4919,8 +4919,8 @@ packages: loose-envify: 1.4.0 dev: false - /scule@1.0.0: - resolution: {integrity: sha512-4AsO/FrViE/iDNEPaAQlb77tf0csuq27EsVpy6ett584EcRTp6pTDLoGWVxCD77y5iU5FauOvhsI4o1APwPoSQ==} + /scule@1.3.0: + resolution: {integrity: sha512-6FtHJEvt+pVMIB9IBY+IcCJ6Z5f1iQnytgyfKMhDKgmzYG+TeH/wx1y3l27rshSbLiSanrR9ffZDrEsmjlQF2g==} dev: false /semver-diff@4.0.0: @@ -5550,7 +5550,7 @@ packages: mlly: 1.4.2 pathe: 1.1.1 pkg-types: 1.0.3 - scule: 1.0.0 + scule: 1.3.0 strip-literal: 1.3.0 unplugin: 1.5.0 transitivePeerDependencies: diff --git a/src/core/builders/vite/index.ts b/src/core/builders/vite/index.ts index 1f4ddfb4..2824f6e3 100644 --- a/src/core/builders/vite/index.ts +++ b/src/core/builders/vite/index.ts @@ -19,6 +19,7 @@ import { } from '~/core/utils/virtual-modules'; import { Hookable } from 'hookable'; import { toArray } from '~/core/utils/arrays'; +import { safeVarName } from '~/core/utils/strings'; export async function createViteBuilder( wxtConfig: ResolvedConfig, @@ -87,14 +88,22 @@ export async function createViteBuilder( plugins.push(wxtPlugins.cssEntrypoints(entrypoint, wxtConfig)); } + const iifeReturnValueName = safeVarName(entrypoint.name); const libMode: vite.UserConfig = { mode: wxtConfig.mode, plugins, + esbuild: { + // Add a footer with the returned value so it can return values to `scripting.executeScript` + // Footer is added apart of esbuild to make sure it's not minified. It + // get's removed if added to `build.rollupOptions.output.footer` + // See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting/executeScript#return_value + footer: iifeReturnValueName + ';', + }, build: { lib: { entry, formats: ['iife'], - name: '_', + name: iifeReturnValueName, fileName: entrypoint.name, }, rollupOptions: { diff --git a/src/core/utils/strings.ts b/src/core/utils/strings.ts index 287b1619..856c316e 100644 --- a/src/core/utils/strings.ts +++ b/src/core/utils/strings.ts @@ -5,6 +5,14 @@ export function kebabCaseAlphanumeric(str: string): string { .replace(/\s+/g, '-'); // Replace spaces with hyphens } +/** + * Return a safe variable name for a given string. + */ +export function safeVarName(str: string): string { + // _ prefix to ensure it doesn't start with a number + return '_' + kebabCaseAlphanumeric(str).replace('-', '_'); +} + /** * Removes import statements from the top of a file. Keeps import.meta and inline, async `import()` * calls. diff --git a/src/virtual/content-script-isolated-world-entrypoint.ts b/src/virtual/content-script-isolated-world-entrypoint.ts index efa66e12..7363e07f 100644 --- a/src/virtual/content-script-isolated-world-entrypoint.ts +++ b/src/virtual/content-script-isolated-world-entrypoint.ts @@ -17,7 +17,8 @@ const result = (async () => { } })(); -// Return the main function's result to the background when executed via the scripting API. +// Return the main function's result to the background when executed via the +// scripting API. Default export causes the IIFE to return a value. // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting/executeScript#return_value // Tested on both Chrome and Firefox -result; +export default result; diff --git a/src/virtual/content-script-main-world-entrypoint.ts b/src/virtual/content-script-main-world-entrypoint.ts index a4578682..95e78ca0 100644 --- a/src/virtual/content-script-main-world-entrypoint.ts +++ b/src/virtual/content-script-main-world-entrypoint.ts @@ -14,7 +14,8 @@ const result = (async () => { } })(); -// Return the main function's result to the background when executed via the scripting API. +// Return the main function's result to the background when executed via the +// scripting API. Default export causes the IIFE to return a value. // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting/executeScript#return_value // Tested on both Chrome and Firefox -result; +export default result; diff --git a/src/virtual/unlisted-script-entrypoint.ts b/src/virtual/unlisted-script-entrypoint.ts index fa0ba550..e9006408 100644 --- a/src/virtual/unlisted-script-entrypoint.ts +++ b/src/virtual/unlisted-script-entrypoint.ts @@ -13,7 +13,8 @@ const result = (async () => { } })(); -// Return the main function's result to the background when executed via the scripting API. +// Return the main function's result to the background when executed via the +// scripting API. Default export causes the IIFE to return a value. // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting/executeScript#return_value // Tested on both Chrome and Firefox -result; +export default result;