2015-01-07 41 views
0

我正在使用Yeoman和Grunt一起使用AngularJS應用程序。當使用grunt serve命令時,根本沒有問題。然而,當我使用grunt命令,並看看該網站,我得到這些錯誤:Yeoman AngularJS Grunt中斷模塊

Uncaught Error: [$injector:modulerr] Failed to instantiate module webshopAdminApp due to: 
Error: [$injector:modulerr] Failed to instantiate module ui.slider due to: 
Error: [$injector:nomod] Module 'ui.slider' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument. 

我咕嚕文件:

// Generated on 2014-10-02 using generator-angular 0.9.3 
'use strict'; 

// # Globbing 
// for performance reasons we're only matching one level down: 
// 'test/spec/{,*/}*.js' 
// use this if you want to recursively match all subfolders: 
// 'test/spec/**/*.js' 

module.exports = function (grunt) { 

    // Load grunt tasks automatically 
    require('load-grunt-tasks')(grunt); 

    // Time how long tasks take. Can help when optimizing build times 
    require('time-grunt')(grunt); 

    var modRewrite = require('connect-modrewrite'); 

    // Configurable paths for the application 
    var appConfig = { 
    app: require('./bower.json').appPath || 'app', 
    dist: 'dist' 
    }; 

    // Define the configuration for all the tasks 
    grunt.initConfig({ 

    // Project settings 
    yeoman: appConfig, 

    // Watches files for changes and runs tasks based on the changed files 
    watch: { 
     bower: { 
     files: ['bower.json'], 
     tasks: ['wiredep'] 
     }, 
     js: { 
     files: ['<%= yeoman.app %>/scripts/{,*/}*.js'], 
     tasks: ['newer:jshint:all'], 
     options: { 
      livereload: '<%= connect.options.livereload %>' 
     } 
     }, 
     jsTest: { 
     files: ['test/spec/{,*/}*.js'], 
     tasks: ['newer:jshint:test', 'karma'] 
     }, 
     styles: { 
     files: ['<%= yeoman.app %>/styles/{,*/}*.css'], 
     tasks: ['newer:copy:styles', 'autoprefixer'] 
     }, 
     gruntfile: { 
     files: ['Gruntfile.js'] 
     }, 
     livereload: { 
     options: { 
      livereload: '<%= connect.options.livereload %>' 
     }, 
     files: [ 
      '<%= yeoman.app %>/{,*/}*.html', 
      '.tmp/styles/{,*/}*.css', 
      '<%= yeoman.app %>/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}' 
     ] 
     } 
    }, 

    // The actual grunt server settings 
    connect: { 
     options: { 
     port: 9090, 
     // Change this to '0.0.0.0' to access the server from outside. 
     hostname: 'lumino.gidomanders.nl', 
     livereload: 35730 
     }, 
     livereload: { 
     options: { 
      open: true, 
      middleware: function (connect) { 
      return [ 
       modRewrite(['^[^\\.]*$ /index.html [L]']), //Matches everything that does not contain a '.' (period) 
       connect.static('.tmp'), 
       connect().use(
       '/bower_components', 
       connect.static('./bower_components') 
      ), 
       connect.static(appConfig.app) 
      ]; 
      } 
     } 
     }, 
     test: { 
     options: { 
      port: 9091, 
      middleware: function (connect) { 
      return [ 
       connect.static('.tmp'), 
       connect.static('test'), 
       connect().use(
       '/bower_components', 
       connect.static('./bower_components') 
      ), 
       connect.static(appConfig.app) 
      ]; 
      } 
     } 
     }, 
     dist: { 
     options: { 
      open: true, 
      base: '<%= yeoman.dist %>' 
     } 
     } 
    }, 

    // Make sure code styles are up to par and there are no obvious mistakes 
    jshint: { 
     options: { 
     jshintrc: 'test/.jshintrc', 
     reporter: require('jshint-stylish') 
     }, 
     all: { 
     src: [ 
      'Gruntfile.js', 
      '<%= yeoman.app %>/scripts/{,*/}*.js' 
     ] 
     }, 
     test: { 
     options: { 
      jshintrc: 'test/.jshintrc' 
     }, 
     src: ['test/spec/{,*/}*.js'] 
     } 
    }, 

    // Empties folders to start fresh 
    clean: { 
     dist: { 
     files: [{ 
      dot: true, 
      src: [ 
      '.tmp', 
      '<%= yeoman.dist %>/{,*/}*', 
      '!<%= yeoman.dist %>/.git*' 
      ] 
     }] 
     }, 
     server: '.tmp' 
    }, 

    // Add vendor prefixed styles 
    autoprefixer: { 
     options: { 
     browsers: ['last 1 version'] 
     }, 
     dist: { 
     files: [{ 
      expand: true, 
      cwd: '.tmp/styles/', 
      src: '{,*/}*.css', 
      dest: '.tmp/styles/' 
     }] 
     } 
    }, 

    // Automatically inject Bower components into the app 
    wiredep: { 
     options: { 
     cwd: '<%= yeoman.app %>/../' 
     }, 
     app: { 
     src: ['<%= yeoman.app %>/index.html'], 
     ignorePath: /\.\.\// 
     } 
    }, 

    // Renames files for browser caching purposes 
    filerev: { 
     dist: { 
     src: [ 
      '<%= yeoman.dist %>/scripts/{,*/}*.js', 
      '<%= yeoman.dist %>/styles/{,*/}*.css', 
      '<%= yeoman.dist %>/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}', 
      '<%= yeoman.dist %>/styles/fonts/*' 
     ] 
     } 
    }, 

    // Reads HTML for usemin blocks to enable smart builds that automatically 
    // concat, minify and revision files. Creates configurations in memory so 
    // additional tasks can operate on them 
    useminPrepare: { 
     html: '<%= yeoman.app %>/index.html', 
     options: { 
     dest: '<%= yeoman.dist %>', 
     flow: { 
      html: { 
      steps: { 
       js: ['concat', 'uglifyjs'], 
       css: ['cssmin'] 
      }, 
      post: {} 
      } 
     } 
     } 
    }, 

    // Performs rewrites based on filerev and the useminPrepare configuration 
    usemin: { 
     html: ['<%= yeoman.dist %>/{,*/}*.html'], 
     css: ['<%= yeoman.dist %>/styles/{,*/}*.css'], 
     options: { 
     assetsDirs: ['<%= yeoman.dist %>','<%= yeoman.dist %>/images'] 
     } 
    }, 

    // The following *-min tasks will produce minified files in the dist folder 
    // By default, your `index.html`'s <!-- Usemin block --> will take care of 
    // minification. These next options are pre-configured if you do not wish 
    // to use the Usemin blocks. 
    // cssmin: { 
    // dist: { 
    //  files: { 
    //  '<%= yeoman.dist %>/styles/main.css': [ 
    //   '.tmp/styles/{,*/}*.css' 
    //  ] 
    //  } 
    // } 
    // }, 
    // uglify: { 
    // dist: { 
    //  files: { 
    //  '<%= yeoman.dist %>/scripts/scripts.js': [ 
    //   '<%= yeoman.dist %>/scripts/scripts.js' 
    //  ] 
    //  } 
    // } 
    // }, 
    // concat: { 
    // dist: {} 
    // }, 

    imagemin: { 
     dist: { 
     files: [{ 
      expand: true, 
      cwd: '<%= yeoman.app %>/images', 
      src: '{,*/}*.{png,jpg,jpeg,gif}', 
      dest: '<%= yeoman.dist %>/images' 
     }], 
     options: { 
      cache: false 
     } 
     } 
    }, 

    svgmin: { 
     dist: { 
     files: [{ 
      expand: true, 
      cwd: '<%= yeoman.app %>/images', 
      src: '{,*/}*.svg', 
      dest: '<%= yeoman.dist %>/images' 
     }] 
     } 
    }, 

    htmlmin: { 
     dist: { 
     options: { 
      collapseWhitespace: true, 
      conservativeCollapse: true, 
      collapseBooleanAttributes: true, 
      removeCommentsFromCDATA: true, 
      removeOptionalTags: true 
     }, 
     files: [{ 
      expand: true, 
      cwd: '<%= yeoman.dist %>', 
      src: ['*.html', 'views/{,*/}*.html', 'views/{,*/*/}*.html'], 
      dest: '<%= yeoman.dist %>' 
     }] 
     } 
    }, 

    // ngmin tries to make the code safe for minification automatically by 
    // using the Angular long form for dependency injection. It doesn't work on 
    // things like resolve or inject so those have to be done manually. 
    ngmin: { 
     dist: { 
     files: [{ 
      expand: true, 
      cwd: '.tmp/concat/scripts', 
      src: '*.js', 
      dest: '.tmp/concat/scripts' 
     }] 
     } 
    }, 

    // Replace Google CDN references 
    cdnify: { 
     dist: { 
     html: ['<%= yeoman.dist %>/*.html'] 
     } 
    }, 

    // Copies remaining files to places other tasks can use 
    copy: { 
     dist: { 
     files: [{ 
      expand: true, 
      dot: true, 
      cwd: '<%= yeoman.app %>', 
      dest: '<%= yeoman.dist %>', 
      src: [ 
      '*.{ico,png,txt}', 
      '.htaccess', 
      '*.html', 
      'views/{,*/}*.html', 
      'views/{,*/*/}*.html', 
      'images/{,*/}*.{webp}', 
      'fonts/{,*/}*' 
      ] 
     }, { 
      expand: true, 
      cwd: '.tmp/images', 
      dest: '<%= yeoman.dist %>/images', 
      src: ['generated/*'] 
     }, { 
      expand: true, 
      cwd: 'bower_components/bootstrap/dist', 
      src: 'fonts/*', 
      dest: '<%= yeoman.dist %>' 
     }, { 
      expand: true, 
      cwd: 'bower_components/font-awesome', 
      src: 'fonts/*', 
      dest: '<%= yeoman.dist %>' 
     }] 
     }, 
     styles: { 
     expand: true, 
     cwd: '<%= yeoman.app %>/styles', 
     dest: '.tmp/styles/', 
     src: '{,*/}*.css' 
     } 
    }, 

    // Run some tasks in parallel to speed up the build process 
    concurrent: { 
     server: [ 
     'copy:styles' 
     ], 
     test: [ 
     'copy:styles' 
     ], 
     dist: [ 
     'copy:styles', 
     'imagemin', 
     'svgmin' 
     ] 
    }, 

    // Test settings 
    karma: { 
     unit: { 
     configFile: 'test/karma.conf.js', 
     singleRun: true 
     } 
    } 
    }); 


    grunt.registerTask('serve', 'Compile then start a connect web server', function (target) { 
    if (target === 'dist') { 
     return grunt.task.run(['build', 'connect:dist:keepalive']); 
    } 

    grunt.task.run([ 
     'clean:server', 
     'wiredep', 
     'concurrent:server', 
     'autoprefixer', 
     'connect:livereload', 
     'watch' 
    ]); 
    }); 

    grunt.registerTask('server', 'DEPRECATED TASK. Use the "serve" task instead', function (target) { 
    grunt.log.warn('The `server` task has been deprecated. Use `grunt serve` to start a server.'); 
    grunt.task.run(['serve:' + target]); 
    }); 

    grunt.registerTask('test', [ 
    'clean:server', 
    'concurrent:test', 
    'autoprefixer', 
    'connect:test', 
    'karma' 
    ]); 

    grunt.registerTask('build', [ 
    'clean:dist', 
    'wiredep', 
    'useminPrepare', 
    'concurrent:dist', 
    'autoprefixer', 
    'concat', 
    'ngmin', 
    'copy:dist', 
    'cdnify', 
    'cssmin', 
    'uglify', 
    'filerev', 
    'usemin', 
    'htmlmin' 
    ]); 

    grunt.registerTask('default', [ 
    'newer:jshint', 
    'test', 
    'build' 
    ]); 
}; 

我karma.config.js有這樣的:

// list of files/patterns to load in the browser 
    files: [ 
     'bower_components/angular/angular.js', 
     'bower_components/angular-mocks/angular-mocks.js', 
     'bower_components/angular-animate/angular-animate.js', 
     'bower_components/angular-cookies/angular-cookies.js', 
     'bower_components/angular-resource/angular-resource.js', 
     'bower_components/angular-route/angular-route.js', 
     'bower_components/angular-sanitize/angular-sanitize.js', 
     'bower_components/angular-touch/angular-touch.js', 
     'bower_components/angular-ui-router/release/angular-ui-router.js', 
     'bower_components/angular-bootstrap/ui-bootstrap.js', 
     'bower_components/angular-bootstrap/ui-bootstrap-tpls.js', 
     'bower_components/angular-bootstrap-lightbox/dist/angular-bootstrap-lightbox.js', 
     'bower_components/angular-loading-bar/build/loading-bar.js', 
     'bower_components/angular-cached-resource/angular-cached-resource.js', 
     'bower_components/angular-ui-slider/src/slider.js', 
     'bower_components/angular-local-storage/dist/angular-local-storage.js', 
     'bower_components/angular-bootstrap-lightbox/dist/angular-bootstrap-lightbox.js', 
     'bower_components/ng-file-upload/angular-file-upload.js', 
     'bower_components/ng-tags-input/ng-tags-input.min.js', 
     'bower_components/angular-ui-tinymce/src/tinymce.js', 
     'app/scripts/**/*.js', 
     'test/mock/**/*.js', 
     'test/spec/**/*.js' 
    ], 

我的目錄結構是這樣的:

app/ 
    scripts/ 
     app.js 
    styles/ 
    views/ 
bower_components/ 
dist/ 
node_modules/ 
test/ 
bower.json 
Gruntfile.js 
package.json 

在我app.js你會發現這一點:

angular 
     .module('webshopAdminApp', [ 
      'ngAnimate', 
      'ngCookies', 
      'ngResource', 
      'ngRoute', 
      'ngSanitize', 
      'ngTouch', 
      'ui.router', 
      'ui.bootstrap', 
      'ngCachedResource', 
      'ui.slider', 
      'LocalStorageModule', 
      'bootstrapLightbox', 
      'angularFileUpload', 
      'ngTagsInput', 
      'ui.tinymce' 
     ]) 

我敢肯定,所有我使用加載的模塊。我檢查了供應商。{hash} .js文件和所有模塊都在那裏。當我完全刪除ui.slider模塊時,我得到了與其他模塊相同的錯誤,所以它不是模塊或者它的實現。

我已經在互聯網上搜索,只發現了一個類似的問題here,其中問題與uglify轉換注射。我試過所有的解決方案,但都沒有成功。我不想禁用uglify。我不知道問題是什麼,我已經用完全相同的規格和配置構建了另一個AngularJS應用程序,沒有任何問題。

有沒有人有任何想法如何解決這個問題?

+0

'serve'和直'grunt'之間的主要區別是測試。您的錯誤消息出現在哪裏,因爲您的測試模塊中可能存在配置問題 –

+0

「grunt serve」不會爲所有模塊創建供應商文件,它還會創建對bower_components目錄中實際文件的引用。所以當使用'grunt serve'時,沒有問題,因爲模塊很好。 – gidomanders

回答

1

我們已確定所有模塊依賴關係配置是正確的或似乎是。

爲了更清楚地確定哪些模塊發生故障,需要一個排除過程。

如果您發現從modulerr中爲缺少的模塊或提供程序彈出錯誤,請逐個逐個刪除模塊依賴項並重新構建,以確定哪個依賴項將引發錯誤。

然後,刪除依賴關係並找到可能滿足它的另一個。

如果你的問題是與LocalStorageModule,我建議用ngStorage去,看是否能解決你的問題,here's the link

+0

我已經過了我的問題 – gidomanders

+0

再次更新我的問題,你會發現我的karma.config.js裏面有ui-slider。 – gidomanders

+1

真正解決這個問題的唯一方法是查看你錯過了哪些依賴關係。如果即使在刪除模塊時仍然會出錯,您需要繼續刪除出現錯誤的模塊,並找到不滿足的根相關性。您應該**不需要編輯Gruntfile.js生成器應該爲您處理它。 – TommyMac

1

工作約4小時後,我終於發現了問題!我正在使用LocalStorageModule,通常應該可以工作,但在咕嚕聲失敗後。也許是一個錯誤的配置,但我用$ cookie替換它,現在網站運行沒有錯誤。

我用LocalStorageModule最主要的原因是因爲它非常容易使用自動保存過濾變量localStorageService.bind($scope, 'filters', {}, 'filters');所以刷新頁面或導航到詳細信息頁面和回來時,他們不會丟失。

我要在他們的GitHub上打開一個問題,並嘗試找到正確的配置,使其再次工作。謝謝您的幫助!