gulp_css2js

Posted 袜子破了

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

xml2js:输出如何?

Promisifying xml2js 解析函数(ES6 Promises)

调试D2JS

D2JS 的数据绑定

dart2js 后异步函数中两个 keyCode 的比较不正确

ReferenceError:全局未在xml2js中定义