Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack html-loader returns full module definition

Webpack, super cool and great, yay

also running it with grunt-webpack, omg so happy

whats more? some nice fellow made an inliner so I can require .html files, I sure am lucky

var html = require("html!./some_template.html");

only troublesome detail is that an html file containing

<h3><%= any_variable %></h3>

comes out as

module.exports = "<h3><%= any_variable %></h3>"

I feel like I must be missing some silly detail, otherwise maybe the thing to do is alter html-loader? It's a fairly trivial detail but I still feel like I'm misunderstanding this tool.

check it out https://github.com/webpack/html-loader/blob/master/index.js

as requested, from webpack.config.js, or in my case Gruntfile.js

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

SOLUTION: turns out I can't actually read, require("html!./some_template.html"); runs the loader, and then I was also running it in my config, so I wound up with commonjs declaration in my html.

like image 640
James Avatar asked Jun 12 '15 04:06

James


People also ask

What are module rules in webpack?

module. rules allows you to specify several loaders within your webpack configuration. This is a concise way to display loaders, and helps to maintain clean code.

What is the purpose of HtmlWebpackPlugin?

The HtmlWebpackPlugin simplifies creation of HTML files to serve your webpack bundles. This is especially useful for webpack bundles that include a hash in the filename which changes every compilation.

What is HtmlWebpackPlugin template?

template: './src/index. html' – this option specifies which file to use as template for the index. html being created. title: 'My Awesome application' – this is a custom property we added to the config object, the value of this property would be used to embed in the placeholder element <%= htmlWebpackPlugin. options.

What are 4 core concept of webpack?

There are four basic concepts in webpack: entry , output , modules and plug-ins . These configurations are added in webpack.


2 Answers

For others who are struggling like me here's the solution - https://www.reddit.com/r/javascript/comments/39jp8z/webpack_weirdness_no_love_at_stackoverflow/

Basically the html-loader is running twice!!

Remove either of the setting. (in webpack config or while importing)

like image 175
Nachiketha Avatar answered Sep 25 '22 09:09

Nachiketha


It is explicitly coded to do that - https://github.com/webpack/html-loader/blob/master/index.js#L71

You could contact the author through github and/or file an issue there?

like image 28
Anti Veeranna Avatar answered Sep 24 '22 09:09

Anti Veeranna