sagold / handlebars-webpack-plugin

Renders your html-template at build time
161 stars 45 forks source link

Can't seem to get helpers to work properly #5

Closed jamesdbruner closed 7 years ago

jamesdbruner commented 8 years ago

I'm very new to webpack so, I apologize if this is something super simple... So here's what my webpack.config.js looks like

var path = require('path');
var webpack = require('webpack');
var HandlebarsPlugin = require("handlebars-webpack-plugin");
var BrowserSyncPlugin = require('browser-sync-webpack-plugin');
module.exports = {
    entry: './src/js/app.js',
    output: {
        path: './build/js/',
        filename: 'bundle.js'
    },
    plugins: [
        new BrowserSyncPlugin({
          // browse to http://localhost:3000/ during development, 
          // ./public directory is being served 
          host: 'localhost',
          port: 3000,
          server: { baseDir: ['build/'] }
        }),
        new HandlebarsPlugin({
            // path to main hbs template 
            entry: path.join(process.cwd(), "src", "markup", "_layouts", "base.hbs"),
            // filepath to result 
            output: path.join(process.cwd(), "build", "index.html"),
            // data passed to main hbs template: `main-template(data)` 
            data: require("./src/markup/_data/data.json"),
            // globbed path to partials, where folder/filename is unique 
            partials: [
                path.join(process.cwd(), "src", "markup", "_partials", "*.hbs")
            ],
            // register custom helpers 
            helpers: {
                nameOfHbsHelper: Function.prototype, 
                path.join(process.cwd(), "src", "markup", "_helpers", "*.js") 
            },
            // hooks 
            onBeforeSetup: function (Handlebars) {},
            onBeforeAddPartials: function (Handlebars, partialsMap) {},
            onBeforeCompile: function (Handlebars, templateContent) {},
            onBeforeRender: function (Handlebars, data) {},
            onBeforeSave: function (Handlebars, resultHtml) {},
            onDone: function (Handlebars) {}
        }),
    ]
};

but every time I run webpack --watch I get this error

λ webpack
C:\Users\...\project\webpack.config.js:39
                        path.join(process.cwd(), "src", "markup", "_helpers", "*.js")
                            ^

SyntaxError: Unexpected token .
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:414:25)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at module.exports (C:\Users\james\AppData\Roaming\npm\node_modules\webpack\bin\convert-argv.js:80:13)
    at Object.<anonymous> (C:\Users\james\AppData\Roaming\npm\node_modules\webpack\bin\webpack.js:39:40)
    at Module._compile (module.js:435:26)
sagold commented 8 years ago

Hi James.

That is an error in the documentation. Since helpers is an object, a property is required for the glob-pattern. I Update the README.

Thank you for the hint.