简体   繁体   English

使用gulp访问打字稿文件变量值

[英]Accessing typescript file variable values using gulp

I have several typescript files, some of them export a const named APIS . 我有几个打字稿文件,其中一些导出一个名为APIS的const。

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. 例如,我有一个名为services的文件夹,其中包含2个文件:service1.ts,service2.ts。

service1.ts: service1.ts:

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

service2.ts: does not contain the APIS var. service2.ts:不包含APIS 变量

This is my gulpfile.js: 这是我的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); 当我添加console.log(file.APIS); to the map function, I get undefined for all the values (although it is defined in service1.ts!). 到地图的功能,我得到undefined所有值(尽管它在service1.ts定义!)。

This is following to: Extracting typescript exports to json file using gulp 这是要执行以下操作: 使用gulp将打字稿导出到json文件中

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 : 编辑:好的,所以我尝试将导出保存为.js文件而不是.ts文件,现在我可以使用require来访问这些var了:

gulp.task('default', function() { 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); 现在,如果我尝试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. Gulp本身对打字稿文件一无所知,该文件是黑胶文件 ,对内容中的打字稿代码一无所知。

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;
});

Suggestion 建议

If you want to collect multiple variables into a single file ie a common variables file I suggest gulp-replace . 如果要将多个变量收集到单个文件(即公共变量文件)中,建议使用gulp-replace

Steps 脚步

Create a file, require it and use tags within that file to place your variables. 创建一个文件,需要它,并在该文件中使用标签放置变量。

Advice 忠告

If you are already using services don't create an array. 如果您已经在使用服务,请不要创建数组。 Instead create an object (JSON) where every property is a constant. 而是创建一个对象(JSON),其中每个属性都是常量。 ie

var constants =  {
   const_1: 0,
   const_2: 1,
   const_3: 2,
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM