【问题标题】:SASS - Output separate stylesheets for page templatesSASS - 为页面模板输出单独的样式表
【发布时间】:2016-08-27 16:35:28
【问题描述】:

试图了解构建我的 sass underscores 项目的最佳实践。

我有一个使用 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。

【问题讨论】:

  • 你的实际代码有什么作用吗?
  • 现在它需要我的 style.scss 并将其编译成常规的人类可读 style-human.css 和缩小的 style.css 并将它们放入 /compiled 文件夹,然后在这些文件夹上运行 autoprefixer文件并将它们放在我的主题文件的根目录中。
  • 我基本上希望它继续这样做,并获取我的 /page-template scss 文件并重复该过程,然后编译并为这些文件添加前缀,最后将它们放入 /page templates 文件夹中我可以在functions.php中的主要主要样式.css之后将它们排入队列

标签: javascript css wordpress sass gruntjs


【解决方案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']);
}

基本上这是我想要的,页面模板样式表在预期的页面布局文件夹中保存为人类可读/缩小的自动前缀版本,这样我就可以在functions.php中的主要style.css之后将它们排入队列

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

【讨论】:

  • 让它按预期工作。问题是我在我的frontage.scss 文件中以错误的顺序@imported 文件,当然我必须先导入变量-site.scss 和mixing-site.scss,然后才能使用变量和mixins 导入我的页面模板文件。 ..
【解决方案2】:

就我所见,您的默认任务仅启动 watch 任务,如果 scss 文件更新,谁将仅启动 sass 和 autoprefixer 任务。

你应该使用:

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

在实践中,我们不会将 watch 任务放入默认任务中,因为它会停止线程。一旦服务器启动,我们就会调用 watch 任务。 (在与 gruntfile 相同级别的终端中使用命令 grunt watch

【讨论】:

  • 我不确定我在关注你吗?当我运行 grunt 时,我希望 watch 任务运行并监视 .scss 更新,并且直到今天它才真正开始与我的 Gruntfile.js 混淆,它做了我想要的,也就是说,每当我改变我的scss 文件,它更新并自动添加了两个样式表 style-human.css 和 style.css。你的建议有什么不同?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-14
  • 2022-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多