I'm trying to figure out an easy way to require a html template in the script and then run browserify from the CLI.
我试图找到一种简单的方法,在脚本中要求一个html模板,然后从CLI中运行browserify。
Say I want to grab a template and append it to the body.
比方说,我想抓取一个模板并将其添加到body中。
//index.js
var template = require('./template.html');
document.body.appendChild(template);
and
和
<!-- template.html -->
<p>Woooo!</p>
Then using the CLI to wrap it all up in Browserify.
然后使用CLI将其全部封装到Browserify中。
browserify index.js > build.js
browserify指数。js > build.js
When loading an index.html template in the browser that references build.js
I get this error in the console:
当加载一个索引。在浏览器中引用构建的html模板。我在控制台得到这个错误:
Uncaught SyntaxError: Unexpected token <
which is referencing
这是引用
....
},{}],3:[function(require,module,exports){
<div class="slide">
<h2 data-slide-title></h2>
<div data-slide-copy></div>
</div>
},{}]},{},[1])
2 个解决方案
#1
9
Use: https://github.com/substack/brfs
使用:https://github.com/substack/brfs
1
1
npm install brfs
npm安装brfs
2
2
var fs = require('fs');
var html = fs.readFileSync(__dirname + '/robot.html', 'utf8');
console.log(html);
3
3
browserify -t brfs example/main.js > bundle.js
#2
#1
9
Use: https://github.com/substack/brfs
使用:https://github.com/substack/brfs
1
1
npm install brfs
npm安装brfs
2
2
var fs = require('fs');
var html = fs.readFileSync(__dirname + '/robot.html', 'utf8');
console.log(html);
3
3
browserify -t brfs example/main.js > bundle.js