Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

7.2 KB
Raw Blame
import { execFileSync } from 'node:child_process';
import { existsSync } from 'node:fs';
import path from 'node:path';

import { defineConfig, type WxtUnimportOptions } from 'wxt';
import { defaultClientConditions, type FilterPattern, type Plugin } from 'vite';
import { viteStaticCopy } from 'vite-plugin-static-copy';

/**
 * Forces emitted JS chunks to ASCII-only output.
 *
 * Safari before 18.4 could decode extension resources using the system text
 * encoding instead of UTF-8, corrupting bundled non-ASCII strings. Content
 * scripts are classic scripts, so we escape non-ASCII characters regardless of
 * bundler to keep extension JS portable.
 *
 * @see https://github.com/aliasvault/aliasvault/issues/2162
 */
function asciiOnlyJsPlugin(): Plugin {
  return {
    name: 'aliasvault:ascii-only-js',
    generateBundle(_options, bundle): void {
      for (const file of Object.values(bundle)) {
        if (file.type !== 'chunk' || !file.fileName.endsWith('.js')) {
          continue;
        }

        file.code = file.code.replace(/[\u0080-\uFFFF]/g, (ch) =>
          `\\u${ch.charCodeAt(0).toString(16).padStart(4, '0')}`
        );
      }
    },
  };
}

/**
 * Removes the default WASM URL from the wasm-bindgen glue, so Vite does not inline the WASM as base64.
 * Without this the entrypoint will attempt to embed the WASM, causing filesize to grow beyond 5MB limit of Firefox AMO.
 */
function stripWasmDefaultUrlPlugin(): Plugin {
  const glue = /[\\/]wasm-extension[\\/]aliasvault_core\.js$/;
  const defaultUrl = /new URL\('aliasvault_core_bg\.wasm', import\.meta\.url\)/;
  return {
    name: 'aliasvault:strip-wasm-default-url',
    enforce: 'pre',
    transform(code, id): string | undefined {
      if (!glue.test(id.split('?')[0])) {
        return undefined;
      }
      if (!defaultUrl.test(code)) {
        throw new Error(`Default WASM URL not found in ${id}, update stripWasmDefaultUrlPlugin in wxt.config.ts.`);
      }
      return code.replace(defaultUrl, `(() => { throw new Error('The WASM module must be passed to init explicitly.'); })()`);
    },
  };
}

const CORE_DIR = path.resolve(import.meta.dirname, '../../core');

/*
 * Exclude the shared core packages from auto-imports as this can lead to issues with the unimport plugin.
 */
const AUTO_IMPORTS: WxtUnimportOptions & { exclude: FilterPattern } = {
  exclude: [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/, `${CORE_DIR}/**`],
};

/*
 * README that is placed in the root of the Firefox sources archive. It is added after zipping because
 * wxt keeps the path of every included file, and the archive root maps to the repository root which
 * already has its own README.md.
 */
const SOURCES_README = path.resolve(import.meta.dirname, 'build-assets/firefox-sources/README.md');

// See https://wxt.dev/api/config.html
export default defineConfig({
  manifest: ({ browser, manifestVersion, mode, command }) => {
    const permissions = [
      "storage",
      "unlimitedStorage",
      "activeTab",
      "contextMenus",
      "scripting",
      "clipboardWrite",
      "alarms"
    ];

    // Only add offscreen permission for Chrome and Edge
    if (browser === 'chrome' || browser === 'edge') {
      permissions.push("offscreen");
    }

    // Safari: allow messaging the native app (e.g. to open Safari's extension shortcut settings)
    if (browser === 'safari') {
      permissions.push("nativeMessaging");
    }

    return {
      name: "AliasVault",
      description: "AliasVault Browser AutoFill Extension. Keeping your personal information private.",
      version: "0.32.0",
      content_security_policy: {
        extension_pages: "script-src 'self' 'wasm-unsafe-eval'; object-src 'self';"
      },
      action: {
        default_title: "AliasVault"
      },
      permissions,
      host_permissions: [
        "<all_urls>"
      ],
      commands: {
        "show-autofill-popup": {
          suggested_key: {
            default: "Ctrl+Shift+L",
            mac: "Command+Shift+L"
          },
          description: "Show the autofill popup (while focusing an input field)"
        }
      },
      web_accessible_resources: [{
        resources: [
          "webauthn.js",
          "src/aliasvault_core_bg.wasm"
        ],
        matches: ["<all_urls>"]
      }],
      ...(browser === 'firefox' ? {
        browser_specific_settings: {
          gecko: {
            id: "{a06e3383-fc5f-431d-8405-1c54c2f85971}",
            data_collection_permissions: {
              required: ["none"]
            }
          }
        }
      } : {})
    };
  },
  modules: ['@wxt-dev/module-react'],
  imports: AUTO_IMPORTS,
  srcDir: 'src',
  outDir: 'dist',
  vite: () => ({
    // Resolve the `#wasm/*` import of core/client to the speed-optimized extension build.
    resolve: {
      conditions: ['aliasvault-extension', ...defaultClientConditions],
    },
    // WXT imports the entrypoints at build time through its own `inline` environment.
    environments: {
      inline: {
        resolve: {
          conditions: ['aliasvault-extension'],
        },
      },
    },
    // Allow to serve files from the shared core directory
    server: {
      fs: {
        allow: [path.resolve('.'), CORE_DIR],
      },
    },
    build: {
      // Browser extension bundles load froom disk so chunk size is not a concern.
      chunkSizeWarningLimit: 10_000,
    },
    optimizeDeps: {
      entries: ['src/**/*.html', 'public/**/*.html'],
      exclude: ['@aliasvault/client', '@aliasvault/models', '@aliasvault/vault'],
    },
    plugins: [
      asciiOnlyJsPlugin(),
      stripWasmDefaultUrlPlugin(),
      viteStaticCopy({
        targets: [
          {
            src: path.resolve(CORE_DIR, 'client/wasm-extension/aliasvault_core_bg.wasm'),
            dest: 'src'
          }
        ]
      })
    ],
  }),
  hooks: {
    'zip:sources:done': (_wxt, zipPath): void => {
      // The README is excluded from the archive itself, so a build from inside the archive has nothing to add.
      if (!existsSync(SOURCES_README)) {
        return;
      }
      try {
        execFileSync('zip', ['-jq', zipPath, SOURCES_README]);
      } catch (error) {
        throw new Error(`Could not add README.md to ${path.basename(zipPath)}, is the 'zip' command available? ${error}`);
      }
    },
  },
  zip: {
    // Firefox source archive (zip) requires all the files the build needs locally inside the archive.
    sourcesRoot: path.resolve(CORE_DIR, '..'),
    includeSources: [
      'apps/browser-extension/**/*',
      'core/client/**/*',
      'core/i18n/**/*',
      'core/models/**/*',
      'core/vault/**/*',
      'core/scripts/**/*',
      'core/rust/**/*',
      'core/rust/.cargo/**/*',
      'LICENSE.md',
    ],
    excludeSources: [
      '**/node_modules/**',
      '**/dist/**',
      '**/.wxt/**',
      'core/rust/target/**',
      'core/client/wasm-web/**',
      'apps/browser-extension/build-assets/safari-xcode/build/**',
      '**/xcuserdata/**',
      'apps/browser-extension/playwright-report/**',
      'apps/browser-extension/test-results/**',
      'apps/browser-extension/tests/**',
      'apps/browser-extension/build-assets/firefox-sources/**',
      'apps/browser-extension/stats.html',
      'apps/browser-extension/stats-*.json',
      '**/*.log',
      'apps/browser-extension/build-and-submit.sh'
    ],
  },
});