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

How to add bundled css and js to generated "pages" #52

Closed
brightpixels opened this issue Jun 7, 2019 · 2 comments
Closed

How to add bundled css and js to generated "pages" #52

brightpixels opened this issue Jun 7, 2019 · 2 comments
Assignees
Labels
question questioning about a functionality or usage

Comments

@brightpixels
Copy link

brightpixels commented Jun 7, 2019

Hello,
I am able to generate html pages using partials but I would like the bundle.css and bundle.js added to the pages but can't see any examples of doing that.

My simplified webpack config (based on https://github.com/wbkd/webpack-starter)

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HandlebarsPlugin = require('handlebars-webpack-plugin');

module.exports = {
  entry: {
    app: path.resolve(__dirname, '../src/scripts/index.js'),
  },
  output: {
    path: path.join(__dirname, '../build'),
    filename: 'js/[name].js',
  },
  optimization: {
    splitChunks: {
      chunks: 'all',
      name: false,
    },
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, '../src/index.html'),
    }),
    new HandlebarsPlugin({
      entry: path.join(process.cwd(), 'src', 'pages', '*.hbs'),
      output: path.join(process.cwd(), 'build', '[name].html'),
      data: path.join(process.cwd(), 'src/data.json'),
      partials: ['src/components/*.hbs'],
    }),
  ],
  resolve: {
    alias: {
      '~': path.resolve(__dirname, '../src'),
    },
  },
  module: {
    rules: [
      {
        test: /\.mjs$/,
        include: /node_modules/,
        type: 'javascript/auto',
      }
    ],
  },
};

Files

  • src/components/*.hbs (e.g. header, footer, nav, card, etc)
  • src/pages/*.hbs (home, video, etc)
  • src/data.json
@TomSinclair
Copy link

Any update with getting this working? I'm having similar issues with no bundled CSS/JS appearing when using this plugin alongside html-webpack-plugin

@sagold
Copy link
Owner

sagold commented Jun 15, 2019

Hi.

There are multiple options, none of them involves the handlebars-plugin, since its job is to compile handlebars-templates only. I am no longer working with this plugin, so i can only give you some directions right now.

  • When you know your bundles filename, you can simply add the file with a <script src>-tag to your index.hbx file
  • If your bunde-file is hashed, you could try using the stats-replace-webpack-plugin to insert it on build time
  • In case your bundle-filename is dynamically set in webpack.config you could also add the name into the json-file passed to handlebars and render the source into the script-tag
  • Being webpack, there might be multiple other options, depending on config

Looking at your config, it seems the quickest option is to <script src="app.js"></script> to your templates.

I hope this helps you getting started.
Cheers.

@sagold sagold added the question questioning about a functionality or usage label Jun 15, 2019
@xavierfoucrier xavierfoucrier self-assigned this Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question questioning about a functionality or usage
Projects
None yet
Development

No branches or pull requests

4 participants