Reputation: 7918
I am trying to inject a part of my config.JSON file into my javascript file.
For example, here is my appconfig.json
:
{
"prod": {
"CONSTANTs" : {
"url1": "https://someurl",
"url2": "https://anotherurl",
}
},
"qa":{
"CONSTANTS" : {
"url1": "http://differenturl",
"url2": "https://differenturl2",
}
}
}
I want the prod values for my gulp build-prod
job and the qa values for my gulp build-qa
.
How do I do that using gulp?
Upvotes: 0
Views: 1054
Reputation: 7918
Here is what I did to inject a part of a JSON file into a variable in my js file:
The secret sauce is gulp-template
and fs
Code:
gulpfile.js
var gulp = require('gulp');
var template = require('gulp-template');
var fs = require('fs');
var configJSON = fs.readFileSync('appconfig.json');
var configDict = JSON.parse(configJSON).prod;
gulp.task('myConstants', function() {
return gulp.src('./app/background.js')
.pipe(template({myConfig: JSON.stringify(configDict)}))
.pipe(gulp.dest('./dist'));
});
My js file:
background.js
var config = <%= myConfig %>
Upvotes: 4