Skip to content
master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 

#de-heredoc Transform "heredoc call" to "string literal" in JavaScript source code before minifications.

Install

$ npm install de-heredoc

Usage

heredoc is a function to make multiline strings in JavaScript, there are already some libraries you can use e.g. heredoc and multiline.

$ cat test.js
var heredoc = require("heredoc");
var html = heredoc(function(){/*
    <div id="test">
        <span>foo</span>
        <span>bar</span>
    </div>
*/});
$ node
> var deHeredoc = require("de-heredoc")
> deHeredoc("test.js")
(^C again to quit)
>
$ cat test.js
var html='<div id="test">\n    <span>foo</span>\n    <span>bar</span>\n</div>';

Options

deHeredoc(file[, options])

options has 4 properties, from dependency beautify and whitespace.

from

Control file's type.

"file" (default)

file is a file path, change this file in place and return undefined. If the value of from is "file", then the file parameter can be an array.

deHeredoc(["a.js", "b.js", "c.js"], {from: "file"})

"string"

file is a string of JS source code, return the new source code.

deHeredoc('var foo = heredoc(function(){/*foo*/})',{
    from: "string"
}) // 'var foo="foo";'

"ast"

file is an UglifyJS AST object, return the new AST object.

var ast = UglifyJS.parse('var foo = heredoc(function(){/*foo*/})')
var newAst = deHeredoc(ast, {
    from: "ast"
})
newAst.print_to_string() // 'var foo="foo";'

dependency

The heredoc library's name.

"heredoc" (default)

deHeredoc('var multiline = require("maybesomepath/multiline");' +
          'var foo = multiline(function(){/*foo*/})',{
    from: "string",
    dependency: "multiline"
}) // 'var foo="foo";'

###beautify Beautify output source code or not.

false (default)

deHeredoc('if(1+1){var foo=heredoc(function(){/*foo*/})}', {
    from: "string",
    beautify: true
}) 
/*
if (1 + 1) {
    var foo = "foo";
}
*/

###whitespace Control whitespaces in output string literals, see Whitespaces

"indent" (default), "raw" and "oneline".

deHeredoc('var foo=heredoc(function(){/*  foo   */})', {
    from: "string",
    whitespace: "oneline"
}) // 'var foo="foo";'

Whitespaces

You can use parameter name to control whitespaces in output string literals.

indent

Strip the redundant leading whitespaces, and preserve other indents.

var html = heredoc(function(indent){/*
    <div id="test">
        <span>foo</span>
        <span>bar</span>
    </div>
*/});

will be transformed to

var html='<div id="test">\n    <span>foo</span>\n    <span>bar</span>\n</div>';

raw

Preserve all whitespaces.

var html = heredoc(function(raw){/*
    <div id="test">
        <span>foo</span>
        <span>bar</span>
    </div>
*/});

will be transformed to

var html='    <div id="test">\n        <span>foo</span>\n        <span>bar</span>\n    </div>';

oneline

Remove all leading and trailing whitespaces.

var html = heredoc(function(oneline){/*
    <div id="test">
        <span>foo</span>
        <span>bar</span>
    </div>
*/});

will be transformed to

var html='<div id="test"><span>foo</span><span>bar</span></div>';

License

MIT

About

Transform "heredoc call" to "string literal" in JavaScript source code before minifications

Resources

Releases

No releases published

Packages

No packages published