Accessing typescript file variable values using gulp

こ雲淡風輕ζ 提交于 2019-12-12 03:25:20

问题


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?


回答1:


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



回答2:


Suggestion

If you want to collect multiple variables into a single file i.e. a common variables file I suggest 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. i.e.

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


来源:https://stackoverflow.com/questions/36868988/accessing-typescript-file-variable-values-using-gulp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!