shannonmoeller / gulp-hb

A sane Gulp plugin to compile Handlebars templates. Useful as a static site generator.
http://npm.im/gulp-hb
MIT License
148 stars 14 forks source link

Wildcard in partials path is not discovering files #43

Closed virenratan closed 7 years ago

virenratan commented 8 years ago

We've got our codebase split up into components, and each components html, scss and js lives in it's own directory.

When I'm using a path, such as 'src/components/**/*.hbs' the task throws an error saying the partial couldn't be found. When I add each individually as an array, as below it's able to find the partials.

[
  'src/components/header/*.hbs',
  'src/components/tools-menu/*.hbs',
  'src/components/footer/*.hbs'
]

I'm using node v6.2.1 and gulp-hb v5.1.0. I'm not too sure how to debug this further but I'm happy to take any suggestions at this point 😬

efender commented 8 years ago

Do you use special characters like "[] () {}" (and possibly others?) in path? It may cause errors, e.g.:

'bad-path-[to]-gulp-hb-project'
'bad-(path-to)-gulp-hb-project'
'bad-path-{to-gulp-hb-project}'
virenratan commented 8 years ago

@efender no special characters, just alpha, hyphens and slashes

If I place the files in the first directory in the array it is able to find them, so it seems at though it ignores subsequent paths.

virenratan commented 8 years ago

I found that using the array syntax the debug output was showing the complete path to most of the partials besides the first one.

When I switched back to a single string with ./src/**/*.hbs it had relative paths but including their subdirectory name, which kind of works for me. Still seems really odd from the array syntax though.

shannonmoeller commented 8 years ago

The array syntax is mostly for backwards compatibility with the pre-3.0 API and as a quick way to support exclusions (!**/some-dir/**). For any glob or glob-set (array of globs) it is assumed that the glob-parent of the first glob pattern (string or first item in an array). That glob parent will then be removed from the beginning of all glob results.

If you need to grab partials from multiple directories, you'll want to use the fluent API:

.pipe(hb()
    // `foo/` will be stripped from the beginning
    .partials('foo/**/*.hbs')
    // `bar/baz/` will be stripped from the beginning
    .partials('bar/baz/**/*.hbs')
)
.pipe(hb()
    // setting your own `base` will override the `glob-parent` stuff
    // so `foo/` and `bar/baz/` will be included
    .partials(
        ['foo/**/*.hbs', 'bar/baz/**/*.hbs'],
        { base: process.cwd() }
    )
)

Edit: Updated for completeness.

shannonmoeller commented 8 years ago

Closing due to inactivity and no similar reported bugs. Feel free to reopen if you're still having issues!

Ellomend commented 8 years ago

i have similiar error

gulp.task('handlebars', function () {
    var hbStream = hb()
        // Partials
        .partials(paths.fractal + '/components/**/*.{hbs,js}')
        .partials('./templates/layouts/**/*.{hbs,js}')
        .partials('./templates/partials/hbs/**/*.{hbs,js}')
        // Helpers
        .helpers(require('handlebars-layouts'))
        .helpers('./helpers/**/*.js')
        // Data
        .data('./data/**/*.{js,json}');
    return gulp
        .src('./templates/pages/{,posts/}*.html')
        .pipe(hbStream)
        .pipe(gulp.dest('./dest/hbs_build'));
});

eg works fine if partial sits directly in folder

/hbs/

if it nested deeply in subfolders task results in error

MickL commented 7 years ago

Same here please reopen.

File-structure:

.pipe(hb()
    .partials('./src/components/**/*.hbs') // Not working!
    .partials('./src/components/partials/partial1/*.hbs') // Working!
)

Then: {{> partial1 }} -> Not found! {{> partials/partial1 }} -> Not found! {{> components/partials/partial1 }} -> Not found!

MickL commented 7 years ago

@shannonmoeller

shannonmoeller commented 7 years ago

@MickL Given your use of .partials('./src/components/**/*.hbs'), I think your partials are being registered as the following:

Could you use hb({ debug: true }), re-run the task, and provide the output that lists the partials, please?

shannonmoeller commented 7 years ago

@Ellomend I'm not sure how I missed your comment on this issue! My apologies. Did you get a working solution? My guess is that you had a similar issue as Mick. If you're still having issues, I would also request that you use hb({ debug: true }), re-run the task, and provide the output that lists the partials. Thanks!

MickL commented 7 years ago

Oh your answer was too fast! I already debugged until handlebars itself to find out that actually {{> partials/partial1/partial1}} is correct and working! (Same "issue" for gulp-compile-handlebars)

Man this debug is gorgious i needed this so much when i was debugging all the way down to handlebars.

shannonmoeller commented 7 years ago

That's good feedback for how I can improve the README. Glad you got it working!

MickL commented 7 years ago

Any ideas on the other issue? Sorry for posting here but you answered so fast but not the other one :)

MickL commented 7 years ago

Is there any way to change the namespace? E.g. My team might want to just have partial1 instead of partials/partial1/partial1

shannonmoeller commented 7 years ago

I originally missed your other issue, but have since responded to it.

You can customize how the partials are named using the parsePartialName option. gulp-hb uses handlebars-wax under the hood, so you can use any of the options provided there.