useminPrepare导致`src`错误

时间:2013-11-10 14:49:02

标签: angularjs gruntjs grunt-usemin

我的index.html有:

<!doctype html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title></title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width">
    <!-- Place favicon.ico and apple-touch-icon.png in the root directory -->


    <link rel="stylesheet" href="/public/stylesheets/bootstrap.css">
    <link rel="stylesheet" href="styles/main.css">
  </head>
  <body ng-app="WebApp">
    <base href="/app">
    <!--[if lt IE 7]>
      <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
    <![endif]-->

    <!--[if lt IE 9]>
      <script src="bower_components/es5-shim/es5-shim.js"></script>
      <script src="bower_components/json3/lib/json3.min.js"></script>
    <![endif]-->

    <!-- Add your site or application content here -->
    <div class="container" ng-view=""></div>

    <!-- Google Analytics: change UA-XXXXX-X to be your site's ID -->
     <script>
       (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
       (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
       m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
       })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

       ga('create', 'UA-XXXXX-X');
       ga('send', 'pageview');
    </script>

    <script src="/app/bower_components/angular/angular.js"></script>
    <!-- build:js /app/scripts/angular.js -->
    <script src="/app/bower_components/angular-route/angular-route.js"></script> 
    <script src="/app/bower_components/angular-resource/angular-resource.js"></script>
    <script src="/app/bower_components/angular-cookies/angular-cookies.js"></script>
    <script src="/app/bower_components/angular-sanitize/angular-sanitize.js"></script>
    <!-- endbuild -->

    <!-- build:js({.tmp,app}) /app/scripts/scripts.js -->
    <script src="/app/scripts/app.js"></script>
    <script src="/app/scripts/controllers/main.js"></script>
    <!-- endbuild -->
</body>
</html>

我的所有角度代码都位于顶层的app目录中。我的Gruntfile.coffee是:

useminPrepare:
  html: "<%= appConfig.app %>/index.html"
  options:
    dest: "<%= appConfig.dist %>"

usemin:
  html: "<%= appConfig.dist %>/*.html"
  css: ["<%= appConfig.dist %>/styles/**/*.css"]
  options:
    dirs: ["<%= appConfig.dist %>"]

但是我收到了一个错误:

Running "useminPrepare:html" (useminPrepare) task
Going through app/index.html to update the config
Looking for build script HTML comment blocks
Warning: An error occurred while processing a template (Cannot read property 'src' of undefined). Use --force to continue.

我做错了什么?

2 个答案:

答案 0 :(得分:1)

我认为你的问题是,你想要连续/缩小角度源。记录的方法是使用提供的文件,如angular-route.min.js,并将它们包含在html的head部分。

因此,以下行会抛出该错误:

<script src="/app/bower_components/angular/angular.js"></script>
<!-- build:js /app/scripts/angular.js -->
<script src="/app/bower_components/angular-route/angular-route.js"></script> 
<script src="/app/bower_components/angular-resource/angular-resource.js"></script>
<script src="/app/bower_components/angular-cookies/angular-cookies.js"></script>
<script src="/app/bower_components/angular-sanitize/angular-sanitize.js"></script>
<!-- endbuild -->

作为旁注:这些有角度的文库在某些部分会产生沉重的复杂源代码,对于精确配置您的个人缩小器(如角度开发人员对他们所做的那样)提供公共角度来说是相当大的挑战。 -xxxxx.min.js文件。

答案 1 :(得分:1)

如果我理解正确,您的index.html文件位于app目录中,因此您可以尝试使用html脚本的相对链接。

<!-- build:js scripts/angular.js -->
<script src="bower_components/angular-route/angular-route.js"></script> 
<script src="bower_components/angular-resource/angular-resource.js"></script>
<script src="bower_components/angular-cookies/angular-cookies.js"></script>
<script src="bower_components/angular-sanitize/angular-sanitize.js"></script>
<!-- endbuild -->

<!-- build:js({.tmp,app}) scripts/scripts.js -->
<script src="scripts/app.js"></script>
<script src="scripts/controllers/main.js"></script>
<!-- endbuild -->