2016-05-02 55 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。 .. –