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‘)); });