如何在Browserify中使用html模板

Den*_*mon 11 javascript commonjs browserify

我试图想出一个简单的方法来在脚本中要求一个html模板,然后从CLI运行browserify.

说我想抓住一个模板并将其附加到身体上.

//index.js

var template = require('./template.html');
document.body.appendChild(template);
Run Code Online (Sandbox Code Playgroud)

<!-- template.html -->
<p>Woooo!</p>
Run Code Online (Sandbox Code Playgroud)

然后使用CLI将其全部包装在Browserify中.

browserify index.js > build.js

在引用的浏览器中加载index.html模板时,build.js我在控制台中收到此错误:

Uncaught SyntaxError: Unexpected token <
Run Code Online (Sandbox Code Playgroud)

这是参考

....

},{}],3:[function(require,module,exports){
<div class="slide">
    <h2 data-slide-title></h2>
    <div data-slide-copy></div>
</div>
},{}]},{},[1])
Run Code Online (Sandbox Code Playgroud)

lav*_*ton 10

使用:https://github.com/substack/brfs

1

npm install brfs

2

var fs = require('fs');
var html = fs.readFileSync(__dirname + '/robot.html', 'utf8');
console.log(html);
Run Code Online (Sandbox Code Playgroud)

3

browserify -t brfs example/main.js > bundle.js
Run Code Online (Sandbox Code Playgroud)