【问题标题】:Concatenating php files and Javascript with Grunt.js使用 Grunt.js 连接 php 文件和 Javascript
【发布时间】:2016-12-28 15:58:26
【问题描述】:

我在尝试让我的 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 网站上的文档,但要么我没有正确理解某些内容,要么我只是做错了事

【问题讨论】:

    标签: gruntjs grunt-contrib-concat


    【解决方案1】:

    Gruntfile.js 的配置几乎正确。

    要解决此问题,只需避免在 dist 对象中为 jsphp 目标嵌套 srcdest。例如:

    更新了 Gruntfile.js

    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']);
    
    };
    

    更多信息:

    1. 查看grunt-contrib-concat 文档中multiple-targets 的示例配置和grunt 文档中Task Configuration and Targets 的示例配置以获取更多信息。

    2. 注意:如果您在uglify 连接的结果文件时遇到任何问题,可能需要包含分号在您的 separator 值中。有关更多信息,请参阅here。内容如下:

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

    【讨论】:

    • 我要谢谢你。这 100% 有效,我必须将 rn 替换为 ; 才能获得正确的格式。感谢您抽出宝贵时间查看并纠正它!
    猜你喜欢
    • 2012-11-22
    • 1970-01-01
    • 2014-06-23
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多