javascript - grunt-bower-install 将 bower_components 链接到当前目录

标签 javascript gruntjs yeoman bower

我最近开始了一个新项目,使用 Yeoman Angular 生成器与 Grunt 和 Bower。

每当 grunt 构建我的应用程序时,grunt-bower-install 都会在 index.html 文件中重新生成指向我的 bower_components 的所有链接。

无论出于何种原因,这些 Assets 都链接到当前目录而不是根目录,因此当我导航到一个深度超过一层的新 Url 时,我的所有依赖项都会中断。

如何使组件链接到根目录而不是当前目录?

当前结果:

<script src="bower_components/modernizr/modernizr.js"></script>

期望的结果:

<script src="/bower_components/modernizr/modernizr.js"></script>

Grunt 文件:

'bower-install': {
  app: {
    html: '<%= yeoman.app %>/index.html',
    ignorePath: '<%= yeoman.app %>/'
  }
}

最佳答案

我和 yo 1.1.2 有同样的问题,这就是我解决问题的方法,在 Gruntfile.js 中,在 wiredep 任务中添加 fileTypes 选项:

// Automatically inject Bower components into the app
wiredep: {
  app: {
    src: ['<%= yeoman.app %>/index.html'],
    ignorePath: new RegExp('^<%= yeoman.app %>/|../'),
    fileTypes: {
      html: {
        replace: {
          js: '<script src="/{{filePath}}"></script>',
          css: '<link rel="stylesheet" href="/{{filePath}}" />'
        }
      }
    }
  }
},

关于javascript - grunt-bower-install 将 bower_components 链接到当前目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21860918/

相关文章:

javascript - HTML 按钮在 Javascript 文本字段中添加数字

deployment - Foundation 5 生产环境文件

node.js - openshift 上的 socket.io 和 Angular-fullstack

javascript - 我怎样才能取消之前的请求发送矩形

java - spring 文件上传中回调

javascript - ngx-google-places-autocomplete 输入不更新所选字符串

javascript - 找不到本地 Npm 模块 "grunt-template-jasmine-istanbul"

javascript - 删除未使用 Assets 的繁重任务、脚本或应用程序?

javascript - 如何在 Yeoman 中等到复制操作完成

angularjs - 是否有任何自动化的 [grunt] 任务可以在 index.html 中为 CSS/JS 文件添加 CDN?