首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >grunt监视和连接

grunt监视和连接
EN

Stack Overflow用户
提问于 2014-02-18 02:20:22
回答 3查看 18.5K关注 0票数 25

我还是个新手,想把它用在Jekyll和一些编译较少的地方。

我现在的问题是,我已经有了功能齐全的LESS-comipiling与实时重载和观看任务,并可以通过grunt构建我的jekyll站点,但我如何同时运行jekyll serve或grunt-connect和grunt watch?我想要一个grunt任务,提供我的较少的文件等观看,建立jekyll网站,然后运行一个小型web服务器与grunt-connect或其他什么。

到目前为止我的Gruntfile.js:

代码语言:javascript
复制
'use strict';
module.exports = function (grunt) {

    grunt.initConfig({
        jshint: {
            options: {
                jshintrc: '.jshintrc'
            },
            all: [
                'Gruntfile.js',
                'js/*.js',
                '!js/scripts.min.js'
            ]
        },
        less: {
            dist: {
                files: {
                    'css/styles.min.css': [
                        'less/app.less'
                    ]
                },
                options: {
                    compress: true,
                    // LESS source map
                    // To enable, set sourceMap to true and update sourceMapRootpath based on your install
                    sourceMap: false,
                    sourceMapFilename: 'css/styles.min.css.map',
                    sourceMapRootpath: '/'
                }
            },
            dev: {
                files: {
                    'css/styles.min.css': [
                        'less/app.less'
                    ]
                },
                options: {
                    compress: false,
                    // LESS source map
                    // To enable, set sourceMap to true and update sourceMapRootpath based on your install
                    sourceMap: true,
                    sourceMapFilename: 'css/styles.min.css.map',
                    sourceMapRootpath: '/'
                }
            }
        },
        uglify: {
            dist: {
                files: {
                    'js/scripts.min.js': [
                        'vendor/bootstrap/js/transition.js',
                        'vendor/bootstrap/js/alert.js',
                        'vendor/bootstrap/js/button.js',
                        'vendor/bootstrap/js/carousel.js',
                        'vendor/bootstrap/js/collapse.js',
                        'vendor/bootstrap/js/dropdown.js',
                        'vendor/bootstrap/js/modal.js',
                        'vendor/bootstrap/js/tooltip.js',
                        'vendor/bootstrap/js/popover.js',
                        'vendor/bootstrap/js/scrollspy.js',
                        'vendor/bootstrap/js/tab.js',
                        'vendor/bootstrap/js/affix.js',
                        'vendor/*.js',
                        'js/_*.js'
                    ]
                },
                options: {
                    // JS source map: to enable, uncomment the lines below and update sourceMappingURL based on your install
                    // sourceMap: 'assets/js/scripts.min.js.map',
                    // sourceMappingURL: '/app/themes/roots/assets/js/scripts.min.js.map'
                }
            }
        },
        watch: {
            less: {
                files: [
                    'less/*.less',
                    'less/bootstrap/*.less'
                ],
                tasks: ['less:dev']
            },
            js: {
                files: [
                    '<%= jshint.all %>'
                ],
                tasks: ['jshint', 'uglify']
            },
            livereload: {
                // Browser live reloading
                // https://github.com/gruntjs/grunt-contrib-watch#live-reloading
                options: {
                    livereload: true
                },
                files: [
                    '_site/*'
                ]
            }
        },
        clean: {
            dist: [
                'css/styles.min.css',
                'css/styles.min.css.map',
                'js/scripts.min.js',
                '_site/*'
            ]
        },
        jekyll: {                             // Task
            options: {                          // Universal options
                bundleExec: true,
                src : '<%= app %>'
            },
            dist: {                             // Target
                options: {                        // Target options
                    dest: '<%= dist %>',
                    config: '_config.yml'
                }
            },
            serve: {                            // Another target
                options: {
                    serve: true,
                    drafts: true
                }
            }
        },
        connect: {
            server: {
                options: {
                    keepalive: true
                }
            }
        }
    });

    // Load tasks
    grunt.loadNpmTasks('grunt-contrib-clean');
    grunt.loadNpmTasks('grunt-contrib-jshint');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-less');
    grunt.loadNpmTasks('grunt-jekyll');
    grunt.loadNpmTasks('grunt-contrib-connect');

    // Register tasks
    grunt.registerTask('default', [
        'clean',
        'less:dist',
        'uglify',
        'jekyll:dist'
    ]);
    grunt.registerTask('dev', [
        'watch'
    ]);

};
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-02-18 12:34:58

您需要使用"base“选项告诉connect在配置中提供哪个目录,在本例中它将是静态_site目录。您也可以随心所欲地更改端口,但在我的示例中,最终导航到localhost:9009

代码语言:javascript
复制
connect: {
  server: {
    options: {
      livereload: true,
      base: '_site/',
      port: 9009
    }
  }
}

您还需要在更改html模板时添加监视任务。像这样的东西会起作用的。

代码语言:javascript
复制
watch: {
  html: {
    files: ['**/*.html', '!_site/**/*.html'],
    tasks: ['jekyll:dist']
  }
}

然后按照华莱士的建议创建一个“服务”任务。

代码语言:javascript
复制
// Start web server
grunt.registerTask('serve', [
'jekyll:dist',
'connect:server',
'watch'
]);

最后,在命令行中运行"grunt serve“,并使用您指定的端口导航到localhost。

@评论的jiggy

的关键更改是不将keepalive设置为true。Keepalive将阻止所有后续任务运行。只要connect后面跟着watch,服务器就不会终止。

票数 37
EN

Stack Overflow用户

发布于 2015-09-06 23:23:45

我花了两天的时间拼命地尝试了我能在互联网上找到的所有gruntfile配置。从来没成功过。然后我找到了这个https://stackoverflow.com/a/24765175/1541141。使用grunt-contrib-connect,而不是grunt-connectgrunt-connect正在阻止...希望能有所帮助。

票数 3
EN

Stack Overflow用户

发布于 2014-02-18 04:50:44

我认为你的解决方案的核心是创建一个新任务或编辑一个现有任务,如下所示:

代码语言:javascript
复制
// Start web server
grunt.registerTask('serve', [
    'jekyll:dist',
    'connect:livereload',
    'watch'
]);

你可以用$ grunt serve来运行...which。lessjshintuglifyconnect已经包含在watch下。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21836478

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档