Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with bundling hbs files in Express SSR project. #189

Open
borispov opened this issue Oct 17, 2019 · 0 comments
Open

Problem with bundling hbs files in Express SSR project. #189

borispov opened this issue Oct 17, 2019 · 0 comments

Comments

@borispov
Copy link

I try to use handlebars in production with Express + Webpack and face some problems. 
I use express' res.render to server-side render, and I'm not sure how to deal with bundling all my .hbs files. 

I have a public folder which contains 'views', 'js', 'css' directories, and in views I have layouts and partials folders.

building with webpack bundles everything into a single js file that has nothing to do with handlebars templates in it..

  1. should I manually copy views directory to dist/public ?
  2. reconfigure correctly the webpack.config.js

This is my webpack.config.js 

const path = require('path');
const Config = (entry, name, target, dest) => {
  return {
    entry,
    target,
    mode: process.env.NODE_ENV,
    module: {
      rules: [
        {
          test: /\.js?/,
          exclude: /node_modules/,
          loader: "babel-loader",
        },
        {
          test: /\.hbs$/,
          loader: "handlebars-loader",
          options: {
            helperDirs: path.join(__dirname, "src", "public", "js"),
            knownHelpersOnly: false,
            partialDirs: path.join(__dirname, "src", "public", "views", "partials"),
          }
        },
        {
          test: /\.(jpg|png|gif|jpeg)$/,
          loader: "file-lodaer",
          options: {
            name: '[name].[ext]',
            output: 'static/',
            useRelativePath: true
          }
        }
      ],
    },
    output: {
      path: dest,
      filename: `${name}.js`,
      publicPath: '/'
    }
  }
}

const serverEntry = path.resolve(__dirname, "server.js");
const serverPath = path.resolve(__dirname, "dist");
const serverConfig = Config(serverEntry, "bundled_server", "node", serverPath);
module.exports = [serverConfig]

after building, I have a single bundle js file, and when I run it I receive an error 'failed to lookup view directory ...'obviously because I didn't manually copied all views directories to my dist folder... should I ? how should I handle it ? Just manually copy the .hbs files and folders to dist? Shouldn't handlebars-loader handle it somehow ? 
Thanks. 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant