UNPKG

9.28 kBMarkdownView Raw
1# grunt-jsbeautifier [![Gittip](http://img.shields.io/gittip/vkadam.png)](https://www.gittip.com/vkadam/) [![Build Status](https://travis-ci.org/vkadam/grunt-jsbeautifier.png)](https://travis-ci.org/vkadam/grunt-jsbeautifier) [![NPM](https://nodei.co/npm/grunt-jsbeautifier.png?downloads=true&stars=true)](https://npmjs.org/package/grunt-jsbeautifier)
2
3[jsbeautifier.org](http://jsbeautifier.org/) for grunt
4
5## Getting Started
6This plugin recommends using Grunt `~0.4.1`. Grunt `~0.3.0` is only suported till version 0.1.4
7
8If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
9
10### Grunt `~0.4.1`
11```
12npm install grunt-jsbeautifier --save-dev
13```
14
15One the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
16
17```
18grunt.loadNpmTasks("grunt-jsbeautifier");
19```
20
21### Grunt `~0.3.0`
22Install this grunt plugin next to your project's [grunt.js gruntfile][getting_started] with:
23
24```
25npm install grunt-jsbeautifier@0.1.4
26```
27
28Then add this line to your project's `grunt.js` gruntfile:
29
30```
31grunt.loadNpmTasks("grunt-jsbeautifier");
32```
33
34[grunt]: http://gruntjs.com/
35[getting_started]: https://github.com/gruntjs/grunt/blob/master/docs/getting_started.md
36
37## Examples
38
39### Single Task
40 - files (required): Your source files to beautify
41 - options (optional): Overwrite default jsbeautifier options
42
43```javascript
44"jsbeautifier" : {
45 files : ["src/**/*.js"],
46 options : {
47 }
48}
49```
50
51### Multi Task
52 - src (required): Your source files to beautify
53 - options (optional): Overwrite default jsbeautifier options
54
55```javascript
56"jsbeautifier" : {
57 "default": {
58 src : ["src/**/*.js"]
59 },
60 "git-pre-commit": {
61 src : ["src/**/*.js"],
62 options : {
63 mode:"VERIFY_ONLY"
64 }
65 }
66}
67```
68
69## Config
70
71#### files
72Type: `Files`
73Default value: `[]`
74
75List of files to be beautified. This option is applicable only for **Single Task**
76
77#### src
78Type: `Files`
79Default value: `[]`
80
81List of files to be beautified. This option is applicable only for **Multi Task**
82
83#### options.mode (optional)
84Type: `String`
85Default value: `VERIFY_AND_WRITE`
86
87If mode is "VERIFY_ONLY", then task will fail if at least one file can be beautified. This is useful for pre-commit check.
88
89#### options.dest (optional)
90Type: `String`
91Default value: `""`
92
93All beautified file will be stored under "dest".
94
95#### options.config (optional)
96Type: `String`
97Default value: `null`
98
99Recommended for version < 0.2.7, use `.jsbeautifyrc` for > 0.2.7
100
101If a filename is specified, options defined therein will be used. The `config` file must be valid JSON and looks like the one supported by js-beautify itself. Values defined in gruntfile will take precedence on config file values.
102
103```javascript
104"jsbeautifier": {
105 files: ["src/**/*.js"],
106 options: {
107 config: "path/to/config/file",
108 css: {
109 indentSize: 5
110 },
111 html: {
112 indentSize: 7
113 }
114 }
115},
116```
117
118#### options.jsBeautifyVersion (optional)
119Type: `String`
120Default value: `null`
121
122A valid version/range of [js-beautify](https://github.com/einars/js-beautify) according to [semver](https://github.com/npm/node-semver).
123By default plugin uses latest version of "js-beautify", if you need to use otherwise specify it here.
124
125### Default options from [js-beautify](https://github.com/einars/js-beautify#options) can be used
126```javascript
127 "jsbeautifier": {
128 files: ["src/**/*.js"],
129 options: {
130 config: "path/to/configFile",
131 html: {
132 braceStyle: "collapse",
133 indentChar: " ",
134 indentScripts: "keep",
135 indentSize: 4,
136 maxPreserveNewlines: 10,
137 preserveNewlines: true,
138 unformatted: ["a", "sub", "sup", "b", "i", "u"],
139 wrapLineLength: 0
140 },
141 css: {
142 indentChar: " ",
143 indentSize: 4
144 },
145 js: {
146 braceStyle: "collapse",
147 breakChainedMethods: false,
148 e4x: false,
149 evalCode: false,
150 indentChar: " ",
151 indentLevel: 0,
152 indentSize: 4,
153 indentWithTabs: false,
154 jslintHappy: false,
155 keepArrayIndentation: false,
156 keepFunctionIndentation: false,
157 maxPreserveNewlines: 10,
158 preserveNewlines: true,
159 spaceBeforeConditional: true,
160 spaceInParen: false,
161 unescapeStrings: false,
162 wrapLineLength: 0,
163 endWithNewline: true
164 }
165 }
166 },
167```
168Only specifiy options to overwrite.
169
170**NOTE:** All options can be specified similar to [js-beautify](https://github.com/einars/js-beautify#options) using underscore.
171
172
173### FAQ/Tips
174#### 1. Exclude files
175All files from foo folder except bar.js
176```javascript
177jsbeautifier: {
178 files: ["foo/*.js", "!foo/bar.js"]
179}
180```
181#### 2. Use specific version of js-beautify
182If you want use specific version of js-beautify instead of latest, please refer to [npm shrinkwrap](https://npmjs.org/doc/shrinkwrap.html)
183
184#### 3. Beautify files other than js, json, es6, css & html
185If you need to beautify files other than js, json, css & html, it can be done by passing **fileTypes**.
186
187For example, beautifying **".js.erb"** files **along with ".js"**, beautifying **".less"** files **along with ".css"**, beautifying **".html.erb"** files **along with ".html"**.
188```
189jsbeautifier: {
190 files: ["foo/css/*.css", "bar/css/*.less", "foo/js/*.js", "bar/js/*.js.erb", "foo/html/*.html", "bar/html/*.html.erb"],
191 options: {
192 js: {
193 fileTypes: [".js.erb"]
194 },
195 css: {
196 fileTypes: [".less"]
197 },
198 html: {
199 fileTypes: [".html.erb"]
200 }
201 }
202}
203```
204
205## Release History
206* 0.2.11: Format .es6 as .js by default, [56](https://github.com/vkadam/grunt-jsbeautifier/issues/56)
207* 0.2.10: Support for jsBeautifyVersion, [44](https://github.com/vkadam/grunt-jsbeautifier/issues/44)
208* 0.2.9: Honor end_with_newline attribute of [js-beautify](https://github.com/beautify-web/js-beautify/blob/f67aa02ef7da30b726843c4e3940c85e1757cae6/js/lib/beautify.js#L75). [45](https://github.com/vkadam/grunt-jsbeautifier/issues/45),[46](https://github.com/vkadam/grunt-jsbeautifier/issues/46)
209* 0.2.8: Support for .jsbeautifyrc, [28](https://github.com/vkadam/grunt-jsbeautifier/issues/28)
210* 0.2.7: Use latest dependencies without throwing "latest" keyword warning, [35](https://github.com/vkadam/grunt-jsbeautifier/issues/35)
211* 0.2.6: Bumped version after for changing dist to dest
212* 0.2.5: Support dist folder and show all failed files in VERIFY_MODE [22](https://github.com/vkadam/grunt-jsbeautifier/issues/22), [24](https://github.com/vkadam/grunt-jsbeautifier/issues/24), [25](https://github.com/vkadam/grunt-jsbeautifier/issues/25)
213* 0.2.4: Support custom file types other than js, json, css & html. Use latest versions for dependencies [23](https://github.com/vkadam/grunt-jsbeautifier/issues/23), [26](https://github.com/vkadam/grunt-jsbeautifier/issues/26)
214* 0.2.3: Always use latest version of js-beautify. [21](https://github.com/vkadam/grunt-jsbeautifier/issues/21)
215* 0.2.2: Configuration is now lint happy. Options can be specified in cameCase, [17](https://github.com/vkadam/grunt-jsbeautifier/issues/17)
216* 0.2.1: Added Support for flat config file
217* 0.2.0: Added support for css, html and json files
218* 0.1.10: Added verify only mode [15](https://github.com/vkadam/grunt-jsbeautifier/issues/15). This will fail if any file needs beautification.
219* 0.1.9: Upgraded [js-beautify](https://npmjs.org/package/js-beautify) to 1.4.0. Fixed issues [13](https://github.com/vkadam/grunt-jsbeautifier/issues/13)
220* 0.1.8: Upgraded [js-beautify](https://npmjs.org/package/js-beautify) to ~1.3.1. Fixed issues [10](https://github.com/vkadam/grunt-jsbeautifier/issues/10), [12](https://github.com/vkadam/grunt-jsbeautifier/issues/12)
221* 0.1.7: Upgraded grunt to ~0.4.1, [js-beautify](https://npmjs.org/package/js-beautify) to ~1.2.0. Fixed issues [6](https://github.com/vkadam/grunt-jsbeautifier/issues/6), [7](https://github.com/vkadam/grunt-jsbeautifier/issues/7), [8](https://github.com/vkadam/grunt-jsbeautifier/issues/8), [9](https://github.com/vkadam/grunt-jsbeautifier/issues/9)
222* 0.1.6: Make sure new line at the end of file. Fixed issue [5](https://github.com/vkadam/grunt-jsbeautifier/issues/5)
223* 0.1.5: Upgraded to grunt ~0.4.0. Fixed issues [2](https://github.com/vkadam/grunt-jsbeautifier/issues/2), [3](https://github.com/vkadam/grunt-jsbeautifier/issues/3), [4](https://github.com/vkadam/grunt-jsbeautifier/issues/4)
224* 0.1.4: Upgraded [js-beautify](https://npmjs.org/package/js-beautify) to latest version (0.3.2)
225* 0.1.3: Added fix for block comment formatting.
226* 0.1.2: Supported grunt 0.4~.
227* 0.1.1: Readme file changes
228* 0.1.0: Initial version.
229
230## License
231Copyright (c) 2012 Vishal Kadam
232Licensed under the MIT license.