【问题标题】:Load libraries with wiredep使用wiredep加载库
【发布时间】:2015-06-12 00:38:19
【问题描述】:

我正在 AngulaJS 中迈出第一步。我尝试通过 Bower 和 Gulpfile 加载依赖关系。

我已经安装了wiredep、gulp-inject 和bower。在我的 Gulpfile.js 中,我定义了下一个任务:

'use strict';

var gulp = require('gulp');
var inject = require('gulp-inject');
var wiredep = require('wiredep').stream;

gulp.task('inject', function() {
    var sources = gulp.src(['./app/scripts/**/*.js','./app/stylesheets/**/*.css']);
    return gulp.src('index.html', {cwd: './app'}).pipe(inject(sources, {
        read: false,
        ignorePath: '/app'
    })).pipe(gulp.dest('./app'));
});

gulp.task('wiredep', function () {
    gulp.src('./app/index.html').pipe(wiredep({
        directory: './app/lib'
    })).pipe(gulp.dest('./app'));
});

gulp.task('watch', function() {
    gulp.watch(['./app/stylesheets/**/*.css'], ['css', 'inject']);
    gulp.watch(['./app/scripts/**/*.js', './Gulpfile.js'], ['jshint', 'inject']);
    gulp.watch(['./bower.json'], ['wiredep']);
});

gulp.task('default', ['watch', 'inject', 'wiredep']);

当我执行 gulp 命令时,app/stylesheets 中允许的 css 被正确包含,但 app/lib 中允许的 JS 文件没有。控制台输出为:

[21:37:17] Using gulpfile /var/www/angular/fullcity-dashboard/Gulpfile.js
[21:37:17] Starting 'watch'...
[21:37:17] Finished 'watch' after 15 ms
[21:37:17] Starting 'inject'...
[21:37:17] Starting 'wiredep'...
[21:37:17] Finished 'wiredep' after 3.2 ms
[21:37:17] gulp-inject 1 files into index.html.
[21:37:17] Finished 'inject' after 65 ms
[21:37:17] Starting 'default'...
[21:37:17] Finished 'default' after 15 μs

我在 index.html 中定义了下一行:

<!-- bower:js -->
<!-- endbower -->
<!-- inject:js -->
<!-- endinject -->

有什么想法吗?

更新 1

我在项目根目录中有我的文件 .bowerrc,其中包含下一个内容:

{
    "directory": "app/lib"
}

还有我的 bower.json:

{
  "name": "sarasa",
  "version": "0.0.1",
  "description": "Sarasa",
  "dependencies": {
    "angular": "~1.4.0"
  }
}

我通过bower安装了angular,angular包在app/lib/angular下。

【问题讨论】:

  • 您是否有bower.json 文件,您是否运行过bower install,并且deps 是否实际安装到app/lib 中?另外,你不妨在同一个任务中运行inject()wiredep()
  • bower.json 文件中没有依赖项。我什至不认为directory 进入bower.json,这更像是一个.bowerrc 配置项

标签: angularjs gulp bower


【解决方案1】:

我会说您的任务执行不正常,因为您没有返回任何流或在其中一些中执行任何回调(请参阅此处的注释 ~https://github.com/gulpjs/gulp/blob/master/docs/API.md#deps

这可能意味着您的wiredep 任务没有./app/index.html 可以处理。我会将这两个功能移到一个任务中,例如

gulp.task('injectables', function() {
    var sources = gulp.src(['./app/scripts/**/*.js','./app/stylesheets/**/*.css'], {read: false});
    return gulp.src('index.html', {cwd: './app'})
        .pipe(wiredep())
        .pipe(inject(sources, {
            ignorePath: '/app'
        }))
        .pipe(gulp.dest('./app'));
});

【讨论】:

  • 谢谢@Phil !!现在有效:) 但我不明白......当阅读凉亭依赖时?
  • @ramiromd 在.pipe(wiredep()) 行中。这就是wiredep所做的;读取 bower 依赖项并将它们注入 HTML。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-04
  • 2016-05-11
  • 2015-04-17
  • 2018-05-10
相关资源
最近更新 更多