r.js缩小优化文件未运行

时间:2016-05-31 14:34:05

标签: optimization requirejs

之前,我正在使用r.js来成功优化和缩小我的javascript。我有一个main.js文件,看起来像这样:

require.config({
    baseUrl: "scripts/lib",
    paths: {
        jquery: "http://code.jquery.com/jquery-1.11.2.min",
        underscore: "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min",
        d3: "d3-for-development",
        katex: "https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.3.0/katex.min", // or 0.2.0
        mathjax: "http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML&delayStartupUntil=configured",
        etc...
    },
    shim: {
        underscore: { exports: "_" },
        chosen: { deps: ["jquery"] },
        mathjax: {
            exports: "MathJax",
            init: function (){
                MathJax.Hub.Config({
                    tex2jax: {
                        inlineMath: [['$','$'], ['\\(','\\)']],
                        processEscapes: true,
                    },
                });
                MathJax.Hub.Startup.onload();
                return MathJax;
            }
        },
    },
});

require( [
    "jquery",
    "underscore",
    "browser-detect",
    "check-types",
    "katex",
    "mathjax",
    etc
], function(
    $,
    _,
    browser,
    check,
    katex,
    mathjax,
    etc
){

/////////////////////////// INITIALIZATION ///////////////////////////
loginInit()
show('#login')

etc...

我可以成功运行node build/r.js -o mainConfigFile=www/scripts/main.js baseUrl=www/scripts/lib name=../main out=www/scripts/main-optimized.min.js generateSourceMap=true preserveLicenseComments=false optimize=uglify2来缩小。一切正常。

现在,我有一个如下所示的config.js文件:

require.config({
    urlArgs: "bust=" + new Date().getTime(),
    baseUrl: "scripts/lib",
    paths: {
        jquery: ["jquery-min", "http://code.jquery.com/jquery-1.11.2.min"],
        underscore: ["underscore-min", "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min"],
        d3: "d3-for-development", // if we add patches separately, then we can just use https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min
        katex: ["katex-min", "https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.3.0/katex.min"], // or 0.2.0
        mathjax: "http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML&delayStartupUntil=configured",
        main: "../main",
        etc...
    },
    shim: {
        underscore: { exports: "_" },
        chosen: { deps: ["jquery"] },
        mathjax: {
            exports: "MathJax",
            init: function init() {
                MathJax.Hub.Config({
                    tex2jax: {
                        inlineMath: [['$', '$'], ['\\(', '\\)']],
                        processEscapes: true }
                });
                MathJax.Hub.Startup.onload();
                return MathJax;
            }
        }
    }
});

require(["main"], function (main) {
    // pass.  by loading main, we run main.js
});

我没有直接将minify / optimize参数传递给命令行,而是为此创建了一个rbuild.js文件:

({
    mainConfigFile: "../www/scripts/config.js",
    baseUrl: "../www/scripts/lib",
    name: "../config",
    out: "../www/scripts/config-optimized.min.js",
    generateSourceMap: true,
    preserveLicenseComments: false, // this is necessary for generateSourceMap to work
    optimize: "uglify2",
    // removeCombined: true,
    // findNestedDependencies: true,
    paths: {
        // https://github.com/jrburke/requirejs/issues/791
        // http://www.anthb.com/2014/07/04/optimising-requirejs-with-cdn-fallback
        jquery: "jquery-min",
        underscore: "underscore-min",
        d3: "d3-for-development",
        katex: "katex-min",
        mathjax: "http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML&delayStartupUntil=configured",
        marked: "marked",
        chosen: "chosen-min",
        jsnetworkx: "jsnetworkx-min",
        main: "../main",
    },
})

我在命令行中使用node build/r.js -o build/rbuild.js运行它。它似乎成功运行并按预期生成config-optimized.min.js文件。输出是:

Tracing dependencies for: ../config
Cannot optimize network URL, skipping: http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML&delayStartupUntil=configured
Uglify2 file: /Users/Matthew/programming/prove-math/www/scripts/config-optimized.min.js

/Users/Matthew/programming/prove-math/www/scripts/config-optimized.min.js
----------------
/Users/Matthew/programming/prove-math/www/scripts/lib/jquery-min.js
/Users/Matthew/programming/prove-math/www/scripts/lib/underscore-min.js
/Users/Matthew/programming/prove-math/www/scripts/lib/browser-detect.js
/Users/Matthew/programming/prove-math/www/scripts/lib/check-types.js
/Users/Matthew/programming/prove-math/www/scripts/lib/katex-min.js
/Users/Matthew/programming/prove-math/www/scripts/lib/profile.js
/Users/Matthew/programming/prove-math/www/scripts/lib/marked.js
/Users/Matthew/programming/prove-math/www/scripts/lib/d3-for-development.js
/Users/Matthew/programming/prove-math/www/scripts/lib/user.js
/Users/Matthew/programming/prove-math/www/scripts/lib/graph-animation.js
/Users/Matthew/programming/prove-math/www/scripts/lib/graph.js
/Users/Matthew/programming/prove-math/www/scripts/lib/node.js
/Users/Matthew/programming/prove-math/www/scripts/lib/blinds.js
/Users/Matthew/programming/prove-math/www/scripts/lib/chosen-min.js
/Users/Matthew/programming/prove-math/www/scripts/main.js
/Users/Matthew/programming/prove-math/www/scripts/lib/../config.js

但是当我通过我的服务器访问index.html时,页面是空白的。 JS控制台没有提供任何错误或日志消息,这表明没有JS正在运行。我的服务器没有错误,这表明一切都已成功发送到客户端,客户端JS没有运行。

所以我很确信JS在那里但没有运行。我的设置是否有问题导致config.js无法运行代码?没有错误消息,我无法排除故障:)

1 个答案:

答案 0 :(得分:1)

所以我评论了

    generateSourceMap: true,
    preserveLicenseComments: false, // this is necessary for generateSourceMap to work
    optimize: "uglify2",

它有效!然后,我取消注释那些东西,它仍然有效!

似乎从requireJS 2.2开始(我之前使用的是RequireJS 2.1.6),你现在可以使用

    optimize: "uglify",

或根本没有,因为这是默认设置。从requireJS 2.2开始,它在这种情况下使用uglify2。这是我能给出的最接近的解释:/