【问题标题】:How use .less files in sails.js?如何在sails.js 中使用.less 文件?
【发布时间】:2014-02-14 18:02:27
【问题描述】:

我正在阅读http://sailsjs.org/#!documentation/assets,但我不知道如何使用 .less 文件。

我将 vairables.less 和 bootswatch.less 都放在 assets/linker/styles/ 中

我预计 grunt 会编译这两个文件,但事实并非如此,而是在浏览器控制台中出现错误:

GET http://localhost:5000/linker/styles/bootstrap-responsive.css 404 (Not Found) (index):14
GET http://localhost:5000/linker/styles/bootstrap.css 404 (Not Found) (index):15
GET http://localhost:5000/linker/js/jquery.js 404 (Not Found) (index):29
GET http://localhost:5000/linker/js/socket.io.js 404 (Not Found) (index):30
GET http://localhost:5000/linker/js/sails.io.js 404 (Not Found) (index):31
GET http://localhost:5000/linker/js/app.js 404 (Not Found) (index):32
GET http://localhost:5000/linker/js/bootstrap.js 404 (Not Found) 

如果我删除两个 .less 文件,它可以正常工作。 我在这里错过了什么?

【问题讨论】:

  • 您的 .tmp 目录似乎没有被填充。 Grunt 是否抛出任何错误?使用 --verbose 来检查升降帆。
  • 是因为less文件有一些错误。谢谢。详细:Grunt :: >> NameError: .box-shadow 在 assets/linker/styles/bootswatch.less:21:2 >> 20 边界顶部颜色中未定义:@dropdown-divider-bg; >> 21 .box-shadow(无); >> 22 }
  • 我真的认为这种错误不应该显示为冗长。难以调试!

标签: javascript twitter-bootstrap less sails.js


【解决方案1】:

我决定不使用sails 默认导入器。主要是因为我觉得它很糟糕,太多基于配置而不是约定。因此,我基于约定优于配置制作了自己的动态资产加载器。

基本上,在我的情况下,没有什么可做的,资产将根据所使用的控制器、控制器动作、主题和布局来应用。而且它仍然使用 LESS。

这是一个基于sails0.10.5的简单项目: https://github.com/Vadorequest/sails-dynamic-assets

【讨论】:

    【解决方案2】:

    如果您使用的是最新版本的 Sails.js,请将 .less 文件放在 assets/styles 目录下,并将其包含在 importer.less 文件中,如下所示:

    /**
     * importer.less
     *
     * By default, new Sails projects are configured to compile this file
     * from LESS to CSS.  Unlike CSS files, LESS files are not compiled and
     * included automatically unless they are imported below.
     *
     * The LESS files imported below are compiled and included in the order
     * they are listed.  Mixins, variables, etc. should be imported first
     * so that they can be accessed by subsequent LESS stylesheets.
     *
     * (Just like the rest of the asset pipeline bundled in Sails, you can
     * always omit, customize, or replace this behavior with SASS, SCSS,
     * or any other Grunt tasks you like.)
     */
    
    
    
    // For example:
    //
    // @import 'variables/colors.less';
    // @import 'mixins/foo.less';
    // @import 'mixins/bar.less';
    // @import 'mixins/baz.less';
    //
    // @import 'styleguide.less';
    // @import 'pages/login.less';
    // @import 'pages/signup.less';
    //
    // etc.
    @import 'custom.less';
    

    【讨论】:

    • 感谢您的回答,我很久以前就解决了这个问题,忘记了这个问题;)我不这样做,因为您必须指定每个无聊的文件。因为我把我的代码分成了很多个文件,所以这需要时间。我所做的是我有一个 File Watcher ,每次我更改其中一个文件时,它都会编译我所有的 less 文件。然后 grunt 任务只需在启动时导入所有 css 文件并在任何更改时刷新 .tmp。这种方式效果很好,而且是动态的。
    • 我是 node.js 和sails.js 的新手。我很想知道您详细遵循的过程。您能否将其发布为您自己问题的答案?
    • 好的!我也会尝试设置一个小示例项目,这样就很容易理解了。
    猜你喜欢
    • 1970-01-01
    • 2015-03-29
    • 2021-08-07
    • 2014-01-13
    • 1970-01-01
    • 2013-11-25
    • 2016-07-27
    • 1970-01-01
    • 2012-05-13
    相关资源
    最近更新 更多