2017-06-21 2 views
0

JSONをエクスポートする一連のJSファイルがあります。私は、各ファイルからエクスポートされたJSONを取得したいので、別のプラグインにパイプすることができます。ストリーム内の各ファイルのmodule.exportsにはどうすればアクセスできますか?これのためのプラグインはありますか?ストリーム内の各JSファイルのGulp.jsアクセスモジュール.exports

例JSファイル:

let typographyFamilyFallback = 'Verdana, sans-serif'; 
let typographyFamily = 'Gotham, gotham, ' + typographyFamilyFallback; 
let typographyFamilyFineprint = typographyFamily; 
let typographyBaseSize = 15; 

let typographyJSON = { 
    typographyFamilyFallback, 
    typographyFamily, 
    typographyFamilyFineprint, 
    typographyBaseSize 
}; 

module.exports = typographyJSON; 

gulpfile.js:

const gulp = require('gulp'); 
const jsonCss = require('gulp-json-css'); 

gulp.task('generate-less-vars', function() { 
    return gulp 
    .src(['./src/variables/*.js']) 
    // Get the module.export and convert to json for the next piped task. 
    .pipe(jsonCss({targetPre: 'less'})) 
    .pipe(gulp.dest('target/static-zsg/zsg/variables/')); 
}); 

答えて

0

は、ここに私がやってしまったものです:

私はの.jsファイルの数を設定しています、私はファイル名から来るキーと値がrequireステートメントであるオブジェクトを構築します。次にオブジェクトがループされます。それぞれの項目について、値をJSON.stringし、gulp-fileに送信し、jsonCssにパイプして、ターゲットディレクトリに書き込みます。

JSファイル

let typographyFamilyFallback = 'Verdana, sans-serif'; 
let typographyFamily = 'Gotham, gotham, ' + typographyFamilyFallback; 
let typographyFamilyFineprint = typographyFamily; 
let typographyBaseSize = 15; 

let typographyJSON = { 
    typographyFamilyFallback, 
    typographyFamily, 
    typographyFamilyFineprint, 
    typographyBaseSize 
}; 

module.exports = typographyJSON; 

gulpfile.js

const jsVars = { 
    layout: require('./src/static-zsg/zsg/variables/_layout'), 
    trapezoid: require('./src/static-zsg/zsg/variables/_trapezoid'), 
    typography: require('./src/static-zsg/zsg/variables/_typography') 
} 
gulp.task('generate-less-vars', function() { 
    for (let key in jsVars) { 
     const variableJson = JSON.stringify(jsVars[key]); 
     file(`${key}.json`, `${variableJson}`) 
      .pipe(jsonCss({targetPre: 'less'})) 
      .pipe(gulp.dest(`target/static-zsg/zsg/variables/`)); 
    } 
}); 
関連する問題