RequireJS - 省略优化构建的测试代码

时间:2011-11-21 12:14:12

标签: javascript unit-testing qunit requirejs

我一直在研究基于RequireJS将测试集成到我的应用程序中。我发现了this example如何将QUnit测试集成到RequireJS结构中。显然,您不希望测试代码位于Production构建中。 如何在RequireJS中继续测试最终的生产版本?

1 个答案:

答案 0 :(得分:2)

您可以在构建文件中设置许多选项。请参阅GitHub上的完整示例(https://github.com/jrburke/r.js/blob/master/build/example.build.js

您要做的是从您的模块中排除某些项目:

 //This module entry combines all the dependencies of foo/bar/bip into one file,
 //but excludes foo/bar/bop and its dependencies from the built file. If you want
 //to exclude a module that is also another module being optimized, it is more
 //efficient if you define that module optimization entry before using it
 //in an exclude array.
    {
        name: "foo/bar/bip",
        exclude: [
            "foo/bar/bop"
        ]
    },

 //This module entry shows how to specify a specific module be excluded
 //from the built module file. excludeShallow means just exclude that
 //specific module, but if that module has nested dependencies that are
 //part of the built file, keep them in there. This is useful during
 //development when you want to have a fast bundled set of modules, but
 //just develop/debug one or two modules at a time.
    {
        name: "foo/bar/bin",
        excludeShallow: [
            "foo/bar/bot"
        ]
    }

您也可以使用正则表达式排除项目,但这可能是过度的:

//When the optimizer copies files from the source location to the
//destination directory, it will skip directories and files that start
//with a ".". If you want to copy .directories or certain .files, for
//instance if you keep some packages in a .packages directory, or copy
//over .htaccess files, you can set this to null. If you want to change
//the exclusion rules, change it to a different regexp. If the regexp
//matches, it means the directory will be excluded. This used to be
//called dirExclusionRegExp before the 1.0.2 release.
//As of 1.0.3, this value can also be a string that is converted to a
//RegExp via new RegExp().

fileExclusionRegExp: /^\./,