How to include raw static html file into another html file with webpack

女生的网名这么多〃 提交于 2019-12-02 13:28:51

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/51527619/how-to-include-raw-static-html-file-into-another-html-file-with-webpack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!