Git Product home page Git Product logo

syn's Introduction

SYN

The Web Bundler for Web Environments

Definition:

/**
 * Bundle a React application
 * @param entry Path to the app entry point (e.g. /index.tsx)
 * @param files Virtual File System containing files needed to bundle the app
 * @param config ESBuild config https://esbuild.github.io/api/#bundle
 * @param importMap The import map used to determine external dependencies
 * @returns a string containing the bundled application code
 */
export async function bundle(
  entry: string,
  files: Map<string, string | Uint8Array>,
  config?: BuildOptions,
  importMap?: Record<string, string>,
): Promise<string> {

  const resolver = new Resolver(files);
  const compiler = new Compiler();

  return await compiler.compile(entry, {
    define: {'process.env.NODE_ENV': '"development"'},
    inject: ['import-react'],
    jsx: 'automatic',
    target: 'esnext',
    format: 'esm',
    jsxDev: true,
    ...config,
  }, [
    png({resolver}),
    svg({resolver}),
    css({resolver}),
    react({resolver, importMap}),
  ]);
}

Usage:

// Import bundler
import {bundle} from 'syn-bundler';

// Create a Virtual File System
const files: Map<string, string | Uint8Array> = new Map();

// Add code to VFS
for (const [name, component] of Object.entries(components)) {
  files.set(`/components/${name}`, component.code);
}

// Add assets to VFS
for (const [name, asset] of Object.entries(assets)) {
  const extension = asset.isVector ? 'svg' : 'png';
  const folder = asset.isVector ? 'vectors' : 'rasters';
  const path = `/assets/${folder}/${name}.${extension}`;
  files.set(path, asset.bytes);
}

// Add entrypoint to VFS
files.set('/index.tsx', mainComponent);

// Bundle VFS
const output = await bundle('/index.tsx', files);

// Print string output (or render in iframe)
console.log(output);

syn's People

Contributors

theultdev avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.