grunt-sass和grunt-contrib-watch似乎无法按预期一起工作

时间:2016-07-06 11:12:39

标签: javascript sass gruntjs

我使用grunt-sass和grunt-watch来编译和缩小我的代码

Sreekanth-2:19:02:16 Sreekanth$ cordova build
Running command: "/Users/apple/Desktop/Sreekanth/Togetherly Daily Work/19:02:16/platforms/android/cordova/build" 
Buildfile: /Users/apple/Desktop/Sreekanth/Togetherly Daily Work/19:02:16/platforms/android/build.xml

BUILD FAILED
/Users/apple/Desktop/Sreekanth/Togetherly Daily Work/19:/Users/apple/Desktop/Sreekanth/Togetherly Daily Work/19:02:16/platforms/android/build.xml:7: The following error occurred while executing this line:
java.io.FileNotFoundException: /Users/apple/Desktop/Sreekanth/Togetherly Daily Work/19:/usr/local/Cellar/ant/1.9.7/libexec/lib/ant.jar (No such file or directory)
    at java.io.RandomAccessFile.open0(Native Method)
    at java.io.RandomAccessFile.open(RandomAccessFile.java:316)
    at java.io.RandomAccessFile.<init>(RandomAccessFile.java:243)
    at org.apache.tools.zip.ZipFile.<init>(ZipFile.java:212)
    at org.apache.tools.zip.ZipFile.<init>(ZipFile.java:177)
    at org.apache.tools.ant.helper.ProjectHelper2.parse(ProjectHelper2.java:256)
    at org.apache.tools.ant.helper.ProjectHelper2.parseUnknownElement(ProjectHelper2.java:131)
    at org.apache.tools.ant.helper.ProjectHelper2.parseAntlibDescriptor(ProjectHelper2.java:111)
    at org.apache.tools.ant.taskdefs.Antlib.createAntlib(Antlib.java:94)
    at org.apache.tools.ant.taskdefs.Definer.loadAntlib(Definer.java:425)
    at org.apache.tools.ant.taskdefs.Definer.execute(Definer.java:277)
    at org.apache.tools.ant.ComponentHelper.checkNamespace(ComponentHelper.java:874)
    at org.apache.tools.ant.ComponentHelper.getDefinition(ComponentHelper.java:307)
    at org.apache.tools.ant.ComponentHelper.createComponent(ComponentHelper.java:284)
    at org.apache.tools.ant.ComponentHelper.createComponent(ComponentHelper.java:263)
    at org.apache.tools.ant.UnknownElement.makeObject(UnknownElement.java:430)
    at org.apache.tools.ant.UnknownElement.maybeConfigure(UnknownElement.java:164)
    at org.apache.tools.ant.Task.perform(Task.java:347)
    at org.apache.tools.ant.Target.execute(Target.java:435)
    at org.apache.tools.ant.helper.ProjectHelper2.parse(ProjectHelper2.java:180)
    at org.apache.tools.ant.ProjectHelper.configureProject(ProjectHelper.java:93)
    at org.apache.tools.ant.Main.runBuild(Main.java:829)
    at org.apache.tools.ant.Main.startAnt(Main.java:236)
    at org.apache.tools.ant.launch.Launcher.run(Launcher.java:285)
    at org.apache.tools.ant.launch.Launcher.main(Launcher.java:112)

Total time: 0 seconds

/Users/apple/Desktop/Sreekanth/Togetherly Daily Work/19:02:16/platforms/android/cordova/node_modules/q/q.js:126
                    throw e;
                          ^
Error code 1 for command: ant with args: debug,-f,/Users/apple/Desktop/Sreekanth/Togetherly Daily Work/19:02:16/platforms/android/build.xml,-Dout.dir=ant-build,-Dgen.absolute.dir=ant-gen
Error: /Users/apple/Desktop/Sreekanth/Togetherly Daily Work/19:02:16/platforms/android/cordova/build: Command failed with exit code 1
    at ChildProcess.whenDone (/usr/local/lib/node_modules/cordova/node_modules/cordova-lib/src/cordova/superspawn.js:135:23)
    at ChildProcess.emit (events.js:110:17)
    at maybeClose (child_process.js:1015:16)
    at Process.ChildProcess._handle.onexit (child_process.js:1087:5)
Sreekanth-2:19:02:16 Sreekanth$ 

问题:

当我修改一个scss文件时,grunt.config('watch', { scripts: { files: ['<%= project.src.js.directory %>/*.js'], tasks: ['concat:scripts', 'uglify:compile'] }, styles: { files: [ '<%= project.src.css.directory %>/**/*.scss' ], tasks: ['sass:compile', 'cssmin:minify'] } }); 启动,sass:compile检测到所有javascript文件的更改,并且永远不会运行grunt-contrib-watch

但是,运行我的任务(没有手表)效果很好。

我认为cssmin:minify是原因(使用spawn为false,就像https://github.com/gruntjs/grunt-contrib-watch/pull/509中指示的那样解决问题)。

是否有人遇到过同样的问题?

切换到grunt-contrib-watch(使用ruby-sass)可以解决问题。

我的Gruntfile.js:

grunt-contrib-sass

有关信息,我的package.json看起来像这样:

'use strict';

module.exports = function(grunt) {

  /*
   * Time grunt tasks execution time.
   */
  require('time-grunt')(grunt);

  /*
   * Autoload grunt npm load tasks.
   */
  require('load-grunt-tasks')(grunt);

  /*
   * ==========================================================================
   *  PROJECT
   * ==========================================================================
   */

  var project = {
    'bower': grunt.file.readJSON('./.bowerrc') || { 'directory': './bower_components/' },
    'src': {
      'css' : {
        'directory': 'assets/css',
        'files': [
          '<%= project.bower.directory %>/jquery-ui/themes/smoothness/jquery-ui.css',
          '<%= project.bower.directory %>/jquery-ui/themes/smoothness/jquery.ui.theme.css',
          '<%= project.bower.directory %>/ionicons/css/ionicons.min.css',
        ]
      },
      'js': {
        'directory': 'assets/js',
        'files': [
          '<%= project.bower.directory %>/jsgettext/lib/Gettext.js',
          '<%= project.bower.directory %>/jquery-ui/ui/minified/jquery-ui.min.js',
          '<%= project.bower.directory %>/matchHeight/jquery.matchHeight-min.js',
          '<%= project.bower.directory %>/twig.js/twig.min.js',
          '<%= project.bower.directory %>/slick-carousel/slick/slick.min.js',
          '<%= project.bower.directory %>/jquery-validation/dist/jquery.validate.min.js',
          '<%= project.bower.directory %>/dropzone/dist/min/dropzone.min.js',
          '<%= project.bower.directory %>/bootstrap-sass/assets/javascripts/bootstrap.min.js',
          '<%= project.src.js.directory %>/main.js',
          '<%= project.src.js.directory %>/messenger.js',
          '<%= project.src.js.directory %>/item.uploader.js',
        ]
      }
    },
    'dest': {
      'css': { 'directory': 'assets/css/dist', },
      'js': { 'directory': 'assets/js/dist', }
    }
  };

  /*
   * ==========================================================================
   *  INIT
   * ==========================================================================
   */

  grunt.initConfig({

    // Load package.json
    pkg: grunt.file.readJSON('package.json'),

  });

  /*
   * Project configuration.
   *
   * It is set in an early setter to avoid array flattening.
   */
  grunt.config.set('project', project);

  /*
   * ==========================================================================
   *  CONFIGURATION
   * ==========================================================================
   */

  /*
   * sass:compile
   * Compile SASS files to dist directory.
   */
  grunt.config('sass', {
    compile: {
      options: {
        includePaths: [
          '<%= project.bower.directory %>'
        ],
        sourceMap: true,
        style: 'compressed',
      },
      files: {
        "<%= project.dest.css.directory %>/theme1.css": "<%= project.src.css.directory %>/theme1.scss",
        "<%= project.dest.css.directory %>/theme2.css": "<%= project.src.css.directory %>/theme2.scss",
      }
    }
  });

  /*
   * cssmin:minify
   *
   * Concat and minify CSS files.
   *
   * It also rebase url() correctly like relativePath does in LESS.
   */
  grunt.config('cssmin', {
    minify: {
      options: {
        rebase: true
      },
      files: {
        '<%= project.dest.css.directory %>/theme1.min.css': grunt.config('project.src.css.files').concat(['<%= project.dest.css.directory %>/theme1.css']),
        '<%= project.dest.css.directory %>/theme2.min.css': grunt.config('project.src.css.files').concat(['<%= project.dest.css.directory %>/theme2.css'])
      }
    }
  });

  /*
   * concat:scripts
   *
   * Concat Javascript files to a single one (non-minified).
   */
  grunt.config('concat', {
    scripts: {
      options: {
        separator: ';'
      },
      src: grunt.config('project.src.js.files'),
      dest: '<%= project.dest.js.directory %>/main.js'
    }
  });

  /*
   * uglify
   *
   *
   */
  grunt.config('uglify', {
    compile: {
      options: {
        compress: true,
        mangle: true,
        preserveComments: 'some',
        drop_console: true,
      },
      src: '<%= concat.scripts.dest %>',
      dest: '<%= project.dest.js.directory %>/main.min.js'
    }
  });

  /*
   * watch
   *
   * Watch
   */
  grunt.config('watch', {
    options: {
      // interrupt: true, // Cancel build if another changes is detected.
      // reload: true // Reload Grunt if Gruntfiles.js is changed.
    },
    scripts: {
      files: ['<%= project.src.js.directory %>/*.js', /*'Gruntfile.js'*/],
      tasks: ['build:scripts']
    },
    styles: {
      files: [
        '<%= project.src.css.directory %>/**/*.scss',
        '<%= project.bower.directory %>/cssr/src/**/*.scss'
      ],
      tasks: ['build:styles']
    }
  });

  /*
   * ==========================================================================
   *  TASKS
   * ==========================================================================
   */

  grunt.registerTask(
    'build:styles',
    'Compile SASS and minify CSS to dist directory',
    ['sass:compile', 'cssmin:minify']
  );

  grunt.registerTask(
    'build:scripts',
    'Concat scripts files and uglify to dist directory',
    ['concat:scripts', 'uglify:compile']
  );

  grunt.registerTask(
    'build',
    'Build scripts and styles (default task)',
    ['build:styles', 'build:scripts']
  );
  grunt.registerTask(
    'dev',
    'Deprecated, use `grunt build` instead',
    ['build']
  ); // @deprecated

  grunt.registerTask('default', ['watch']);
};

0 个答案:

没有答案