【问题标题】:How can I require a html template with Browserify如何使用 Browserify 需要一个 html 模板
【发布时间】:2014-06-18 02:06:16
【问题描述】:

我正在尝试找出一种简单的方法来在脚本中要求一个 html 模板,然后从 CLI 运行 browserify。

假设我想获取一个模板并将其附加到正文中。

//index.js

var template = require('./template.html');
document.body.appendChild(template);

<!-- template.html -->
<p>Woooo!</p>

然后使用 CLI 将其全部打包到 Browserify 中。

browserify index.js &gt; build.js

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

Uncaught SyntaxError: Unexpected token <

引用

....

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

【问题讨论】:

    标签: javascript browserify commonjs


    【解决方案1】:

    使用: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);
    

    3

    browserify -t brfs example/main.js > bundle.js
    

    【讨论】:

    • 感谢@lavtron,正是我所需要的。
    【解决方案2】:

    好的选择是jstify

    browserify ./app/main.js -t jstify > static/bundle.js
    

    欲了解更多信息,请查看link

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-14
      • 1970-01-01
      • 1970-01-01
      • 2014-12-02
      • 2014-10-24
      相关资源
      最近更新 更多