【问题标题】:Grunt Watch LiveReload on site on serverGrunt Watch LiveReload 在服务器上的现场
【发布时间】:2017-10-04 02:57:36
【问题描述】:

我正在服务器(非本地)上开发 WordPress 网站。每当我修改 sass 文件时,我都想在浏览器中刷新页面。我列出了一些繁重的任务,但现在我只想让它在任何 sass 修改时刷新。现在,它会在文件被修改时捕获,但不会刷新页面。

Gruntfile:

module.exports = function(grunt) {

	// Project configuration.
	grunt.initConfig({
		pkg: grunt.file.readJSON('package.json'),
		
		watch: {
			scripts: {
				options: { livereload: true },
				files: ['**/*.scss'],
				//tasks: ['criticalcss:front', 'criticalcss:page', 'cssmin', 'postcss'],
			}
		},
		
		 postcss: {
			options: {
				processors: [
					require('autoprefixer')({browsers: 'last 6 versions'}), // add vendor prefixes
					//require('cssnano')() // minify the result
				]
			},
			dist: {
				src: 'style.css',
				dest: 'style.css'
			}
		},
	
		criticalcss: {
			front : {
				options: {
					url: "https://grandeurflooring.ca/grand_dev/",
					minify: true,
					width: 1500,
					height: 900,
					outputfile: "critical_css/critical-front.css",
					filename: "style.css",
					buffer: 800*1024,
					ignoreConsole: true
				}
			},
			page : {
				options: {
					url: "https://grandeurflooring.ca/grand_dev/sample-page/",
					minify: true,
					width: 1500,
					height: 900,
					outputfile: "critical_css/critical-page.css",
					filename: "style.css",
					buffer: 800*1024,
					ignoreConsole: true
				}
			}
		},
		
		cssmin: {
			target: {
				files: [{
				  expand: true,
				  cwd: 'critical_css',
				  src: ['*.css', '!*.min.css'],
				  dest: 'critical_css',
				  ext: '.min.css'
				}]
			}
		}

	});

	// Load the plugin that provides the "critical" task.
	grunt.loadNpmTasks('grunt-criticalcss');
	// Load the plugin that provides the "cssmin" task.
	grunt.loadNpmTasks('grunt-contrib-cssmin');
	// Load the plugin that provides the "watch" task.
	grunt.loadNpmTasks('grunt-contrib-watch');
	// Load the plugin that provides the "PostCSS" task.
	grunt.loadNpmTasks('grunt-postcss');

	// Critical task.
	grunt.registerTask('critical', ['criticalcss:front']);

};

在footer.php中,在wp_footer()之前,我放了脚本:

<script src="http://localhost:35729/livereload.js"></script>

【问题讨论】:

  • 如果你想将消息推送到浏览器并让它重新加载页面,你需要使用 websocket 或服务器发送的事件。或者您可以使用 AJAX 进行轮询。但我建议不要添加强制页面重新加载的代码。当您的网站上线时,您无论如何都希望删除代码。
  • 不过,我正在服务器上进行开发,所以我希望启动并运行它,即使这意味着稍后删除代码。如果您碰巧知道如何执行此操作并且它有效,您介意发布答案吗?

标签: gruntjs livereload


【解决方案1】:

您可以将 Grunt 配置为在您的 dist 目录中查看编译后的 css 文件,每次重新编译 Sass 时都会更新该文件。

这是我的watch 配置,可以实现您想要的:

watch: {
  options: {
    livereload: true,
  },
  html: {
    files: ['index.html'],
  },
  js: {
    files: ['js/**/*.js'],
    tasks: ['jshint'],
  },
  sass: {
    options: {
      livereload: false
    },
    files: ['css/scss/**/*.scss'],
    tasks: ['sass'],
  },
  css: {
    files: ['dist/css/master.css'],
    tasks: []
  }
}

您可能还需要将 spawn: false 更改为 spawn: true,具体取决于您的设置。

编辑:此外,您可以使用grunt-contrib-watch 插件,它允许您:

在添加、更改或删除监视的文件模式时运行预定义任务

此插件包含许多附加选项,用于实时重新加载、观看等,您可能会发现这些选项很有用。

【讨论】:

  • 在服务器上开发时这对您有用吗?另外,我不想问,但是您知道在服务器开发中是否有类似的 gulp 插件允许这样做?我刚开始接触 gulp,到目前为止我很喜欢它。
  • 是的,这对我在服务器上开发时有用。我已经更新了我的答案以包含您可能更喜欢使用的插件。
猜你喜欢
  • 2015-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-18
  • 2013-11-18
  • 1970-01-01
  • 2023-03-29
  • 2017-06-12
相关资源
最近更新 更多