i like to minify all js-files from a config.php by gulp.
//config.php
c::set('myvar', true);
c::set('styles', [
'test1.css',
'test2.css'
]);
c::set('scripts', array(
'node_modules/abc.min.js',
'node_modules/def.js',
'assets/js/xyz.js'
));
i read the file by fs.readFile to a string. so far so good.
unfortunately i can't find the correct regex/match to get only the paths between:
c::set('scripts', array(
and
));
anybody knows the right regex?
i am regex newbie.
tnx
Update
With the regex from @Ken following the working solution:
var gulp = require('gulp'),
fs = require("fs"),
concat = require('gulp-concat'),
uglify = require('gulp-uglify');
var jsfromconfigphp = [];
gulp.task('get-js-by-config-php', function(done) {
const regex = /\s+(\'[\w\/\.]+\.js\')/gi;
let m;
fs.readFile('site/config/config.php', {encoding: 'utf-8', flag: 'rs'}, function(e, data) {
if (e) {
return console.log(e);
}
while ((m = regex.exec(data)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, index) => {
if(index === 1) {
console.log(`Found match, group ${index}: ${match}`);
jsfromconfigphp.push(match.slice(1, -1));
}
});
}
done();
});
});
// wait for get-js-by-config-php is done
gulp.task('build-js', ['get-js-by-config-php'], function() {
return gulp.src(jsfromconfigphp)
.pipe(concat('main.min.js'))
.pipe(uglify({
compress: {
drop_console: true
}
}))
.pipe(gulp.dest('assets/js'));
});