43

好的,所以我在 Visual Studio 2015 Preview 中创建了一个新的ASP.Net 5/项目。MVC 6为了与我们当前的做事方法保持一致,我想使用.less文件进行样式设置。创建文件很简单,但 Web Essentials 不再编译它们。

.css所以我的问题是:当我保存文件时,我需要做什么才能生成我的.less文件?

根据我让 Typescript 正常工作的冒险经历,我将不得不使用它Grunt来完成这项任务,但我对 Grunt 来说是全新的,所以我不确定如何做到这一点?

请帮忙!

4

3 回答 3

42

VS 2015 Web Essential被拆分为多个扩展,您可以从这里下载 Web Compiler 扩展,它还包含有关如何使用它的详细信息。

它肯定不像以前那样优雅,但是如果您正在使用现有项目并想为 LESS 使用编译器,那么这可能会完成基本工作。

于 2015-07-22T19:30:36.057 回答
40

所以这里是如何做到的(编译时编译和非优雅编译保存):

步骤1

打开您的package.json文件(它位于项目的根目录中)并添加以下行:

"grunt-contrib-less": "^1.0.0",
"less": "^2.1.2"

显然,您可以更改版本号(您将获得有用的智能感知),这些只是当前版本。

第2步

右键单击NPM文件夹(在 下Dependencies),然后单击Restore Packages。这将安装lessgrunt-contrib-less.

第 3 步

恢复这些包后,转到您的gruntfile.js文件(同样,在项目的根目录中)。在这里,您需要将以下部分添加到grunt.initConfig

less: {
    development: {
        options: {
            paths: ["importfolder"]
        },
        files: {
            "wwwroot/destinationfolder/destinationfilename.css": "sourcefolder/sourcefile.less"
        }
    }
}

您还需要在结尾附近添加此行gruntfile.js

grunt.loadNpmTasks("grunt-contrib-less");

第4步

然后只需View->Other Windows->Task Runner Explorer在菜单中点击刷新图标/按钮,然后右键单击less下方Tasks并转到Bindings并打勾After Build

万岁,现在编译的文件更少了,我们(我)了解了 grunt,它看起来真的很强大。

第 5 步:保存时编译

我仍然没有让我满意,但这是我到目前为止所得到的:

如上,添加另一个 NPM 包grunt-contrib-watch(添加到 package.json,然后恢复包)。

然后在 gruntfile.js 中添加一个 watch 部分,像这样(显然这也适用于其他类型的文件):

watch: {
    less: {
        files: ["sourcefolder/*.less"],
        tasks: ["less"],
        options: {
            livereload: true
        }
    }
}

所以你现在在你的 gruntfile.js 中有这样的东西:

/// <binding AfterBuild='typescript' />
// This file in the main entry point for defining grunt tasks and using grunt plugins.
// Click here to learn more. http://go.microsoft.com/fwlink/?LinkID=513275&clcid=0x409

module.exports = function (grunt) {
    grunt.initConfig({
        bower: {
            install: {
                options: {
                    targetDir: "wwwroot/lib",
                    layout: "byComponent",
                    cleanTargetDir: false
                }
            }
        },
        watch: {
            less: {
                files: ["less/*.less"],
                tasks: ["less"],
                options: {
                    livereload: true
                }
            }
        },
        less: {
            development: {
                options: {
                    paths: ["less"]
                },
                files: {
                    "wwwroot/css/style.css": "less/style.less"
                }
            }
        }
    });

    // This command registers the default task which will install bower packages into wwwroot/lib
    grunt.registerTask("default", ["bower:install"]);

    // The following line loads the grunt plugins.
    // This line needs to be at the end of this this file.
    grunt.loadNpmTasks("grunt-bower-task");
    grunt.loadNpmTasks("grunt-contrib-less");
    grunt.loadNpmTasks("grunt-contrib-watch");
};

然后可以简单地将此任务设置为在 Project Open 上运行(右键单击watch下面TasksTask Runner Explorer(它View->Other Windows在顶部菜单中),你就完成了。我希望你必须关闭并重新打开项目/解决方案来启动它,否则您可以手动运行任务。

于 2015-01-02T02:16:15.983 回答
8

(注意:这里现在有一个直接关于 sass 的新问题。我试图改变这个问题中的问题和标签以包含 sass,但有人不允许。)

我想为sass (.scss)添加相同问题的答案。答案是如此相关,我认为最好将这些答案组合为同一篇文章中的两个答案(如果您不同意,请告诉我;否则,我们可能会在帖子标题中添加“或 sass”?)。因此,请参阅 Maverick 的回答以获取更多详细信息,以下是 sass 的简述:

(空项目的前置步骤) 如果您从一个空项目开始,首先添加 Grunt 和 Bower:

右键单击解决方案->添加->“Grunt and Bower to Project”(然后等待一分钟以使其全部安装)

包.json:

"devDependencies": {
    "grunt": "^0.4.5",
    "grunt-bower-task": "^0.4.0",
    "grunt-contrib-watch": "^0.6.1",
    "grunt-contrib-sass": "^0.9.2"
}

(仅供参考:grunt-contrib-sass 链接

然后:

依赖项 -> 右键单击​​ NPM -> 恢复包。

gruntfile.js

1)添加或确保这三行在底部附近注册(作为 NPM 任务):

grunt.loadNpmTasks("grunt-bower-task");
grunt.loadNpmTasks("grunt-contrib-watch");
grunt.loadNpmTasks("grunt-contrib-sass");

2) 再次在 gruntfile.js 中,添加初始化配置,如下所示。

{ 警告:我不是此类配置的专家。前段时间我在一篇优秀的博客文章中发现了 sass 配置,但我目前无法找到该配置以表谢意。关键是我想在某个文件夹(加上后代)中找到项目中的所有文件。以下是这样做的(注意"someSourceFolder/**/*.scss",并在此处查看重要的相关说明)。}

// ... after bower in grunt.initConfig ...
"default": {
    "files": [
        {
            "expand": true,
            "src": [ "someSourceFolder/**/*.scss" ],
            "dest": "wwwroot/coolbeans", // or "<%= src %>" for output to the same (source) folder
            "ext": ".css"
        }
    ]
},
"watch": {
    "sass": {
        "files": [ "someSourceFolder/**/*.scss" ],
        "tasks": [ "sass" ],
        "options": {
            "livereload": true
        }
    }
}

现在按照其他答案中给出的 Task Runner Explorer 的说明进行操作。确保关闭并重新打开项目。每次项目启动时,您似乎都必须运行(双击)“watch”(在“Tasks”下)才能让手表观看,但随后它会在后续保存中起作用。

于 2015-02-19T21:34:54.713 回答