【发布时间】: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