条件@import in.less或文件丢失时无错误

条件@import in.less或文件丢失时无错误,less,Less,我需要在特定类型的构建期间有条件地导入文件。使用grunt contrib-jshint@0.7.0位于less@1.4.2 到目前为止我所尝试的 尝试使用受保护的mixin来确定我需要的构建类型,即跳过在开发模式下中断的某些较少文件的导入 @isProduction: 1; ... .getImports() when (@isProduction = 1){ } .getImports() { @import "productionStyles"; } ... .getImp

我需要在特定类型的构建期间有条件地导入文件。使用
grunt contrib-jshint@0.7.0
位于
less@1.4.2

到目前为止我所尝试的

尝试使用受保护的mixin来确定我需要的构建类型,即跳过在开发模式下中断的某些较少文件的导入

@isProduction: 1;

...

.getImports() when (@isProduction = 1){
}

.getImports() {
    @import "productionStyles";
}

...
.getImports();
然而,这似乎失败了,它一直尝试导入和解析
productionStyles.less
。我猜受保护的mixin不包括
@import
,那么您将如何解决这个问题

我也试过了

@productionStyles: "productionStyles"; // or 0

...
@productionStyles: 0;


.getImports() when not (@productionStyles = 0){
    @import "@{productionStyles}";
}

...
同样,它仍将尝试导入它。
>文件错误:在…
中找不到“0.less”

我开始认为它需要一个更大的重构,
devStyles
productionStyles
都是一个东西,我只是在它们之间切换——只是productionStyles是一个附加的东西,由于deps的原因,它只能在完整构建之后编译,我更愿意通过有条件地编译来解决这个问题

我也可以不再使用
grunt contrib jshint
,而是编写自己的less解析器,但我想先探索内置选项


由于
productionStyle.less
引用了文件系统中不存在的多个文件,是否可以忽略失败的
@imports
并继续构建?由于其他地方可能存在解析器错误,我不希望对所有错误禁用错误检查/中断

我已通过以下方法解决了您的问题:

index.html

无测试

less.js比直接从下载的v1.4.1版本要少

关于的代码应显示为红色背景。 切换到
html{.mixin(dev);}
将禁用
@import
,背景变为绿色


您的解决方案也可能会起作用,但您在HTML或类似内容中有错误-我还没有检查。

为什么不使用不同版本的
productionStyles。对于不同的环境,使用更少的
  <!DOCTYPE html>
  <html>
    <head>
      <title></title>
      <link rel="stylesheet/less" type="text/css"  href="style.less" />
      <script src="less.js" type="text/javascript"></script>
    </head>
    <body>
      [foo]
    </body>
  </html>
.mixin(production) {
  @import "test.less";
  background: @color;
}

.mixin(dev) {
  background: green;
}

html {
  .mixin(production);
}
@color: red;