Skip to content

Building problemer på linux #6

Description

@kjullu

Kan godt være at det er mig som er dum

Siden firefox version var meget gammel valgte jeg at prøve at bygge den med bun.

Men fik en error:
 ~/.git/betterlectio > bun install
bun install v1.3.3 (274e01c7)

$ wxt prepare
                                                                                                                                                            11.17.33
WXT 0.20.18
ℹ Generating types...                                                                                                                                      11.17.33
✖ Command failed after 690 ms                                                                                                                              11.17.34

 ERROR  defineBackground is not defined                                                                                                                     11.17.34

    at entrypoints/background.ts:1:16
    at ViteNodeRunner.runModule (node_modules/vite-node/dist/client.mjs:397:10)
    at ViteNodeRunner.directRequest (node_modules/vite-node/dist/client.mjs:375:14)
    at ViteNodeRunner.cachedRequest (node_modules/vite-node/dist/client.mjs:189:11)
    at ViteNodeRunner.executeFile (node_modules/vite-node/dist/client.mjs:162:10)
    at node_modules/wxt/dist/core/builders/vite/index.mjs:183:17
    at async Promise.all (index 0)
    at Object.run (node_modules/wxt/dist/core/utils/environments/environment.mjs:14:11)
    at Object.importEntrypoints (node_modules/wxt/dist/core/builders/vite/index.mjs:182:16)
    at node_modules/wxt/dist/core/utils/building/find-entrypoints.mjs:101:4
    at async Promise.all (index 0)
    at importEntrypoints (node_modules/wxt/dist/core/utils/building/find-entrypoints.mjs:98:2)
    at findEntrypoints (node_modules/wxt/dist/core/utils/building/find-entrypoints.mjs:50:28)
    at prepare (node_modules/wxt/dist/core/prepare.mjs:10:23)
    at node_modules/wxt/dist/cli/commands.mjs:58:2
    at CAC.<anonymous> (node_modules/wxt/dist/cli/cli-utils.mjs:22:10)
    at node_modules/wxt/dist/cli/index.mjs:12:1

error: postinstall script from "wxt-starter" exited with 1

Jeg køre dog på NixOS, prøvede også i en Arch distrobox, men fik også en error.

Jeg satte opencode på den og den lavede nogle ændringer som fik `bun install` til at virke:
diff --git a/wxt.config.ts b/wxt.config.ts
index c64d594..58787f9 100644
--- a/wxt.config.ts
+++ b/wxt.config.ts
@@ -1,6 +1,59 @@
 import { defineConfig } from 'wxt';
 import tailwindcss from '@tailwindcss/vite';
-import path from 'path';
+import path from 'node:path';
+
+// These are matched in the plugin to inject stubs
+const defineFunctions = [
+  'defineBackground',
+  'defineContentScript',
+  'defineUnlistedScript',
+  'defineWxtPlugin',
+  'defineAppConfig',
+];
+
+// Stub function definitions to inject
+const stubs = `
+function defineBackground(arg) {
+  if (arg == null || typeof arg === "function") return { main: arg };
+  return arg;
+}
+function defineContentScript(options) { return options; }
+function defineUnlistedScript(options) { return options; }
+function defineWxtPlugin(setup) { return { name: 'plugin', setup }; }
+function defineAppConfig(config) { return config; }
+`;
+
+function createBetterLectioPlugin() {
+  return {
+    name: 'betterlectio-stubs',
+    enforce: 'pre' as const,
+    transformInclude(id: string) {
+      // Only transform project entrypoints, not node_modules
+      return !id.includes('node_modules') && /\.[jt]sx?$/.test(id);
+    },
+    async transform(code: string, id: string) {
+      // Find any undefined identifiers that match our stubs
+      const identifiersNeeded = new Set<string>();
+      const varPattern = /\b([a-zA-Z_$][a-zA-Z0-9_$]*)\b/g;
+      let match;
+      while ((match = varPattern.exec(code)) !== null) {
+        if (defineFunctions.includes(match[1])) {
+          identifiersNeeded.add(match[1]);
+        }
+      }
+      
+      if (identifiersNeeded.size === 0) return;
+      
+      // Inject stub functions
+      const newCode = stubs + code;
+      
+      return {
+        code: newCode,
+        map: null
+      };
+    },
+  };
+}
 
 // See https://wxt.dev/api/config.html
 export default defineConfig({
@@ -70,7 +123,7 @@ export default defineConfig({
     ],
   },
   vite: () => ({
-    plugins: [tailwindcss()],
+    plugins: [tailwindcss(), createBetterLectioPlugin()],
     resolve: {
       alias: {
         '@': path.resolve(__dirname, './'),

(Gad ikke lave en PR, da jeg ikke selv har læst kode, fik dog er virkene version ud af det)

Opencode sagde problemet var:
> what was the problem?

Thinking: The user wants to understand what the root cause of the build failure was. Let me explain it concisely.

The issue was a version incompatibility between WXT and unimport.

WXT uses unimport (via its unplugin-auto-import integration) to auto-import functions like defineContentScript and defineBackground into your entrypoint files. However:

1. WXT 0.20.x added unimport integration that registers these functions as auto-imports
2. The unimport v5 API changed - UnimportPlugin.vite() was removed
3. WXT's built-in unimport plugin was never actually applied to the vite config used during the prepare phase (
vite-node execution)

So when WXT tried to execute entrypoints with vite-node, the defineContentScript function was never in scope, causing the error.

The fix: I added a custom Vite plugin (betterlectio-stubs) that injects stub function definitions directly into your entrypoint files at transform time, bypassing the broken unimport system.

Vigtig tig, jeg kørte git clone på dette repo, da det er det der står under: Installation > From Source, så det kan godt være at det er meningen at main ikke skal virke lige nu. (så burde guiden dog, måske ændres)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions