【问题标题】:LESS: make to create less files itselfLESS:make 自己创建更少的文件
【发布时间】:2015-02-17 16:49:39
【问题描述】:

晚安,

我对 makefiles 完全陌生,并制定了一个非常适合我们需求的文件,但我并不完全满意。我们使用 bootstrap3 并且有大约 40 个不同颜色设置的客户。这就是为什么我们需要编译 40 个稍微不同的 css 文件。到现在为止,我们有如下文件结构

  • less/customer1.less
  • css/customer1.css
  • color/customer.less 包含引导变量文件

customer1.less 包含

@variables: 'myCompany/color/customer1'; //this is forwarded to where bootrstrap loads the variables template
@import "bootstrap";
@import 'myCompany/modifications';

我们的makefile

SOURCES = $(shell ls less/*.less)
# Files we don't want to be build
SOURCES := $(filter-out less/bootstrap.less, $(SOURCES))
SOURCES := $(filter-out less/a11y.less, $(SOURCES))
TARGETS = $(patsubst less/%.less,css/%.css,$(SOURCES))
DEPEND = $(patsubst less/%.less,make/%.d,$(SOURCES))
css/%.css: less/%.less 
    # First building dependency files
    lessc -M $< $@ > 'make/$*.d'
    # Then building CSS and sourcemap
    lessc -s $< > $@ --source-map=map/$*.css.map --source-map-basepath=map --clean-css
-include $(DEPEND)
all: $(TARGETS)

打电话

$ make all

在 make/ 中创建 Makefile,在 css/ 中创建 CSS/在 map/ 中创建 CSS 源映射,并期望 LESS 在 less/ 中。

这可行,但我们需要为每个客户手动创建 customerX.less,即使唯一的区别是分配的配色方案/变量文件。 如果有此 customerX 的文件,Make 应查看颜色文件夹,然后在 less 目录中创建(但不覆盖!)customerX.less。

这里有任何制作大师知道如何使用 make 做到这一点吗?

【问题讨论】:

  • 上面的makefile好像还是有问题。对于customer1,一切都很好。但它不会续订customer2。该行为取决于包含声明中的顺序。如果我首先包含 customer2 一切都适用于该客户,但随后 customer1 丢失。有人知道为什么吗?已经尝试设置一个包含依赖项的新变量 DEPEND。尽管如此,依赖顺序也存在同样的问题。
  • 发生这种情况是因为我调用了“make”而不是“make all”。我已经改进了上面的代码。

标签: makefile less


【解决方案1】:

我相信您可以在这里通过order-only prerequisite 做您想做的事。

类似:

less/customer%.less: | color/customer.less
        [ -f '$@' ] || cp $^ $@

我不认为 -f 测试在那里是绝对必要的,但它不应该伤害并且更安全。

在不同的主题上,$(shell ls less/*.less) 使用$(shell echo less/*.less)(你不关心ls 你只想要shell glob 扩展)或$(wildcard less/*.less) 可能会做得更好。 (从技术上讲,脱壳和通配符略有不同,但我不知道这对你来说是否重要。)

另请注意,all 目标不会为您创建这些缺少的文件(因为SOURCES 不会包含它们,因为文件不存在)但make css/customer#.css 会在必要时创建它们。

【讨论】:

  • 非常感谢您的回答。我还没有时间尝试,但会尝试的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-07
  • 1970-01-01
相关资源
最近更新 更多