【问题标题】:Phonegap www folder and optimization issuePhonegap www 文件夹和优化问题
【发布时间】:2014-08-07 08:03:47
【问题描述】:

我正在做一个 phonegap 项目,我有很多 css、js 文件。我已将这些 css、js 文件放在 www 文件夹下的 cssjs 子目录中。我打算使用 require js 优化工具来连接和缩小 css 和 js 文件。压缩后的 js 和 css 文件被放置在 www 下名为 dist 的单独文件夹中。

MyPhonegapProject
       |
       |__ www 
            |
            |__ css (contains unminified css files)
            |__ js  (contains unminifies js files)
            |__ dist (contains minified js and css files)
            |__ index.html (references minified js and css files from the dist folder)     

现在我的问题是关于启动 phonegap 构建是否复制了 www 文件夹中的所有内容?我不希望将未缩小的 js 和 css 文件复制到构建文件夹中。我该如何解决这个问题?

【问题讨论】:

    标签: cordova phonegap-build


    【解决方案1】:

    您可以在src 目录中创建和工作,然后在开始构建之前将所需的(dist)文件复制到www 目录。

    我建议使用任务运行器,例如 Grunt - http://gruntjs.com 为您自动执行此操作。

    这是我整理的 Gruntfile.js 示例,您可以将其用作模板。它清理www 目录,将所有文件从src 复制到www,从www 中删除未压缩的文件,然后启动cordova build cli 命令:

    module.exports = function (grunt) {
        grunt.initConfig({
            pkg: grunt.file.readJSON("package.json"),
            shell: {
                build: {
                    // Run the cordova build command
                    command: "cordova build"
                }
            },
            copy: {
                main: {
                    // Copy all files from src to www
                    src: ["**/*"],
                    dest: "www/",
                    expand: true,
                    cwd: "src/"
                }
            },
            clean: {
                www: {
                    // Remove all files currenty in the www dir
                    src: ["www"]
                },
                js: {
                    // Remove unminified js etc
                    src: ["www/js/*.js"]
                }
            }
        });
    
        grunt.loadNpmTasks("grunt-contrib-copy");
        grunt.loadNpmTasks("grunt-contrib-clean");
        grunt.loadNpmTasks("grunt-shell");
    
        grunt.registerTask("build", ["clean:www", "copy", "clean:js", "shell"]);
    };
    

    您可以添加工作流程所需的任何其他内容。

    【讨论】:

    • 我添加了一个示例 Gruntfile.js 以用作模板