Grunt在usemin块中包含bower_components

13

我在我的index.html页面中有以下代码块...

<!-- build:css(.tmp) styles/style.css -->
    <link rel="stylesheet" href="bower_components/normalize-css/normalize.css">
    <link rel="stylesheet" href="styles/main.css">
<!-- endbuild -->

我的应用程序结构中正确存在CSS文件,但是当我运行'grunt build'时,仅在生成的styles/style.css文件中包含来自styles/main.css的CSS。

我想我需要告诉Grunt在bower_components目录中查找CSS文件?但我不确定如何做到这一点?

下面是我的gruntfile.js副本...

    grunt.initConfig({
      yeoman: yeomanConfig,
      watch: {
        styles: {
          files: ['<%= yeoman.app %>/styles/{,*/}*.css'],
          tasks: ['copy:styles', 'autoprefixer']
        },
        livereload: {
          options: {
            livereload: LIVERELOAD_PORT
          },
          files: [
            '<%= yeoman.app %>/{,*/}*.html',
            '.tmp/styles/{,*/}*.css',
            '{.tmp,<%= yeoman.app %>}/scripts/{,*/}*.js',
            '<%= yeoman.app %>/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}'
          ]
        }
      },
      autoprefixer: {
        options: ['last 1 version'],
        dist: {
          files: [{
            expand: true,
            cwd: '.tmp/styles/',
            src: '{,*/}*.css',
            dest: '.tmp/styles/'
          }]
        }
      },
      connect: {
        options: {
          port: 9000,
          hostname: 'localhost'
        },
        livereload: {
          options: {
            middleware: function (connect) {
              return [
                lrSnippet,
                mountFolder(connect, '.tmp'),
                mountFolder(connect, yeomanConfig.app)
              ];
            }
          }
        },
        test: {
          options: {
            middleware: function (connect) {
              return [
                mountFolder(connect, '.tmp'),
                mountFolder(connect, 'test')
              ];
            }
          }
        },
        dist: {
          options: {
            middleware: function (connect) {
              return [
                mountFolder(connect, yeomanConfig.dist)
              ];
            }
          }
        }
      },
      open: {
        server: {
          url: 'http://localhost:<%= connect.options.port %>'
        }
      },
      clean: {
        dist: {
          files: [{
            dot: true,
            src: [
              '.tmp',
              '<%= yeoman.dist %>/*',
              '!<%= yeoman.dist %>/.git*'
            ]
          }]
        },
        server: '.tmp'
      },
      jshint: {
        options: {
          jshintrc: '.jshintrc'
        },
        all: [
          'Gruntfile.js',
          '<%= yeoman.app %>/scripts/{,*/}*.js'
        ]
      },
      rev: {
        dist: {
          files: {
            src: [
              '<%= yeoman.dist %>/scripts/{,*/}*.js',
              '<%= yeoman.dist %>/styles/{,*/}*.css',
              '<%= yeoman.dist %>/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}',
              '<%= yeoman.dist %>/styles/fonts/*'
            ]
          }
        }
      },
      useminPrepare: {
        html: '<%= yeoman.app %>/index.html',
        options: {
          dest: '<%= yeoman.dist %>'
        }
      },
      usemin: {
        html: ['<%= yeoman.dist %>/{,*/}*.html'],
        css: ['<%= yeoman.dist %>/styles/{,*/}*.css'],
        options: {
          dirs: ['<%= yeoman.dist %>']
        }
      },
      imagemin: {
        dist: {
          files: [{
            expand: true,
            cwd: '<%= yeoman.app %>/images',
            src: '{,*/}*.{png,jpg,jpeg}',
            dest: '<%= yeoman.dist %>/images'
          }]
        }
      },
      svgmin: {
        dist: {
          files: [{
            expand: true,
            cwd: '<%= yeoman.app %>/images',
            src: '{,*/}*.svg',
            dest: '<%= yeoman.dist %>/images'
          }]
        }
      },
      cssmin: {

      },
      htmlmin: {
        dist: {
          options: {

          },
          files: [{
            expand: true,
            cwd: '<%= yeoman.app %>',
            src: ['*.html', 'views/*.html'],
            dest: '<%= yeoman.dist %>'
          }]
        }
      },
      // Put files not handled in other tasks here
      copy: {
        dist: {
          files: [{
            expand: true,
            dot: true,
            cwd: '<%= yeoman.app %>',
            dest: '<%= yeoman.dist %>',
            src: [
              '*.{ico,png,txt}',
              '.htaccess',
              'bower_components/**/*',
              'images/{,*/}*.{gif,webp}',
              'styles/fonts/*'
            ]
          }, {
            expand: true,
            cwd: '.tmp/images',
            dest: '<%= yeoman.dist %>/images',
            src: [
              'generated/*'
            ]
          }]
        },
        styles: {
          expand: true,
          cwd: '<%= yeoman.app %>/styles',
          dest: '.tmp/styles/',
          src: '{,*/}*.css'
        }
      },
      concurrent: {
        server: [
          'copy:styles'
        ],
        test: [
          'copy:styles'
        ],
        dist: [
          'copy:styles',
          'imagemin',
          'svgmin',
          'htmlmin'
        ]
      },
      karma: {
        unit: {
          configFile: 'karma.conf.js',
          singleRun: true
        }
      },
      cdnify: {
        dist: {
          html: ['<%= yeoman.dist %>/*.html']
        }
      },
      ngmin: {
        dist: {
          files: [{
            expand: true,
            cwd: '<%= yeoman.dist %>/scripts',
            src: '*.js',
            dest: '<%= yeoman.dist %>/scripts'
          }]
        }
      },
      uglify: {
        dist: {
          files: {
            '<%= yeoman.dist %>/scripts/scripts.js': [
              '<%= yeoman.dist %>/scripts/scripts.js'
            ]
          }
        }
      }
    });
4个回答

10

我已经在HTML本身解决了这个问题:

    <!-- build:css styles/modules.css -->
    <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css">
    <link rel="stylesheet" href="bower_components/animate.css/animate.css">
    <link rel="stylesheet" href="bower_components/font-awesome/css/font-awesome.css">
    <link rel="stylesheet" href="not_bower/foo/plugin.css">
    <!-- endbuild -->

    <!-- build:css(.tmp) styles/main.css -->
    <link rel="stylesheet" href="styles/main.css">
    <!-- endbuild -->

你只需要生成两个文件,一个是放在你的 app/css 文件夹外的“外部”CSS文件,另一个是你的应用程序CSS文件。

使用此解决方案,您不必触摸 Gruntfile.js 文件。

希望能帮到你。


谢谢Marcal,这对我非常有帮助。比调整Usemin设置要简单得多。在修订之前确保这两个脚本也被连接起来会很好,但两个文件也不算太糟糕。 - eriksssss
@eriksssss 抱歉耽擱了,我認為目前並不可能,你只能使用一個替代路徑,而非多個 (https://github.com/yeoman/grunt-usemin#blocks)。 - Marçal Juan
如果您正在使用Angular生成器v0.9.0之后的版本,您还应该小心不要在<!-- bower:js -->和<!-- endbower -->之间放置任何内容,因为新的生成器使用grunt-wiredep会覆盖您的更改。 - christang
@christang 我把它禁用了,因为当我测试时出现了错误。 - Marçal Juan

9

Yeoman Angular生成器用于版本大于0.9.0的Angular使用grunt-wiredep来自动将Bower依赖注入到app/index.html中。wiredep查看bower_components中每个依赖项的bower.json配置,并根据main属性向app/index添加href。

例如,您可能有一个jquery-ui依赖项,其bower.json文件如下:

{
  "name": "jquery-ui",
  "version": "1.11.1",
  "main": [
    "jquery-ui.js"
  ], ...
}

当你运行grunt时,wiredep会自动将“jquery-ui.js”注入到你的app/index.html文件中。很方便!
问题是它可能会缺少依赖项。如果你使用bower来管理你的bower_components目录,你不想改变bower_components/jquery-ui中的bower.json文件,因为任何更改都将在重新构建依赖项时被清除。
相反,你可以在你的app bower.json中覆盖主属性。你可以使用bower overrides
  "overrides": {
    "jquery-ui": {
      "main": [
        "jquery-ui.js",
        "this/was/missing/jquery-ui.css"
      ]
    }
  }

这样可以让你将项目特定的覆盖保留在自己的应用程序配置中。

非常感谢。上述所有解决方案对我来说都没有起作用,但是你的解决方案完美地解决了我的问题!在我的情况下,它适用于https://github.com/jaysalvat/vegas。以下是我在bower.json中添加的内容: "overrides": { "vegas": { "main": [ "dist/jquery.vegas.js", "dist/jquery.vegas.css" ] } }。 - MaximeBernard

8

这里是其中一种解决方案。

copy: {

  ...

  styles: {
    expand: true,
    cwd: '<%= yeoman.app %>',
    dest: '.tmp/',
    src: [
      'styles/{,*/}*.css',
      'bower_components/**/*.css'
    ]
  }

问题的原因在于.normalize.css文件不存在于.tmp目录下,尽管index.html文件中有<!-- build:css(.tmp) styles/style.css -->代码。因此,你需要将CSS文件(实际上是上述情况下bower_components目录下的所有CSS文件)复制到.tmp目录下。
另外,你也可以在styles/main.css(或main.scss?)中使用@import语句。

将 bower_components 添加到样式 src 值中并没有解决问题。然而,使用 @import 可以解决问题,并且在这种情况下是更整洁的解决方案。谢谢。 - umpljazz
1
这对我有用,但必须考虑'cwd'(当前工作目录),以便可以相对于该位置找到bower_components目录,即我有cwd:'<%= yeoman.app%>/styles',这意味着我需要为bower的src条目添加'../',因此 '../bower_components/[etc]'。 - Darren Shewry

0
你可以尝试这个:
<!-- build:css({app,.tmp}) styles/style.css -->
    <link rel="stylesheet" href="bower_components/normalize-css/normalize.css">
    <link rel="stylesheet" href="styles/main.css">
<!-- endbuild -->

请参见此问题以获取更多详细信息


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接