or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

client-api.mdcontent-scripts.mdfile-writer-system.mdindex.mdmanifest-definition.mdplugin-configuration.md
tile.json

file-writer-system.mddocs/

File Writer System

Development server integration providing file watching, processing, and build coordination for Chrome extension assets with HMR support. Manages the complete lifecycle of extension files from source to output during development and production builds.

Capabilities

File Readiness Utilities

Functions to coordinate build completion and file availability for Chrome extension development.

/**
 * Wait for all extension files to be processed and written
 * @returns Promise that resolves when all files are ready
 */
function allFilesReady(): Promise<void>;

/**
 * Wait for a specific file and its dependencies to be written
 * @param script - File identifier string for the target script
 * @returns Promise that resolves when the specified file is ready
 */
function filesReady(script: string): Promise<void>;

// Alias - the actual export is fileReady, aliased as filesReady
const fileReady = filesReady;

type FileWriterId = string;

Usage Examples:

import { allFilesReady, filesReady } from "@crxjs/vite-plugin";

// Wait for all files in development
async function initializeExtension() {
  await allFilesReady();
  console.log("All extension files are ready");
  // Safe to load extension or perform operations
}

// Wait for specific content script
async function loadContentScript() {
  await filesReady("src/content.ts");
  console.log("Content script is ready");
  // Content script and its dependencies are available
}

// In a Vite plugin or build script
export default defineConfig({
  plugins: [
    crx({ manifest }),
    {
      name: "post-build",
      closeBundle: async () => {
        await allFilesReady();
        console.log("Extension build complete");
      },
    },
  ],
});

File Writer Architecture

The file writer system manages different types of extension assets and coordinates their processing.

interface OutputFile {
  fileName: string;
  source: string | Uint8Array;
  type: 'asset' | 'chunk';
}

interface ManifestFiles {
  contentScripts: string[];
  contentStyles: string[];
  html: string[];
  icons: string[];
  locales: string[];
  rulesets: string[];
  background: string[];
  webAccessibleResources: string[];
}

interface WebAccessibleFiles {
  webScripts: string[];
  webResources: string[];
}

File Types Managed:

  • Content Scripts: JavaScript/TypeScript files for content script injection
  • Content Styles: CSS files associated with content scripts
  • HTML Pages: Extension pages (popup, options, devtools)
  • Icons: Extension icons and action icons
  • Locales: Internationalization message files
  • Rulesets: Declarative Net Request rule files
  • Background: Service worker and background script files
  • Web Accessible Resources: Files accessible from web pages

Development Server Integration

The file writer integrates with Vite's development server to provide HMR and file watching.

interface FileWriterStartOptions {
  server: ViteDevServer;
}

// Internal file writer functions (not directly exported)
interface FileWriterSystem {
  start(options: FileWriterStartOptions): Promise<void>;
  write(files: OutputFile[]): Promise<void>;
  close(): Promise<void>;
}

Development Features:

  • Hot Module Replacement: Automatic updates for content scripts and extension pages
  • File Watching: Monitors source files for changes
  • Incremental Builds: Only rebuilds changed files and dependencies
  • Error Handling: Graceful error recovery and reporting
  • Performance Optimization: Efficient file processing and caching

File Processing Pipeline

The file writer processes different types of files through specialized pipelines.

Content Script Processing:

  1. TypeScript/JavaScript compilation
  2. Dependency resolution and bundling
  3. HMR client injection (development)
  4. CSS extraction and processing
  5. Web-accessible resource registration
  6. Output file generation

Extension Page Processing:

  1. HTML template processing
  2. Asset reference resolution
  3. Script and style injection
  4. Build optimization
  5. Output generation

Asset Processing:

  1. File copying and optimization
  2. Path resolution and rewriting
  3. Manifest registration
  4. Web-accessible resource handling

Build Coordination

The file writer coordinates between development and production builds.

type BuildMode = 'development' | 'production';

interface BuildContext {
  mode: BuildMode;
  outDir: string;
  publicDir: string;
  isProduction: boolean;
}

Development Mode:

  • Enables HMR for all extension components
  • Injects development utilities and debugging tools
  • Provides live reload for extension runtime
  • Maintains source maps and debugging information

Production Mode:

  • Optimizes and minifies all assets
  • Removes development utilities
  • Generates final extension package
  • Validates manifest and file references

Error Handling and Validation

The file writer includes comprehensive error handling and validation.

File Validation:

  • Manifest file reference validation
  • Asset existence verification
  • Path format checking
  • Dependency resolution validation

Error Recovery:

  • Graceful handling of missing files
  • Build error reporting and recovery
  • Development server error handling
  • Hot reload error recovery

Usage Example:

import { defineConfig } from "vite";
import { crx, defineManifest, allFilesReady } from "@crxjs/vite-plugin";

export default defineConfig({
  plugins: [
    crx({ manifest }),
    {
      name: "extension-validator",
      buildStart: async () => {
        console.log("Starting extension build...");
      },
      generateBundle: async () => {
        try {
          await allFilesReady();
          console.log("All extension files validated and ready");
        } catch (error) {
          console.error("Extension build failed:", error);
          throw error;
        }
      },
    },
  ],
  build: {
    rollupOptions: {
      onwarn(warning, warn) {
        // Handle build warnings
        if (warning.code === 'UNRESOLVED_IMPORT') {
          console.warn(`Unresolved import: ${warning.message}`);
        }
        warn(warning);
      },
    },
  },
});

Advanced File Writer Features

Custom File Processors:

import { CrxPlugin } from "@crxjs/vite-plugin";

const customFileProcessor: CrxPlugin = {
  name: "custom-file-processor",
  renderCrxDevScript(code, script) {
    if (script.type === 'module') {
      // Add custom processing for module scripts
      return `// Custom header\n${code}\n// Custom footer`;
    }
    return code;
  },
  generateBundle(options, bundle) {
    // Process output bundle
    Object.keys(bundle).forEach(fileName => {
      const file = bundle[fileName];
      if (file.type === 'chunk' && fileName.includes('content')) {
        // Custom processing for content script chunks
        file.code = `/* Processed */ ${file.code}`;
      }
    });
  },
};

File System Utilities:

The file writer provides utilities for working with extension file structures, though these are primarily internal APIs used by the plugin system.