• Stars
    star
    137
  • Rank 266,121 (Top 6 %)
  • Language
    CoffeeScript
  • License
    Other
  • Created almost 11 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

(unmaintained) a unified interface for compiled languages and templates in javascript

accord

npm tests coverage dependencies

A unified interface for compiled languages and templates in JavaScript.

Note: This project is in early development, and versioning is a little different. Read this for more details.

Why should you care?

There are two other libraries that already attempt to provide a common compiler interface: consolidate.js and JSTransformers. After reviewing & using both of them, we designed accord to provide a more maintainable code base and way of writing adapters.

Accord:

  • Uses standard JavaScript inheritance (aka: classes in CoffeeScript) in its adapters
  • Supports source maps
  • Lets you use any major version of an adapter

Installation

npm install accord

Usage

Accord itself exposes only a JavaScript API. If you are interested in using this library from the command line, check out the accord-cli project.

Since some templating engines are async and others are not, accord keeps things consistent by returning a promise for any task (using when.js). Here's an example in CoffeeScript:

fs = require 'fs'
accord = require 'accord'
jade = accord.load('jade')

# render a string
jade.render('body\n  .test')
  .done(console.log.bind(console))

# or a file
jade.renderFile('./example.jade')
  .done(console.log.bind(console))

# or compile a string to a function
# (only some to-html compilers support this, see below)
jade.compile('body\n  .test')
  .done(console.log.bind(console))

# or a file
jade.compileFile('./example.jade')
  .done(console.log.bind(console))

# compile a client-side js template
jade.compileClient('body\n  .test')
  .done (res) -> console.log(res.result.toString())

# or a file
jade.compileFileClient('./example.jade')
  .done (res) -> console.log(res.result.toString())

It's also important to note that accord returns an object rather than a string from each of these methods. You can access the compiled result on the result property of this object. If the adapter supports source maps, the source map will also be on this object if you have passed in the correct options. Docs below should explain the methods executed in the example above.

Accord Methods

  • accord.load(string, object) - loads the compiler named in the first param, npm package with the name must be installed locally, or the optional second param must be the compiler you are after. The second param allows you to load the compiler from elsewhere or load an alternate version if you want, but be careful.

  • accord.supports(string) - quick test to see if accord supports a certain compiler. accepts a string, which is the name of language (like markdown) or a compiler (like marked), returns a boolean.

Accord Adapter Methods

  • adapter.name
  • adapter.render(string, options) - render a string to a compiled string
  • adapter.renderFile(path, options) - render a file to a compiled string
  • adapter.compile(string, options) - compile a string to a function
  • adapter.compileFile(path, options) - compile a file to a function
  • adapter.compileClient(string, options) - compile a string to a client-side-ready function
  • adapter.compileFileClient(string, options) - compile a file to a client-side-ready function
  • adapter.clientHelpers() - some adapters that compile for client also need helpers, this method returns a string of minfied JavaScript with all of them
  • adapter.extensions - array of all file extensions the compiler should match
  • adapter.output - string, expected output extension
  • adapter.engine - the actual compiler, no adapter wrapper, if you need it

Supported Languages

HTML

CSS

JavaScript

Minifiers

Escapers

Evergreen Version Support

As of version 0.20.0, accord ships with a system that can be used to offer full support for any engine across any version, so that the interface remains consistent even in the face of breaking changes to the adapter's API. With this feature in place, you can freely upgrade accord without worrying about any breakage in any libraries you are using, ever.

So for example, if you are using sass and they release a breaking version bump, we will release a new adapter for the new version and cut a new release of accord that includes support for this version. However, if you are still using the old version, it will still work as before so you have as much time as you need to upgrade to the new version.

This does not mean that we immediately support every version of every library infinitely into the past. However, going forward, we will support any new updates to libraries from now on to ensure that nothing breaks for users.

This is a feature that is unique to accord and we are beyond excited to make it available to everyone.

Languages Supporting Compilation

Accord can also compile templates into JavaScript functions, for some languages. This is really useful for client-side rendering. Languages with compile support are listed below. If you try to compile a language without support for it, you will get an error.

  • jade
  • ejs
  • handlebars
  • mustache

We are always looking to add compile support for more languages, but it can be difficult, as client-side template support isn't always the first thing on language authors' minds. Any contributions that help to expand this list are greatly appreciated!

When using a language supporting client-side templates, make sure to check the docs for that language for more details. In general, you'll get back a stringified function from the compileClient or compileFileClient methods, and a string of client helpers from the clientHelpers methods. You can take these, organize them, and write them to files however you wish. Usually the best way is to write the helpers to a file first, then iterate through each of the client-compiled functions, assigning them a name so they can be accessed later on.

Adding Languages

Want to add more languages? We have put extra effort into making the adapter pattern structure understandable and easy to add to and test. Rather than requesting that a language be added, please add a pull request and add it yourself! We are quite responsive and will quickly accept if the implementation is well-tested.

Details on running tests and contributing can be found here

Source Maps

Accord now supports source map generation for any language that also supports source maps. At the moment, this includes the following languages:

  • stylus
  • less
  • myth
  • scss
  • coffeescript
  • minify-js
  • 6to5
  • postcss

Accord returns all source maps as javascript objects, and if available will prefer a v3 sourcemap over any other format. You can find the primary sourcemap on the object returned from accord under the sourcemap key. If there are multiple sourcemaps generated, alternate ones will be avaiable under different keys, which you can find on the object returned from accord after a compile.

To generate a sourcemap, you can pass sourcemap: true as an option to any compiler and you will get back a sourcemap with the file names, sources, and mappings correctly specified, guaranteed. Each compiler also has it's own way of specifying source map options. If you'd like to dive into those details to customize the output, you are welcome to do so, but it is at your own risk.

If there is a language that now supports sourcemaps and you'd like support for them to be added, get a pull request going and we'll make it happen!

License

Licensed under MIT

More Repositories

1

roots

a toolkit for rapid advanced front-end development
CoffeeScript
1,451
star
2

rupture

Simple media queries in stylus
CSS
752
star
3

autoprefixer-stylus

[UNMAINTAINED] Autoprefixer for stylus
JavaScript
154
star
4

gps

🚗 A reasonable way to write css
87
star
5

babel-plugin-import-glob-array

Allows import paths to expand globs and import into an array
JavaScript
42
star
6

roots-www-old

Documentation for v2 of roots. Probably not what you are looking for.
CSS
36
star
7

config

Preferred configuration for any computer I do work on
PHP
30
star
8

color-contrast

Performance-sensitive WCAG contrast ratio calculation
JavaScript
21
star
9

listplayer

A simple headless API for managing audio playlists in the browser
JavaScript
14
star
10

Fontforge-Installer

The most ghetto installer you will ever lay eyes on
Objective-C
13
star
11

dotfiles

My bash profile settings
Shell
13
star
12

atom-fold-comments

(UNMAINTAINED) Quick toggling of all block comments in atom
CoffeeScript
12
star
13

machete

fast, beautiful slideshows in markdown
CoffeeScript
11
star
14

up

painlessly set up a ruby environment on your computer
Shell
10
star
15

axis-www

Documentation for axis!
HTML
8
star
16

stockfighter-node

Node API wrapper for https://www.stockfighter.io
JavaScript
8
star
17

axis-rails

Generators & engine to easily use axis in a rails app
Ruby
8
star
18

objectfn

Map, reduce, forEach, and filter for objects. Lazy evaluation, no dependencies.
JavaScript
7
star
19

goodwebsites.club

an open community of folks making good websites at any scale
TypeScript
7
star
20

fs-snapshot

Creates a snapshot of a directory which can be used to determine which files have changed
JavaScript
7
star
21

cli-js

simple, fast cdnjs command line tool written in node with a public api
JavaScript
6
star
22

burner-api

Host a file for one download, then burn it
Go
5
star
23

zustand-next13-testing

CSS
5
star
24

roots-express

Adds live reload and the roots css library to an express app
JavaScript
5
star
25

indx

require a folder of files into an object
JavaScript
4
star
26

antimatter

flexible command line documentation
CoffeeScript
3
star
27

file-map

A node library that builds a recursive file tree from a root directory
CoffeeScript
3
star
28

lazy_require

Node's require, but lazy loading
CoffeeScript
3
star
29

symlink-rewriter

A small node.js utility for rewriting symlinks
JavaScript
3
star
30

file-matcher

Runs through directories recursively and trashes identical files
Ruby
2
star
31

linkcheck

npm wrapper for filiph/linkcheck
2
star
32

spike-example

a little spike project to be deployed to netlify
JavaScript
1
star
33

spike-collections-test

JavaScript
1
star
34

jenius.me

my personal website
CoffeeScript
1
star
35

testing

Don't worry about this one.
1
star
36

mailTo

A javascript plugin to securely and conveniently handle email addresses, because 'mailto:' is stupid and writing my [at] email [dot] com is also stupid.
JavaScript
1
star