【问题标题】:Gulp error with yeoman webapp generator using gulp-jade使用 gulp-jade 的 yeoman webapp 生成器出现 Gulp 错误
【发布时间】:2014-04-21 10:21:46
【问题描述】:

我很难将jade 与yeoman 的gulp webapp 生成器集成在一起。 监视任务已经按预期工作,但是一旦我尝试构建项目,我就会收到以下错误:

stream.js:94
      throw er; // Unhandled stream error in pipe.
            ^
TypeError: path must be a string

我认为这是因为我将 html 任务更改为 return gulp.src('.tmp/*.html')最初是 'app/*.html',它运行时没有错误,但当然会忽略我的玉模板)。

我的 gulpfile:

'use strict';
// generated on 2014-04-17 using generator-gulp-webapp 0.0.7

var gulp = require('gulp'),
    jade = require('gulp-jade');

// load plugins
var $ = require('gulp-load-plugins')();

gulp.task('styles', function () {
    return gulp.src('app/styles/main.sass')
        .pipe($.rubySass({
            style: 'expanded',
            compass: true,
            loadPath: 'app/bower_components'
        }))
        .pipe($.autoprefixer('> 5%', 'last 3 versions', 'ff >= 20', 'ie 9'))
        .pipe(gulp.dest('.tmp/styles'))
        .pipe($.size());
});

gulp.task('templates', function() {
    var YOUR_LOCALS = {};

    return gulp.src('app/*.jade')
        .pipe(jade({
            locals: YOUR_LOCALS,
            pretty: true
        }))
        .pipe(gulp.dest('.tmp'))
        .pipe($.size());
});

gulp.task('scripts', function () {
    return gulp.src('app/scripts/**/*.js')
        .pipe($.jshint())
        .pipe($.jshint.reporter($.jshintStylish))
        .pipe($.size());
});

gulp.task('html', ['templates', 'styles', 'scripts'], function () {
    var jsFilter = $.filter('**/*.js');
    var cssFilter = $.filter('**/*.css');

    return gulp.src('.tmp/*.html')
        .pipe($.useref.assets())
        .pipe(jsFilter)
        .pipe($.uglify())
        .pipe(jsFilter.restore())
        .pipe(cssFilter)
        .pipe($.csso())
        .pipe(cssFilter.restore())
        .pipe($.useref.restore())
        .pipe($.useref())
        .pipe(gulp.dest('dist'))
        .pipe($.size());
});

gulp.task('images', function () {
    return gulp.src('app/images/**/*')
        .pipe($.cache($.imagemin({
            optimizationLevel: 3,
            progressive: true,
            interlaced: true
        })))
        .pipe(gulp.dest('dist/images'))
        .pipe($.size());
});

gulp.task('fonts', function () {
    return $.bowerFiles()
        .pipe($.filter('**/*.{eot,svg,ttf,woff}'))
        .pipe($.flatten())
        .pipe(gulp.dest('dist/styles/fonts'))
        .pipe($.size());
});

gulp.task('clean', function () {
    return gulp.src(['.tmp', 'dist'], { read: false }).pipe($.clean());
});

gulp.task('build', ['html', 'images', 'fonts']);

gulp.task('default', ['clean'], function () {
    gulp.start('build');
});

gulp.task('connect', function () {
    var connect = require('connect');
    var app = connect()
        .use(require('connect-livereload')({ port: 35729 }))
        .use(connect.static('app'))
        .use(connect.static('.tmp'))
        .use(connect.directory('app'));

    require('http').createServer(app)
        .listen(9000)
        .on('listening', function () {
            console.log('Started connect web server on http://localhost:9000');
        });
});

gulp.task('serve', ['connect', 'styles', 'templates'], function () {
    require('opn')('http://localhost:9000');
});

// inject bower components
gulp.task('wiredep', function () {
    var wiredep = require('wiredep').stream;

    gulp.src('app/styles/*.sass')
        .pipe(wiredep({
            directory: 'app/bower_components'
        }))
        .pipe(gulp.dest('app/styles'));

    gulp.src('app/*.html')
        .pipe(wiredep({
            directory: 'app/bower_components'
        }))
        .pipe(gulp.dest('app'));
});

gulp.task('watch', ['connect', 'serve'], function () {
    var server = $.livereload();

    // watch for changes

    gulp.watch([
        'app/*.html',
        'app/**/*.jade',
        '.tmp/styles/**/*.css',
        'app/scripts/**/*.js',
        'app/images/**/*'
    ]).on('change', function (file) {
        server.changed(file.path);
    });

    gulp.watch('app/**/*.jade', ['templates']);
    gulp.watch('app/styles/**/*.sass', ['styles']);
    gulp.watch('app/scripts/**/*.js', ['scripts']);
    gulp.watch('app/images/**/*', ['images']);
    gulp.watch('bower.json', ['wiredep']);
});

有什么建议吗?

【问题讨论】:

  • 您使用的是gulp-load-plugins,因此您可以删除var jade = require('gulp-jade'); 并将下面的jade() 引用替换为$.jade()
  • 错误TypeError: path must be a string 很模糊。这基本上意味着 useref 找不到源文件。

标签: javascript node.js pug yeoman gulp


【解决方案1】:

显然错误来自 gulp-useref 我的构建块有问题。 更改所有块以使用 {app,.tmp} 作为搜索路径修复了行为,例如:

// build:js({app,.tmp}) scripts/vendor/modernizr.js 
script(src='bower_components/modernizr/modernizr.js')
// endbuild

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。

    要修复您的 wiredep 任务,请将其添加到 gulp.src('app/*.html') 块之后:

    gulp.src('app/*.jade')
        .pipe(wiredep({
            directory: 'app/bower_components'
        }))
        .pipe(gulp.dest('app'));
    

    这将在您的 bower.json 更改时更新您的 .jade 文件中的 // bower:js ... // endbower 块,由手表触发:

    gulp.watch('bower.json', ['wiredep']);
    

    在 gulpfile 的末尾。

    例如。

    当我在bower.json 中添加"jquery": "~1.11.0" 作为依赖项时(并保存)。

    在我的/app/layout.jade 文件中,这个:

    body
        // bower:js
        // endbower
    

    被替换为:

    body
        // bower:js
        script(src='bower_components/jquery/dist/jquery.js')
        // endbower
    

    如果您想使用build:js 而不将({app,.tmp}) 添加到所有块中,请在您的html 任务中将app.tmp 添加到源。

    您的.js.css 位于app 目录中,因此当您将源更改为.tmp/*.html 而不是app/*.html 时,您的两个$.filter 没有找到任何文件。

    要解决此问题,请将 gulp.src() 设为包含两个路径的数组。

    替换:

    gulp.task('html', ['templates', 'styles', 'scripts'], function () {
        var jsFilter = $.filter('**/*.js');
        var cssFilter = $.filter('**/*.css');
    
        return gulp.src('.tmp/*.html')
        ...
    

    与:

    gulp.task('html', ['templates', 'styles', 'scripts'], function () {
        var jsFilter = $.filter('**/*.js');
        var cssFilter = $.filter('**/*.css');
    
        return gulp.src(['app/*.html', '.tmp/*.html'])
        ...
    

    【讨论】:

    • 来自 Grunt Gulp 是一个完全不同的系统,但感谢您,我得到了这一切(也感谢您在 cmets 中的提示)。
    • 很抱歉,使用您的方法我仍然收到错误消息。也许是因为我的 CSS 也在 .tmp 文件夹中?我的 CSS 块如下所示:// build:css styles/main.css link(rel='stylesheet', href='styles/main.css') // endbuild 我还尝试将 (.tmp) 和 (app) 添加到所有块或将其排除在外……
    • 我自己今天才开始使用它。我使用 yeoman 生成器 0.0.8 构建了我的(你的是 0.0.7)。我注意到有一些差异,特别是 .scss.sass 扩展名。我的使用main.scss
    • 尝试再次运行npm install -g generator-gulp-webappyo gulp-webapp 以安装最新版本并比较两个gulpfile.js 文件。
    • 如果您想知道为什么您的 jshint-stylish 不起作用,请尝试在您的 scripts 任务中将 $.jshintStylish 替换为 require('jshint-stylish')。我向 Yeoman 提交了 PR 以在较新的版本中修复它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    • 2015-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-03
    • 2016-12-07
    相关资源
    最近更新 更多