Created
February 21, 2017 13:25
-
-
Save onefriendaday/c2ad5159caefaf4d11f234b995455a27 to your computer and use it in GitHub Desktop.
Uikit scss transform
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var pkg = require('./package.json'), | |
| fs = require('fs'), | |
| path = require('path'), | |
| glob = require('glob'), | |
| mkdirp = require('mkdirp'), | |
| gulp = require('gulp'), | |
| gutil = require('gulp-util'), | |
| concat = require('gulp-concat'), | |
| ignore = require('gulp-ignore'), | |
| rename = require('gulp-rename'), | |
| rimraf = require('gulp-rimraf'), | |
| replace = require('gulp-replace'), | |
| header = require('gulp-header'), | |
| less = require('gulp-less'), | |
| minifycss = require('gulp-minify-css'), | |
| uglify = require('gulp-uglify'), | |
| watch = require('gulp-watch'), | |
| tap = require('gulp-tap'), | |
| zip = require('gulp-zip'), | |
| runSequence = require('run-sequence'), | |
| browserSync = require('browser-sync'), | |
| sass = require('gulp-sass') | |
| Promise = require('promise'); | |
| gulp.task('styles', function () { | |
| return gulp.src('./src/scss/uikit.scss') | |
| .pipe(sass()) | |
| .pipe(gulp.dest('./dist/css/')) | |
| }) | |
| gulp.task('sass-copy', function() { | |
| return gulp.src('./src/less/**/*.less').pipe(rename(function (path) { | |
| path.extname = ".scss"; | |
| })).pipe(gulp.dest('./src/scss')); | |
| }); | |
| gulp.task('sass-convert', ['sass-copy'], function() { | |
| return gulp.src('./src/scss/**/*.scss') | |
| .pipe(replace(/\/less\//g, '/scss/')) // change less/ dir to scss/ on imports | |
| .pipe(replace(/\.less/g, '.scss')) // change .less extensions to .scss on imports | |
| .pipe(replace(/@/g, '$')) // convert variables | |
| .pipe(replace(/\\\$/g, '@')) // convert variables | |
| .pipe(replace(/ e\(/g, ' unquote(')) // convert escape function | |
| .pipe(replace(/\.([\w\-]*)\s*\((.*)\)\s*\{/g, '@mixin $1($2){')) // hook -> mixins | |
| .pipe(replace(/\.svg-fill/g, '@svg-fill')) // hook -> mixins | |
| .pipe(replace(/when \(/g, '@if (')) // hook -> mixins | |
| .pipe(replace(/@mixin ([\w\-]*)\s*\((.*)\)\s*\{\s*\}/g, '// @mixin $1($2){}')) // comment empty mixins | |
| .pipe(replace(/\.(hook[a-zA-Z\-\d]+);/g, '@include $1();')) // hook calls | |
| .pipe(replace(/\$(import|media|font-face|page|-ms-viewport|keyframes|-webkit-keyframes)/g, '@$1')) // replace valid '@' statements | |
| .pipe(replace(/(\$[\w\-]*)\s*:(.*);\n/g, '$1: $2 !default;\n')) // make variables optional | |
| .pipe(replace(/\$\{/g, '#{$')) // string literals: from: /~"(.*)"/g, to: '#{"$1"}' | |
| .pipe(replace(/~('[^']+')/g, 'unquote($1)')) // string literals: for real | |
| .pipe(gulp.dest('./src/scss')); | |
| }); | |
| gulp.task('sass', ['sass-convert'], function(done) { | |
| glob('./src/scss/**/*.scss', function (err, files) { | |
| if (err) return; | |
| var re = /\/\/ @mixin ([\w\-]*)\s*\((.*)\)\s*\{\s*\}/g, | |
| mixins = [], | |
| promises = [], | |
| cache = {}; | |
| files.forEach(function(file) { | |
| promises.push(new Promise(function(resolve, reject){ | |
| fs.readFile(file, {encoding: 'utf-8'},function read(err, content) { | |
| if (err) throw err; | |
| var matches, tmp; | |
| while(matches = re.exec(content)) { | |
| tmp = matches[0].replace(/\/\/\s*/, ''); | |
| if (!cache[tmp]) { | |
| mixins.push(String(tmp)); | |
| cache[tmp] = true; | |
| } | |
| } | |
| resolve(); | |
| }); | |
| })); | |
| }); | |
| Promise.all(promises).then(function(){ | |
| fs.writeFile('./src/scss/uikit-mixins.scss', mixins.join('\n'), function (err) { | |
| if (err) throw err; | |
| done(); | |
| }); | |
| }); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment