用Grunt.js连接php文件和Javascript

时间:2016-12-28 15:58:26

标签: gruntjs grunt-contrib-concat

我在尝试让我的Grunt.js文件工作时遇到问题。目前我已将它设置为仅连接所有我的php函数文件,并决定因为我的很多工作项目使用Bootstrap,只连接我将使用的javascript文件。但是,当我设置它时,希望我确实正确设置它,我的grunt执行不创建/编辑javascript最终目标文件,也不创建php函数文件。我不确定我做错了什么,但这里是以下代码:

module.exports = function (grunt) {

    // Project configuration.
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        concat: {
            js: {
                options: {
                    separator: 'rn'
                },
                dist: {
                    src: [
                        // Comment or uncomment any Bootstrap JS files
                        // you will not be using
                        'wp-content/themes/base_theme/assets/js/required/bootstrap/plugins/alert.js',
                        'wp-content/themes/base_theme/assets/js/required/bootstrap/plugins/button.js',
                        'wp-content/themes/base_theme/assets/js/required/bootstrap/plugins/carousel.js',
                        'wp-content/themes/base_theme/assets/js/required/bootstrap/plugins/collapse.js',
                        'wp-content/themes/base_theme/assets/js/required/bootstrap/plugins/dropdown.js',
                        'wp-content/themes/base_theme/assets/js/required/bootstrap/plugins/modal.js',
                        'wp-content/themes/base_theme/assets/js/required/bootstrap/plugins/phantom.js',
                        'wp-content/themes/base_theme/assets/js/required/bootstrap/plugins/popover.js',
                        'wp-content/themes/base_theme/assets/js/required/bootstrap/plugins/qunit.js',
                        'wp-content/themes/base_theme/assets/js/required/bootstrap/plugins/scrollspy.js',
                        'wp-content/themes/base_theme/assets/js/required/bootstrap/plugins/tether.js',
                        'wp-content/themes/base_theme/assets/js/required/bootstrap/plugins/tab.js',
                        'wp-content/themes/base_theme/assets/js/required/bootstrap/plugins/tooltip.js'
                    ],
                    dest: 'wp-content/themes/base_theme/assets/js/required/bootstrap/bootstrap.min.js'
                }
            },
            php: {
                options: {
                    stripBanners: true,
                    separator: '\n?>\n',
                },
                dist: {
                    src: [
                        // To create a new location, follow pattern below
                        // Comment out what you don't need a re-concat
                        'wp-content/themes/base_theme/assets/functions/basic.php',
                        'wp-content/themes/base_theme/inc/custom-header.php',
                        'wp-content/themes/base_theme/inc/customizer.php',
                        'wp-content/themes/base_theme/inc/extras.php',
                        'wp-content/themes/base_theme/inc/jetpack.php',
                        'wp-content/themes/base_theme/inc/template-tags.php',
                        'wp-content/themes/base_theme/inc/wpcom.php',
                        'wp-content/themes/base_theme/assets/functions/bootstrap-nav.php',
                        'wp-content/themes/base_theme/assets/functions/enqueue.php'
                        'wp-content/themes/base_themey/assets/functions/custom-post-type.php',
                        'wp-content/themes/base_theme/assets/functions/internal-template.php',
                        'wp-content/themes/base_theme/assets/functions/custom-taxonomy.php',
                        'wp-content/themes/base_theme/assets/functions/acf.php',
                        'wp-content/themes/base_theme/assets/functions/custom.php'
                        'wp-content/themes/base_theme/assets/functions/custom-sidebar.php'
                        ],
                    dest: 'wp-content/themes/base_theme/functions-mod.php'
                }
            }
        }
});

    // Load the plugins
    grunt.loadNpmTasks('grunt-contrib-concat');

    // Default task(s).
    grunt.registerTask('default', ['concat']);

};

我的package.json如下(我知道我在grunt.js中没有使用jshint或uglify,我只是一次测试一个)。

{
  "name": "base_theme",
  "version": "0.1.0",
  "devDependencies": {
    "grunt": "~0.4.5",
    "grunt-contrib-concat": "~1.0.0",
    "grunt-contrib-jshint":  "~0.8.0",
    "grunt-contrib-uglify":  "~0.2.7"
  }
}

我已经尝试阅读grunt网站上的文档,但要么我没有正确理解某些内容,要么我只是做了一些完全错误的事情

1 个答案:

答案 0 :(得分:2)

Gruntfile.js的配置几乎正确。

要解决此问题,只需避免在srcdest目标中嵌套distjs。例如:

更新了 Gruntfile.js

php

更多信息:

  1. 请查看module.exports = function(grunt) { // Project configuration. grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), concat: { js: { options: { separator: 'rn' }, //dist: { <-- REMOVE the 'dist' object. src: [ // Comment or uncomment any Bootstrap JS files // you will not be using 'wp-content/themes/base_theme/assets/js/required/bootstrap/plugins/alert.js', 'wp-content/themes/base_theme/assets/js/required/bootstrap/plugins/button.js', 'wp-content/themes/base_theme/assets/js/required/bootstrap/plugins/carousel.js' // ... ], dest: 'wp-content/themes/base_theme/assets/js/required/bootstrap/bootstrap.min.js' }, php: { options: { stripBanners: true, separator: '\n?>\n', }, //dist: { <-- REMOVE the 'dist' object here too. src: [ // To create a new location, follow pattern below // Comment out what you don't need a re-concat 'wp-content/themes/base_theme/assets/functions/basic.php', 'wp-content/themes/base_theme/inc/custom-header.php', 'wp-content/themes/base_theme/inc/customizer.php' // ... ], dest: 'wp-content/themes/base_theme/functions-mod.php' } } }); // Load the plugins grunt.loadNpmTasks('grunt-contrib-concat'); // Default task(s). grunt.registerTask('default', ['concat']); }; 文档中multiple-targets的示例配置和grunt文档中的Task Configuration and Targets以获取更多信息。

  2. 注意:如果您在grunt-contrib-concat连结的结果文件时遇到任何问题, 可能 需要包含uglify值中的分号。有关详细信息,请参阅here。它写着:

  3.   

    连接文件将在此字符串上加入。如果您使用缩小器对连接的JavaScript文件进行后处理,则可能需要使用分号'; \ n'作为分隔符。