3

I have a header.html file

<header>
  <nav>
    ... some code here ...
  </nav>
</header>

And I have an index.html file too where I want to include this header.html.

My index.html looks like this:

<body>
  ${require('./header.html')}
  <div class="content">
    <!-- some content here -->
  </div>
  <div class="footer">
    <!-- some content here -->
  </div>
</body>

I'm using this webpack.config.js:

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

module.exports = {
  entry: ['./src/js/index.js', ...],
  output: {
    path: path.resolve(__dirname, './dist'),
    filename: 'js/index.js'
  },
  module: {
    rules: [
      {
        test: /\.html$/,
        use: ['html-loader']
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './src/main.html',
      filename: 'main.html'
    })
  ]
};

I tried to include header.html like this:

${require('./header.html')}

and this too:

<%= require('./header.html') %>

tried to import in js/index.js file with this line:

import header from 'html-loader!../header.html';

...combined with this:

${require('./header.html')}

and this:

${require(header)}

but nothing work. I just give back my ${require ...} code as plain text in index.html, and nothing included.

Any idea how to include the header.html file?

2 Answers 2

4

Note that the interpolate option was removed in html-loader v1.0.0. The solution works with html-loader v0.5.5 and ${require('./header.html')}

Sign up to request clarification or add additional context in comments.

2 Comments

is there any option / solution instead of interpolation?
how can we interpolate now? :(
3

EDIT: This answer is not valid anymore with html-loader > 0.5.5

You need to enable interpolation like this:

  module: {
    rules: [
      {
        test: /\.html$/,
        use: {
          loader: 'html-loader',
          options: {
            interpolate: true
          }
        }
      }
    ]
  },

Source for my answer, this question/answer.

1 Comment

the solution don't support any more. Is there any other solution ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.