1

我正在尝试找到一个好的解决方案来观看单个 LESS 文件,其中包含大量导入以进行更改(包括导入文件中的更改,但仅编译主文件)。

我已将 LESS 拆分为单独的文件作为“模块”,以使其更易于管理,而主 LESS 文件只是一堆导入。但是,我查找的每个解决方案都是为了观察更改而不必每次都手动编译,要么编译目录中的每个 LESS 文件,要么不识别导入文件中的更改。

所以我正在寻找一种可以观察所有LESS文件更改的解决方案,但如果发生更改,只会编译主LESS文件。如果有人以前做过这样的事情,这将对我有很大帮助,而不必从头开始制作。

更新: 我发现了这个https://github.com/jonycheung/Dead-Simple-LESS-Watch-Compiler。默认情况下,它会检查并编译目录中的每个 less 文件。修改起来非常简单,可以查看所有较少的文件并在检测到更改时仅编译主文件。

4

2 回答 2

3

LESS 1.3.1 有一个名为@import-once. 而不是@import,使用@import-once.

它没有很好的记录,但这里是变更日志问题,讨论,更多

于 2012-10-24T17:06:28.037 回答
0

我不知道你是否在使用 Coffeescript,但由于 Less 依赖于 Node,你已经安装了它。我创建了以下 Cakefile,它应该很容易转换为纯 JavaScript 文件。它也应该很容易修改多个文件。

option '-c', '--lessFile [main Less Filename]', 'The single file to be compiled into the stylesheet, it should import any files it needs'

task 'watch-less', 'Compile Less scripts on the fly during Development', (opts) ->
  less = options.less

  watchers = []

  compile = () ->
    w.close() for w in watchers
    exec "lessc #{less} #{less.replace /\.less$/, ".css"}"
    watch()

  watch = () ->
    exec "lessc -M #{less} -", (err, stdout, stderr) ->
      throw new Error err if err
      watchers = (fs.watch(f, compile) for f in stdout.trim().split(' ').splice(1))

  compile()

通知和免责声明:

  • 除了 less 没有外部依赖项。
  • 当心fs.watch,而不是跨平台
于 2013-09-10T17:51:49.773 回答