alanshaw / grunt-include-replace

Grunt task to include files and replace variables. Allows for parameterised includes.
MIT License
200 stars 45 forks source link

Trouble getting option.prefix and option.suffix to work #48

Closed spacewindow closed 9 years ago

spacewindow commented 9 years ago

Wanted this feature to work with CSS comment before include.

Tried your example option: WORKS

[HTML]

<style>
<!-- @@include('styles.css') -->
</style>

[GRUNTFILE]

options: {
  prefix: '<!-- @@',
  suffix: ' -->',
}

then tried this: DOESN'T WORK

[HTML]

<style>
/* include('styles.css') */
</style>

[GRUNTFILE]

options: {
  prefix: '/* ',
  suffix: ' */',
}

then this, with Regex escapes: DOESN'T WORK

options: {
  prefix: '\/\* ',
  suffix: ' \*\/',
}

Any tips appreciated, thanks.

alanshaw commented 9 years ago

Hi @spacewindow,

There's a test for this so it should be working.

When you say "DOESN'T WORK", what output are you getting? Perhaps you could post your gruntfile configuration for the includereplace task. I've just run a test to check and the html comment should work with a gruntfile like:

module.exports = function (grunt) {
  grunt.initConfig({
    includereplace: {
      prefixsuffix: {
        options: {
          prefix: '<!-- @@',
          suffix: ' -->'
        },
        src: 'test.html',
        dest: 'dist/',
        expand: true,
        cwd: __dirname
      }
    }
  });
  grunt.loadNpmTasks('grunt-include-replace');
  grunt.registerTask('default', 'includereplace');
};

For the css comment style prefix/suffix you'll need to escape the string like so:

module.exports = function (grunt) {
  grunt.initConfig({
    includereplace: {
      prefixsuffix: {
        options: {
          prefix: '\\/\\* ',
          suffix: ' \\*\\/'
        },
        src: 'test.html',
        dest: 'dist/',
        expand: true,
        cwd: __dirname
      }
    }
  });
  grunt.loadNpmTasks('grunt-include-replace');
  grunt.registerTask('default', 'includereplace');
};

Since the prefix and suffix are added to the regex string like this:

new RegExp(options.prefix + 'include\\(\\s*["\'](.*?)["\'](,\\s*({[\\s\\S]*?})){0,1}\\s*\\)' + options.suffix);
spacewindow commented 9 years ago

Thanks so much alan - double escape on those stars and slashes. When I say 'doesn't work' I get no error (because the string is simply bit found) and the 'include' string is not replaced. That's all.

With this fix it totally works. Thanks for the quick response and the very useful tool.

R

Sent from my iPhone

On 5 Feb 2015, at 7:21 pm, Alan Shaw notifications@github.com wrote:

Hi @spacewindow,

There's a test for this so it should be working.

When you say "DOESN'T WORK", what output are you getting? Perhaps you could post your gruntfile configuration for the includereplace task. I've just run a test to check and the html comment should work with a gruntfile like:

module.exports = function (grunt) { grunt.initConfig({ includereplace: { prefixsuffix: { options: { prefix: '' }, src: 'test.html', dest: 'dist/', expand: true, cwd: __dirname } } }); grunt.loadNpmTasks('grunt-include-replace'); grunt.registerTask('default', 'includereplace'); }; For the css comment style prefix/suffix you'll need to escape the string like so:

module.exports = function (grunt) { grunt.initConfig({ includereplace: { prefixsuffix: { options: { prefix: '\/\ ', suffix: ' \\/' }, src: 'test.html', dest: 'dist/', expand: true, cwd: __dirname } } }); grunt.loadNpmTasks('grunt-include-replace'); grunt.registerTask('default', 'includereplace'); }; Since the prefix and suffix are added to the regex string like this:

new RegExp(options.prefix + 'include(\s["\'](.?)"\'{0,1}\s*)' + options.suffix); — Reply to this email directly or view it on GitHub.