I have downloaded AngularJS setup for PhoneGap from this tutorial.
Now I would like to use Sass instead of Less (since that's what I'm using in the project I am porting to PhoneGap). The default Less task looks like this:
gulp.task('less', function () {
return gulp.src(config.less.src).pipe(less({
paths: config.less.paths.map(function(p){
return path.resolve(__dirname, p);
})
}))
.pipe(mobilizer('app.css', {
'app.css': {
hover: 'exclude',
screens: ['0px']
},
'hover.css': {
hover: 'only',
screens: ['0px']
}
}))
.pipe(cssmin())
.pipe(rename({suffix: '.min'}))
.pipe(gulp.dest(path.join(config.dest, 'css')));
});
I've tried this Gulp task for Sass:
gulp.task('sass', function(){
return gulp.src('./src/sass/css/main.scss')
.pipe(sass()) // Using gulp-sass
.pipe(gulp.dest('./src/css/'));
});
Nothing seems to be happening (cannot see a newly generated main.scss file). Could someone help (I haven't been using Gulp before as you can probably guess. I've read through this though..)
UPDATE:
I am not actually replacing the Less task, I am just adding another task for Sass.
UPDATE 2:
I am calling the Sass task in here
gulp.task('watch', function () {
if(typeof config.server === 'object') {
gulp.watch([config.dest + '/**/*'], ['livereload']);
}
gulp.watch(['./src/html/**/*'], ['html']);
gulp.watch(['./src/sass/css/*'], ['sass']);
gulp.watch(['./src/js/**/*', './src/templates/**/*', config.vendor.js], ['js']);
gulp.watch(['./src/images/**/*'], ['images']);
});
However the problem seems to be that it's not executed.
UPDATE 3:
Here's the build phase code
gulp.task('build', function(done) {
var tasks = ['html', 'fonts', 'images', 'sass', 'js'];
seq('clean', tasks, done);
});
Your path is absolute, not relative. Don't forget the dots ;)
gulp.task('sass', function(){
return gulp.src('./src/sass/css/main.scss')
.pipe(sass())
.pipe(gulp.dest('./src/css/'));
});
Related
I`m trying to create a valid Source Map with Gulp and gulp-sourcemaps. The Source Map is actually created, but inside, the "sources" parameter is not loading the appropriate paths of my SASS files. This is what I get:
"version":3,"file":"style.css","sources":["style.css"]
When I need to load something like this (created by Koala App):
"version":3,"file":"style.css","sources": ["../sass/style.scss","../sass/typography/_fonts.scss","../sass/helpers/_variables.scss"........
This is my Gulp Task
gulp.task('sass', function () {
return gulp.src('style/sass/**/*.scss')
.pipe(sass(
{
'outputStyle': 'expanded'
}
))
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(sourcemaps.write('.')
.pipe(gulp.dest('./style/css'))
.pipe(bs.reload({stream: true}));
});
Thanks for the time.
The sourcemaps.init() must go before the sass pipe, so:
gulp.task('sass', function () {
return gulp.src('style/sass/**/*.scss')
.pipe(sourcemaps.init())
.pipe(sass( {
'outputStyle': 'expanded'
}).on('error', sass.logError))
.pipe(sourcemaps.write())
.pipe(gulp.dest('./style/css'))
.pipe(bs.reload({stream: true}));
});
See gulp-sass with sourcemaps.
You have two sass calls for some reason, get rid of the first and put its options into the second sass pipe call.
So i have a gulp config that concats all my angular files into one.
Here is the relevant bits in question
const jsPaths = [
'src/js/**/*.js', // no more than 100 files
'node_modules/angular-google-places-autocomplete/src/autocomplete.js',
'node_modules/angular-foundation-6/dist/angular-foundation.js',
'node_modules/angular-slugify/angular-slugify.js',
'node_modules/satellizer/satellizer.js',
'node_modules/angular-toastr/dist/angular-toastr.tpls.js',
'node_modules/angular-filter/dist/angular-filter.js'
]
gulp.task('jsbundle', function(done){
jsbundle(done)
})
gulp.task('js', ['jsbundle'], function(){
transpileJs()
})
function jsbundle(done){
gulp.src(jsPaths)
.pipe(concat('concat.js'))
.pipe(gulp.dest('tmp'))
.on('end', function() {
done();
});
}
Finished 'jsbundle' after 5.04 s
The finished file is about 1.5mb
Is there anything i can do to speed this up?
You can easily minify it with uglify.
Start by installing uglify and rename:
npm install -rename gulp-uglify --save-dev
And add:
`function jsbundle(done){
gulp.src(jsPaths)
.pipe(concat('concat.js'))
.pipe(gulp.dest('tmp'))
.pipe(rename('concat.min.js'))
.pipe(uglify())
.pipe(gulp.dest('tmp'));
.on('end', function() {
done();
});
}`
I'm new to Gulp and I wanted to make use of its automatic scss compiling and browser sync. But I can't get it to work.
I stripped everything down to leave only the contents of the example on the Browsersync website:
http://www.browsersync.io/docs/gulp/#gulp-sass-css
var gulp = require('gulp');
var browserSync = require('browser-sync').create();
var sass = require('gulp-sass');
// Static Server + watching scss/html files
gulp.task('serve', ['sass'], function() {
browserSync.init({
server: "./app"
});
gulp.watch("app/scss/*.scss", ['sass']);
gulp.watch("app/*.html").on('change', browserSync.reload);
});
// Compile sass into CSS & auto-inject into browsers
gulp.task('sass', function() {
return gulp.src("app/scss/*.scss")
.pipe(sass())
.pipe(gulp.dest("app/css"))
.pipe(browserSync.stream());
});
gulp.task('default', ['serve']);
I can call gulp serve. The site is showing and I get a message from Browsersync. When I modify the HTML, the page is reloaded. When however I modify the scss, I can see this:
[BS] 1 file changed (test.css)
[15:59:13] Finished 'sass' after 18 ms
but I have to reload manually. What am I missing?
I also faced a similar problem when I was new to browser-sync usage, the command-line was saying "reloading browsers" but the browser was not refreshed at all, the problem was I had not included body tag in my HTML page where the browser-sync can inject script for its functionality, make sure your HTML page has body tag.
You can just inject the changes instead of having to force a full browser refresh on SASS compile if you like.
browserSync.init({
injectChanges: true,
server: "./app"
});
gulp.task('sass', function() {
return gulp.src("app/scss/*.scss")
.pipe(sass())
.pipe(gulp.dest("app/css"))
.pipe(browserSync.stream({match: '**/*.css'}));
});
This is because you're calling browserSync.reload on the html watch and not on the scss watch.
Try this:
gulp.watch("app/scss/*.scss", ['sass']).on('change', browserSync.reload);
gulp.watch("app/*.html").on('change', browserSync.reload);
This is what I use and it work's fine in sass or any other files
gulp.task('browser-sync', function () {
var files = [
'*.html',
'css/**/*.css',
'js/**/*.js',
'sass/**/*.scss'
];
browserSync.init(files, {
server: {
baseDir: './'
}
});
});
I include this on my html, right below the body tag. It works.
<script type='text/javascript' id="__bs_script__">//<![CDATA[
document.write("<script async src='http://HOST:3000/browser-sync/browser-sync-client.2.11.1.js'><\/script>".replace("HOST", location.hostname));//]]>
</script>
Ran into this same problem trying to reload php and js files and stream css files. I was able to use stream only by using a pipe method, which makes sense. Anyway, here's what worked for me:
gulp.watch(['./**/*.css']).on('change', function (e) {
return gulp.src( e.path )
.pipe( browserSync.stream() );
});
But, I actually prefer #pixie's answer modified:
gulp.task('default', function() {
var files = [
'./**/*'
];
browserSync.init({
files : files,
proxy : 'localhost',
watchOptions : {
ignored : 'node_modules/*',
ignoreInitial : true
}
});
});
I also had the same issue. It worked when I called the reload method as a separate task.
gulp.task('browserSync', function() {
browserSync.init(null, {
server: {
baseDir: './'
},
});
})
gulp.task('reload', function(){
browserSync.reload()
})
gulp.task('watch', ['sass', 'css', 'browserSync'], function(){
gulp.watch('*.html', ['reload']);
})
Sometimes when using the CLI you don't have the script inserted in your HTML main files so you should manually add this or use gulp.
<!-- START: BrowserSync Reloading -->
<script type='text/javascript' id="__bs_script__">
//<![CDATA[
document.write("<script async src='/browser-sync/browser-sync-client.js'><\/script>".replace("HOST", location.hostname));
//]]>
</script>
<!-- END: BrowserSync Reloading -->
var gulp = require('gulp');
var sass = require('gulp-sass');
var runSequence = require('run-sequence');
var changed = require('gulp-changed');
var plumber = require('gulp-plumber');
var to5 = require('gulp-babel');
var sourcemaps = require('gulp-sourcemaps');
var paths = require('../paths');
var compilerOptions = require('../babel-options');
var assign = Object.assign || require('object.assign');
// transpiles changed es6 files to SystemJS format
// the plumber() call prevents 'pipe breaking' caused
// by errors from other gulp plugins
// https://www.npmjs.com/package/gulp-plumber
gulp.task('build-system', function () {
return gulp.src(paths.source)
.pipe(plumber())
.pipe(changed(paths.output, {extension: '.js'}))
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(to5(assign({}, compilerOptions, {modules:'system'})))
.pipe(sourcemaps.write({includeContent: false, sourceRoot: paths.sourceMapRelativePath }))
.pipe(gulp.dest(paths.output));
});
gulp.task('build-sass', function() {
gulp.src(paths.sass + '**/*.scss')
.pipe(sourcemaps.init())
.pipe(sass({
style: 'expanded',
includePaths: [
paths.sass,
paths.jspmDir + '/github/Dogfalo/materialize#0.96.0/sass',
],
errLogToConsole: true }))
.pipe(sourcemaps.write(paths.sourceMapRelativePath))
.pipe(gulp.dest(paths.cssOutput))
});
// copies changed css files to the output directory
gulp.task('build-css', function () {
return gulp.src(paths.css)
.pipe(changed(paths.output, {extension: '.css'}))
.pipe(gulp.dest(paths.output));
});
// copies changed html files to the output directory
gulp.task('build-html', function () {
return gulp.src(paths.html)
.pipe(changed(paths.output, {extension: '.html'}))
.pipe(gulp.dest(paths.output));
});
// this task calls the clean task (located
// in ./clean.js), then runs the build-system
// and build-html tasks in parallel
// https://www.npmjs.com/package/gulp-run-sequence
gulp.task('build', function(callback) {
return runSequence(
'clean',
['build-system', 'build-html','build-css','build-sass'],
callback
);
});
gulp.task('default', ['build']);
I have gulp-sass working but I am not sure how to reference the System.config({
"map": { short hand to paths.
I am trying to use the materialize css framework so I imported it using
jspm install github:Dogfalo/materialize#0.96.0
which worked fine, but my concern now is that in my build task I have to reference the specific path to the sass folder including the version numbers in the includePaths property
If I look at the config.js file, jspm saved a reference to materialize under the System.config.map section, it seems if I could just reference the short hand materialize name in the code below this would solve my problem
Here is my build-sass task that I added to build.js
gulp.task('build-sass', function() {
gulp.src(paths.sass + '**/*.scss')
.pipe(sourcemaps.init())
.pipe(sass({
style: 'expanded',
includePaths: [
paths.sass,
paths.jspmDir + '/github/Dogfalo/materialize#0.96.0/sass', //I would like to just reference to shorcut path included in the config.js to materialize
],
errLogToConsole: true }))
.pipe(sourcemaps.write(paths.sourceMapRelativePath))
.pipe(gulp.dest(paths.cssOutput))
});
Or if you have any better way to include a github package such as materialize using jspm and reference it in code letting jspm manage the package and version and just referencing the shorthand that jspm created
Thanks,
Dan
SASS build task
You'll need to install gulp-sass, like you mentioned. Then, you'll want to add the following task to your build file. Notice the task includes plumber and changed as well. This will signal watch to rebuild your sass when you edit it and not break serving on syntax errors.
// compiles sass to css with sourcemaps
gulp.task('build-css', function() {
return gulp.src(paths.style)
.pipe(plumber())
.pipe(changed(paths.style, {extension: '.css'}))
.pipe(sourcemaps.init())
.pipe(sass())
.pipe(sourcemaps.write())
.pipe(gulp.dest('./styles'));
});
Build task
You'll also need to add this new sass build task to your general build task, so that it is included in the build pipeline.
gulp.task('build', function(callback) {
return runSequence(
'clean',
['build-system', 'build-html', 'build-css'],
callback
);
});
Using a CSS framework in code
As you mentioned, having jspm install materialize will let jspm take care of all the heavy lifting for you. Once installed, jspm will modify the config paths to point to the right place. Then, when you need to reference it in code, you can import it normally. To install, you will want to add materialize to your package.json dependencies.
"jspm": {
"dependencies": {
"materialize": "github:Dogfalo/materialize#0.96.0",
Then, jspm will set up a map for you so you can use the normal module syntax.
import 'materialize/js/collapsible';
Materialize is not using the module syntax so, at the moment, you will need to (a) import each piece that you want specifically, as above, and (b) manually import jQuery, since materialize doesn't declare dependencies.
For more information, please see my full write up including examples here:
http://www.foursails.co/blog/building-sass/
I have set up a gulp-ruby-sass task in gulp, with some options.
Options 'precision' and 'style' work as expected, but I get no debugInfo or lineNumbers in the css (I do get them with compass).
My gulpfile.js:
var gulp = require('gulp'),
sass = require('gulp-ruby-sass');
function errorLog(error)
{
console.log(error.toString());
this.emit('end');
}
// SASS
gulp.task('styles', function(){
gulp.src('scss/**/*.scss')
.pipe(sass({
debugInfo : true,
lineNumbers : true,
precision : 6,
style : 'normal'
}))
.on('error', errorLog)
.pipe(gulp.dest('css/'));
});
What am I doing wrong?
I was able to figure this out. The solution was two-fold for me. If I added the lineNumbers option, it would fail with a file not found error. This is probably because I am compiling both normal css (with line numbers) along with a minified version.
I needed to add the "container" option in order for it to work correctly.
Here's what my working gulp tasks look like:
gulp.task('styles', function()
{
return sass('app/assets/sass/app.scss', { style: 'expanded', lineNumbers: true, container: 'gulp-ruby-sass' })
.pipe(autoprefixer('last 15 version'))
.pipe(gulp.dest('public/css'));
});
gulp.task('styles-min', function()
{
return sass('app/assets/sass/app.scss', { style: 'expanded', container: 'gulp-ruby-sass-min' })
.pipe(autoprefixer('last 15 version'))
.pipe(rename({ suffix: '.min' }))
.pipe(minifycss())
.pipe(gulp.dest('public/css'));
});
NOTE: The lineNumbers option is somewhat misleading because it's actually the path to the source sass/scss file AND the line number.