• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

tarunc/gulp-jsbeautifier: Beautify js, css, html and json files using Gulp and h ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

tarunc/gulp-jsbeautifier

开源软件地址(OpenSource Url):

https://github.com/tarunc/gulp-jsbeautifier

开源编程语言(OpenSource Language):

JavaScript 100.0%

开源软件介绍(OpenSource Introduction):

gulp-jsbeautifier

Build Status npm version Coverage Status Code Climate Dependency Status devDependency Status

Beautifier for JavaScript, JSON, HTML and CSS.
js-beautify for gulp.

Install

npm install --save-dev gulp-jsbeautifier

Basic Usage

const gulp = require('gulp');
const beautify = require('gulp-jsbeautifier');

gulp.task('beautify', () =>
  gulp.src(['./*.css', './*.html', './*.js'])
    .pipe(beautify())
    .pipe(gulp.dest('./dist'))
);

Options

All options are optional.

Plugin options

css, html, js

Type: Object
Default value: { file_types: [...] }

Contains specific beautifier options for CSS, HTML and JavaScript.

  • file_types
    Type: Array
    Default value for css: ['.css', '.less', '.sass', '.scss']
    Default value for html: ['.html']
    Default value for js: ['.js', '.json']

    Specifies which files should be treated as CSS, HTML or JavaScript.

// In addition to the default file_types, the '.eslintrc' file is also considered as JavaScript.
gulp.task('beautify', () =>
  gulp.src(['./*.js', './*.json', './.eslintrc'])
    .pipe(beautify({
      js: {
        file_types: ['.eslintrc']
      }
    }))
    .pipe(gulp.dest('./dist'))
);

config

Type: String
Default value: null

If you provide a path to a configuration file, the options defined in it will be loaded.
Otherwise, a configuration file will be automatically searched as explained in cosmiconfig docs.

The configuration file must be a valid JSON or YAML and can contain all the options of this documentation except config (it will be ignored).

// Use options loaded from './config/jsbeautify.json'.
gulp.task('beautify', () =>
  gulp.src('./*.js')
    .pipe(beautify({
      config: './config/jsbeautify.json'
    }))
    .pipe(gulp.dest('./dist'))
);

// Use options automatically loaded from './jsbeautifyrc'.
gulp.task('beautify', () =>
  gulp.src('./*.js')
    .pipe(beautify())
    .pipe(gulp.dest('./dist'))
);

debug

Type: Boolean
Default value: false

If true lists the options loaded from the configuration file and the final ones.
if you encounter any problems with the options try to enable it and use the reporter.

// Shows debug messages.
gulp.task('beautify', () =>
  gulp.src(['./*.css', './*.html', './*.js'])
    .pipe(beautify({
      debug: true
    }))
    .pipe(gulp.dest('./dist'))
);

Beautifier options

The beautifier options are the same underscored options used by js-beautify and use the same setting inheritance.
See the js-beautify docs for a list of them.

// The indentation is 4 spaces for CSS and HTML, it's 1 tab for JavaScript.
gulp.task('beautify', () =>
  gulp.src(['./*.css', './*.html', './*.js'])
    .pipe(beautify({
      indent_size: 4,
      js: {
        indent_char: '\t',
        indent_size: 1
      }
    }))
    .pipe(gulp.dest('./dist'))
);

The options provided as a parameter in gulp will be merged with those in the configuration file.
The merge order is: default values, configuration file, parameters.
Subsequent options overwrite the previous ones.

// 'config.json'
// 4 spaces indentation for CSS and HTML.
// 1 tab indentation for JavaScript.
{
  "indent_char": " ",
  "indent_size": 4,
  "js": {
    "indent_char": "\t",
    "indent_size": 1
  }
}

// Overwrite the indentation defined in 'config.json' with 2 tab for CSS and HTML.
// JavaScript files continue to mantain 1 tab indentation.
gulp.task('beautify', () =>
  gulp.src(['./*.css', './*.html', './*.js'])
    .pipe(beautify({
      config: './config.json',
      indent_char: '\t',
      indent_size: 2
    }))
    .pipe(gulp.dest('./dist'))
);

// Options resulting after merge (only the relevant parts).
{
    "indent_char": "\t",
    "indent_size": 2,
    "js": {
      "indent_char": "\t",
      "indent_size": 1
    }
}

Validate

Checks if it is possible to beautify some files.
The reporter is responsible for displaying the validate results and will emit an error before the stream ends if a file could be beautified.

var gulp = require('gulp');
var beautify = require('gulp-jsbeautifier');

gulp.task('beautify', () =>
  gulp.src(['./*.css', './*.html', './*.js'])
    .pipe(beautify.validate())
    .pipe(beautify.reporter())
);

Reporter

Lists files that have been beautified, those already beautified, and those that can not be beautified.
If the validate feature is used, the reporter lists files that can be beautified and emits an error before the stream ends if such a file was detected.

var gulp = require('gulp');
var beautify = require('gulp-jsbeautifier');

gulp.task('beautify', () =>
  gulp.src(['./*.css', './*.html', './*.js'])
    .pipe(beautify())
    .pipe(beautify.reporter())
    .pipe(gulp.dest('./dist'))
);

Reporter options

verbosity

Type: number
Default value: beautify.report.BEAUTIFIED
Other values: beautify.report.ALL

With BEAUTIFIED value, the reporter lists only beautified files (or those that can be beautified in the case of validate).
With ALL value, the reporter also lists the other files.

var gulp = require('gulp');
var beautify = require('gulp-jsbeautifier');

gulp.task('beautify', () =>
  gulp.src(['./*.css', './*.html', './*.js'])
    .pipe(beautify())
    .pipe(beautify.reporter({
      verbosity: beautify.report.ALL
    }))
    .pipe(gulp.dest('./dist'))
);

License

gulp-jsbeautifier is released under the MIT License.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
beautify-web/js-beautify: Beautifier for javascript发布时间:2022-06-21
下一篇:
aluxian/nwjs-starter: NW.js starter project powered by Gulp.发布时间:2022-06-21
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap