2016-05-02 51 views
2

试图了解构建我的sass下划线项目的最佳实践。SASS - 为页面模板输出单独的样式表

我有一个使用npm和grunt的基本工作环境,并获得我的主css编译,但我想创建多个页面模板在一个子文件夹中,并将其各自的.scss文件输出在/ layout文件夹中我可以在function.php

主要样式表后排队单独的页面模板样式表作为我的CSS结构大致的顺序我的项目文件:// //更新

.gitignore 
404.php 
archive.php 
comments.php 
/compiled 
    style-human.css // Readable CSS Before prefixing // 
    style.css // Minified CSS Before Prefixing // 
    /page-layouts 
     page-frontage.human.css // Readable page-template CSS before prefixing // 
     page-frontage.css // minified page-template CSS before prefixing // 
/fonts 
footer.php 
functions.php 
gruntfile.js 
header.php 
/inc 
index.php 
/js 
/languages 
/node_modules 
package.json 
/page-layouts 
    page-frontage.css // prefixed minified CSS to be Enqueued after main style.css in functions.php // 
    page-frontage-human.css // prefixed readable CSS // 
/page-templates 
    page-frontpage.php 

page.php 
rtl.css 
/sass 
    _normalize.scss 
    /elements 
    /forms 
    /layout 
     _footer 
     _header 
     /page-layouts 
      _page-frontpage.scss 
    /media 
    /mixins 
    /modules 
    /navigation 
    /site 
    style.css // @imports everything SCSS except page-layouts // 
    /variables-site 
search.php 
sidebar.php 
single.php 
style-human.css // readable main stylesheet // 
style.css // minified main stylesheet Enqueued in functions.php // 
/template-parts  

这是代码我用在我的gruntfile.js中

module.exports = function(grunt){ 

    grunt.initConfig({ 

     pkg: grunt.file.readJSON('package.json'), 

     /** 
     * sass task 
     */ 
     sass: { 
      dev: { 
       options: { 
        style: 'expanded', 
        sourcemap: 'none', 
       }, 
       files: { 
       'compiled/style-human.css': 'sass/style.scss' 
       } 
     }, 
     dist: { 
       options: { 
        style: 'compressed', 
        sourcemap: 'none', 
       }, 
       files: { 
       'compiled/style.css': 'sass/style.scss' 
       } 
      } 
     }, 

     /** 
     * Autoprefixer 
     */ 
     autoprefixer: { 
      options: { 
      browsers: ['last 2 versions'] 
      }, 
      // prefix all files // 
      multiple_files: { 
      expand: true, 
       flatten: true, 
       src: 'compiled/*.css', 
       dest: '' 
      } 
     }, 

     /** 
     * Watch task 
     */ 
     watch: { 
      css: { 
       files: '**/*scss', 
       tasks: ['sass','autoprefixer']  
     } 
    } 

    }); 
    grunt.loadNpmTasks ('grunt-contrib-sass'); 
    grunt.loadNpmTasks ('grunt-contrib-watch'); 
    grunt.loadNpmTasks ('grunt-autoprefixer'); 
    grunt.registerTask('default',['watch']); 
} 

现在我已经尝试了几种不同的解决方案,但我无处可去了解如何构建我的gruntfile.js,以便它在布局文件夹中输出我的两个页面模板scss作为自动前缀CSS。

+0

Doe的你的实际代码做什么? – sab

+0

现在它需要我的style.scss并将其编译为常规的人类可读样式-human.css和缩小的style.css,并将它们放到/编译的文件夹中,然后在这些文件上运行autoprefixer并将它们放入根我的主题文件。 –

+0

我基本上希望它继续这样做,并采取我/页面模板scss文件,并重复编制和前缀这些文件的过程,并最终把它们放入一个/页面模板文件夹,以便我可以排队后我main主要style.css在functions.php –

回答

0

对于我所看到的,您的默认任务仅启动watch任务,只有在scss文件更新后才会启动sass和autoprefixer任务。

你应该使用:

grunt.registerTask('default',['sass','autoprefixer']); 

在实践中,我们没有手表任务放到默认的任务,因为它停止线程。一旦服务器启动,我们就会调用watch任务。 (在终端使用命令grunt watch在同一水平的gruntfile的)

+0

林不知道我跟着你?当我运行咕噜声时,我想让watch任务运行并观察.scss更新,并且直到今天,当真正开始搞乱我的Gruntfile.js时,它才做我想要的东西,这就是说,随时我改变了我的东西scss文件,它更新并自动添加两个样式表,style-human.css和style.css。 与您的建议有什么区别? –

1

排序设法找到一个可行的解决方案,可能不是最优雅之一,但它的工作原理,有点...

我结构我的文件是这样的:

.gitignore 
404.php 
archive.php 
comments.php 
/compiled 
    style-human.css //-- Readable CSS Before prefixing --// 
    style.css //-- Minified CSS Before Prefixing --// 
    /page-layouts 
     frontage.human.css //-- Readable page-template CSS before prefixing --// 
     frontage.css //-- minified page-template CSS before prefixing --// 
/fonts 
footer.php 
functions.php 
gruntfile.js 
header.php 
/inc 
index.php 
/js 
/languages 
/node_modules 
package.json 
/page-layouts 
    frontage.css //-- prefixed minified CSS to be Enqueued after main style.css in functions.php --// 
    frontage-human.css //-- prefixed readable CSS --// 
/page-templates 
    frontage.php //-- code for template goes here, Enqueued in functions.php --// 
page.php 
rtl.css 
/sass 
    _normalize.scss 
    /elements 
    /forms 
    /layout 
     _footer 
     _header 
     /page-layouts //-- working folder for all page templates --// 
      _frontpage.scss //-- SASS for page template goes here! --// 
    /media 
    /mixins 
    /modules 
    /navigation 
    /page-templates 
     frontpage.scss //-- @imports content of ../layout/page-layouts/_frontpage.scss --// 
    /site 
    style.css //-- @imports everything SCSS except /page-layouts --// 
    /variables-site 
search.php 
sidebar.php 
single.php 
style-human.css //-- readable main stylesheet --// 
style.css //-- minified main stylesheet Enqueued in functions.php --// 
/template-parts  

而且我我的结构作为Gruntfile.js如下

module.exports = function(grunt){ 

    grunt.initConfig({ 

     pkg: grunt.file.readJSON('package.json'), 

     /** 
     * sass task 
     */ 
     sass: {    // Task 
      dev: {    // Target 
       options: {  // Target options 
        style: 'expanded', 
        sourcemap: 'none', 
       }, 
       files: {  // Dictionary of files 
       'compiled/style-human.css': 'sass/style.scss', // 'destination': 'source' 
        'compiled/page-layouts/frontpage-human.css': 'sass/page-templates/frontpage.scss' // 'destination': 'source' 
       } 
     }, 
     dist: {    // Target 
       options: { // Target options 
        style: 'compressed', 
        sourcemap: 'none', 
       }, 
       files: {  // Dictionary of files 
       'compiled/style.css': 'sass/style.scss', // 'destination': 'source' 
        'compiled/page-layouts/frontpage.css': 'sass/page-templates/frontpage.scss' // 'destination': 'source' 

       } 
      } 
     }, // close sass task 

     /** 
     * Autoprefixer 
     */ 
     autoprefixer: { 
      options: { 
      browsers: ['last 2 versions'] 
      }, 
      // prefix all files // 
      multiple_files: { 
      expand: true, 
       flatten: true, 
       src: 'compiled/*.css', 
       dest: '' 
      }, 
      // prefix frontpage template // 
      multiple_files: { 
      expand: true, 
       flatten: true, 
       src: 'compiled/page-layouts/*.css', 
       dest: './page-layouts' 
      }, 
     }, 

     /** 
     * Watch task 
     */ 
     watch: { 
      css: { 
       files: '**/*scss', 
       tasks: ['sass','autoprefixer']  
     } 
    } 

    }); 
    grunt.loadNpmTasks ('grunt-contrib-sass'); 
    grunt.loadNpmTasks ('grunt-contrib-watch'); 
    grunt.loadNpmTasks ('grunt-autoprefixer'); 
    grunt.registerTask('default',['watch']); 
} 

基本上,这工作我如何打算时,PA ge-template样式表会保存为预定的page-layouts文件夹中的可读/缩小自动添加版本,这样我就可以将它们排入函数中的main style.css之后

只剩下一个“小”问题出,我的页面模板scss文件不理解变量...

+0

让它按预期工作。问题在于我在我的frontage.scss文件中以错误的顺序输入了文件,当然在使用变量和mixins导入我的页面模板文件之前,我必须首先导入variables-site.scss和mixing-site.scss。 .. –