0.3.1 • Published 1 year ago

unifee v0.3.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

unifee

unifee is Edge friendly bundler, all assets bundle into a single HTML.

Motivation

Since Edge Computing Platforms (e.g Cloudflare Workers, Fastly Compute@Edge, etc...) are prepared for production use, occasinally we want to server static contents on the Edge, without origin server.

To server them, it's annoying to route static assets for each paths. It occurs multiple HTTP requests and needs to bundle for each assets into runtime binary (WASM). We'd like to server more simply so we developed easy bundle tool that bundles optimized all assets into single HTML file, we dont' need to think about asset routing and also caching at the Edge for single endpoint.

unifee provides underlying features for development, local server, JavaScript (TypeScript) and CSS compiling, image optimization.

Installation

Install via npmjs.org with your favorite package manager:

$ npm install unifee
$ yarn add unifee

Usage

unifee is cli tool so you can run it on npm package scripts.

Basic

<!doctype html>
<html>
  <head>
    ...
    <link rel="stylesheet" href="./styles.scss">
  </head>
  <body>
    ...
    <img src="./image.png">
    <script src="./script.ts"></script>
  </body>
</html>

On above HTML:

  1. Find "./styles.scss" and compile from SCSS to CSS, replace <link> into <style> as inline
  2. Find "./image.png" and optimize image, replace src attribute value as base64-encoded data-uri
  3. Find "./script.ts" and compile from TypeScript to JavaScript, append built script as inline

Finally you will get single HTML file. See example/basic in detail.

Build yourself

Almost project has your own build system (e.g tailwindcss for CSS, webpack or swc for TypeScript) then unifee also support them. in project directory, you can define unifee spefic npm scripts like:

script namehow it works
unifee:jsBuild JavaScript for your project way
unifee:cssBuild CSS for your project way

If unifee finds above scripts in project directory, use it instead of unifee internal build process. See example/project in detail.

Note that if you have own build scripts, make sure the build artifact is referenced from entry HTML file.

unifee support some mimetypes and languages following table:

Find in HTMLLanguage / MimeTypesCompile / Optimize withBundle
<script src="path/to/script.ts"></script>TypeScriptesbuildinline
<script src="path/to/script.js"></script>JavaScriptesbuildinline
<link rel="stylesheet" href="path/to/styles.scss">SCSSdart-sassinline
<link rel="stylesheet" href="path/to/styles.css">CSSdart-sassinline
<img src="path/to/image.png">image/pngsharpdata-uri
<img src="path/to/image.jpg">image/jpegsharpdata-uri
<img src="path/to/image.gif">image/gifsharpdata-uri
<img src="path/to/image.svg">image/svg+xmlsvgodata-uri

CLI commands

Usage: unifee [options] <directory>

Unify assets into single HTML file

Arguments:
  directory                 build source directory

Options:
  -V, --version             output the version number
  -o, --output <directory>  output target directory
  -s, --server              run dev server and watch changes (default: false)
  -w, --watch               watch source file change
  --yarn                    use yarn instead of npm on project build (default: false)
  -h, --help                display help for command

We show some examples following.

Simple bundle

$ npx unifee -o dist ./src

Find HTML files into src, bundles into single file and output to dist directory.

Local server, hot-reload

$ npx unifee ./src -s

Find HTML files into src, bundles into single file and serve from http://localhost:4001 with hot reloading.

Watch mode

$ npx unifee ./src -w

Find HTML files into src, bundles into single file with watching file changes.

Author

Yoshiaki Sugimoto sugimoto@wnotes.net

License

MIT