忽略MVC捆绑包中的文件

巴尔

我们正在使用功能标志(在Web.Config中设置)来切换尚未完成的某些功能在UI中的可见性。我也希望我的MVC捆绑包不包括相关的JS文件,因为当这些功能未启用时,它们将是客户端必须下载的无用文件。

到目前为止,我已经找到了,IgnoreList.Ignore但似乎无法使它正常工作。这基本上就是我正在做的:

public static void RegisterBundles(BundleCollection bundles)
{
    if (!appConfiguration.FeatureXEnabled)
    {
        //Skip these files if the Feature X is not enabled!
        //When this flag is removed, these lines can be deleted and the files will be included like normal
        bundles.IgnoreList.Ignore("~/App/Organization/SomeCtrl.js", OptimizationMode.Always);
        bundles.IgnoreList.Ignore("~/App/Filters/SomeFilter.js", OptimizationMode.Always);
    }

    var globalBundle = new ScriptBundle("~/bundles/app-global").Include(
        "~/App/RootCtrl.js",
        "~/App/Directives/*.js",
        "~/App/Services/*.js",
        "~/App/Application.js"
    );
    bundles.Add(globalBundle);

    var appOrgBundle = new ScriptBundle("~/bundles/app-org");
    appOrgBundle.Include(
        "~/App/Filters/*.js",
        "~/App/Organization/*.js"
    );

    bundles.Add(appOrgBundle);
}

使用上面的代码,忽略列表中的文件仍然被包含!我在这里做错了什么?

根特

在使用表达式时,我还与忽略列表进行了斗争。我发现一个简单的解决方法是实现IBundleOrderer,它将排除我不需要的文件,并对文件的包含方式进行一些排序。尽管这并不是真正的预期用途,但我发现它填补了空白。

IBundleOrderer获得访问文件的完整列表(表达扩展到哪些文件匹配)。

public class ApplicationOrderer : IBundleOrderer {
    public IEnumerable<BundleFile> OrderFiles(BundleContext context, IEnumerable<BundleFile> files)
    {
        if (!AppSettings.FeatureFlag_ServiceIntegrationsEnabled)
        {
            //Skip these files if the Service Integrations Feature is not enabled!
            //When this flag is removed, these lines can be deleted and the files will be included like normal
            var serviceIntegrationPathsToIgnore = new[]
            {
                "/App/ServiceIntegrations/IntegrationSettingsModel.js",
                "/App/ServiceIntegrations/IntegrationSettingsService.js",
                "/App/ServiceIntegrations/ServiceIntegrationsCtrl.js"
            };
            files = files.Where(x => !serviceIntegrationPathsToIgnore.Contains(x.VirtualFile.VirtualPath));
        }

        return files;
    }
}

用法示例:

public static void RegisterBundles(BundleCollection bundles)
{
        var appBundle = new ScriptBundle("~/bundles/app")
            .IncludeDirectory("~/App/", "*.js", true)
        appBundle.Orderer = new ApplicationOrderer();
        bundles.Add(appBundle);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章