UNPKG

5.1 kBMarkdownView Raw
1# grunt-coffee-build
2
3> Compiles Coffeescript files, optionally merging and generating source maps.
4
5## Getting Started
6```shell
7npm install grunt-coffee-build --save-dev
8```
9
10Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
11
12```js
13grunt.loadNpmTasks('grunt-coffee-build');
14```
15
16### Overview
17
18This task will take care of compiling, merging and generating source maps for
19your .coffee/.js files. It also supports merging javascript files, and the
20resulting source map will contain information about each individual file.
21
22While the name is 'coffee-build', this task may be used with javascript-only
23projects just for the automatic dependency resolution and merged source map
24generation.
25
26Unlike other solutions that normalize commonjs projects to work in web
27browsers, this one won't bundle small commonjs runtime. Instead, it will
28parse all require calls for relative paths, concatenate files in dependency
29order while wrapping each file into a commonjs-like closure. All require
30calls are replaced by a module identifier generated from the file path(An
31idea taken from the google-traceur compiler).
32
33The task integrates nicely with grunt-contrib-watch, as it will keep an
34in-memory cache of the compiled files and their modification date, so only
35modified files will be recompiled.
36
37This intends to provide a one-stop solution for building projects for
38web browsers or node.js using coffeescript and/or javascript
39
40### Example usage
41
42This example shows how you can configure a project that includes third party
43libraries and needs to have platform-specific builds (browser/node.js):
44
45```coffeescript
46# Gruntfile.coffee
47grunt.initConfig
48 # This project should work seamless in browser and node.js. Each platform
49 # will have a single .js file containing all the code, and a single .map
50 # file that can be used to easily debug using node-inspector or any browser
51 # that supports source map debugging.
52
53 coffee_build:
54 options:
55 # default options
56 wrap: true # wrap the result into an anonymous function
57 sourceMap: true # generate source maps
58 browser_build:
59 options:
60 # Merge the third party library into the browser dist, but disable source
61 # map generation and module wrapping for it. The browser_export.js file
62 # will export the public API to the window object(it needs to be included last).
63 disableModuleWrap: ['third_party/lib.js', 'platform/browser_export.js']
64 disableSourceMap: ['third_party/lib.js']
65 files: [
66 # src files
67 {src: ['third_party/lib.js', 'src/**/*.coffee'], dest: './dist/browser_src.js'}
68 # Test files. Since sources are likely to be required by the test files,
69 # 'browser_test' is the only file that needs to be included in the
70 # index.html that bootstraps the tests
71 {src: ['third_party/lib.js', 'src/**/*.coffee', 'platform/browser_export.js' ], dest: './dist/browser_test.js'}
72 ]
73 nodejs_build:
74 options:
75 # The node.js version dont need to merge the library, but cannot include
76 # it using require in the sources shared with the browser(require calls to
77 # relative paths are preprocessed), so we use a special node-only file that is
78 # concatenated first and will require the library into the package namespace.
79 # Besides dependency initialization, this file might contain nodejs-specific code,
80 # so we dont include it in 'disableSourceMap' for debugging. We also need to
81 # export the package API, so the 'index.js' file is not wrapped into a module so
82 # the 'module/exports' names are bound to the nodejs module object.
83 disableModuleWrap: ['platform/nodejs_init.js', 'platform/nodejs_export.js']
84 files: [
85 # src files
86 {src: ['platform/nodejs_init.js', 'src/**/*.coffee', 'platform/nodejs_export.js'], dest: './dist/nodejs_src.js'}
87 # test files
88 {src: ['platform/nodejs_init.js', 'test/**/*.coffee', 'platform/nodejs_export.js'], dest: './dist/nodejs_test.js'}
89 ]
90```
91
92As the above example shows, when a filename is passed to 'dest', the task will
93concatenate all files, generating a source map that maps to the original files.
94
95By default each merged file is wrapped into a module function that simulates a
96commonjs environment, excluding files passed to the 'disableModuleWrap'
97option(useful if you want a piece of code that isn't bound to a scope that has
98module/exports defined).
99
100### Comments
101
102The main reason I wrote this task is because I couldn't get any existing grunt
103task to do what I wanted: Provide me with a single javascript file/source map
104that maps(correctly) to the original source files and that lets me easily
105integrate javascript/coffeescript with automatic dependency resolution, while
106letting me handle platform-specific particularities without runtime hacks.
107
108The source maps generated by this task work flawless(at least in my tests),
109debugging with [node-inspector](https://github.com/node-inspector/node-inspector)(0.3.2)
110or google chrome should just work.