【问题标题】:Gulp-less not building LESS when included file is changed更改包含的文件时,Gulp-less 不会构建 LESS
【发布时间】:2015-02-06 12:55:01
【问题描述】:

我正在使用以下文件夹结构:

css
|_ main.css
|
|_ less
   |_ main.less
   |_ sub.less
   |
   |_ shared
      |_ variables.less
      |_ header.less
      |_ footer.less

我在我的 gulpfile.js

中使用以下内容
gulp.task('less', function() {
    gulp.src(paths.less + 'main.less', {read: true})
        .pipe(plugins.plumber({errorHandler: onError}))
        .pipe(plugins.less({
            paths: [paths.less + 'shared', paths.less]
        }))
        .pipe(plugins.plumber.stop())
        .pipe(gulp.dest(paths.css));
});

...

gulp.task('less:watch', function(){
    gulp.watch([paths.less + '**/*.less'], ['less']);
});

gulp.task('default', ['less', 'less:watch']);

我的 ma​​in.less 包括所有其他 LESS 文件的 @imports。

@import "sub.less";
@import "./shared/variables.less";
@import "./shared/header.less";
@import "./shared/footer.less";
  • 如果更新了 LESS 文件(main.less 除外),则文件 main.css 不会更改。

  • 如果 main.less 更新(在任何其他 LESS 文件更改后需要),main.css 然后更新。

如何修改我的 gulpfile.js 以更新“main.css”文件 不管哪个LESS文件被改变?

谢谢。

【问题讨论】:

  • 这个问题更多的是关于观看而不是关于 gulp-less。你能把你gulpfile的手表部分包括进来吗?

标签: javascript less gulp gulp-watch gulp-less


【解决方案1】:

我无法直接重现您的问题,但我预计这可能是由于从文件系统文件缓存中读取了修改后的文件。

Less 使用fs.readFile 调用来读取导入的文件。默认情况下,此调用使用r 选项打开文件。文档没有明确说明 r 选项使用文件系统文件缓存,但文档确实告诉您 rs option 绕过了文件系统文件缓存:

'rs' - 打开文件以同步模式读取。指示 操作系统绕过本地文件系统缓存。

这主要用于在 NFS 挂载上打开文件,因为它允许 您跳过可能陈旧的本地缓存。它有一个非常真实的 对 I/O 性能的影响,因此除非您需要,否则不要使用此标志。

请注意,这不会将 fs.open() 变成同步阻塞 称呼。如果这就是你想要的,那么你应该使用 fs.openSync()

可能的解决方案:

让 Less 使用 rs 选项打开文件。找到node_modules/gulp-less/node_modules/less/lib/less-node/file-manager.js文件并将line 56替换为以下代码:

fs.readFile(fullFilename, {encoding: 'utf-8', flag: 'rs'}, function(e, data) {

否则你可能想知道为什么你修改的文件还在文件缓存中并尝试 rsync 文件缓存:https://superuser.com/questions/319286/how-to-totally-clear-the-filesytems-cache-on-linux

【讨论】:

    【解决方案2】:

    gulp-progeny 插件针对您的工作流程。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-09
      • 1970-01-01
      相关资源
      最近更新 更多