使用gulp访问打字稿文件变量值 [英] Accessing typescript file variable values using gulp

查看:194
本文介绍了使用gulp访问打字稿文件变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个打字稿文件,其中一些文件输出一个名为 APIS 的常量。



来访问这些导出(我想将它们连接到一个文件),但它似乎不起作用。例如,我有一个名为services的文件夹,其中包含两个文件:service1.ts,service2 .ts。



service1.ts:

  ... 
export const APIS = [{field1:blabla}];

service2.ts:不包含 APIS var。



这是我的gulpfile.js:

  var gulp = require('gulp'); 
var concat = require('gulp-concat');
var map = require('gulp-map');

gulp.task('default',function(){
return gulp.src('... / services / *。ts')
.pipe(map(函数(file){
return file.APIS;
}))
.pipe(concat('all.js'))
.pipe(gulp.dest('。 / test /'));
});

当我运行这个任务时,我什么都没有。当我将 console.log(file.APIS); 添加到map函数时,我得到 undefined (虽然它是在service1.ts中定义的)。



这是以下内容:



编辑: OK,所以我尝试将导出保存在.js文件而不是.ts文件中,现在我可以使用 require 访问这些变量:



gulp.task('default',function(){

  return gulp.src ('./**/*.service.export.js')
.pipe(map(function(file){
var fileObj = require(file.path);
.. 。
)))

现在,如果我尝试 console.log (fileObj.APIS); 我得到正确的值,我仍然感到困惑的是我如何传递这些价值,并创造一个唱歌从所有这些变量中解脱出来。是否有可能将它们推入数组?

解决方案

这不会像你想象的那样工作。 Gulp本身对打字稿文件一无所知,该文件是乙烯基文件,并且具有没有关于其内容中的打字稿代码的知识。

编辑
$ b 基于你的例子,你可以做这样的事情:

  var gulp = require('gulp'); 
var concat = require('gulp-concat');
var map = require('gulp-map');
var fs = require('fs');

gulp.task('test',function()
{
var allConstants = [];
var stream = gulp.src('./** /*.service.export.js')
.pipe(map(function(file)
{
var obj = require(file.path);
if(obj。 APIS!= null)
allConstants = allConstants.concat(obj.APIS);
返回文件;
}));

stream.on(end ,函数(cb)
{
//在这里做你自己的格式
var content = allConstants.map(函数(常量)
{
return Object.keys常量).reduce(function(aggregatedString,key)
{
return aggregatedString + key +:+ constants [key];
},);
}) .join(,);

fs.writeFile('filename.txt',content,cb);
});
return stream;
} );


I have several typescript files, some of them export a const named APIS.

I'm trying to access those exports (I want to concatenated all of them to a single file), but it doesn't seem to work. I'm obviously doing something wrong, but I'm not sure what.

For example, I have a folder named services, with 2 files: service1.ts, service2.ts.

service1.ts:

...
export const APIS = [ { "field1" : "blabla" } ];

service2.ts: does not contain the APIS var.

This is my gulpfile.js:

var gulp = require('gulp');
var concat = require('gulp-concat');
var map = require('gulp-map');

gulp.task('default', function() {
  return gulp.src('.../services/*.ts')
        .pipe(map(function(file) {
            return file.APIS;
          }))
        .pipe(concat('all.js'))
        .pipe(gulp.dest('./test/'));
});

When I run this task, I get nothing. When I added console.log(file.APIS); to the map function, I get undefined for all the values (although it is defined in service1.ts!).

This is following to: Extracting typescript exports to json file using gulp

EDIT: OK, so I tried saving the exports in a .js file instead of a .ts file, and now I can access those vars using require:

gulp.task('default', function() {

  return gulp.src('./**/*.service.export.js')
        .pipe(map(function(file) {
            var fileObj = require(file.path);
            ...
          }))

Now if I try console.log(fileObj.APIS); I get the correct values. What I'm still confused about is how I can pass these value on, and create a single file out of all these vars. Is it possible to push them into an array?

解决方案

This will not work as you think it would work. Gulp itself knows nothing about typescript files, that file is a vinyl-file and has no knowledge about the typescript code within its content.

Edit

Based on your example, you can do something like this:

var gulp = require('gulp');
var concat = require('gulp-concat');
var map = require('gulp-map');
var fs = require('fs');

gulp.task('test', function ()
{
    var allConstants = [];
    var stream = gulp.src('./**/*.service.export.js')
        .pipe(map(function(file)
        {
            var obj = require(file.path);
            if (obj.APIS != null)
                allConstants = allConstants.concat(obj.APIS);
            return file;
        }));

    stream.on("end", function (cb)
    {
        // Do your own formatting here
        var content = allConstants.map(function (constants)
        {
            return Object.keys(constants).reduce(function (aggregatedString, key)
            {
                return aggregatedString + key + " : " + constants[key];
            }, "");
        }).join(", ");

        fs.writeFile('filename.txt', content, cb);
    });
    return stream;
});

这篇关于使用gulp访问打字稿文件变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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