How to require raw static html file on webpack

15,870

Try to use html-loader:

 import TestTemplate from 'html!./test.html';
Share:
15,870
user3531149
Author by

user3531149

Updated on July 26, 2022

Comments

  • user3531149
    user3531149 almost 2 years

    I have written a simple template

    test.html

    <div>raw text with content</div>
    

    all I want to do is requiring the raw file, with no modifications

    like

    require('./test.html'); // should return "<div>raw text with content</div>"
    

    I have tried loading the html using the extra-text-plugin, but it doesn't work

    var ExtractTextPlugin = require('extract-text-webpack-plugin'); 
    
    module.exports =
    {
        module:
        {
            loaders:
                [
                    { test: /\.html$/, loader: 'html' }
                ]
        },
        plugins: [
            new ExtractTextPlugin("[name].html")
        ]
    };
    
  • user3531149
    user3531149 almost 8 years
    Why does it return "exports = template_content" instead of just the template content? how am I supposed to fetch the content
  • Ali Khosro
    Ali Khosro about 5 years
    He did not have TestTemplate in his file!!!! Why everyone is saying import template form file.html in all answeres. There is no export in html file.
  • Tofandel
    Tofandel about 2 years
    @AliKhosro because it's using html-loader.... Which basically puts your html in an export