如何与QUOT;要求"文本文件browserify? [英] How to "require" text files with browserify?

查看:146
本文介绍了如何与QUOT;要求"文本文件browserify?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用browserify(使用browserify中间件)
我怎么能要求简单的文本文件,是这样的:

I am using browserify (using browserify-middleware) how can I require simple text files, something like:

var myTmpl = require("myTmpl.txt");

我cheked 字符串化插件browserify但文档在code未与browserify V2工作

I cheked stringify plugin for browserify but the code in the documentation is not working with browserify V2

推荐答案

要求()真的是最好的只是JavaScript的code和JSON文件保持平价节点和改善你的code的可读性谁指望外人要求()工作它在节点的方式。

require() is really best for just javascript code and json files to maintain parity with node and to improve readability of your code to outsiders who expect require() to work the way it does in node.

而不是使用要求()加载文本文件,可以考虑使用 BRFS 变换。随着BRFS,通过调用保持与节点的奇偶 fs.readFileSync()但是在做同步IO为节点,将BRFS内嵌文件的内容到就地包所以

Instead of using require() to load text files, consider using the brfs transform. With brfs, you maintain parity with node by calling fs.readFileSync() but instead of doing synchronous IO as in node, brfs will inline the file contents into the bundle in-place so

var src = fs.readFileSync(__dirname + '/file.txt');

变为

var src = "beep boop\n";

在束输出。

只是编译 -t BRFS

browserify -t brfs main.js > bundle.js

为什么超载的更多讨论要求()太多是一个坏主意:<一href=\"http://mattdesl.svbtle.com/browserify-vs-webpack\">http://mattdesl.svbtle.com/browserify-vs-webpack

这篇关于如何与QUOT;要求&QUOT;文本文件browserify?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆