使用Grunt服务在本地主机上显示网站

Using Grunt Serve to show website on localhost?

本文关键字:主机 显示 网站 Grunt 服务 使用      更新时间:2023-09-26

我以前构建过Gruntfiles,但主要是为了编译Less和Jade,所以这一步有点超出了我的舒适区,我正在努力弄清楚该怎么做。

我想使用Gruntfile来:

  1. 编译Jade到html
  2. 编译成css
  3. localhost:9000建立并展示网站
  4. 在保存文件时刷新localhost:9000(我假设这使用grunt watch ?)

基本上,我想保持它轻松,简单,所以一旦我学会了我可以用它来教别人我知道。:)

这是我的Gruntfile到目前为止的样子。我有grunt-serve在那里,但没有加载到页面,当我运行它,所以我真的很困惑。谢谢你的帮助!

module.exports = function(grunt) {
  grunt.loadNpmTasks('grunt-contrib-jade');
  grunt.loadNpmTasks('grunt-contrib-less');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.initConfig({
    jade: {
      compile: {
        options: {
          client: false,
          pretty: true
        },
        files: [ {
          cwd: "assets/views",
          src: "**/*.jade",
          dest: "public",
          expand: true,
          ext: ".html"
        } ]
      }
    },    
    less: {
      development: {
        options: {
          compress: true,
          yuicompress: true,
          optimization: 2
        },
        files: {
          // target.css file: source.less file
          'public/css/main.css': 'assets/less/main.less',
        }
      }
    },
    watch: {
      styles: {
        files: [
          'less/main.less',
        ],
        tasks: ['less'],
        options: {
          nospawn: true
        }
      }
    },
    serve: {
      options: {
        port: 9000
      }
    }
  });
  grunt.registerTask('default', ['jade','less']);
  grunt.loadNpmTasks('grunt-serve');
};

我认为您的构建任务正在运行玉和少,但不服务。而不是

grunt.registerTask('default', ['jade','less']);

grunt.registerTask('default', ['jade','less','serve']);