Grunt Handlebars partials name modification issue

24 Views Asked by At

I am trying to compile the files .hbs files inside components/client folder. Compiling working fine.
I am having issue with adding the subfolder names in the partial name. Here is my code.

handlebars: {
  build: {
    options: {
      partialRegex: /.*/,
      partialsPathRegex: /\/components\/client\//,
      namespace: 'tmpl',
      processName: function(filePath) {
        // Extract subfolder name and join it with the file name (excluding extension)
        var subfolderMatch = filePath.match(/\/([^\/]*)\/[^\/]*\.hbs$/);
        var subfolder = subfolderMatch ? subfolderMatch[1] + '/' : '';
        return subfolder + filePath.replace(/^.*[\\\/]/, '').replace('.hbs', '');
      },
    },
    files: {
      'public/js/tmpl.js': ['views/components/client/*.hbs', 'views/components/client/**/*.hbs'],
    },
  },
}
1

There are 1 best solutions below

0
Suresh Pattu On

Found the answer.

Try this.

handlebars: {
  build: {
    options: {
      partialRegex: /.*/,
      partialsPathRegex: /\/components\/client\//,
      namespace: 'tmpl',
      processPartialName: function(filePath) {
        var subfolderMatch = filePath.match(/\/([^\/]*)\/[^\/]*\.hbs$/);
        var subfolder = subfolderMatch ? subfolderMatch[1] : '';
        var fileName = filePath.replace(/^.*[\\\/]/, '').replace('.hbs', '');
        if (subfolder !== 'client') {
          fileName = subfolder + '-' + filePath.replace(/^.*[\\\/]/, '').replace('.hbs', '');
        }
        return fileName;
      },
    },
    files: {
      'public/js/tmpl.js': ['views/components/client/*.hbs', 'views/components/client/**/*.hbs'],
    },
  },
}