fixed vars2js task: transform json into javascript object (for shared colors between sass and js)

This commit is contained in:
Bachir Soussi Chiadmi
2018-01-10 12:33:17 +01:00
parent e0d2c6ea20
commit f963af44cd
+34 -26
View File
@@ -10,11 +10,11 @@ var cssmin = require('gulp-cssmin');
var rename = require('gulp-rename');
var mainBowerFiles = require('main-bower-files');
var jsonToSass = require('gulp-json-to-sass');
// var fs = require('fs');
var json2js = require('gulp-json2js');
var data = require('gulp-data');
var concat = require('gulp-concat');
var through = require('through2');
// var fs = require('fs');
// var json2js = require('gulp-json2js');
// var data = require('gulp-data');
// var shared_variables = JSON.parse(fs.readFileSync('./assets/json/shared_variables.json'));
@@ -27,14 +27,36 @@ var config = {
production: !!util.env.production
}
gulp.task('variables2js', function() {
gulp.src('./assets/scripts/shared_variables_model.js')
gulp.task('vars2sass', function () {
gulp.src('./assets/json/shared_variables.json')
.pipe(jsonToSass({
jsonPath: './assets/json/shared_variables.json',
scssPath: './assets/styles/base/_shared_variables.scss'
}));
});
// this one is not working, works first run but then it's like it's caching the source
// gulp.task('variables2js', function() {
// gulp.src('./assets/scripts/shared_variables_model.js')
// .pipe(rename('shared_variables.js'))
// .pipe(data(function(file) {
// delete require.cache['./assets/json/shared_variables.json'];
// return require('./assets/json/shared_variables.json');
// }))
// .pipe(json2js())
// .pipe(gulp.dest('./assets/scripts/'));
// });
gulp.task('vars2js', function() {
gulp.src('./assets/json/shared_variables.json')
.pipe(rename('shared_variables.js'))
.pipe(data(function(file) {
delete require.cache['./assets/json/shared_variables.json'];
return require('./assets/json/shared_variables.json');
.pipe(through.obj(function (file, enc, cb) {
// console.log('file',String(file.contents));
// small pipe to wrappe json data into a js variable
file.contents = new Buffer("edlp_vars = "+String(file.contents).trim()+";");
cb(null, file);
}))
.pipe(json2js())
.pipe(gulp.dest('./assets/scripts/'));
});
@@ -46,20 +68,6 @@ gulp.task('scripts', function () {
.pipe(gulp.dest('./assets/dist/scripts/'));
});
// gulp.task('scripts', function() {
// return gulp.src(['./lib/file3.js', './lib/file1.js', './lib/file2.js'])
// .pipe(concat('all.js'))
// .pipe(gulp.dest('./dist/'));
// });
gulp.task('variables2sass', function () {
gulp.src('./assets/json/shared_variables.json')
.pipe(jsonToSass({
jsonPath: './assets/json/shared_variables.json',
scssPath: './assets/styles/base/_shared_variables.scss'
}));
});
gulp.task('styles', function () {
gulp.src('./assets/styles/app.scss')
.pipe(sass().on('error', sass.logError))
@@ -93,8 +101,8 @@ gulp.task('bower', function() {
});
// default gulp task
gulp.task('default', ['bower', 'variables2js', 'scripts', 'variables2sass', 'styles'], function() {
gulp.watch('./assets/json/*.json', ['variables2js', 'variables2sass', 'styles', 'scripts']);
gulp.task('default', ['bower', 'vars2js', 'scripts', 'vars2sass', 'styles'], function() {
gulp.watch('./assets/json/*.json', ['vars2js', 'vars2sass', 'styles', 'scripts']);
gulp.watch('./assets/styles/*.scss', ['styles']);
gulp.watch('./assets/styles/base/*.scss', ['styles']);
gulp.watch('./assets/scripts/*.js', ['scripts']);