首页 > 代码库 > gulp_css2js

gulp_css2js

var gulp = require(‘gulp‘);var rename = require(‘gulp-rename‘);var concat = require(‘gulp-concat‘);var uglify = require(‘gulp-uglify‘);var cssmin = require(‘gulp-minify-css‘);var through = require(‘through-gulp‘);var fs = require(‘fs‘);function gulp_css2js() {    return through(function (file, enc, cb) {        if (file.isBuffer()) {            var cssContents = file.contents;            cssContents = cssContents.toString();            cssContents = cssContents.replace(/‘/gm, ‘"‘);            var insertStyleCode = ‘‘ +                ‘function insertStyleCode(styleCode) {‘ +                ‘    var styleNode = document.createElement("style");‘ +                ‘    styleNode.type = "text/css";‘ +                ‘    if (styleNode.styleSheet) { styleNode.styleSheet.cssText = styleCode;‘ +                ‘    } else { styleNode.innerHTML = styleCode;}‘ +                ‘    document.getElementsByTagName("head")[0].appendChild(styleNode);‘ +                ‘}‘ +                ‘‘;            var jsContents = ‘‘ +                ‘(function(){‘ +                ‘   ‘ + insertStyleCode +                ‘   var styleCode = \‘‘ + cssContents + ‘\‘ ;‘ +                ‘   insertStyleCode(styleCode);‘ +                ‘})();‘;            file.contents = new Buffer(jsContents);            this.push(file);            cb();        } else {            cb();        }    });}gulp.task(‘soma_news_ad_css‘,function(){    return gulp.src(‘./events/soma_news_ad/soma_news_ad.css‘)        .pipe(cssmin())        .pipe(gulp_css2js())        .pipe(rename(‘soma_news_ad_css.js‘))        .pipe(gulp.dest(‘./events/soma_news_ad/dist‘));});gulp.task(‘soma_news_ad‘,[‘soma_news_ad_css‘],function () {    var jsArray = [        ‘./events/soma_news_ad/soma_news_ad.js‘,        ‘./events/soma_news_ad/dist/soma_news_ad_css.js‘    ];    return gulp.src(jsArray)        .pipe(concat(‘soma_news_ad_pack.js‘))        .pipe(gulp.dest(‘./events/soma_news_ad/dist‘))        .pipe(rename({suffix: ‘.min‘}))        .pipe(uglify())        .pipe(gulp.dest(‘./events/soma_news_ad/dist‘));});

  

gulp_css2js