Created
November 4, 2015 15:28
-
-
Save fxck/d1670f66f7ee8e69ee8d to your computer and use it in GitHub Desktop.
This file contains 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 gulp = require('gulp'); | |
var livereload = require('gulp-livereload'); | |
var concat = require('gulp-concat'); | |
var fileSystem = require('fs'); | |
var PATHS = { | |
TS: { | |
src: 'src/bootstrap.ts', | |
src: 'src/**/*.ts' | |
}, | |
VENDOR_JS: { | |
src: [ | |
'node_modules/es6-shim/**/*', | |
'node_modules/systemjs/**/*', | |
'node_modules/angular2/**/*' | |
] | |
}, | |
HTML: { | |
src: 'src/**/*.html' | |
}, | |
SASS: { | |
main: 'src/styles/app.scss', | |
src: 'src/**/*.scss' | |
} | |
}; | |
gulp.task('clean', function (done) { | |
var del = require('del'); | |
del(['dist'], done); | |
}); | |
gulp.task('ts2js', function() { | |
var typescript = require('gulp-typescript'); | |
var tsResult = gulp.src(PATHS.TS.src) | |
.pipe(typescript({ | |
noImplicitAny: true, | |
module: 'system', | |
target: 'ES5', | |
moduleResolution: 'node', | |
emitDecoratorMetadata: true, | |
experimentalDecorators: true | |
})) | |
tsResult.pipe(livereload()); | |
return tsResult.js.pipe(gulp.dest('dist')); | |
}); | |
gulp.task('proccessHtml', function() { | |
gulp.src(PATHS.HTML.src) | |
.pipe(gulp.dest('dist')) | |
.pipe(livereload()); | |
}); | |
gulp.task('proccessVendorScripts', function() { | |
gulp.src(PATHS.VENDOR_JS.src, { "base" : "./node_modules" }) | |
.pipe(gulp.dest('dist/vendor/')); | |
}); | |
gulp.task('generateAutoloadedSass', function () { | |
var autoloadedFiles = { | |
arr: [], | |
string: '' | |
}; | |
gulp | |
.src([ | |
'src/app/**/*.scss', | |
'src/components/**/*.scss', | |
'src/styles/*/*.scss' | |
]) | |
.on('data', function(file) { | |
var priority = _findPriority(file.contents.toString()); | |
autoloadedFiles.arr.push({ | |
priority: priority, | |
path: file.path.replace(__dirname + '/', '') | |
}); | |
}) | |
.on('end', function() { | |
autoloadedFiles.arr = _sort(autoloadedFiles.arr); | |
autoloadedFiles.arr.forEach(function(item) { | |
autoloadedFiles.string += "@import '" + item.path + "'; // @priority " + item.priority + "\n"; | |
}); | |
var prependerString = '// AUTOMATICALLY GENERATED, DO NOT TOUCH! \n'; | |
fileSystem.writeFile('src/styles/_autoload.generated.scss', prependerString + autoloadedFiles.string); | |
}); | |
function _findPriority(str) { | |
var priority = 0; | |
if (str.indexOf('@priority') > -1) { | |
priority = str.substring(str.lastIndexOf("@priority")+10,str.indexOf(";")); | |
} | |
return priority; | |
} | |
function _sort(arr) { | |
return arr.sort(function compare(a,b) { | |
if (a.priority < b.priority) { | |
return -1; | |
} | |
if (a.priority > b.priority) { | |
return 1; | |
} | |
return 0; | |
}); | |
} | |
}); | |
gulp.task('sass', function () { | |
var sass = require('gulp-sass'); | |
gulp.src(PATHS.SASS.main) | |
.pipe(sass().on('error', sass.logError)) | |
.pipe(gulp.dest('dist/styles')) | |
.pipe(livereload()); | |
}); | |
gulp.task('play', | |
[ | |
'proccessVendorScripts', | |
'ts2js', | |
'proccessHtml', | |
'generateAutoloadedSass', | |
'sass' | |
], | |
function() { | |
livereload.listen({ | |
port: 20102 | |
}); | |
gulp.watch(PATHS.TS.src, ['ts2js']); | |
gulp.watch(PATHS.VENDOR_JS.src, ['proccessVendorScripts']); | |
gulp.watch(PATHS.HTML.src, ['proccessHtml']); | |
gulp.watch(PATHS.SASS.src, ['generateAutoloadedSass', 'sass']); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment