5

How to make this work in webpack?

var templateHtml = require("text!templates/main.html");
console.log(templateHtml); // outputs string content of the "main.html" file

I need to migrate from requirejs + require-text plugin to webpack with minimum code changes so I need that loader in webpack... Have any suggestions?

Dmitry Yaremenko
  • 2,542
  • 20
  • 31

2 Answers2

4

Should use 'text-loader': https://www.npmjs.com/package/text-loader

My mistake was that I used both text! notation in require and loaders config with "text-loader".

Dmitry Yaremenko
  • 2,542
  • 20
  • 31
  • 1
    Hi Dimitry, I'm in similar situation. Can you explain what did you do? I saw this question after posting my question [link](http://stackoverflow.com/questions/38691170/migrating-requirejs-and-backbone-based-application-to-webpack). Would like to have your inputs. Thanks – user1050134 Aug 01 '16 at 05:35
  • You need to install the text-loader plugin via npm install. No extra config is needed. – bsandhu Sep 24 '16 at 18:40
  • just install text-loader npm package locally & add alias: { "text!": "text-loader!" } this in webpack.config file – Vish May 14 '20 at 13:31
0

As the other answer mentions, use https://www.npmjs.com/package/text-loader. With Webpack 2.2, I also needed to add the following to the root of the Webpack config object:

resolveLoader: {
    alias: {
        // Support for require('text!file.json').
        'text': 'full/path/to/node_modules/text-loader'
    }
}
chris
  • 1,638
  • 2
  • 15
  • 17