- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我目前正在尝试为 NPM Flat 编写一个 Gulp 包装器可以很容易地在 Gulp 任务中使用。我觉得这对 Node 社区很有用,也可以实现我的目标。 <强> The repository is here for everyone to view , contribute to, play with and pull request. 我正在尝试制作多个 JSON 文件的扁平化(使用点表示法)副本。然后我想将它们复制到同一个文件夹并修改文件扩展名以从 *.json 更改为 *.flat.json。
我在 JSON 文件中返回的结果看起来像乙烯基文件或字节码。例如,我希望输出像 "views.login.usernamepassword.login.text": "Login"
,但我得到类似 {"0":123,"1":13,"2":10 "3":9,"4":34,"5":100,"6":105
...等等
我是开发 Gulp 任务和 Node 模块的新手,所以一定要留意根本性的错误。
存储库将是最新的代码,但我也会尽量使问题与它保持同步。
Gulp 任务文件
var gulp = require('gulp'),
plugins = require('gulp-load-plugins')({camelize: true});
var gulpFlat = require('gulp-flat');
var gulpRename = require('gulp-rename');
var flatten = require('flat');
gulp.task('language:file:flatten', function () {
return gulp.src(gulp.files.lang_file_src)
.pipe(gulpFlat())
.pipe(gulpRename( function (path){
path.extname = '.flat.json'
}))
.pipe(gulp.dest("App/Languages"));
});
Node 模块的 index.js(也就是我希望变成 gulp-flat 的东西)
var through = require('through2');
var gutil = require('gulp-util');
var flatten = require('flat');
var PluginError = gutil.PluginError;
// consts
const PLUGIN_NAME = 'gulp-flat';
// plugin level function (dealing with files)
function flattenGulp() {
// creating a stream through which each file will pass
var stream = through.obj(function(file, enc, cb) {
if (file.isBuffer()) {
//FIXME: I believe this is the problem line!!
var flatJSON = new Buffer(JSON.stringify(
flatten(file.contents)));
file.contents = flatJSON;
}
if (file.isStream()) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Streams not supported! NYI'));
return cb();
}
// make sure the file goes through the next gulp plugin
this.push(file);
// tell the stream engine that we are done with this file
cb();
});
// returning the file stream
return stream;
}
// exporting the plugin main function
module.exports = flattenGulp;
最佳答案
你说的对,错误在哪里。修复很简单。您只需要解析 file.contents
,因为 flatten
函数在 object 上运行,而不是在 Buffer 上运行。
...
var flatJSON = new Buffer(JSON.stringify(
flatten(JSON.parse(file.contents))));
file.contents = flatJSON;
...
这应该可以解决您的问题。
由于您是 Gulp 插件的新手,希望您不介意我提出建议。您可能需要考虑为您的用户提供美化 JSON 输出的选项。为此,只需让您的 main 函数接受一个 options
对象,然后您就可以执行如下操作:
...
var flatJson = flatten(JSON.parse(file.contents));
var jsonString = JSON.stringify(flatJson, null, options.pretty ? 2 : null);
file.contents = new Buffer(jsonString);
...
如果您计划在未来扩展您的插件,您可能会发现选项对象对其他事情很有用。
请随意查看我编写的名为 gulp-transform 的插件的存储库。 .我很乐意回答有关它的任何问题。 (例如,如果您愿意,我可以为您提供一些有关实现插件流模式版本的指导)。
我决定接受你的贡献邀请。你可以查看我的fork here我打开的问题here .欢迎您随意使用,如果您真的喜欢它,我可以随时提交拉取请求。希望它至少能给您一些想法。
感谢您让这个项目继续进行。
关于javascript - NodeJS & Gulp Streams & Vinyl File Objects - Gulp Wrapper for NPM package producing incorrect output,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35564976/
实习测试框架有没有 有任何 gulp 插件或文档可以与 gulp 集成吗? 最佳答案 它看起来不像,但我已经做到了 使用 gulp-shell : gulp.task('intern', shell.
我正在寻找答案,不必深入或详细。只是想确切地知道任务序列发生了什么。 gulp.task('name',['*this right here*'], function() { // conte
假设有四个任务 'a'、'b'、'c'、'd'(b 依赖于 c 和 d;c 和 d 都依赖于 a),因此任务按以下顺序运行: a-> (c, d) -> b 这是相应地工作的 gulpfile.js:
我安装了本地gulp插件,并意识到我不需要它。如何删除它,是否有例如npm remove gulp-sass这样的命令? 最佳答案 只需运行npm uninstall --save即可。转到此链接h
我使用del包删除文件夹: gulp.task('clean', function(){ return del('dist/**/*', {force:true}); }); 但是,如果dis
有一个 super 简单的 gulp 文件,我想在其中依次运行一些基本的 gulp 任务。 我似乎无法在 Gulp v4 中运行它。使用 run-sequence 在 Gulp v3 中有类似的东西而
我目前有这样的文件结构 SASS gulpfile.js node_modules sites example-site scss
我创建了一个简单的 Gulp 任务来检查我的 ES6 文件中的更改。我想转换它们并在出现问题时显示错误消息。 正在显示错误屏幕。但是,当一切都成功时,我想显示不同的信息。 我试过 .on('end')
我已经安装了gulp在全局范围内 sudo npm install --global gulp-cli 和本地 npm install --save-dev gulp /usr/local/bin/g
我正在尝试使用 gulp-rev、gulp-rev-replace 和 gulp-rev-css-url 重写对我的版本图像文件的引用。 我已设法修改文件并将 list 与以下 gulp 代码合并:
尝试创建一个 gulp 任务,通过 LESS 通过管道传输来自不同文件夹的一堆文件,然后将它们输出到基于原始源的文件夹。考虑这个文件夹结构: Project +-- /Module_A | +-
我在终端上的项目文件夹中,并且: 如果我执行: gulp -v 我得到: [15:47:15] CLI version 3.9.0 [15:47:15] Local version 3.9.
设置很简单: gulp.task('rev-js', function() { return gulp.src('/js/main.js, {base: '.'}) .pipe(ne
我有两个任务,B 依赖于 A。 任务A需要循环一个数组,然后执行gulp.dest,但似乎B会在A完成之前执行。 A 所做的就是加载所有 html,并将每个 html 注入(inject)到 temp
我尝试在工作流程中将 gulp-data 与 gulp-jade 结合使用,但收到与 gulp-data 插件相关的错误。 这是我的 gulpfile.js var gulp = require('g
编辑:有没有办法清理这段代码? 任务.咖啡 # Watch pages gulp.task 'jade', -> # Watch index gulp.src('src/jade/index.
这可能听起来很愚蠢,但请耐心等待。我遇到了麻烦。 我正在开发一个使用 Grunt 来编译和构建其发行版的项目。但是,它使用另一个名为 prism 的库,您可以在 http://prismjs.com
我有一个简单的 less 任务的 gulp 工作流程,如下所示: gulp.task('less', function() { gulp.src(source_less) .p
我有一个 gulp 工作流程,一切都运行良好,但似乎它只适用于最上面的文件,例如:public/index.html,但如果我想使用 public/products/item.html,gulp-da
我想使用 gulp-useref 将我所有的 JavaScript 文件连接成一个。 在我的 JavaScript 文件中,我混合了pre-minified 和non-minified 文件。 我只想
我是一名优秀的程序员,十分优秀!