Five Server for VS Code

repository·main·Indexed 41 days ago

https://github.com/yandeu/five-server-vscode

An enhanced live server extension for VS Code (version 0.4.0) providing instant file updates, browser log streaming to the terminal, and code highlighting. It includes built-in support for PHP and SSR applications, and can be configured via fiveserver.config.js or fiveserver.config.cjs to manage features like injectBody and remoteLogs.

Tokens
2.4K
Snippets
11
Records
15
Agent score
99%

What's inside Five Server

  1. Using Five Server with PHP

    main
    Five Server can render, display, and live reload .php files. However, it is not a full PHP server. For complex PHP applications, you must manually link the client-side Five Server script with your PHP files. Refer to the five-server-with-php repository for detailed instructions.
  2. Start Five Server in VS Code

    main

    You can launch the Five Server live server using any of the following four methods:

    1. Status Bar: Click the Go Live button in the VS Code status bar (bottom of the editor).
    2. Open File Context Menu: Right-click an open file in the editor and select Open with Five Server.
    3. Explorer Context Menu: Right-click a file in the Explorer sidebar and select Open with Five Server.
    4. Folder Context Menu: Right-click a folder in the Explorer sidebar and select Open with Five Server (root). This sets the selected folder as the server root until the server is stopped.
  3. Configure Five Server via fiveserver.config.js

    main

    Advanced features are disabled by default. To enable them, create a fiveserver.config.js file in the root of your workspace.

    Key Configuration Notes:

    • highlight and injectBody work with .html and .php files.
    • To use the highlight feature, injectBody must be activated.
    • injectBody performs HTML validation and will display a message if your HTML is invalid.
    • remoteLogs displays browser/phone logs in a VS Code terminal named "Five Server".
    • To temporarily disable highlighting for a specific HTML tag, add an H attribute to the tag (e.g., <div H>).
    // fiveserver.config.js
    module.exports = {
      highlight: true, // enable highlight feature
      injectBody: true, // enable instant update
      remoteLogs: true, // enable remoteLogs
      remoteLogs: "yellow", // enable remoteLogs and use the color yellow
      injectCss: false, // disable injecting css
      navigate: true, // enable auto-navigation
    };
  4. Workaround for inline JavaScript issues with injectBody

    main

    The injectBody feature may occasionally conflict with inline JavaScript located inside the <body> tag. To resolve this, ensure your inline scripts execute only after the Five Server connection is established by listening for the connected event on the element with data-id="five-server".

    <script>
      const main = () => {
        console.log("Some JavaScript Code...");
      };
    
      // wait for five-server to connect
      const five = document.querySelector('[data-id="five-server"]');
      if (five) five.addEventListener("connected", main);
      else window.addEventListener("load", main);
    </script>
  5. Troubleshoot Five Server setup

    main

    If the server is not working as expected, check the following:

    1. Conflicts: Ensure the old Live Server extension is uninstalled.
    2. Workspace Context: Ensure you have opened a folder in VS Code, not just a single file.
    3. File Location: Avoid running projects from network drives, USB sticks, or cloud-synced folders like OneDrive or Dropbox, as these can cause file system watching issues.
    4. Version: Verify you are using the latest version of the extension.
  6. Configure Five Server via fiveserver.config.cjs

    main

    You can configure Five Server behavior by creating a fiveserver.config.cjs file in your project root. This file allows you to control features like code highlighting and instant updates (injection).

    module.exports = {
      highlight: true, // enable highlight feature
      injectBody: true, // enable instant update
    };
  7. Configure Five Server settings in VS Code

    main

    Five Server settings are managed within the fiveServer namespace in your VS Code configuration. You can customize the server behavior by setting the following keys in your settings.json file:

    {
      "fiveServer.browser": ["chrome"],
      "fiveServer.ignore": ["node_modules"],
      "fiveServer.navigate": true,
      "fiveServer.php.executable": "/path/to/php",
      "fiveServer.php.ini": "/path/to/php.ini",
      "fiveServer.host": "127.0.0.1",
      "fiveServer.port": 5500,
      "fiveServer.injectBody": true,
      "fiveServer.highlight": true
    }
  8. Decorate files with text and color using decorate()

    main

    The decorate function allows you to add or replace text decorations (like import costs or status messages) at specific lines in a file. It takes a filename, an array of properties containing the text and line number, and a color string. The decorations are rendered as text appearing after the line content.

    Parameters:

    • fileName: The path/name of the file to decorate.
    • props: An array of objects { text: string; line: number; } specifying what to display and on which line.
    • color: The CSS color string for the decoration text.

    Note: The function automatically calls refreshDecorations after updating the internal state.

    import { decorate } from './decorator';
    
    decorate(
      'example.html',
      [
        { text: ' (1.2kb)', line: 10 },
        { text: ' (5.0kb)', line: 15 }
      ],
      '#ff0000'
    );
  9. Refresh decorations for a file with refreshDecorations()

    main

    The refreshDecorations function updates the visual decorations in all visible editors associated with a specific file. It includes a built-in debounce mechanism to prevent excessive UI updates.

    Parameters:

    • fileName: The name of the file to refresh.
    • options: An optional configuration object:
      • delay (number, default: 250): The debounce delay in milliseconds before applying decorations.
      • force (boolean, default: false): If true, bypasses the check that prevents refreshing if the decoration data hasn't changed.

    If force is false and the current decoration state matches the previous state, the refresh is skipped.

    import { refreshDecorations } from './decorator';
    
    // Refresh with default 250ms delay
    refreshDecorations('index.html');
    
    // Force an immediate refresh regardless of changes
    refreshDecorations('index.html', { force: true });