Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly am I supposed to do with "module.exports = 'html_template_content'" on webpack

So I want to do a very simple task using webpack.

I have a few static HTML templates like e.g.

test.html

<div><span>template content</span></div>

and all I want to do is return the string inside the template e.g

require("raw!./test.html")

with should return a string like:

"<div><span>template content</span></div>"

but instead, it returns the following string

"modules.exports = <div><span>template content</span></div>"

I have tried several modules, like the raw-loader and html-loader. and they both behave the same way.So I took a look at the source code, just to find out that its SUPPOSED to behave this way.

raw module source code

so what exactly am I expected to do with this, if I just want the raw HTML? is it a bad practice just to remove the prepended "module.exports =" string? from the bundle edit: removing the 'modules.export =' part results in the bundle returning nothing :/

my config

module.exports =
{
    module:
    {
        loaders:
            [
                { test: /\.html$/, loader: "raw-loader" }
            ]
    }
};
like image 360
user3531149 Avatar asked Aug 01 '16 07:08

user3531149


People also ask

What is the purpose of module exports explain with example?

Module exports are the instruction that tells Node. js which bits of code (functions, objects, strings, etc.) to “export” from a given file so other files are allowed to access the exported code.

How do I use module exports in HTML?

In order to use the export declaration in a source file, the file must be interpreted by the runtime as a module. In HTML, this is done by adding type="module" to the <script> tag, or by being imported by another module.

What module exports do?

Module exports are the instructions that tell Node. js which bits of code (functions, objects, strings, etc.) to export from a given file so that other files are allowed to access the exported code.

What's the difference between module exports and exports?

When we want to export a single class/variable/function from one module to another module, we use the module. exports way. When we want to export multiple variables/functions from one module to another, we use exports way. 2.


1 Answers

The solution is to require your file without specifying any additional loader, as this is already specified in the webpack config

const test = require('./test.html')

Explanation: With your current code, you are applying the raw loader twice to your file. When you specify a loader chain in your configuration:

loaders:
    [
        { test: /\.html$/, loader: "raw-loader" }
    ]

... you are already telling webpack to add this loader to the loader chain every time you require a file matching the test condition (here, every html file)

Therefore, when you write this

const test = require('raw!./test.html')

... it is actually equivalent to this

const test = require('raw!raw!./test.html')
like image 85
Thomas Guillory Avatar answered Oct 26 '22 23:10

Thomas Guillory