• Stars
    star
    158
  • Rank 237,131 (Top 5 %)
  • Language
    CoffeeScript
  • License
    BSD 3-Clause "New...
  • Created almost 12 years ago
  • Updated about 5 years ago

Reviews

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

Repository Details

🌈 minimal CommonJS browser bundler with aliasing, extensibility, and source maps

CommonJS Everywhere

CommonJS (node module) browser bundler with source maps from the minified JS bundle to the original source, aliasing for browser overrides, and extensibility for arbitrary compile-to-JS language support.

Install

npm install -g commonjs-everywhere

Usage

CLI

$ bin/cjsify --help

  Usage: cjsify OPT* path/to/entry-file.ext OPT*

  -a, --alias ALIAS:TO      replace requires of file identified by ALIAS with TO
  -h, --handler EXT:MODULE  handle files with extension EXT with module MODULE
  -m, --minify              minify output
  -o, --output FILE         output to FILE instead of stdout
  -r, --root DIR            unqualified requires are relative to DIR; default: cwd
  -s, --source-map FILE     output a source map to FILE
  -v, --verbose             verbose output sent to stderr
  -w, --watch               watch input files/dependencies for changes and rebuild bundle
  -x, --export NAME         export the given entry module as NAME
  --deps                    do not bundle; just list the files that would be bundled
  --help                    display this help message and exit
  --ignore-missing          continue without error when dependency resolution fails
  --inline-source-map       include the source map as a data URI in the generated bundle
  --inline-sources          include source content in generated source maps; default: on
  --node                    include process object; emulate node environment; default: on
  --version                 display the version number and exit

Note: use - as an entry file to accept JavaScript over stdin

Note: to disable an option, prefix it with no-, e.g. --no-node

Example:

Common usage

cjsify src/entry-file.js --export MyLibrary --source-map my-library.js.map >my-library.js

Watch entry file, its dependencies, and even newly added dependencies. Notice that only the files that need to be rebuilt are accessed when one of the watched dependencies are touched. This is a much more efficient approach than simply rebuilding everything.

cjsify -wo my-library.js -x MyLibrary src/entry-file.js

Use a browser-specific version of /lib/node-compatible.js (remember to use root-relative paths for aliasing). An empty alias target is used to delay errors to runtime when requiring the source module (fs in this case).

cjsify -a /lib/node-compatible.js:/lib/browser-compatible.js -a fs: -x MyLibrary lib/entry-file.js

Module Interface

cjsify(entryPoint, root, options) → Spidermonkey AST

Bundles the given file and its dependencies; returns a Spidermonkey AST representation of the bundle. Run the AST through escodegen to generate JS code.

  • entryPoint is a file relative to process.cwd() that will be the initial module marked for inclusion in the bundle as well as the exported module
  • root is the directory to which unqualified requires are relative; defaults to process.cwd()
  • options is an optional object (defaulting to {}) with zero or more of the following properties
    • export: a variable name to add to the global scope; assigned the exported object from the entryPoint module. Any valid Left-Hand-Side Expression may be given instead.
    • aliases: an object whose keys and values are root-rooted paths (/src/file.js), representing values that will replace requires that resolve to the associated keys
    • handlers: an object whose keys are file extensions ('.roy') and whose values are functions from the file contents to either a Spidermonkey-format JS AST like the one esprima produces or a string of JS. Handlers for CoffeeScript and JSON are included by default. If no handler is defined for a file extension, it is assumed to be JavaScript.
    • node: a falsey value causes the bundling phase to omit the process stub that emulates a node environment
    • verbose: log additional operational information to stderr
    • ignoreMissing: continue without error when dependency resolution fails

Examples

CLI example

Say we have the following directory tree:

* todos/
  * components/
    * users/
      - model.coffee
    * todos/
      - index.coffee
  * public/
    * javascripts/

Running the following command will export index.coffee and its dependencies as App.Todos.

cjsify -o public/javascripts/app.js -x App.Todos -r components components/todos/index.coffee

Since the above command specifies components as the root directory for unqualified requires, we are able to require components/users/model.coffee with require 'users/model'. The output file will be public/javascripts/app.js.

Node Module Example

jsAst = (require 'commonjs-everywhere').cjsify 'src/entry-file.coffee', __dirname,
  export: 'MyLibrary'
  aliases:
    '/src/module-that-only-works-in-node.coffee': '/src/module-that-does-the-same-thing-in-the-browser.coffee'
  handlers:
    '.roy': (roySource, filename) ->
      # the Roy compiler outputs JS code right now, so we parse it with esprima
      (require 'esprima').parse (require 'roy').compile roySource, {filename}

{map, code} = (require 'escodegen').generate jsAst,
  sourceMapRoot: __dirname
  sourceMapWithCode: true
  sourceMap: true

Sample Output

More Repositories

1

CoffeeScriptRedux

😓 rewrite of the CoffeeScript compiler with proper compiler design principles and a focus on robustness and extensibility
CoffeeScript
1,844
star
2

everything.js

🌌 a single javascript file that contains every ECMA-262 grammatical production
JavaScript
129
star
3

purescript-demo-mario

🐢 implementation of Elm's Mario demo in PureScript
PureScript
74
star
4

coffee-of-my-dreams

💡 some minor language changes and lots of awesome additions
57
star
5

cscodegen

♻️ CoffeeScript code generator
CoffeeScript
30
star
6

cjs-string-scanner

🔍 string-tokenizing CommonJS module; mimicks Ruby's StringScanner API
CoffeeScript
21
star
7

ambilight

🏮 ambilight clone for your arduino and some WS2801s using boblight
Arduino
16
star
8

bigint-serialiser

a transcoder for serialising JavaScript BigInt values to a Uint8Array or any other array-like object
JavaScript
14
star
9

coffeescript-project

🌱 a great starting point for any CoffeeScript project
CoffeeScript
11
star
10

purescript-spidermonkey-ast

🐒 PureScript bindings for Mozilla's SpiderMonkey AST format
PureScript
10
star
11

FunctionalJS

Extensions to the Function object that allow a more functional programming style
JavaScript
9
star
12

us-states

🗽 mappings from USPS code to US state name
8
star
13

commonjs-everywhere-web

demos for commonjs-everywhere
CoffeeScript
7
star
14

tumblr-scraper

a quick and dirty image scraper for tumblr blogs; no tests, no docs, just sloppy code
Ruby
6
star
15

home-configuration-files

🏡 configuration files commonly found in one's home directory (.bashrc, .vimrc, etc), customized to my personal preferences
Vim Script
6
star
16

chase

an implementation of the not-so-famous chase algorithm, plus a first order logic parser
Haskell
5
star
17

cs502

CS502 projects
C
5
star
18

ECMAScript-Glossary

🔣 A shared vocabulary for people who need to talk about ECMAScript
5
star
19

transmissionDownload

📥 Chrome extension to aid in the addition of torrents to Transmission through the RPC
JavaScript
5
star
20

proposal-first-class-protocols-polyfill

polyfill for first-class protocols runtime component
JavaScript
4
star
21

jedediah

[unmaintained] CLI option parser that behaves as one would expect
CoffeeScript
3
star
22

samevalueset

ECMAScript Set, but using SameValue instead of SameValueZero
JavaScript
3
star
23

MooTools-LazyClass

Generates a MooTools class that lazily loads the real class on first instantiation
JavaScript
3
star
24

MooTools-Range

MooTools class to provide functionality similar to Ruby's Range class
JavaScript
2
star
25

unicode-confusables-data

Unicode's confusables.txt document in a programmatically consumable format
JavaScript
2
star
26

MooTools-Abbrev

MooTools class to calculate the unique abbreviations for a given set of strings
JavaScript
2
star
27

project-euler

solutions to project euler problems
Haskell
2
star
28

MooTools-Struct

MooTools class to generate classes that are used similarly to C's struct construct
JavaScript
2
star
29

purescript-filterable

💡 trying to generalise the concept of a structure that can be filtered
PureScript
2
star
30

esdispatch-based-linter

💡 proof-of-concept pluggable javascript linter with query support
JavaScript
2
star
31

cpsa-ocsp-pcs

modeling optimistic contract signing protocols with private contract signatures in CPSA
Scheme
2
star
32

MARS

Measuring Architectures for Resilient Security
Scala
1
star
33

NDLog-Actors

the beginnings of a naive NDLog implementation in Scala using Actors
Scala
1
star
34

PHPDB

A nice, minimal database framework for PHP
PHP
1
star
35

MooShell-Libs

Provides common JS and CSS resources for use in MooShell demos
JavaScript
1
star
36

purescript-confusables

an implementation of the Confusable Detection algorithm from Unicode Technical Standard #39
PureScript
1
star
37

url-shortener

🔗 a toy application for databases class
CoffeeScript
1
star
38

MooTools-StringScanner

A MooTools class that performs lexical scanning operations on a String
JavaScript
1
star
39

neo4j-experiment

some neo4j stuff for class
Ruby
1
star
40

resume

👔 my personal résumé, in latex
Ruby
1
star
41

purescript-demo-url-shortener

URL shortener demo app in PureScript
PureScript
1
star
42

MooTools-Footnotes

Finds all links or citations for a given element and generates footnotes
JavaScript
1
star
43

node-farmhash

🐮 native node bindings to the FarmHash family of hash functions
C++
1
star
44

MooTools-Inspect

adds an inspect method to native javascript classes that returns a human-readable interpretation of the object
JavaScript
1
star
45

ssss.hs

👭 Shamir's Secret Sharing Scheme
Haskell
1
star
46

droid-turbo-wireless-charging-cradle

3D model of charging cradle for Droid Turbo wireless charger
1
star
47

PHPutil

a small, random collection of useful PHP utility functions
PHP
1
star