Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use with LESS/SASS? #5

Closed
brandonreid opened this issue May 24, 2016 · 15 comments · Fixed by #8
Closed

Use with LESS/SASS? #5

brandonreid opened this issue May 24, 2016 · 15 comments · Fixed by #8

Comments

@brandonreid
Copy link

By chance does livingcss work with LESS/SASS css pre-processors? Was hoping to do something like...

gulp.task('less', function () {
  gulp.src(['./less/app.less'])
    .pipe(less({
        plugins: [autoprefix]
    }))
    .pipe(livingcss())
    .pipe(gulp.dest('./patterns'));
});

Or something.

Thanks!

@straker
Copy link
Owner

straker commented May 24, 2016

Yep! Works in any file and file type that can support jsDoc-like syntax (meaning the /** */ comments)

@brandonreid
Copy link
Author

@straker Appreciate it! Any chance you know of a good example of this? Having trouble getting going, trying to start mine with gulp & less.

@brandonreid
Copy link
Author

@straker NM, got it! I'll post how I did it here when I get a little further along. Might make a PR with a LESS example.

@brandonreid
Copy link
Author

Ok. For future reference, I got things rolling with a gulp file that looked like so:

var gulp = require('gulp');
var livingcss = require('gulp-livingcss');
var less = require('gulp-less');
var LessAutoprefix = require('less-plugin-autoprefix'),
    autoprefix = new LessAutoprefix({ browsers: ['last 2 versions'] });
var LessPluginCleanCSS = require('less-plugin-clean-css'),
    cleanCSSPlugin = new LessPluginCleanCSS({advanced: true});


gulp.task('patterns', function () {
  gulp.src('less/*.less')
    .pipe(livingcss({
      loadcss: false,
      template: './template/template.hbs'
    }))
    .pipe(gulp.dest('pattern_library'))
});

gulp.task('less', function () {
  gulp.src(['./less/app.less'])
    .pipe(less({
      plugins: [autoprefix, cleanCSSPlugin]
    }))
    .pipe(gulp.dest('./css'));
});

I copied the template file from the node_modules/gulp-livingcss/node_modules/livingcss/template, copied it to my root and set livingcss to use it in the options there ^. Now I can compile my less separately, reference it in my own template copy, style my template, etc. 💥

@straker
Copy link
Owner

straker commented May 25, 2016

Glad you got it working. Why did you need to copy the template into your directory? Was it not loading properly?

@brandonreid
Copy link
Author

I wanted to copy the template so I could customize the style of the actual style guide, but it also allowed me to import my compiled styles file manually into the demos. Couldn't figure out how to have livingcss recognize my compiled less styles through the options (if that's possible).

In all it would be helpful to have a demo of options used in the gulp-livingcss version like you show in the node version's readme.

@straker
Copy link
Owner

straker commented May 25, 2016

I see. There's actually is a way to bring in any stylesheets you need into the demos. It's not an option but actually part of customizing the context object by adding onto the stylesheets property.

So for your less files you'd probably do the following (making use of the utility functions):

gulp.task('patterns', function () {
  gulp.src('less/*.less')
    .pipe(livingcss({
      loadcss: false,
      preprocess: function(context, template, Handlebars) {
        return livingcss.utils.readFileGlobs('./css/*', function(data, file) {
          context.stylesheets.push(file);
        }
     }))
    .pipe(gulp.dest('pattern_library'))
});

I'll make a demo of doing something like this for future reference since it's definitely not clear enough in the docs. Thanks for bringing it to my attention.

@brandonreid
Copy link
Author

@straker Ok so was trying to get this working. Getting this err: TypeError: livingcss.readFileGlobs is not a function. I tried to find the function various ways, when I log out livingcss in the function I don't see the readFileGlobs anywhere. Any ideas?

@straker
Copy link
Owner

straker commented May 25, 2016

You're right. The function is only made available from the node version of livingcss, but not on the gulp version. I'll create an issue on the gulp-livingcss repo to add the utility functions onto the gulp plugin.

@straker
Copy link
Owner

straker commented May 26, 2016

Alright, latest gulp-livingcss now exposes the utility functions.

@brandonreid
Copy link
Author

brandonreid commented May 26, 2016

Wooot! Many thanks @straker

For the record, now that that's all good I was able to import my compiled less styles by compiling the less first into a ./dist/ folder, then (in sequence so the styles existed before livingcss ran) was able to import those styles like so..

gulp.task('library', function () {
  gulp.src('less/*.less')
    .pipe(livingcss({
      loadcss: false,
      preprocess: function(context, template, Handlebars) {
        return livingcss.utils.readFileGlobs('./dist/app.css', function(data, file) {
          _context.stylesheets.push(file);
        });
      }
    }))
    .pipe(gulp.dest('./pattern_library/'))
});

Side note: This is by far the best pattern library generator, I'm off and running. You can see mine as it grows here if looking for reference.

@straker
Copy link
Owner

straker commented May 26, 2016

Awesome, glad everything worked out for you. I'll add an example of using LESS/SASS as the file sources soon. Thanks for the great compliment.

@brandonreid
Copy link
Author

Bah, one last nbd hangup. So that readFileGlobs method will record the path to the styles but doesn't copy the styles file to the final pattern-library (in my case) folder so the reference matches. I just set gulp-less up to send my styles to two folders, one ./dist/ and one ./pattern-library/dist/.

^Had to add that for the future me figuring it out. All set.

@straker
Copy link
Owner

straker commented May 26, 2016

Ya, copying the files over would destroy any relative file paths to images and whatnot, so I just reference them to preserve that. I'll make sure to point that out as well.

straker added a commit that referenced this issue Jun 30, 2016
Shadow DOM in safari and firefox didn't work, so trying to encapsulate the styles didn't work. Switched to using polymer to create the proper encapsulation with shady DOM in all browsers. However, this means I need to inline both polymer and all style guide styles in order to reduce FOUC and get proper heights for calculating scroll positions.

also:

* docs(@page): add example of multiple pages (#7)
* docs(preprocessor): add example of using a preprocessor (#5)
* fix(utils): fix readFiles to not error when passed `undefined`

closes #7
closes #5
@straker
Copy link
Owner

straker commented Jun 30, 2016

I added an example for future reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants