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 🙏

© 2024 – Pkg Stats / Ryan Hefner

fusion

v1.0.0-beta

Published

JavaScript, HTML, and CSS language framework.

Downloads

68

Readme

fusion: A language framework and superset for JavaScript, HTML, and CSS

Framework

Fusion provides a simple and straightforward collection of functions to analyize JavaScript, HTML, and CSS. A syntax highlighter along with other lexer-parser utilities for these languages are available in this framework. The lexical analysis phase also provides an optional lightweight parser hack to allow for fast and efficient conversions while still offering language transitions and advanced detection of regexp and object literals.

Language

Fusion utilizes various language components of JavaScript, HTML, and CSS to provide more robust and maintainable code. This superset aims for a very natural development platform that seamlessly transitions between each language. It not only makes source code easier to understand and maintain, but can also remove the need for JavaScript libraries such as jQuery to construct DOM elements using document fragments and innerHTML. Since it's designed to work natively with the DOM, Fusion can increase performance by assisting front-end libraries or sometimes eliminating the need for them altogether. Otherwise, an optional lightweight DOM library (3KB, gzipped) is also provided to further simplify your code and make working with the DOM a little more intuitive by extending Element and Array objects.

Fusion does not have any releases and is still a work in progress. Please feel free to create an issue if you'd like to propose any other language features.

Contents

Features

Syntax highlighting

fusion.highlight(source [, language [, strict]])

Returns: string of HTML

Lexical analysis

fusion.tokenize(source [, language [, strict]])

Returns: array of { type, start, end } objects

Syntax + Semantic analysis

Eventually...

Arguments
  • source
    • string of source code
  • language
    • string of source code language
    • supports js, html, css, and fjs
    • defaults to fjs
  • strict
    • boolean for strict mode, which forces the js, html, and css languages to be context-free
    • removes <script> and <style> language transitions in html
    • removes template strings and advanced regexp and object literal detection in js
    • defaults to false

Converting Fusion to JavaScript

fusion.transpile(source [, create [, find [, query [, attr [, html]]]]])

Returns: string of JavaScript

Arguments
  • source
    • string of fusion source code
  • create
    • string for a function similar to createElement()
    • defaults to document.__create
  • find
    • string for a function similar to querySelector()
    • defaults to document.__find
  • query
    • string for a function similar to querySelectorAll()
    • defaults to document.__query
  • attr
    • string for a function similar to setAttribute()
    • defaults to __attr
    • must return a reference to this
  • html
    • string for a function similar to innerHTML property
    • defaults to __html
    • must return a reference to this

Language Features

Inline HTML

Inline HTML

Template strings

Template String

Template strings in HTML attributes

Template String in HTML Attribute

Inline CSS styles

Inline CSS Style

Inline CSS selectors

Inline CSS Selectors

Template substitutions in HTML

Inline HTML Substitution

Template substitutions in CSS styles

Inline CSS Style Substitution

Template substitutions in CSS selectors

Inline CSS Selector Substitution

Setup

If you'd like to natively use the DOM, the following __attr() and __html() functions must be assigned to Element.prototype for inline HTML and CSS:

Element.prototype.__attr = function(n,v){this.setAttribute(n,v);return this};
Element.prototype.__html = function(c){this.innerHTML=c;return this};

The transpile() function can then be called with the first three optional arguments as follows:

fusion.transpile(source, "document.createElement", "document.querySelector", "document.querySelectorAll")

Or they can be omitted if assigned to Document.prototype:

Document.prototype.__create = Document.prototype.createElement;
Document.prototype.__find   = Document.prototype.querySelector;
Document.prototype.__query  = Document.prototype.querySelectorAll;

Otherwise, an optional lightweight DOM plugin (3KB, gzipped) can be included using fusion-dom.js.

Node.js

The following example configures Node.js to transpile files in the /fjs directory and cache them in memory when requested through the /static URL:

var http    = require('http'),
    through = require('through'),
    st      = require('st'),
    fusion  = require('fusion');

var mount  = st({ url:  '/static', path: __dirname + '/fjs' }),
    filter = through(function(data) { this.emit('data', fusion.transpile(data.toString())) });

http.createServer(function(req, res)
{
    res.filter = filter;

    if (mount(req, res))
        res.setHeader('content-type', 'application/javascript');
})
.listen(1337);

console.log('Server running on port 1337...');

This concept can also be applied with the fusion.highlight() function (and either a text/plain or text/html content-type) to serve up highlighted HTML of the source code.

Settings for the st() function can be found here: https://github.com/isaacs/st

DOM Library (optional)

The fusion-dom.js file can be included to extend the Element, Array, and Document prototypes. This file does NOT require fusion.js, and the prototype assignments above are also not required when including this library since it already defines __attr() and __html() functions on the Element prototype and __create(), __find(), and __query() functions on the Document prototype.

These prototype functions can be accessed natively or using the @ operator:

@(div.container).__attr('title', null);
@(div.container)@attr('title', null);

This DOM library allows a seamless transition between arrays and elements. The __find() function returns either an element or null, and the __query() function returns either an array of elements or an empty array.

Element

  • after()
  • append()
  • attr()
  • before()
  • child()
  • children()
  • contains()
  • find()
  • first()
  • html()
  • last()
  • matches()
  • next()
  • parent()
  • prepend()
  • prev()
  • query()
  • remove()
  • replace()
  • style()
  • text()
  • document
  • length
  • tag

Array

  • attrs()
  • children()
  • filter()
  • find()
  • first()
  • html()
  • last()
  • matches()
  • next()
  • not()
  • parent()
  • prev()
  • query()
  • remove()
  • styles()
  • text()

Document

  • contains()
  • create()
  • find()
  • query()
[
	<div />,
	<div />,
	<pre />
]
@attrs("data-ready", Date.now())
@prependTo(element)
@style(@{
	-webkit-animation: slide ${duration / 1000}s ease-in-out;
});

document
	@query("span")
	@attrs("style", null)
	@first()
		@attr("data-ready", Date.now())
		@after(<span class="second" />);