【问题标题】:Compile LESS files with source maps使用源映射编译 LESS 文件
【发布时间】:2018-04-28 21:49:35
【问题描述】:

除了 CSS 文件之外,我如何编译 LESS 文件以输出源映射文件 (.css.map)?有没有办法在命令行(NodeJS 的 lessc)和任何基于 GUI 的程序上执行此操作?

【问题讨论】:

标签: css compilation less source-maps lesscss-resources


【解决方案1】:

更新:新的最短答案

文档已更新!随着新功能越来越少,有时文档会落后一点,所以如果您正在寻找最前沿的功能,您最好还是运行lessc(查看更长的答案)并检查帮助中弹出的内容文本。

http://lesscss.org/usage/

简答

您正在从命令行查找以下任意数量的选项:

--source-map[=FILENAME]  Outputs a v3 sourcemap to the filename (or output filename.map)
--source-map-rootpath=X  adds this path onto the sourcemap filename and less file paths
--source-map-basepath=X  Sets sourcemap base path, defaults to current working directory.
--source-map-less-inline puts the less files into the map instead of referencing them
--source-map-map-inline  puts the map (and any less files) into the output css file
--source-map-url=URL     the complete url and filename put in the less file

在我写这篇文章时,我不知道任何生成地图的 GUI 选项(源地图仅在过去几个月中添加到 LESS)——很抱歉没有更好的消息。我相信他们会在明年更新时添加支持。

更长的答案

如果您从命令行运行 lessc 而不使用任何参数,它将为您提供所有选项。 (根据我的经验,这比他们的文档更新,所以它至少可以让你指出正确的方向。)包括所有最新的地图资料。

用于开发的最简单组合是--source-map-less-inline --source-map-map-inline,因为它会为您提供嵌入到输出 css 中的源映射。

如果您想添加单独的地图文件,可以使用--source-map,从my.less 将输出my.cssmy.css.map

供参考:当我运行我的副本时(目前是 v 1.6.1)我得到

usage: lessc [option option=parameter ...] <source> [destination]

If source is set to `-' (dash or hyphen-minus), input is read from stdin.
options:
  -h, --help               Print help (this message) and exit.
  --include-path=PATHS     Set include paths. Separated by `:'. Use `;' on Windows.
  -M, --depends            Output a makefile import dependency list to stdout
  --no-color               Disable colorized output.
  --no-ie-compat           Disable IE compatibility checks.
  --no-js                  Disable JavaScript in less files
  -l, --lint               Syntax check only (lint).
  -s, --silent             Suppress output of error messages.
  --strict-imports         Force evaluation of imports.
  --insecure               Allow imports from insecure https hosts.
  -v, --version            Print version number and exit.
  -x, --compress           Compress output by removing some whitespaces.
  --clean-css              Compress output using clean-css
  --clean-option=opt:val   Pass an option to clean css, using CLI arguments from
                           https://github.com/GoalSmashers/clean-css e.g.
                           --clean-option=--selectors-merge-mode:ie8
                           and to switch on advanced use --clean-option=--advanced
  --source-map[=FILENAME]  Outputs a v3 sourcemap to the filename (or output filename.map)
  --source-map-rootpath=X  adds this path onto the sourcemap filename and less file paths
  --source-map-basepath=X  Sets sourcemap base path, defaults to current working directory.
  --source-map-less-inline puts the less files into the map instead of referencing them
  --source-map-map-inline  puts the map (and any less files) into the output css file
  --source-map-url=URL     the complete url and filename put in the less file
  -rp, --rootpath=URL      Set rootpath for url rewriting in relative imports and urls.
                           Works with or without the relative-urls option.
  -ru, --relative-urls     re-write relative urls to the base less file.
  -sm=on|off               Turn on or off strict math, where in strict mode, math
  --strict-math=on|off     requires brackets. This option may default to on and then
                           be removed in the future.
  -su=on|off               Allow mixed units, e.g. 1px+1em or 1px*1px which have units
  --strict-units=on|off    that cannot be represented.
  --global-var='VAR=VALUE' Defines a variable that can be referenced by the file.
  --modify-var='VAR=VALUE' Modifies a variable already declared in the file.

-------------------------- Deprecated ----------------
  -O0, -O1, -O2            Set the parser's optimization level. The lower
                           the number, the less nodes it will create in the
                           tree. This could matter for debugging, or if you
                           want to access the individual nodes in the tree.
  --line-numbers=TYPE      Outputs filename and line numbers.
                           TYPE can be either 'comments', which will output
                           the debug info within comments, 'mediaquery'
                           that will output the information within a fake
                           media query which is compatible with the SASS
                           format, and 'all' which will do both.
  --verbose                Be verbose.

【讨论】:

  • 能否提供文档链接。我找不到具有所有这些选项的选项。
  • @Sagi_Avinash_Varma 文档最近更新了,所以答案现在有指向它们的链接。
  • 如果某个圣人决定为这该死的东西添加一些示例。我设法用 SASS 完成了这一点,只需阅读 1 分钟的文档,我已经用 LESS 尝试了一周.我的地图文件中的文件名是“未定义”。为什么默认情况下不打开源映射选项,在同一位置输出相同的文件名?为什么讨厌开发者?为什么你让我们的生活变得更加困难?谢谢
【解决方案2】:

如果命令行不适合你,Grunt 非常擅长这种类型的事情。你可以配置 grunt-contrib-less 插件来生成内联地图,配置如下:

 less: {
        options: {
            sourceMap:true,
            outputSourceFiles: true
        },
        lessFiles: {
            expand: true,
            flatten:false,
            src: ['**/*.less'],
            dest: ['dist/'],
            ext: '.css',
        }
    },

https://github.com/gruntjs/grunt-contrib-less

【讨论】:

    【解决方案3】:

    从 Less 文件创建地图和 CSS 文件的示例

    1. 安装最新的 Node JS 并转到命令提示符并运行 npm install less,现在安装成功较少
    2. 转到命令提示符并移动到我们要创建的较少文件夹

    例如,我要更改 HelloWorld [Less File]

    在命令提示符下转到 C:\Project\CSS 或在以下命令中提供正确的路径。

    在命令提示符中运行以下命令

    lessc HelloWorld.less HelloWorld.css --source-map=HelloWorld.css.map –verbose

    现在在各自的文件夹中生成 CSS 和 Map 文件。

    更多参考请查看链接:royalarun.blogspot.com

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多