问题描述
在我的办公室里,我们正在使用 gulp 来构建我们的 less 文件.我想改进构建任务,因为在我们最近从事的一个大型项目上构建需要花费一秒钟的时间.这个想法是缓存文件并只传递更改的文件.所以我从 google 开始,发现 javascript 的增量构建我认为用更少的钱重写它们很容易.这是我开始的一个:https://github.com/gulpjs/gulp/blob/master/docs/recipes/incremental-builds-with-concatenate.md
In my office we are using gulp to build our less files. I wanted to improve the build task as it took over a second to build on a large project we recently worked on. The idea was to cache the files and only pass the one that changed. So I started with google and found incremental builds for javascript ang thought it would be easy to rewrite them for less. Here's the one I started with: https://github.com/gulpjs/gulp/blob/master/docs/recipes/incremental-builds-with-concatenate.md
经过几次不成功的尝试后,我最终得到了以下代码(使用最新的引导发行版进行了测试):
After a few unsuccessful tries I ended up with following code (tested with the latest bootstrap distribution):
但这只会传递更改的文件,并且由于缺少变量定义,less 编译器会失败.如果我在 less 任务之前通过管道连接 concat 插件,gulp 会陷入(看似)无限循环.
But this passes only the changed file and the less compiler fails because of the variable definitions missing. If I pipe the concat plugin before the less task, gulp gets stuck in a (seemingly) endless loop.
有没有人使用过这些插件或设法以其他方式创建增量更少的构建.这是一个用于测试的(杂乱的)github存储库:https://github.com/tuelsch/perfect-less-build
Has anyone experience with those plugins or managed to create an incremental less build in an other way. Here is a (messy) github repository for testing: https://github.com/tuelsch/perfect-less-build
PS:我计划添加 linting、sourcemaps、minification、evtl.稍后缓存清除和自动前缀.
PS: I'm planning on adding linting, sourcemaps, minification, evtl. cache busting and autoprefixer later on.
推荐答案
和 Ashwell 一样,我发现使用导入来确保我的所有 LESS 文件都可以访问他们需要的变量和 mixin 很有用.我还使用带有导入的 LESS 文件来进行捆绑.这有几个优点:
Like Ashwell, I've found it useful to use imports to ensure that all my LESS files have access to the variables and mixins that they need. I also use a LESS file with imports for bundling purposes. This has a few advantages:
- 我可以利用 LESS 的功能来执行复杂的操作,例如覆盖变量值以生成多个主题,或者为另一个 LESS 文件中的每个规则添加一个类.
- 不需要 concat 插件.
- Web Essentials for Visual Studio 等工具可以提供语法帮助和输出预览,因为每个 LESS 文件都完全能够自行呈现.
如果你想导入变量、mixins等,但又不想实际输出另一个文件的全部内容,你可以使用:
Where you want to import variables, mixins, etc, but you don't want to actually output the entire contents of another file, you can use:
经过几天的努力,我终于能够获得一个增量构建,它可以正确地重建依赖于我更改的 LESS 文件的所有对象.我在这里记录了结果.这是最终的 gulpfile:
After a few days of effort, I was finally able to get an incremental build that correctly rebuilds all the objects that depend on the LESS file I changed. I documented the results here. This is the final gulpfile:
我们现在可以简单地运行 gulp live
来构建我们所有的 LESS 文件一次,然后允许每个后续更改只构建依赖于已更改文件的那些文件.
We can now simply run gulp live
to build all our LESS files once, and then allow each subsequent change to just build those files that depend on the changed files.
这篇关于增量 gulp 少构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!