Name: grunt-apps-c
Owner: InterMine
Description: CoffeeScript, JavaScript, JSON, Eco, Mustache as CommonJS/1.1 Modules
Created: 2013-10-31 17:36:51.0
Updated: 2014-02-19 12:12:08.0
Pushed: 2014-02-19 12:12:08.0
Size: 220
Language: CoffeeScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
CoffeeScript, JavaScript, JSON, Eco, Mustache as CommonJS/1.1 Modules. AMD/CommonJS/window external interface.
Example Gruntfile.coffee
:
le.exports = (grunt) ->
grunt.initConfig
pkg: grunt.file.readJSON("package.json")
apps_c:
commonjs:
src: [ 'src/**/*.{coffee,litcoffee,js,json,eco,mustache}' ]
dest: 'build/app.js'
options:
main: 'src/index.js'
name: 'MyApp'
grunt.loadNpmTasks('grunt-apps-c')
grunt.registerTask('default', [ 'apps_c' ])
You can now include the build/app.js
file and, depending on your surrounding environment, you will be able to load it using RequireJS/AMD, CommonJS or straight from window
under the MyApp
key.
The options.main
property specifies which file will be considered the “main” one for your package. Somehow, the external world needs to know what to get when they call require(package_name)
. If you do not specify this property the following actions are taken:
main
as specified in your app's package.json
file. Failing that, we…index.[js|coffee]
file that is closest to the root of your sources.The options.name
overrides the name of the package in package.json
. It specified the name of the exported package as in: require(name)
. One can pass in an array of names, as alternatives, as well.
Sometimes the occasion calls for a loader to be separated out into its own file. One might want, for example, include a loader, then vendor dependencies and then the actual app build.
To create a build in dest
without the loader included:
le.exports = (grunt) ->
grunt.initConfig
pkg: grunt.file.readJSON("package.json")
apps_c:
commonjs:
src: [ 'src/**/*.{coffee,js,eco,mustache}' ]
dest: 'build/app.js'
options:
main: 'src/index.js'
name: 'MyApp'
loader: no
grunt.loadNpmTasks('grunt-apps-c')
grunt.registerTask('default', [ 'apps_c' ])
Notice the boolean loader
option.
By the same token, you might want to produce only a loader:
le.exports = (grunt) ->
grunt.initConfig
pkg: grunt.file.readJSON("package.json")
apps_c:
loader:
dest: 'build/loader.js'
grunt.loadNpmTasks('grunt-apps-c')
grunt.registerTask('default', [ 'apps_c' ])
Notice that we are asking for a loader
target and are providing only dest
; file location where our loader will be created.
The filetypes you can use (handlers) are listed in tasks/
. The base filename of a handler represents the extension we match against.
Languages that have an *
after them support linting.
CoffeeScript is a programming language that transcompiles to JavaScript. The language adds syntactic sugar inspired by Ruby, Python and Haskell to enhance JavaScript's brevity and readability, adding additional features like list comprehension and pattern matching.
Besides being used as an ordinary programming language, CoffeeScript may also be written in “literate” mode. If you name your file with a .litcoffee
extension, you can write it as a Markdown document ? a document that also happens to be executable CoffeeScript code. The compiler will treat any indented blocks (Markdown's way of indicating source code) as code, and ignore the rest as comments.
Save your module with a .js
extension and it will be copied into the build.
Eco templates come precompiled so when you require them, you get back a function that accepts a context
and returns a string back that you inject into DOM.
An example:
template = require('./templates/layout');
ody').html(template({ 'name': 'Radek' }));
Mustache templates are transpiled into an exported string, so you need to use a library to compile it into a template that can actually be rendered.
For example, to use them inside canJS you can do the following:
template = require('./templates/layout');
view.mustache('layout', template);
Save your file as a JSON and it will be exported to you as a JavaScript object.
The following template wraps your modules:
ilename.coffee
ire.register('package/path.js', function(exports, require, module) {
// ...
You can debug all the registered modules in a browser like so:
ct.keys(require.modules)
mkdir -p
, for our builds if they do not exist already..coffee
and .eco
files are both transpiled into one and the same output module.