npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

playwright-dev-server

v0.1.1

Published

A development server for injecting scripts into web pages using Playwright

Readme

Playwright Dev Server

A plugin-based development server for injecting scripts and styles into web pages using Playwright. Inspired by Vite's architecture, it provides a flexible and extensible platform for web development automation.

Features

  • 🔌 Plugin-based Architecture: Extensible plugin system similar to Vite
  • 🎯 Multi-platform Support: Manage multiple browser instances with different configurations
  • 🔄 Hot Reload: Automatic script and style injection with file watching
  • 📝 Event-driven: Rich event system for plugin communication
  • 🎨 Built-in Plugins: Console logging, auto-reload, script/style injection
  • Fast Development: Optimized for rapid development workflows

Quick Start

Installation

npm install playwright-dev-server

Initialize Configuration

npx playwright-dev init

This creates a playwright-dev.config.js file with example configuration.

Start Development Server

npx playwright-dev start

Configuration

Basic Configuration

import { defineConfig, consoleLoggerPlugin, autoReloadPlugin } from 'playwright-dev-server';

export default defineConfig({
  platforms: {
    myApp: {
      name: 'My Application',
      url: 'http://localhost:3000',
      scripts: [
        {
          path: './scripts/debug.js',
          order: 1,
          autoInject: true,
        }
      ],
      styles: [
        {
          path: './styles/debug.css',
          order: 1,
          autoInject: true,
        }
      ],
      browserOptions: {
        viewport: { width: 1280, height: 720 }
      }
    }
  },
  
  plugins: [
    consoleLoggerPlugin(),
    autoReloadPlugin(),
  ],
  
  // 日志配置
  logging: {
    enabled: true,  // 启用日志输出,默认为 false
    prefix: '[DEV]' // 可选的日志前缀
  },
  
  browserOptions: {
    headless: false,
    devtools: true,
  }
});

日志管理

Playwright Dev Server 提供了统一的日志管理系统,支持动态开启和关闭日志输出。

配置日志

在配置文件中设置日志选项:

export default defineConfig({
  logging: {
    enabled: true,  // 启用日志输出,默认为 false
    prefix: '[DEV]' // 可选的日志前缀
  },
  // ... 其他配置
});

API 控制日志

通过服务器实例动态控制日志:

import { PlaywrightDevServer } from 'playwright-dev-server';

const server = new PlaywrightDevServer(config);

// 启用日志
server.enableLogging();

// 禁用日志
server.disableLogging();

// 检查日志状态
console.log(server.isLoggingEnabled()); // true/false

// 设置日志前缀
server.setLogPrefix('[CUSTOM]');

// 通过配置更新日志设置
server.updateConfig({
  logging: {
    enabled: true,
    prefix: '[UPDATED]'
  }
});

直接使用 Logger

也可以直接导入和使用 Logger 实例:

import { logger } from 'playwright-dev-server';

// 各种日志级别
logger.log('普通日志');
logger.info('信息日志');
logger.warn('警告日志');
logger.error('错误日志');
logger.debug('调试日志');

// 性能计时
logger.time('操作耗时');
// ... 执行操作
logger.timeEnd('操作耗时');

Plugin System

Built-in Plugins

Console Logger Plugin

Captures and displays browser console output in your terminal.

import { consoleLoggerPlugin } from 'playwright-dev-server';

export default defineConfig({
  plugins: [
    consoleLoggerPlugin()
  ]
});

Auto Reload Plugin

Automatically reloads pages when HTML/CSS files change, with CSS hot-reload support.

import { autoReloadPlugin } from 'playwright-dev-server';

export default defineConfig({
  plugins: [
    autoReloadPlugin()
  ]
});

Creating Custom Plugins

Plugins are functions that return plugin objects with lifecycle hooks:

function myCustomPlugin() {
  return {
    name: 'my-custom-plugin',
    order: 100, // Execution order (lower = earlier)
    
    // Server lifecycle
    buildStart() {
      console.log('Server starting...');
    },
    
    buildEnd() {
      console.log('Server started!');
    },
    
    // Platform lifecycle
    platformCreated(platformId, page) {
      console.log(`Platform ${platformId} created`);
    },
    
    platformReady(platformId, page) {
      console.log(`Platform ${platformId} ready`);
    },
    
    // File watching
    fileChanged(filePath, event) {
      console.log(`File ${filePath} was ${event}`);
    },
    
    // Content transformation
    transformScript(code, scriptPath, platformId) {
      // Modify script content before injection
      return `console.log('Loading ${scriptPath}');\n${code}`;
    },
    
    transformStyle(code, stylePath, platformId) {
      // Modify style content before injection
      return `/* ${stylePath} */\n${code}`;
    }
  };
}

Plugin Hooks

  • buildStart: Called when server starts
  • buildEnd: Called when server is ready
  • platformCreated: Called when a platform page is created
  • platformReady: Called when a platform page is ready for interaction
  • fileChanged: Called when watched files change
  • transformScript: Transform script content before injection
  • transformStyle: Transform style content before injection

Event System

Plugins can listen to and emit events:

function eventListenerPlugin() {
  return {
    name: 'event-listener',
    
    buildStart() {
      // Listen to events
      this.on('platform:ready', ({ platformId, page }) => {
        console.log(`Platform ${platformId} is ready!`);
      });
      
      // Emit custom events
      this.emit('custom:event', { data: 'hello' });
    }
  };
}

API Reference

PlaywrightDevServer

import { PlaywrightDevServer } from 'playwright-dev-server';

const server = new PlaywrightDevServer(config);

// Start server
await server.start();

// Navigate platform
await server.navigatePage('myApp', 'http://localhost:3001');

// Inject script manually
await server.injectScript('myApp', './scripts/test.js');

// Get page list
const pages = await server.getPageList();

// Stop server
await server.stop();

Examples

Performance Monitoring Plugin

function performancePlugin() {
  return {
    name: 'performance-monitor',
    
    async platformReady(platformId, page) {
      await page.addScriptTag({
        content: `
          window.addEventListener('load', () => {
            const perfData = performance.getEntriesByType('navigation')[0];
            console.log('Page Load Time:', perfData.loadEventEnd - perfData.fetchStart);
          });
        `
      });
    }
  };
}

Auto Screenshot Plugin

function autoScreenshotPlugin(options = {}) {
  return {
    name: 'auto-screenshot',
    
    async platformCreated(platformId, page) {
      page.on('pageerror', async () => {
        const filename = `error-${platformId}-${Date.now()}.png`;
        await page.screenshot({ path: filename });
        console.log(`Screenshot saved: ${filename}`);
      });
    }
  };
}

License

MIT