OpenComponents Server-Side Logic Compiler

4.0.0 · active · verified Sun Apr 19

oc-server-compiler is a foundational module within the OpenComponents (OC) micro-frontends framework, designed specifically to compile the server-side logic (`server.js` or `entry.server.ts`) of isomorphic OC components. These components encapsulate HTML, CSS, JavaScript, and optional server-side code, allowing for universal rendering. This package facilitates the transformation of a component's server-side JavaScript/TypeScript into an executable format, typically used by an OC registry for server-side rendering or by the OC CLI during component development and publishing. It is a core utility that ensures the component's data-fetching and model-composition logic is correctly processed for deployment. The current stable version is 4.0.0. While the OpenComponents project itself follows semantic versioning with releases driven by feature development and bug fixes, this compiler module's updates are tightly coupled with the broader framework's evolution. A key differentiator of OpenComponents is its language-agnostic approach to consumption, allowing components compiled by this module to be rendered and consumed by various backend technologies (e.g., C#, PHP, Java, Go) without requiring Node.js on the edge.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically use `oc-server-compiler` to compile a mock OpenComponents `server.js` file, simulating the build process for component server logic.

import { compile } from 'oc-server-compiler';
import * as fs from 'fs';
import * as path from 'path';

async function runCompilation() {
  const componentPath = path.join(process.cwd(), 'my-component');
  const serverJsPath = path.join(componentPath, 'server.js');
  const componentPackageJsonPath = path.join(componentPath, 'package.json');

  // Create a dummy component directory and files for demonstration
  if (!fs.existsSync(componentPath)) {
    fs.mkdirSync(componentPath, { recursive: true });
  }
  fs.writeFileSync(serverJsPath, `
    module.exports = { 
      data: (context, callback) => {
        console.log('Executing server.js data function...');
        callback(null, { name: context.params.name || 'World', version: '4.0.0' });
      }
    };
  `);
  fs.writeFileSync(componentPackageJsonPath, `
    {
      "name": "my-component",
      "version": "1.0.0",
      "oc": {
        "files": {
          "data": "server.js",
          "template": {
            "src": "template.html",
            "type": "handlebars"
          }
        }
      }
    }
  `);

  try {
    // The compile function takes the component's directory path
    // and returns compiled assets or metadata.
    const result = await compile({
      componentPath: componentPath,
      minify: true,
      verbose: process.env.DEBUG === 'true'
    });
    console.log('Compilation successful! Output:');
    console.log(JSON.stringify(result, null, 2));
    // Expected result might include: { entry: 'compiled/server.js', hash: '...', externals: [] }
  } catch (error) {
    console.error('Compilation failed:', error);
  } finally {
    // Clean up dummy files
    fs.unlinkSync(serverJsPath);
    fs.unlinkSync(componentPackageJsonPath);
    fs.rmdirSync(componentPath);
  }
}

runCompilation();

view raw JSON →