Grunt 没有改变 bower_components

Grunt doesn't change bower_components

我已经生成了这个自耕农 GruntFile.js。当我 运行 "grunt" 时,dist 文件夹不会更改我所有来自 bower_components/*.js 的脚本,只有其中一些。

这是来自 dist 文件夹

的 index.html 示例
<script src="bower_components/jquery/dist/jquery.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.11/angular.min.js"></script>
<script src="bower_components/angular-bootstrap/ui-bootstrap.js"></script>
<script src="bower_components/angular-bootstrap/ui-bootstrap-tpls.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.11/angular-resource.min.js"></script>
<script src="bower_components/angular-cookies/angular-cookies.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.11/angular-sanitize.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.11/angular-animate.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.11/angular-touch.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.11/angular-route.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.11/angular-messages.min.js"></script>
<script src="bower_components/angular-utf8-base64/angular-utf8-base64.js"></script>
<script src="bower_components/ng-table/ng-table.js"></script>
<script src="bower_components/lodash/dist/lodash.js"></script>
<script src="bower_components/angular-growl-v2/build/angular-growl.js"></script>
<script src="bower_components/nya-bootstrap-select/dist/js/nya-bs-select.js"></script>

如您所见,它找不到要使用的正确 js 文件。我怎样才能解决这个问题? 我的 css 文件

也有同样的问题
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css">
  <link rel="stylesheet" href="bower_components/ng-table/ng-table.css">
  <link rel="stylesheet" href="bower_components/angular-growl-v2/build/angular-growl.css">
  <link rel="stylesheet" href="bower_components/nya-bootstrap-select/dist/css/nya-bs-select.css">

谢谢! 如果您在我的 GruntFile.js 中发现任何其他错误或改进,请告诉我

编辑

这是我的 bower.json 文件

{
  "name": "civilization-web",
  "version": "0.0.0",
  "dependencies": {
    "angular": "~1.3.7",
    "json3": "~3.3.1",
    "es5-shim": "~3.1.0",
    "bootstrap": "~3.3.1",
    "angular-resource": "~1.3.7",
    "angular-sanitize": "~1.3.7",
    "angular-animate": "~1.3.7",
    "angular-touch": "~1.3.7",
    "angular-route": "~1.3.7",
    "angular-utf8-base64": "~0.0.5",
    "angular-messages": "~1.3.8",
    "ng-table": "~0.3.3",
    "angular-bootstrap": "~0.12.0",
    "ng-file-upload": "~2.0.5",
    "ng-file-upload-shim": "~2.0.5",
    "lodash": "2.4.1",
    "angular-growl-v2": "~0.7.3",
    "nya-bootstrap-select": "~2.0.8"
  },
  "devDependencies": {
    "angular-mocks": "~1.3.7",
    "angular-scenario": "~1.3.7"
  },
  "appPath": "app",
  "resolutions": {
    "angular": "1.3.8"
  }
}

这是我的 package.json

{
  "name": "civilizationweb",
  "version": "0.0.0",
  "dependencies": {},
  "devDependencies": {
    "grunt": "^0.4.5",
    "grunt-autoprefixer": "^0.7.3",
    "grunt-concurrent": "^0.5.0",
    "grunt-contrib-clean": "^0.5.0",
    "grunt-contrib-concat": "^0.4.0",
    "grunt-contrib-connect": "^0.7.1",
    "grunt-contrib-copy": "^0.5.0",
    "grunt-contrib-cssmin": "^0.9.0",
    "grunt-contrib-htmlmin": "^0.3.0",
    "grunt-contrib-imagemin": "^0.8.1",
    "grunt-contrib-jshint": "^0.10.0",
    "grunt-contrib-uglify": "^0.4.0",
    "grunt-contrib-watch": "^0.6.1",
    "grunt-filerev": "^0.2.1",
    "grunt-google-cdn": "^0.4.0",
    "grunt-karma": "^0.9.0",
    "grunt-newer": "^0.7.0",
    "grunt-ng-annotate": "^0.10.0",
    "grunt-svgmin": "^0.4.0",
    "grunt-usemin": "^2.1.1",
    "grunt-wiredep": "^1.7.0",
    "jshint-stylish": "^0.2.0",
    "karma": "^0.12.24",
    "karma-jasmine": "^0.1.5",
    "karma-phantomjs-launcher": "^0.1.4",
    "load-grunt-tasks": "^0.4.0",
    "time-grunt": "^0.3.1"
  },
  "engines": {
    "node": ">=0.10.0"
  },
  "scripts": {
    "test": "grunt test"
  }
}

检查您是否在 html 文件中进行了设置。这就是触发 grunt uglyfy/css min 任务的原因。 或者你可以在你的 grunt 配置文件中取消注释这些任务,并确保它们在你的构建任务中的按钮中被取消注释。 祝你好运

除非您在构建过程中告诉 grunt 将这些文件复制到 /dist 文件夹,否则它不会执行任何操作。您可以使用 grunt 复制来执行此操作。

// Copies remaining files to places other tasks can use
copy: {
  dist: {
    files: [{
      expand: true,
      dot: true,
      cwd: '<%= yeoman.client %>',
      dest: '<%= yeoman.dist %>/public',
      src: [
        '*.{ico,png,txt}',
        '.htaccess',
        'bower_components/**/*',
        'assets/images/{,*/}*.{webp}',
        'assets/fonts/**/*',
        'index.html'
      ]
    }, {
      expand: true,
      cwd: '.tmp/images',
      dest: '<%= yeoman.dist %>/public/assets/images',
      src: ['generated/*']
    }, {
      expand: true,
      dest: '<%= yeoman.dist %>',
      src: [
        'package.json',
        'server/**/*'
      ]
    }]
  },
  styles: {
    expand: true,
    cwd: '<%= yeoman.client %>',
    dest: '.tmp/',
    src: ['{app,components}/**/*.css']
  }
},

我建议您也使用 concat/usemin 来连接所有 javascript。

<!-- build:js({.tmp,client}) app/concat.js -->
<script src="app/app1.js"></script>
<script src="app/app2.js"></script>
<script src="app/app3.js"></script>
<!-- endbuild -->

这将使用 <!-- build:js --> 标签之间的脚本标签创建一个 concat.js 文件。然后可以使用 grunt copy 复制此文件。

我认为您需要在 index.html 中添加注释以告诉 grunt 应该在何处添加 CSS 和 JS 文件。此外,如果您手动更改这些标签之间的某些内容,它将在下次重新启动 grunt 时被覆盖(这是我注意到的,不确定它是否是标准功能)

    <!-- build:<type>(alternate search path) <path> -->
... HTML Markup, list of script / link tags.
<!-- endbuild -->

这是 angular 生成器生成的内容:

<!-- build:js({client,node_modules}) app/vendor.js -->
  <!-- bower:js -->
  <!-- endbower --><% if(filters.socketio) { %>
  <script src="socket.io-client/socket.io.js"></script><% } %>
<!-- endbuild -->

    <!-- build:js({.tmp,client}) app/app.js -->
    <script src="app/app.js"></script>
      <!-- injector:js -->
      <!-- endinjector -->
    <!-- endbuild -->

grunt 需要用构建标签封装(bower 需要用 bower 封装)

您 Gruntfile.js 中的 usemin 任务将查找

<!-- build:{{type}}  {{path/fileName}} -->

<!-- endbuild -->

并将这两个文件之间列出的所有文件连接成一个文件并将其缩小。这个缩小的文件将写入您在 {{path/fileName}}

中提到的路径

...因此,如果我理解得很好,那么您的 Gruntfile 非常好,并且您可能在 index.html

中缺少 usemin 注释块

将所有 link 和脚本标签放在 usemin 块之间

例如,对于您的凉亭组件,您可以使用类似这样的东西

<!-- build:css(.) styles/vendor.css -->
  <!-- bower:css -->

    <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css" />
    <link rel="stylesheet" href="bower_components/ng-table/ng-table.css">
    <link rel="stylesheet" href="bower_components/angular-growl-v2/build/angular-growl.css">
    <link rel="stylesheet" href="bower_components/nya-bootstrap-select/dist/css/nya-bs-select.css">

  <!-- endbower -->
<!-- endbuild -->


<!-- build:js(.) scripts/vendor.js -->
  <!-- bower:js -->

    <script src="bower_components/jquery/dist/jquery.js"></script>
    <script src="bower_components/angular/angular.js"></script>
    <script src="bower_components/bootstrap/dist/js/bootstrap.js"></script>
    <script src="bower_components/angular-animate/angular-animate.js"></script>
    <script src="bower_components/angular-aria/angular-aria.js"></script>
    <script src="bower_components/angular-cookies/angular-cookies.js"></script>
    <script src="bower_components/angular-messages/angular-messages.js"></script>
    <script src="bower_components/angular-resource/angular-resource.js"></script>
    <script src="bower_components/angular-route/angular-route.js"></script>
    <script src="bower_components/angular-sanitize/angular-sanitize.js"></script>
    <script src="bower_components/angular-touch/angular-touch.js"></script>

  <!-- endbower -->
<!-- endbuild -->

注意: 所有 Bower 组件 css 文件将进入样式目录中的 vendor.css,即 dist/styles/vendor.css同样,Bower 组件 js 文件将进入脚本目录中的 vendor.js,即 dist/scripts/vendor.js

...对于您自己的 js 和 css 文件,您可以使用类似这样的东西

<!-- build:css(.tmp) styles/main.css -->

  <link rel="stylesheet" href="styles/css1.css">
  <link rel="stylesheet" href="styles/css2.css">

<!-- endbuild -->

<!-- build:js({.tmp,app}) scripts/scripts.js -->

    <script src="scripts/app.js"></script>
    <script src="scripts/controllers/main.js"></script>
    <script src="scripts/controllers/about.js"></script>

<!-- endbuild -->

注意: 这里所有的 css 文件都将放在 main.css 样式目录中,即 dist/styles/main.css 和同样,js 文件将进入脚本目录中的 main.js,即 dist/scripts/main.js

...希望对您有所帮助:)