Git Product home page Git Product logo

grunt-processhtml's Introduction

grunt-processhtml

NPM version Build Status NPM downloads

Join the chat at https://gitter.im/dciccale/grunt-processhtml

Process html files at build time to modify them depending on the release environment

Looking for the stand-alone version?

Use node-htmlprocessor

Getting Started

This plugin requires Grunt ^1.0.1

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-processhtml --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-processhtml');

The "processhtml" task

Process html files with special comments:

<!-- build:<type>[:target] [inline] [value] -->
...
<!-- /build -->
type

This is required.

Types: js, css, remove, template, include or any html attribute if written like this: [href], [src], etc.

target

This is optional.

Is the target name of your grunt task, for example: dist. Is supported for all types, so you can always specify the target if needed.

You can pass multiple comma-separated targets, e.g. <!-- build:remove:dist,dev,prod --> and block will be parsed for each.

inline

This modifier can be used with js and css types.

If used, the styles or scripts will be included in the output html file.

value

Required for types: js, css, include and [attr].

Optional for types: remove, template, js and css types with inline modifier.

Could be a file name: script.min.js or a path if an attribute like [src] is specified to keep the original file name intact but replace its path.

Simple examples

build:js[:targets] [inline] <value>

Replace many script tags into one.

[:targets] Optional build targets.

inline Optional modifier.

<value> Required value: A file path.

<!-- build:js app.min.js -->
<script src="my/lib/path/lib.js"></script>
<script src="my/deep/development/path/script.js"></script>
<!-- /build -->

<!-- changed to -->
<script src="app.min.js"></script>

You can embed your javascript:

<!-- build:js inline app.min.js -->
<script src="my/lib/path/lib.js"></script>
<script src="my/deep/development/path/script.js"></script>
<!-- /build -->

<!-- changed to -->
<script>
  // app.min.js code here
</script>

or

<!-- build:js inline -->
<script src="my/lib/path/lib.js"></script>
<script src="my/deep/development/path/script.js"></script>
<!-- /build -->

<!-- changed to -->
<script>
  // my/lib/path/lib.js code here then...
  // my/deep/development/path/script.js code goes here
</script>
build:css[:targets] [inline] <value>

Replace many stylesheet link tags into one.

[:targets] Optional build targets.

inline Optional modifier.

<value> Required value: A file path.

<!-- build:css style.min.css -->
<link rel="stylesheet" href="path/to/normalize.css">
<link rel="stylesheet" href="path/to/main.css">
<!-- /build -->

<!-- changed to -->
<link rel="stylesheet" href="style.min.css">

You can embed your styles like with js type above:

<!-- build:css inline -->
<link rel="stylesheet" href="path/to/normalize.css">
<link rel="stylesheet" href="path/to/main.css">
<!-- /build -->

<!-- changed to -->
<style>
  /* path/to/normalize.css */
  /* path/to/main.css */
</style>

or

<!-- build:css inline style.min.css -->
<link rel="stylesheet" href="path/to/normalize.css">
<link rel="stylesheet" href="path/to/main.css">
<!-- /build -->

<!-- changed to -->
<style>
  /* style.min.css */
</style>
build:<[attr]>[:targets] <value>

Change the value of an attribute. In most cases using [src] and [href] will be enough but it works with any html attribute.

<[attr]> Required html attribute, i.e. [src], [href].

[:targets] Optional build targets.

<value> Required value: A path, a file path or any string value

<!-- If only a path is used, the original file name will remain -->

<!-- build:[src] js/ -->
<script src="my/lib/path/lib.js"></script>
<script src="my/deep/development/path/script.js"></script>
<!-- /build -->

<!-- changed the src attribute path -->
<script src="js/lib.js"></script>
<script src="js/script.js"></script>

<!-- build:[href] img/ -->
<link rel="apple-touch-icon-precomposed" href="skins/demo/img/icon.png">
<link rel="apple-touch-icon-precomposed" href="skins/demo/img/icon-72x72.png" sizes="72x72">
<!-- /build -->

<!-- changed the href attribute path -->
<link rel="apple-touch-icon-precomposed" href="img/icon.png">
<link rel="apple-touch-icon-precomposed" href="img/icon-72x72.png" sizes="72x72">

<!-- build:[class]:dist production -->
<html class="debug_mode">
<!-- /build -->

<!-- this will change the class to 'production' only when the 'dist' build is executed -->
<html class="production">
build:include[:targets] <value>

Include an external file.

[:targets] Optional build targets.

<value> Required value: A file path.

<!-- build:include header.html -->
This will be replaced by the content of header.html
<!-- /build -->

<!-- build:include:dev dev/content.html -->
This will be replaced by the content of dev/content.html
<!-- /build -->

<!-- build:include:dist dist/content.html -->
This will be replaced by the content of dist/content.html
<!-- /build -->
build:template[:targets]

Process a template block with a data object inside options.data.

[:targets] Optional build targets.

<!-- build:template
<p>Hello, <%= name %></p>
/build -->

<!--
notice that the template block is commented
to prevent breaking the html file and keeping it functional
-->
build:remove[:targets]

Remove a block.

[:targets] Optional build targets

<!-- build:remove -->
<p>This will be removed when any processhtml target is done</p>
<!-- /build -->

<!-- build:remove:dist -->
<p>But this one only when doing processhtml:dist target</p>
<!-- /build -->

Overview

In your project's Gruntfile, add a section named processhtml to the data object passed into grunt.initConfig().

grunt.initConfig({
  processhtml: {
    options: {
      // Task-specific options go here.
    },
    your_target: {
      // Target-specific file lists and/or options go here.
    },
  },
})

Options

options.process

Type: Boolean Default value: false

Process the entire html file through grunt.template.process, a default object with the build target will be passed to the template in the form of {environment: target} where environment will be the build target of the grunt task.

Important note: The process option is not needed if you don't want to process the entire html file. See the example below to see that you can have templates blocks to be processed.

If you do want to process the whole file as a template, it will be compiled after compiling the inside template blocks if any.

options.environment

Type: Object Default value: target

The environemnt variable will be available to use in the comments, it defaults to the task target.

options.data

Type: Object Default value: {}

An object data that is passed to the html file used to compile all template blocks and the entire file if process is true.

options.templateSettings

Type: Object Default value: null (Will use default lodash template delimiters <% and %>)

Define the templateSettings option with lodash templateSettings options to customize the template syntax.

templateSettings: {
  interpolate: /{{([\s\S]+?)}}/g // mustache
}

options.includeBase

Type: String Default value: null (Will use the path of the including file)

Specify an optional path to look for include files. ie, app/assets/includes/

options.commentMarker

Type: String Default value: build

Specify the word used to indicate the special begin/end comments. This is useful if you want to use this plugin in conjunction with other plugins that use a similar, conflicting build:<type> comment (such as grunt-usemin).

With options.commentMarker set to process, a typical comment would look like:

<!-- process:<type>[:targets] [value] -->
...
<!-- /process -->

options.strip

Type: Boolean Default value: null

Specifying true will strip comments which do not match the current target:

strip: true

options.recursive

Type: Boolean Default value: false

Recursively process files that are being included using build:include.

recursive: true

options.customBlockTypes

Type: Array Default value: []

Define an array of .js files that define custom block types.

customBlockTypes: ['custom-blocktype.js']

A custom block type example:

custom-blocktype.js

'use strict';

module.exports = function (processor) {
  // This will allow to use this <!-- build:customBlock[:target] <value> --> syntax
  processor.registerBlockType('customBlock', function (content, block, blockLine, blockContent) {
    var title = '<h1>' + block.asset + '</h1>';
    var result = content.replace(blockLine, title);

    return result;
  });
};

file.html

<!-- build:customBlock myValue -->
<p>This will be replaced with the result of the custom block above</p>
<!-- /build -->

The result will be

<h1>myValue</h1>

Usage Examples

Default Options

Set the task in your grunt file which is going to process the index.html file and save the output to dest/index.html

grunt.initConfig({
  processhtml: {
    options: {
      data: {
        message: 'Hello world!'
      }
    },
    dist: {
      files: {
        'dest/index.html': ['index.html']
      }
    }
  }
});

What will be processed?

Following the previous task configuration, the index.html could look like this:

<!doctype html>
<title>title</title>

<!-- build:[href] img/ -->
<link rel="apple-touch-icon-precomposed" href="my/theme/img/apple-touch-icon-precomposed.png">
<link rel="apple-touch-icon-precomposed" href="my/theme/img/apple-touch-icon-72x72-precomposed.png" sizes="72x72">
<!-- /build -->

<!-- build:css style.min.css -->
<link rel="stylesheet" href="normalize.css">
<link rel="stylesheet" href="main.css">
<!-- /build -->

<!-- build:js app.min.js -->
<script src="js/libs/require.js" data-main="js/config.js"></script>
<!-- /build -->

<!-- build:include header.html -->
This will be replaced by the content of header.html
<!-- /build -->

<!-- build:template
<p><%= message %></p>
/build -->

<!-- build:remove -->
<p>This is the html file without being processed</p>
<!-- /build -->

After processing this file, the output will be:

<!doctype html>
<title>title</title>

<link rel="apple-touch-icon-precomposed" href="img/apple-touch-icon-precomposed.png">
<link rel="apple-touch-icon-precomposed" href="img/apple-touch-icon-72x72-precomposed.png" sizes="72x72">

<link rel="stylesheet" href="style.min.css">

<script src="app.min.js"></script>

<h1>Content from header.html</h1>

<p>Hello world!</p>

Advanced example

In this example there are multiple targets where we can process the html file depending on which target is being run.

grunt.initConfig({
  processhtml: {
    dev: {
      options: {
        data: {
          message: 'This is development environment'
        }
      },
      files: {
        'dev/index.html': ['index.html']
      }
    },
    dist: {
      options: {
        process: true,
        data: {
          title: 'My app',
          message: 'This is production distribution'
        }
      },
      files: {
        'dest/index.html': ['index.html']
      }
    },
    custom: {
      options: {
        templateSettings: {
          interpolate: /{{([\s\S]+?)}}/g // mustache
        },
        data: {
          message: 'This has custom template delimiters'
        }
      },
      files: {
        'custom/custom.html': ['custom.html']
      }
    }
  }
});

The index.html to be processed (the custom.html is below):

<!doctype html>
<!-- notice that no special comment is used here, as process is true.
if you don't mind having <%= title %> as the title of your app
when not being processed; is a perfectly valid title string -->
<title><%= title %></title>

<!-- build:css style.min.css -->
<link rel="stylesheet" href="normalize.css">
<link rel="stylesheet" href="main.css">
<!-- /build -->

<!-- build:template
<p><%= message %></p>
/build -->

<!-- build:remove -->
<p>This is the html file without being processed</p>
<!-- /build -->

<!-- build:remove:dist -->
<script src="js/libs/require.js" data-main="js/config.js"></script>
<!-- /build -->

<!-- build:template
<% if (environment === 'dev') { %>
<script src="app.js"></script>
<% } else { %>
<script src="app.min.js"></script>
<% } %>
/build -->

The custom.html to be processed:

<!doctype html>
<html>
  <head>
    <title>Custom template delimiters example</title>
  </head>

  <body>
    <!-- build:template
    {{ message }}
    /build -->
  </body>
</html>

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Release History

  • 0.4.1 [email protected]
  • 0.4.0 Update Grunt to 1.0
  • 0.3.13 [email protected] and clone data object (#85)
  • 0.3.12 Update node-htmlprocessor to version 0.2.2 (escape regex from remove)
  • 0.3.11 get ready for grunt v1.0.0
  • 0.3.10 Update node-htmlprocessor to version 0.2.1
  • 0.3.9 Update node-htmlprocessor to version 0.2.0
  • 0.3.8 Fix #74
  • 0.3.7 Update node-htmlprocessor dependency with added inline modifier
  • 0.3.6 Update node-htmlprocessor version and add specific test for templates
  • 0.3.5 Fixes issue when passing data to a template
  • 0.3.4 Fixes issue when passing a path te replace an [attr]
  • 0.3.3 Add node-htmlprocessor as a dependency
  • 0.3.2 Fix/feature #39
  • 0.3.1 Fix #35
  • 0.3.0 Allow creating custom block types.
  • 0.2.9 Added recursive option
  • 0.2.8 Changed include to not use replace()
  • 0.2.7 Added commentMarker option
  • 0.2.6 Fix #14 and added grunt-release
  • 0.2.5 Create first tag using grunt-release
  • 0.2.3 Fix #8
  • 0.2.2 Small code refactor
  • 0.2.1 Added templateSettings option tu customize template delimiters
  • 0.2.0 Added the build:include feature to include any external file
  • 0.1.1 Lint js files inside tasks/lib/
  • 0.1.0 Initial release

grunt-processhtml's People

Contributors

cvan avatar dciccale avatar denisgorbachev avatar dependabot[bot] avatar dsine-de avatar geoctrl avatar geranyl avatar gitter-badger avatar jacquerie avatar jdreesen avatar jonyd avatar marcobiedermann avatar maxbates avatar nickchristensen avatar orthographic-pedant avatar steveoh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

grunt-processhtml's Issues

include replaces the whole line

i'm sure i'm doing something wrong as i think this was working earlier.

I am using the include function to add base64 images to the splash screen of an app .

#splashScreen .splashContent {
  ...
  background: transparent url("data:image/jpg;base64,<!-- build:include src/images/backgrounds/horizon-small.b64 --><!-- /build -->") center center no-repeat;
}

That seems to delete the whole css line rather then replace the tags. Am i doing something wrong?

Conditional build comment not working

Hi,

Firstly thanks this is a great module, much better than the more popular similar one. I am having an issue though with the following: (using latest node, grunt and proceshtml)

index.html:

<!-- build:remove:prod -->
<link rel="stylesheet" href="app/assets/css/base.css">
<link rel="stylesheet" href="app/assets/css/components.css">
<link rel="stylesheet" href="app/assets/css/modules.css">
<!-- /build -->
<!-- build:prod
<link rel="stylesheet" href="app/assets/css/style.min.css">
/build -->

Gruntfile.js:

processhtml: {
    dev: {
        files: {
            'build/dev/index.html': ['index.html']
        }
    },
    prod: {
        files: {
            'build/prod/index.html': ['index.html']
        }
    }
}

After running processhtml:prod the resulting index.html:

<!-- build
<link rel="stylesheet" href="app/assets/css/style.min.css">
/build -->

So the first build:remove:prod works but the second conditional comment does not. FYI in your doc examples it says to do build:prod:remove which doesn't work.

Thanks

enable attributes in block tag

Hello, thanks for this plugin :)

I don't know if I'm missing something, but I couldn't find information about this...

I've an enhancement to propose: enable passing block arguments in the block tag.
For example:

<!-- build:inline(css) css/style.css -->
  <link rel="stylesheet" type="text/css" href="css/style.css" />
<!-- /process -->
<!-- process:inline(js) js/script.js -->
  <script src="js/script.js" type="text/javascript" charset="utf-8"></script>
<!-- /process -->

In this way is possible to reuse code inside customBlocks avoiding duplicating the same code for little different things ( in this case the inline block puts the content of the file inline in the html ).

Maybe there is way to acheive this but currently I'm using a little hack :)

Thanks again

base / include won't work as include filename

Both

<!-- build:include ./base.html -->
<!-- /build -->

and

<!-- build:include ./include.html -->
<!-- /build -->

will result in comment being written through, instead of being processed.

Strip option with template

I just try to set strip option to true and does not work with template

    <!-- build:template:dist
        some code
    /build -->

And when run another target 'some code' stay at document.

ProcessHTML not running on certain html file.

I have been able to get ProcessHTML to work brilliantly on my other HTML files , unfortunately it doesn't seem to run for a specific file .

<!DOCTYPE html>
<html lang="en">
<head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Contact Us</title>

        <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
        <!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
            <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
        <![endif]-->

        <link href='https://fonts.googleapis.com/css?family=RobotoDraft:400,500,700,400italic' rel='stylesheet' type='text/css'>



        <!-- build:css http://limitlesssoftware.net/theme/css/final.min.css -->
        <link rel="stylesheet" href="http://limitlesssoftware.net/theme/css/font-awesome.min.css">
        <link rel="stylesheet" href="http://limitlesssoftware.net/theme/css/main.min.css" />
        <!-- /build -->


        <!-- Icons -->
        <link rel="shortcut icon" href="http://limitlesssoftware.net/theme/images/favicon.ico" type="image/x-icon" />
        <link rel="apple-touch-icon" href="http://limitlesssoftware.net/theme/images/apple-touch-icon.png" />
</head>

It looks to me as if the usage is the same as in my other files , yet for some reason it will not process

Include blocktype

Hi, Iโ€™ve committed a new blocktype useful with SSI environment.
With this upgrade you can process HTML pieces included with mod_include.
Here's simple usage:

  • in development environment apache manages virtual directive obviously ignoring "build" comment
  • after building processhtml includes html processed fragment

Bye

Small typo in README.md

Hello,

There is a small typo in README.md:

This will be replaced with the result fo the custom block above

Best regards,
Denis

Can I include only in a particular target?

I have several targets configured. Instead of doing:

<!-- build:remove:target1,target3 -->
<myelement></myelement>
<!-- /build -->

I'd like to be able to do:

<!-- build:keep:target2 -->
<myelement></myelement>
<!-- /build -->

Is there any way to do this currently, or could you add this, or could you point me in the right direction so I can issue a pull request ?

include gets confused by regexp

start
<script>
<!-- build:include test_inc.js -->
<!-- /build -->
</script>                                                                                           
ende
// test_inc.js:  (snippet from angular.ui-router)
function c(a){return a.replace(/[\\\[\]\^$*+?.()|{}]/g,"\\$&")}
var d

result:

start
<script>
function c(a){return a.replace(/[\\\[\]\^$*+?.()|{}]/g,"\\<!-- build:include test_inc.js -->
<!-- /build -->")}
var d

</script>
ende

Add wildcard support 'files' config option

It would be great enhancement to support wildcards inside 'files' argument, in order to bulk-process bunch of files through single-line config like:

grunt.initConfig({
  processhtml: {
    dist: {
      files: {
        'dest/*': '*.html'
      }
    }

Using Include with Path Relative to Root of Project

Hey, thanks for the fantastic plugin!

I've got a couple of pages that are nested within folders, but they're generated dynamically. Is there a way to use a path that is relative to the project root? Or where the gruntfile lives?

I'm trying something along these lines, and it just fails silently upon not finding the file in question ... keep in mind, this file is in my 'dist' folder.

<!-- build:include /src/snippets/google_webfonts.html -->

It also does not work with an absolute file path:

<!-- build:include /Users/me/my_private_things/src/snippets/google_webfonts.html -->

It works when I do this:

<!-- build:include ../src/snippets/google_webfonts.html -->

But then I have more dynamically generated files in 'dist/people/' which means I would have to change it for each of those subfiles, which is only a problem because I use the same partial for for the different files in question... Any ideas?

Thanks!

Handle nested comments

I need to remove various segments of code from my index.html to create different distributions. There are some sections that I would like to remove in multiple targets, and some sections where I need to use nested tags. It doesn't appear that grunt-processhtml handles nested comments, e.g.:

        <!-- process:remove:two -->
        <script src="scripts/app.js"></script>
        <script src="scripts/modules/authWait.js"></script>
        <!-- process:remove:one -->
        <script src="scripts/services/user.js"></script>
        <script src="scripts/services/auth.js"></script>
        <!-- /process --> <!-- one -->
        <!-- /process --> <!-- two -->

In the mean time, I set up each target to use its own options.commentMarker but that leaves all the other processhtml comment blocks in the HTML. I feel that handling nested comments fulfills more use cases than supporting a comma-separated list of targets in the comment (though that could be useful too).

Thanks

Template variable including `$&` breaks build

Hi,

I am pretty new to grunt, so this my be a mistake coming from me. But I have noticed this strange behaviour: when trying to include a variable containing the string $&, the generated output seem to actually $& this by the previous template comment bit.
Here is an example of the config and the output:

Template

 <!-- build:template
       <p><%= message %></p>
     /build -->
    <!-- build:template
      <p><%= test %></p>
    /build -->

Grunt conf

processhtml: {
            options: {
                data: {
                    message: ' Hello ',
                    test: 'text_$&_text'
                }
            },
            dist: {
                files: {
                    'build/index.html': ['src/index.html']
                }
            }
        },

Output

<p> Hello </p>
    <p>text_    <!-- build:template
      <p><%= test %></p>
    /build -->_text</p>

Expected Output

<p> Hello </p>
<p>text_$&_text</p>

issue with newline character '\n'

I am including processhtml blocks in index.html through my custom generator by appending each line with a newline character.

Ex:
'\n' +
''

I am including the above string and it appears like

When I run processhtml, it is not including the script because 'content.indexOf(blockLine)' will be -1 as in the 'content' all the \r\n's are replaced with \n and in the 'blockLine' the \r\n's aren't replaced.

Can some body look into that issue. Please?

Is it possible to keep the processhtml comment blocks?

I have a use case where I don't want the processhtml comment blocks removed, is this possible?

It may be a edge case, but I need this feature because I'm using the below grunt work flow.

  1. The bower block inserts the boostrap library

  2. grunt-uncss is run on the bootstrap library and exported to styles/vendor.css

  3. processhtml changes the href from the boostrap link to styles/vendor.css

  4. Finally usemin minifies, revs, etc. and exports to my dist folder

Thanks for any help, or suggestions.

Properly Globbing HTML Files?

I'm wondering what the proper way to setup the Grunt configuration would be for my situation.

I'm looking to process HTML files from several subfolders in my app directory and mirror their directories and names in my dist folder. I've tried doing this:

processhtml: {
  options: {
    commentMarker: 'processhtml'
  },
  dist: {
    files: {
      '<%= yeoman.dist %>/**/*': ['app/**/*.html']
    }
  }
}

But that just creates a ** folder under my dist directory and a file within it called *. What I'm looking to do is if I have HTML files with paths app/views/foo.html and app/views/forms/bar.html to get migrated to dist/views/foo.html and dist/views/forms/bar.html without having to specify each file. Is that possible?

Thanks!

How to replace multiple attributes?

Hi, i want to replace rel attribute also. Is it possible to replace two or more attributes?

<!-- build:[href] /styles.min.css -->
<link rel="stylesheet/less" href="/app/styles/styles.less">
<!-- /build -->

Missing feature in remove block?

Hi,
I was wondering if it was possible to remove and that remain after running a grunt build without the 'processhtml::'.
I mean: if I run a grunt build with that feature, it correctly hides the content and the comment itself; but if I run a grunt build without that feature, I expect the final file to have just the content without the comment.
Is it possible or is it a missing feature?

Regards.

build:js fails if in an include

I've noticed that if you include, say, a header.html and use the build:css rule inside that, it works as expected and you see the changed stylesheet call, plus the file include also works.

However, if you try the same with build:js, it fails to change the text (though the file include works)

Some notes:

  • if I put the code of the footer.html file directly inside my document instead of as in include, the switch happens as expected. So it's not a malformed call on my part.
  • moving the footer include above the header include has no effect (the build:js still fails and the build:css still works). So it's not that processhtml can only do it one or some other recursive problem (that I can spot)

Any ideas?

Update: My mistake, the build:css doesn't work either, nor does including inside includes. Looks like it just isn't set to do recursion at all. Sorry for the confusion.

Data does not work

When trying to use data tokens, e.g:

<%= message %>

And having in the grunt task:

 dev: {
    options: {
        data: {
            message: 'Hello world'
        }
    },
    files: {
        'build/dev/index.html': ['index.html']
    }
}

However after running the processhtml:dev the token is left and not converted.

  • Using latest processhtml, node and grunt

How to select dynamically files to process ?

I'm unable to process a bunch of .html files.
I'm referring to grunt doc.
But when I run the following script, I get an error.

    // Process HTML
    processhtml: {
      dist: {
        files: {
          expand: true,
          cwd: 'patternlab-php/public/patterns/',
          src: ['**/*.html'],
          dest: '/',
        }
      }
    },
$ grunt processhtml
Running "processhtml:dist" (processhtml) task
Warning: Object true has no method 'indexOf' Use --force to continue.

Aborted due to warnings.

Any idea what I'm doing wrong ?

Add possibility to inline please

I have

<!-- build:css:prod style.min.css -->
<link rel="stylesheet" href="css/style1.css"/>
<link rel="stylesheet" href="css/style2.css"/>
<!-- /build -->

And i am trying to inline result, like
<style type="text/css">body{padding-top:20px}...</style>

Can't get working

I'm following your Readme and can't get this to work for the life of me. Any gotcha's I should be aware of?

I assume this will copy the index.html to dest/index.html after parsing the index.html file?

Order of attributes matters

If I do something like this

<!-- build:[data-main] bootstap_v0.0.0 -->
<script data-main="bootstrap" src="my/lib/path/require.js"></script>
<!-- /build -->

it will remove the src attribute.

If i do this

<!-- build:[data-main] bootstap_v0.0.0 -->
<script src="my/lib/path/require.js" data-main="bootstrap"></script>
<!-- /build -->

than it works.

Versioning Assets (using `grunt.template.today`)

Hi,

If I were to have the following other Grunt tasks before processhtml

  • src/coffee/**/*.coffee โ†’ /js/application-<%= grunt.template.today(format) %>.min.js
  • src/less/**/*.less โ†’ /css/application-<%= grunt.template.today(format) %>.min.css

How could I get processhtml to use the versioned asset file names? (e.g. /css/application-20131120110505.min.css

processing multiple html files

Is there a way to process multiple html files without having to explicitly name each file?

I was hoping for something that you either dont specify the destination, meaning it would update itself. useful if the files have already been copied to their destination. i.e.

      files: ['*.html','partials/*.html']

or, you set a baseUrl with a destUrl and the files in the normal way. i.e.

      baseUrl: 'app',
      destUrl: 'dist',
      files: ['*.html','partials/*.html']

I can't see how this is possible at the moment, am i wrong, or is there another preferred way to do this?

I have found this gist which seems to be doing a similar sort of thing:
https://gist.github.com/necolas/3024891

Error when generating HTML with a custom Yeoman generator

Hi,
I have created a Yeoman generator and I am using grunt-processhtml with it. I am trying to use the template type.

When I run yo [generator-name] it throws the following error:

undefined:9
((__t = ( message )) == null ? '' : __t) +
          ^
ReferenceError: message is not defined at eval (/lodash/template/source[1]:9:11)

In the HTML file I have:

<!-- build:template
<%= message %>
/build -->

An in the Gruntfile I have:

processhtml: {
    options: {
        process: true
    },
    dev: {
        files: {
            '<%%= yeoman.dev %>/index.html': ['<%%= yeoman.app %>/index.html']
        }
    },
    dist: {
        options: {
            data: {
                message: '.min'
            }
        },
        files: {
            '<%%= yeoman.dist %>/index.html': ['<%%= yeoman.app %>/index.html']
        }
    }
}

Is there a workaround for this issue?
Many thanks

External templates

Hello.
Plugin is awesome.
I think external templates with JSON could be very useful for hiding some massive html

For example

<!-- build:template src/tmp/tmp1.html -->
{
  "item1": "value1"
}
<!-- /build -->

So, what about realization of this?

Won't process inline elements

Hi guys!

I have this output on my main html file:

<section>
 <!--build:[src] img/--><img src="assets/images/image.svg" alt="image"><img src="assets/images/image.png" alt="image">
 <!--/build-->
</section>

The grunt-processhtml wont process the img elements because they're inline.
Its any way to make its work with inline elements? (I can't make this output come indented) :/

Thank you.
Henry

Parse template tags in build value string

Currently it seems that value string in build tags are not been processed as a template, so there's no means to pass options.data to value. So given we have this setup:

 <!-- index.html -->

<!-- build:[href]:prod <%= path %> -->
    <base href="index.html" />
 <!-- /build -->
#gruntfile
grunt.initConfig({
  processhtml: {
    prod: {
      options: {
          path: 'http://cdn.some.where/index.html'
      },
      files: {
        'dest/index.html': ['index.html']
      }
    }
  }
});

I would expect it to compile to:

    <base href="http://cdn.some.where/index.html" />

Applying same change to multiple file names

Is there an easy way of handling a change to the actual file (versus path) to a group of files?

Such as the below multiple times:

<link rel="stylesheet" href="/static/stylesheets/vendor.css" type="text/css">
to
<link rel="stylesheet" href="/static/stylesheets/vendor.min.css" type="text/css">

How to accomplish the following:

My Index.html

<script>
        App.run('http://cms.dev/getallconfigs_web_tn.json');
    </script>

This starts the application with the dev configurations. However, when we deploy to production, that url will change. I'd like to change it as a part of the build process.

Any help in how to accomplish that?

Prepend path

hi dciccale,

Awesome work on this grunt task. I was wondering if it was possible to prepend a path depending on the environment specified. For a production env, I'd like to be able to prepend an S3/CDN path. Trying to find an example on how to do this but figured I'd ask the creator himself. I appreciate the help!

This is what I have in my index.html:

        <!-- build:css css/min/common.min.css --><!-- /build -->
        <!-- build:css css/min/vendor.min.css --><!-- /build -->

For development, I'd like to get output like this:

         <link rel="stylesheet" href="static/css/min/common.min.css">
         <link rel="stylesheet" href="static/css/min/vendor.min.css">

For development, I'd like to get an output like this:

         <link rel="stylesheet" href="https://s3.amazonaws.com/bucket_name/css/min/common.min.css">
         <link rel="stylesheet" href="https://s3.amazonaws.com/bucket_name/css/min/vendor.min.css">

Weird custom block replace bug with angular

Hello

I'm using processhtml with an angular project and I have come across a weird bug.
I've defined the following custom block type to inline minified javascript files right into the html, while allowing the html to be usable in development before processing.

<!-- build:inlineJsBlock build/js/script.min.js -->
    <script src="../dev/script.js"></script>
<!-- /build -->

This works great, but some angular modules (like angular-resource, and angular-sanitize) use a variable called $$minErr which for some reason is getting replaced with $minErr (one $ is getting stripped)
I've tried it with the simplest replace() possible.

return content.replace(blockLine, fs.readFileSync(block.asset));

But it still happens.

To fix it, I have to make this weird replace. (5 dollar signs to get 2 ?!?)

replaced = replaced.replace(/\$minErr/g, '$$$$$minErr');

which ends up replacing $minErr back with $$minErr.

Any idea why this is happening ?

My custom block type

processor.registerBlockType('inlineJsBlock', function (content, block, blockLine, blockContent) {
    var jsContent = getInlineBlock('script', block.indent, block.asset);

    return content.replace(blockLine, jsContent);
});

function getInlineBlock(type, indent, asset) {
    var fileContent = fs.readFileSync(asset, 'utf8').toString(),
        template = 'INDENT<TYPE>\n' +
                   'INDENT_CONTENT_\n' +
                   'INDENT</TYPE>';

    return template.replace(/INDENT/g, indent)
        .replace(/TYPE/g, type)
        .replace(/_CONTENT_/, fileContent);
}

Thanks

Build:[data-main] attr

Is there some way to change the "data-main" attr?

<!-- build:[data-main] http://www.site.com/app/js/ -->
<script type="text/javascript" data-main="app/js/config" src="http://www.site.com/js/requirejs/require.js" charset="utf-8"></script>
<!-- /build -->```

Add attributes if they don't already exist

This example works:

<!-- build:[class]:dist production -->
<html class="debug_mode">
<!-- /build -->

But it would be nice if this also worked:

<!-- build:[class]:dist production -->
<html>
<!-- /build -->

But because of the attr regex, it never gets matched.

included only first file while proccessing

I have a strange issue. Actually in my index.html everything works smooth but in my home.svg it doesn't.
In my home.svg.html I have multuple includes and only first one is proccessed.
here is the code sample:


<svg version=\"1.1\" id=\"Layer_1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\" y=\"0px\"
viewBox=\"0 0 1600 978\" enable-background=\"new 0 0 1600 978\" xml:space=\"preserve\">
<image overflow=\"visible\" width=\"1600\" height=\"978\" xlink:href=\"data:image/png;base64,
<!-- build:include ../tmp/bg.b64.html -->
This will be replaced
<!-- /build -->
\">
</image>
<image overflow=\"visible\" width=\"207\" height=\"305\" xlink:href=\"data:image/png;base64,
<!-- build:include ../tmp/dizalica.b64.html -->
This will be replaced by dizalica
<!-- /build -->
***

my grunt task is set to process recursively since I have those files in index.html but I have no idea why home.svg.html hasn't been processed properly.
here is my grunt.js file settings for processing:

processhtml: {
      dev: {
        options: {
          process: true,
          recursive: true,
        },
        files: {
          'svg/home.svg': ['templates/home.svg.html'],
          'html/index.html': ['templates/index.html'],
        }
      },
    },

Any clues?

build:css:my_target min.css does not work if you call it's target

If i call the build target to min the css to a single link tag, it does not work. grunt processhtml:debug
However if i just call grunt processhtml with no target it will create the single link tag

    <!-- build:css:debug files/ui/css/homepage_min.css -->
        <link rel="stylesheet" href="files/ui/css/fonts.css">
        <link rel="stylesheet" href="files/ui/css/sprites.css">
        <link rel="stylesheet" href="files/ui/css/pages/homepage.css">                   
    <!-- /build -->

grunt file options
debug: {
files: [
{
expand:true,
cwd:'files/',
src:[
'.html',
'__/
.html',
'!.inc.html',
'!__/
.inc.html'
],
dest:'../htdocs/'
}
],
options:{
strip:true,
data: {
message: 'This is development environment'
}
}
}

Wildcard Selectors?

Is there anyway of selecting all html files within a folder and outputing to different folder with the same name - without having to specifically name each file?

Support for media="print"

Currently media="print" is completely stripped and there is no way to include it.

Can you give a direction to how you would prefer this to be implemented? ea. In the HTML as an extra attribute, or as an option in the Gruntfile, or as auto detect the media attribute of the containing link tags. Then I can spin up a pull request.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.