【问题标题】:How to have a variable path in the index.html for css files based on the grunt task?如何根据 grunt 任务在 css 文件的 index.html 中具有变量路径?
【发布时间】:2015-09-23 12:37:58
【问题描述】:

我正在做一个项目,我检查了左右下载的几个,我可以通过 Grunt。毫无疑问,这是我想使用的工具。

我已经设置了一个基本的 Grunt 文件,它可以让 livereload 工作,所以每当我更改我的 .html 文件时,页面都会刷新。

这一切都很好。请参阅下面的 sn-p,它为您概述了我当前的Gruntfile.js

'use strict';

var mountFolder = function (connect, dir) {
    return connect.static(require('path').resolve(dir));
};

// The main entry point for the Grunt configuration file.
module.exports = function(grunt) {

    // Load all the grunt tasks which are defined in the 'package.json' file.
    require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);

    // Initial grunt configuration.
    grunt.initConfig({

        // grunt-express configuration.
        // Grunt-Express will serve files from the folder listed in the 'bases' property on the specified hostname
        // and port.
        express: {
            all: {
                options: {
                    bases: ['source'],
                    port: 8080,
                    hostname: "0.0.0.0",
                    livereload: true
                }
            }
        },

        // grunt-watch configuration.
        // Grunt-Watch will monitor the project files.
        watch: {
            all: {
                options: {livereload: true },
                files: [
                    '**/*.html'   // Refresh when any HTML file is being updated in any folder.
                ]
            }
        },

        // grunt-open configuration.
        // Grunt-Open will open your browser at the project url's.
        open: {
            all: {
                // The port is loaded dynamically here through the 'express.all.options.port' property.
                path: 'http://localhost:<%= express.all.options.port %>'
            }
        }
    });

    // Creates the various grunt tasks that needs to be executed.
    grunt.registerTask('server', [
        'express',
        'open',
        'watch'
    ]);
};

现在,在我的项目中,我正在使用 SASS (scss) 文件,我知道要编译这些文件,我需要一个名为 https://github.com/gruntjs/grunt-contrib-sass 的插件

在我的应用程序的目录结构中,我有一个“源”文件夹,当然我不想在其中有“css”文件。

那么,我怎样才能在我的 HTML 中放置一个指向将由 grunt 任务生成的 css 文件的链接?

我正在考虑将 scss 文件编译到临时文件夹中,但在我的 HTML 文件中,我不想放置“临时/...”链接。应该安装它们或其他东西。

有人知道如何实现我想要的吗?

亲切的问候

【问题讨论】:

  • 您好,这个stackoverflow.com/questions/21859082/… 应该有帮助吗?
  • 您好。感谢您的回答,但我不明白这对我有什么帮助。它只是给我有关如何设置手表和编译 sass 文件的信息。它没有提供有关如何使用临时文件夹以使 livereload 工作的任何信息。

标签: javascript gruntjs


【解决方案1】:

作为对您的评论的回应,您想从手表中删除“全部”并替换为以下内容:

options: {
    livereload: true
},
html: {
    files: ['**/*.html']
},
css: {
    files: ['**/css/*.css']
},
sass: {
    options: {
        livereload: false
    },
    files: ['**/sass/*.scss'],
    tasks: ['sass']
}

Grunt 将监视所有指定的文件夹并执行您定义的任务,但只会在 html 文件或 css 文件发生更改时livereload。

这意味着您在保存 scss 文件时不会看到重新加载,但是一旦它们被编译为 css,您就会看到重新加载。这是因为你已经明确告诉 grunt 不要livereload sass 文件。

【讨论】:

  • 感谢您的评论,但我是全新的。你介意解释一下为什么以及如何解决我的问题吗?
  • 谢谢。充分注意那里,但越来越近。假设在我的 HTML 文件中我引用了一个 css 文件。这工作正常,但我希望 livereload 将我的 SASS 文件编译到特定目录。现在在不更改我的 HTML 文件中的路径的情况下,我希望看到更改。在 Yeoman 应用程序中,这是通过中间件和挂载完成的。但是我正在使用 grunt-express 插件,不知道应该如何在那里完成。
  • 你没有得到 livereload 来编译任何东西。 Livereload 只是找到您指定的更改并重新加载页面以反映它们。决定文件的位置是您的 sass 任务。文档描述了如何做到这一点github.com/gruntjs/…您的 HTML 文件应该引用公共目录中已编译的 css 文件。
  • 你好。感谢 cmets,但你错了。我知道 SASS 插件确实说明了编译文件应该去哪里。但是有一个插件可以将文件夹挂载到另一个文件夹。假设您的项目中有 2 个文件夹。 “来源”和“构建”。然后您的索引文件将指向构建文件夹中已编译的 css 文件。但是当你在调试(也就是运行一个名为 debug 的 grunt 任务)时,你的页面应该自动从源文件夹中获取已编译的文件,你明白我的意思吗?看一个示例 Yeoman 应用程序。但这不使用更新的手表。
  • 我什至不确定这是否可能。据我所知,您必须将 .scss 文件编译为 .css 才能将它们包含在 html 中。对不起,我帮不上忙,也许别人会知道。您可以使用github.com/yeoman/grunt-usemin,但仍然会创建一个临时文件夹。
【解决方案2】:

在深度互联网中进行了一些搜索后,我设法找到了解决这个特定问题的方法。

首先,让我再次描述一下我想要实现的目标,因为最初我在原始问题的 cmets 中并不清楚。

Application
|-- Source
|   |-- Styles
|   |   |-- main.scss
|   |-- Scripts
|   |   |-- core.js
|   |-- index.html 

所以,您可以在我的应用程序结构中清楚地看到我将源代码托管在文件夹source 下,很明显,不是吗。 该文件夹中的样式目录包含一个 SASS 样式表 (*.scss)。我不想在其中包含任何 css 文件,因为我不是在编写 css 文件,而是在编写 sass 文件。

现在,我确实有将我的 sass 编译成普通 css 的 grunt 任务:

sass: {
  all: {
    files: {
       '.tmp/styles/main.css' : 'source/styles/main.scss'
            }
  }
}

因此,此任务将获取文件main.scss 并将其编译到目录`.tmp/styles/main.css'

请注意,此任务仅与特定的grunt-debug 任务一起执行。

当我在发布时,css文件将被放置在release/styles/main.css

现在,我有一个引用样式表的 index.html 文件:

<link rel="stylesheet" href="styles/main.css"/>

正如你可能猜到的那样,这行不通,因为当我调试时,索引文件在源文件夹中,而样式目录只包含我的 SASS 文件。

我使用 grunt-express 作为服务器:

    express: {
        all: {
            options: {
                port: 9000,
                hostname: "0.0.0.0",
                bases: ['source'],
                livereload: true
            }
        }
    },

现在,要让服务器真正加载 css 文件,我唯一需要做的就是将基本路径更改为:

bases: ['source', '.tmp']

grunt-express 配置如下所示:

快递:{ 全部: { 选项: { 端口:9000, 主机名:“0.0.0.0”, 基础:['源','.tmp'], 重载:真 } } },

通过使用此代码,grunt-express 将提供存储在“源”目录和“.tmp”目录中的文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 2013-07-29
    • 2022-08-02
    • 2020-12-12
    • 2018-01-04
    • 1970-01-01
    相关资源
    最近更新 更多