html2js

    0.2.0 • Public • Published

    html2js

    NPM version Build Status

    simple html2js processer, i use it solve requirejs text crossdomain

    Useage

    html2js(source[, options])

    Options:

    • mode: A string naming js code style. Optional. Possible values:

      • undefined (default): keep space and carriage of source html
      • compress: strip the extra spaces or carriage at the beginnings and ends of the lines
      • format: format js code as spec
    • wrap: A string naming js code wrapper. Optional. Possible values:

      • undefined (default): no wrapper
      • amd: wrap with AMD
      • commonjs: wrap with CommonJS
    • ignoreScriptTag: boolean to ignore fix split the script tag when writing it with document write. Defaults to false

    Example

    npm run example
    

    test.html:

    <div id="i-am-a-id">
      <div class="i-am-a-class">
        i am test
      </div>
    </div>

    test.html.js:

    /**
     * html2js(source, {
     *     mode: 'default'
     *     wrap: 'amd'
     * })
     */
    define(function () {return '<div id="i-am-a-id">\n  <div class="i-am-a-class">\n    i am test\n  </div>\n</div>\n<script>\n    var test = \'test\';\n    alert(test);\n</'+'script>\n';});
    
    /**
     * html2js(source, {
     *     mode: 'default'
     *     wrap: 'commonjs'
     * })
     */
    module.exports = '<div id="i-am-a-id">\n  <div class="i-am-a-class">\n    i am test\n  </div>\n</div>\n<script>\n    var test = \'test\';\n    alert(test);\n</'+'script>\n';
    
    /**
     * html2js(source, {
     *     mode: 'default'
     *     wrap: false
     * })
     */
    '<div id="i-am-a-id">\n  <div class="i-am-a-class">\n    i am test\n  </div>\n</div>\n<script>\n    var test = \'test\';\n    alert(test);\n</'+'script>\n'
    
    /**
     * html2js(source, {
     *     mode: 'format'
     *     wrap: 'amd'
     * })
     */
    define(function () {
        return ''
            + '<div id="i-am-a-id">'
            +   '<div class="i-am-a-class">'
            +     'i am test'
            +   '</div>'
            + '</div>'
            + '<script>'
            +     'var test = \'test\';'
            +     'alert(test);'
            + '</'+'script>'
            + '';
    });
    
    /**
     * html2js(source, {
     *     mode: 'format'
     *     wrap: 'commonjs'
     * })
     */
    module.exports = ''
            + '<div id="i-am-a-id">'
            +   '<div class="i-am-a-class">'
            +     'i am test'
            +   '</div>'
            + '</div>'
            + '<script>'
            +     'var test = \'test\';'
            +     'alert(test);'
            + '</'+'script>'
            + '';
    
    /**
     * html2js(source, {
     *     mode: 'format'
     *     wrap: false
     * })
     */
    ''
            + '<div id="i-am-a-id">'
            +   '<div class="i-am-a-class">'
            +     'i am test'
            +   '</div>'
            + '</div>'
            + '<script>'
            +     'var test = \'test\';'
            +     'alert(test);'
            + '</'+'script>'
            + ''
    
    /**
     * html2js(source, {
     *     mode: 'compress'
     *     wrap: 'amd'
     * })
     */
    define(function () {return '<div id="i-am-a-id"><div class="i-am-a-class">i am test</div></div><script>var test = \'test\';alert(test);</'+'script>';});
    
    /**
     * html2js(source, {
     *     mode: 'compress'
     *     wrap: 'commonjs'
     * })
     */
    module.exports = '<div id="i-am-a-id"><div class="i-am-a-class">i am test</div></div><script>var test = \'test\';alert(test);</'+'script>';
    
    /**
     * html2js(source, {
     *     mode: 'compress'
     *     wrap: false
     * })
     */
    '<div id="i-am-a-id"><div class="i-am-a-class">i am test</div></div><script>var test = \'test\';alert(test);</'+'script>'
    

    Test

    npm test
    

    Keywords

    none

    Install

    npm i html2js

    DownloadsWeekly Downloads

    272

    Version

    0.2.0

    License

    none

    Last publish

    Collaborators

    • avatar