我正在尝试在我的 WordPress 自定义主题中运行 grunt uncss。下面是我的配置文件gruntfile.js
。当我运行grunt
命令时,我得到错误
Running "uncss:dist" (uncss) task
Error: Could not load script: "file:///D:/www/website-wp/wp-content/themes/theme2021/%3C?=get_template_directory_uri()?%3E/assets/js/require.js"
上面,我使用get_template_directory_uri()
了 wordpress 函数来获取当前主题的路径,但是 grunt 无法检测到这个 PHP 函数是动态的。那么,我怎样才能使这个 grunt 配置gruntfile.js
来制作 require.js 文件以查看正确的路径。
谢谢你。
更新
下面是我在主题目录中的 gruntfile.js。
module.exports = function(grunt) {
grunt.initConfig({
exec: {
get_grunt_sitemap: {
command: 'curl --silent --output sitemap.json http://localhost/website-wp/?show_sitemap'
}
},
uncss: {
dist: {
options: {
ignore: [/\w\.in/,
".fade",
".collapse",
".collapsing",
/(#|\.)navbar(\-[a-zA-Z]+)?/,
/(#|\.)dropdown(\-[a-zA-Z]+)?/,
/(#|\.)(open)/,
".modal",
".modal.fade.in",
".modal-dialog",
".modal-document",
".modal-scrollbar-measure",
".modal-backdrop.fade",
".modal-backdrop.in",
".modal.fade.modal-dialog",
".modal.in.modal-dialog",
".modal-open",
".in",
".modal-backdrop",
/expanded/,/js/,/wp-/,/align/,/admin-bar/],
stylesheets : ['/dist/bootstrap/css/bootstrap.min.css', 'style.css','/css/minified/main.css','/css/minified/media.css'],
ignoreSheets : [/fonts.googleapis/,/js.hsforms.net/,/require.js/],
// urls : [], //Overwritten in load_sitemap_and_uncss task
},
// files: {
// 'assets/compiled/style.css': ['http://localhost/website-wp/']
// },
files: {
'style.clean.css': ['**/*.php']
}
}
}
});
grunt.loadNpmTasks('grunt-exec');
grunt.loadNpmTasks('grunt-uncss');
grunt.registerTask('load_sitemap_json', function() {
var sitemap_urls = grunt.file.readJSON('./sitemap.json');
grunt.config.set('uncss.dist.options.urls', sitemap_urls);
});
grunt.registerTask('deploy',
['exec:get_grunt_sitemap','load_sitemap_json','uncss:dist']);
grunt.registerTask('default', 'uncss');
}
另外,我get_template_directory_uri()
在主题目录中的 footer.php 文件中使用。