1 | # grunt-coffee-build
|
2 |
|
3 | > Compiles Coffeescript files, optionally merging and generating source maps.
|
4 |
|
5 | ## Getting Started
|
6 | ```shell
|
7 | npm install grunt-coffee-build --save-dev
|
8 | ```
|
9 |
|
10 | Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
|
11 |
|
12 | ```js
|
13 | grunt.loadNpmTasks('grunt-coffee-build');
|
14 | ```
|
15 |
|
16 | ### Overview
|
17 |
|
18 | This task will take care of compiling, merging and generating source maps for
|
19 | your .coffee/.js files. It also supports merging javascript files, and the
|
20 | resulting source map will contain information about each individual file.
|
21 |
|
22 | While the name is 'coffee-build', this task may be used with javascript-only
|
23 | projects just for the automatic dependency resolution and merged source map
|
24 | generation.
|
25 |
|
26 | Unlike other solutions that normalize commonjs projects to work in web
|
27 | browsers, this one won't bundle small commonjs runtime. Instead, it will
|
28 | parse all require calls for relative paths, concatenate files in dependency
|
29 | order while wrapping each file into a commonjs-like closure. All require
|
30 | calls are replaced by a module identifier generated from the file path(An
|
31 | idea taken from the google-traceur compiler).
|
32 |
|
33 | The task integrates nicely with grunt-contrib-watch, as it will keep an
|
34 | in-memory cache of the compiled files and their modification date, so only
|
35 | modified files will be recompiled.
|
36 |
|
37 | This intends to provide a one-stop solution for building projects for
|
38 | web browsers or node.js using coffeescript and/or javascript
|
39 |
|
40 | ### Example usage
|
41 |
|
42 | This example shows how you can configure a project that includes third party
|
43 | libraries and needs to have platform-specific builds (browser/node.js):
|
44 |
|
45 | ```coffeescript
|
46 | # Gruntfile.coffee
|
47 | grunt.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 |
|
92 | As the above example shows, when a filename is passed to 'dest', the task will
|
93 | concatenate all files, generating a source map that maps to the original files.
|
94 |
|
95 | By default each merged file is wrapped into a module function that simulates a
|
96 | commonjs environment, excluding files passed to the 'disableModuleWrap'
|
97 | option(useful if you want a piece of code that isn't bound to a scope that has
|
98 | module/exports defined).
|
99 |
|
100 | ### Comments
|
101 |
|
102 | The main reason I wrote this task is because I couldn't get any existing grunt
|
103 | task to do what I wanted: Provide me with a single javascript file/source map
|
104 | that maps(correctly) to the original source files and that lets me easily
|
105 | integrate javascript/coffeescript with automatic dependency resolution, while
|
106 | letting me handle platform-specific particularities without runtime hacks.
|
107 |
|
108 | The source maps generated by this task work flawless(at least in my tests),
|
109 | debugging with [node-inspector](https://github.com/node-inspector/node-inspector)(0.3.2)
|
110 | or google chrome should just work.
|