public string BuildConfigurationScript(IEnumerable<IAmdModule> modules, RequireJSConfiguration requireJsConfiguration)
        {
            var config = ConfigurationObject(modules, requireJsConfiguration);
            var json = jsonSerializer.Serialize(config);
            var script = "requirejs.config(" + json + ");";

            if (!string.IsNullOrEmpty(requireJsConfiguration.OnErrorModule))
            {
                script += string.Format("require(['{0}'], function(handler) {{ require.onError = handler; }});", requireJsConfiguration.OnErrorModule);
            }
            return script;
        }
        object ConfigurationObject(IEnumerable<IAmdModule> modules, RequireJSConfiguration requireJsConfiguration)
        {
            var paths = modules.ToDictionary(m => m.ModulePath, CreateUrl);
            dynamic configurationObject = new ExpandoObject();
            configurationObject.paths = paths;
            var shim = modules.OfType<IAmdShimmableModule>().Where(m => m.Shim).ToDictionary(m => m.ModulePath, CreateShimConfiguration);
            if (shim.Count > 0)
            {
                configurationObject.shim = shim;

                if (!this.isDebuggingEnabled)
                {
                    //Needed to solve a load timeout issue "The paths config was used to set two module IDs to the same file, and that file only has one anonymous module in it"
                    var globalMaps = modules.OfType<IAmdShimmableModule>().Where(m => m.Shim).ToDictionary(m => m.ModulePath, m => modules.FirstOrDefault(mod => mod.Bundle.Path == m.Bundle.Path).ModulePath);
                    configurationObject.map = new Dictionary<string, dynamic> { { "*", globalMaps } };
                }
            }

            if (!String.IsNullOrEmpty(requireJsConfiguration.BaseUrl))
            {
                configurationObject.baseUrl = requireJsConfiguration.BaseUrl;
            }

            if (requireJsConfiguration.CatchError != null)
            {
                configurationObject.catchError = requireJsConfiguration.CatchError.Value;
            }
            if (requireJsConfiguration.EnforceDefine != null)
            {
                configurationObject.enforceDefine = requireJsConfiguration.EnforceDefine.Value;
            }
            if (requireJsConfiguration.Config != null)
            {
                configurationObject.config = requireJsConfiguration.Config;
            }
            if (requireJsConfiguration.WaitSeconds != null)
            {
                configurationObject.waitSeconds = requireJsConfiguration.WaitSeconds.Value;
            }
            return configurationObject;
        }
Esempio n. 3
0
 public RequireJsConfigAsset(IEnumerable<IAmdModule> modules, RequireJSConfiguration requireJsConfiguration, IConfigurationScriptBuilder configurationScriptBuilder)
 {
     this.modules = modules;
     this.requireJsConfiguration = requireJsConfiguration;
     this.configurationScriptBuilder = configurationScriptBuilder;
 }