0
# JS Beautify
1
2
JS Beautify is a comprehensive code beautification library that reformat and re-indent JavaScript, CSS, and HTML code. It provides extensive formatting options, supports multiple environments (Node.js, browsers, CLI), and offers both programmatic APIs and command-line interfaces for consistent code formatting across development workflows.
3
4
## Package Information
5
6
- **Package Name**: js-beautify
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install js-beautify`
10
11
## Core Imports
12
13
```javascript
14
// Default import - beautifies JavaScript by default
15
const beautify = require('js-beautify');
16
17
// Named imports for specific beautifiers
18
const { js, css, html } = require('js-beautify');
19
const { js_beautify, css_beautify, html_beautify } = require('js-beautify'); // Legacy names
20
```
21
22
ESM:
23
```javascript
24
import beautify from 'js-beautify';
25
// Access via beautify.js(), beautify.css(), beautify.html()
26
```
27
28
## Basic Usage
29
30
```javascript
31
const beautify = require('js-beautify');
32
33
// Beautify JavaScript (default)
34
const uglifiedJS = 'function test(){var a=1;var b=2;return a+b;}';
35
const beautifiedJS = beautify(uglifiedJS, { indent_size: 2 });
36
37
// Beautify CSS
38
const uglifiedCSS = 'body{margin:0;padding:0;}div{color:red;}';
39
const beautifiedCSS = beautify.css(uglifiedCSS, { indent_size: 4 });
40
41
// Beautify HTML
42
const uglifiedHTML = '<div><p>Hello</p><span>World</span></div>';
43
const beautifiedHTML = beautify.html(uglifiedHTML, {
44
indent_size: 2,
45
wrap_line_length: 80
46
});
47
```
48
49
## Architecture
50
51
JS Beautify is built around several key components:
52
53
- **Unified API**: Single entry point with language-specific methods (`js`, `css`, `html`)
54
- **Beautifier Engines**: Separate parsing and formatting engines for each language
55
- **Options System**: Hierarchical configuration with language-specific overrides
56
- **CLI Integration**: Command-line tools for each beautifier with file processing capabilities
57
- **Cross-Platform**: Works in Node.js, browsers, and provides Python bindings
58
59
## Capabilities
60
61
### JavaScript Beautification
62
63
Core JavaScript code formatting with support for various syntactic constructs, brace styles, and ES6+ features. Includes support for deobfuscation and unpacking of minified code.
64
65
```javascript { .api }
66
/**
67
* Beautify JavaScript source code
68
* @param {string} js_source_text - JavaScript code to beautify
69
* @param {Object} options - Formatting options (optional)
70
* @returns {string} Beautified JavaScript code
71
*/
72
function js_beautify(js_source_text, options);
73
74
/**
75
* Get default options for JavaScript beautifier
76
* @returns {Object} Default options object
77
*/
78
js_beautify.defaultOptions();
79
```
80
81
[JavaScript Beautification](./javascript.md)
82
83
### CSS Beautification
84
85
CSS code formatting with support for selectors, rules, and various CSS syntaxes. Handles formatting of nested structures and preserves important whitespace.
86
87
```javascript { .api }
88
/**
89
* Beautify CSS source code
90
* @param {string} source_text - CSS code to beautify
91
* @param {Object} options - Formatting options (optional)
92
* @returns {string} Beautified CSS code
93
*/
94
function css_beautify(source_text, options);
95
96
/**
97
* Get default options for CSS beautifier
98
* @returns {Object} Default options object
99
*/
100
css_beautify.defaultOptions();
101
```
102
103
[CSS Beautification](./css.md)
104
105
### HTML Beautification
106
107
HTML code formatting with support for various HTML elements, attributes, and embedded JavaScript/CSS. Includes template language support and configurable element handling.
108
109
```javascript { .api }
110
/**
111
* Beautify HTML source code with optional JS/CSS beautification
112
* @param {string} html_source - HTML code to beautify
113
* @param {Object} options - Formatting options (optional)
114
* @param {Function} js_beautify - Custom JavaScript beautifier function (optional)
115
* @param {Function} css_beautify - Custom CSS beautifier function (optional)
116
* @returns {string} Beautified HTML code
117
*/
118
function html_beautify(html_source, options, js_beautify, css_beautify);
119
120
/**
121
* Get default options for HTML beautifier
122
* @returns {Object} Default options object
123
*/
124
html_beautify.defaultOptions();
125
```
126
127
[HTML Beautification](./html.md)
128
129
### Configuration Options
130
131
Comprehensive configuration system with base options shared across all beautifiers and language-specific extensions. Supports EditorConfig integration and hierarchical option inheritance.
132
133
```javascript { .api }
134
/**
135
* Get default options for any beautifier
136
* @returns {Object} Default configuration object with base options
137
*/
138
function defaultOptions();
139
```
140
141
[Configuration](./configuration.md)
142
143
### Command Line Interface
144
145
Command-line tools for each beautifier supporting file processing, glob patterns, and configuration files. Includes support for stdin/stdout processing and batch operations.
146
147
```bash { .api }
148
# JavaScript beautification
149
js-beautify [options] <file1> [file2] ...
150
151
# CSS beautification
152
css-beautify [options] <file1> [file2] ...
153
154
# HTML beautification
155
html-beautify [options] <file1> [file2] ...
156
```
157
158
[Command Line Interface](./cli.md)
159
160
## Types
161
162
```javascript { .api }
163
/**
164
* Main beautify function (JavaScript by default)
165
* @param {string} src - Source code to beautify
166
* @param {Object} config - Formatting options (optional)
167
* @returns {string} Beautified code
168
*/
169
function beautify(src, config);
170
171
// Available beautifier methods
172
beautify.js = js_beautify; // Short alias for JavaScript
173
beautify.css = css_beautify; // Short alias for CSS
174
beautify.html = html_beautify; // Short alias for HTML
175
beautify.js_beautify = js_beautify; // Legacy alias
176
beautify.css_beautify = css_beautify; // Legacy alias
177
beautify.html_beautify = html_beautify; // Legacy alias
178
```