• Stars
    star
    336
  • Rank 121,766 (Top 3 %)
  • Language
    JavaScript
  • License
    Other
  • Created about 13 years ago
  • Updated about 6 years ago

Reviews

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

Repository Details

An AMD loader plugin for CoffeeScript

require-cs

A CoffeeScript loader plugin that may work with module loaders like RequireJS, curl and backdraft.

It is known to work with RequireJS 1.0+.

This loader plugin makes it easy to write your JS functionality in CoffeeScript, and easily use it in the browser, Node or Rhino. Plus, if you use the RequireJS optimizer, your CoffeeScript files can be translated to JavaScript, and inlined into optimized layers for fast performance.

In development, it uses XMLHttpRequest to fetch the .coffee files, so you can only fetch files that are on the same domain as the HTML page, and most browsers place restrictions on using XMLHttpRequest from local file URLs, so use a web server to serve your .coffee files.

Install

Volo

To install with Volo:

  volo add require-cs

This will install cs.js and coffee-script.js into the baseUrl folder, and no further configuration is necessary.

Bower

To install with Bower:

  bower install require-cs

Since bower installs require-cs and coffee-script into separate folders, add the following RequireJS package configuration:

{
  baseUrl: '..path_to_packages..'
  packages: [
    {
      name: 'cs',
      location: 'require-cs',
      main: 'cs'
    },
    {
      name: 'coffee-script',
      location: 'coffeescript',
      main: 'extras/coffee-script'
    }
  ]
}

Manual Download

  1. Download CoffeeScript for the browser that registers as an AMD module. You can do that by using a "raw" GitHub URL. It takes the form of:

    https://raw.github.com/jashkenas/coffee-script/[BRANCH-OR-VERSION]/extras/coffee-script.js

Example links:

Place this in the directory that is your baseUrl for your project, or set up a paths config for it for the module ID coffee-script. The cs.js file specifies coffee-script as a dependency.

  1. Download the latest version of cs.js.

Usage

Reference CoffeeScript files via the cs! plugin name. For example, to load the app.coffee file that is in your baseUrl directory:

require(['cs!app'], function (app) {

});

Or, if creating a module that depends on util.coffee:

define(['cs!util'], function (util) {
    util.doSomething();
});

If you are using define() in a module written with CoffeeScript:

define ['cs!util'], (util) ->
    util.doSomething

Literate CoffeeScript was introduced in CoffeeScript 1.5.0. To utilize this feature with this plugin you will need to have downloaded >= 1.5.0 of CoffeeScript and qualify the file (with extension) of the literate module you wish to use.

A dependency on the literate module app.litcoffee:

require ['cs!app.litcoffee'], (litapp) ->
  litapp.foo()
  # ...

Or a dependency on the literate module util.coffee.md:

define ['cs!util.coffee.md'], (litutil) ->
  litutil.doSomething()
  # ...

Note: This plugin supports a mixture of literate and regular CoffeeScript files in the same project.

VERY IMPORTANT: Only define anonymous modules using CoffeeScript. Otherwise, the optimization work will not happen correctly — the name of the module is changed to allow inlining of the translated JS content.

Complete web project

The demo directory shows a complete web example. See the demo/index.html file as the entry point into the demo. It is not a fancy demo, just shows basic use.

If you have node installed and need to run a web server to try out the demo, in this directory run npm install send, then start up the demo web server by running:

node demoserver.js

Optimizing

See demo/build.sh for an example build script that drives the optimizer with the demo/build.js build config.

The build will generate a demo-build directory with the optimized files. Where the unoptimized demo directory will load 7 files, the optimized one only loads 2, and the CoffeeScript files have been converted to JavaScript. Since all the CoffeeScript modules have been converted to JS after the build, the CoffeeScript module and the source cs.js module are not included/needed in the built file.

If you want to do dynamic loading of CoffeeScript files after a build, then comment out stubModules: ['cs'] and exclude: ['coffee-script'] from the build file so that they will be included in the build.

License

MIT

Code of Conduct

jQuery Foundation Code of Conduct.

More Repositories

1

requirejs

A file and module loader for JavaScript
JavaScript
12,922
star
2

r.js

Runs RequireJS in Node and Rhino, and used to run the RequireJS optimizer
JavaScript
2,567
star
3

almond

A minimal AMD API implementation for use after optimized builds
JavaScript
2,423
star
4

text

An AMD loader plugin for loading text resources
JavaScript
932
star
5

example-multipage

Example RequireJS-based project that has multiple pages that share a common set of modules.
JavaScript
838
star
6

example-multipage-shim

Example RequireJS-based project that has multiple pages that share a common set of modules with shim config
JavaScript
382
star
7

require-jquery

A RequireJS+jQuery sample project
JavaScript
261
star
8

alameda

AMD loader, like requirejs, but with promises and for modern browsers
JavaScript
194
star
9

example-jquery-shim

Example project that uses jQuery and jQuery plugins via shim config
JavaScript
190
star
10

domReady

An AMD loader plugin for detecting DOM ready
JavaScript
182
star
11

example-jquery-cdn

Example project that uses jQuery and jQuery plugins wrapped as modules
JavaScript
151
star
12

cajon

JavaScript module loader for the browser that can load CommonJS/node and AMD modules
JavaScript
117
star
13

i18n

An AMD loader plugin for loading internationalization/localization string resources
JavaScript
98
star
14

xrayquire

An auxillary script that gives extra info on the use of requirejs in a web page.
JavaScript
74
star
15

example-libglobal

Use AMD modules to develop a JS library that builds to a lib that can be used by "globals only" scripts or as an AMD module
JavaScript
61
star
16

element

An AMD loader plugin for custom elements
JavaScript
56
star
17

requirejs-bower

Repo hold assets for publishing to the bower package manager.
JavaScript
34
star
18

requirejs-npm

Wrappers to allow using RequireJS-related scripts
JavaScript
22
star
19

step

AMD loader plugin that takes a step config to know how to sequential load some files
JavaScript
20
star
20

prim

Promise lib for use inside requirejs-related projects
JavaScript
7
star
21

requirejs-branding

Branding/images/design for RequireJS web site
CSS
6
star
22

browsertests

Standalone browser capability tests to help inform what is possible in a loader
PHP
3
star
23

alameda-prim

The alameda AMD loader that includes private promise shim
JavaScript
3
star
24

requirejs.github.io

requirejs.org site
HTML
2
star
25

requirejs-nuget

Artifact used for pushing to NuGet
JavaScript
2
star