Skip to content

Commit

Permalink
Merge branch 'commonjs'
Browse files Browse the repository at this point in the history
  • Loading branch information
aquamoth committed Feb 22, 2017
2 parents 021a17e + d2ac04c commit aca19ad
Show file tree
Hide file tree
Showing 20 changed files with 318 additions and 41 deletions.
24 changes: 14 additions & 10 deletions RequireJsNet.Examples/RequireJS.complex.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
{
"paths": {
"jquery": [ "http://no-domain.unknown/jquery-2.1.3", "jquery-2.1.3" ],
"jqMin": "jquery-2.1.3.min",
"req1": "req1",
"req2": "req2",
"jquery-validate": {
"path": "jquery.validate",
"defaultBundle": "jqValidate"
},
"jquery-validate-unobtrusive": "jquery.validate.unobtrusive"
"paths": {
"jquery": [ "http://no-domain.unknown/jquery-2.1.3", "jquery-2.1.3" ],
"jqMin": "jquery-2.1.3.min",
"req1": "req1",
"req2": "req2",
"jquery-validate": {
"path": "jquery.validate",
"defaultBundle": "jqValidate"
},
"jquery-validate-unobtrusive": "jquery.validate.unobtrusive"
},
"packages": [
"cart",
{ "name": "store", "main": "store", "location": "/Scripts/CommonJS/store" }
],
"shim": {
"jquery-validate": {
"deps": [ "jquery" ],
Expand Down
3 changes: 3 additions & 0 deletions RequireJsNet.Examples/RequireJsNet.Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
<Content Include="RequireJS.complex.json" />
<Content Include="Scripts\bootstrap.js" />
<Content Include="Scripts\bootstrap.min.js" />
<Content Include="Scripts\cart\main.js" />
<Content Include="Scripts\Controllers\Foo\Home\index.js" />
<Content Include="Scripts\Controllers\Root\Home\errback.js" />
<Content Include="Scripts\Controllers\Root\Home\complexLoad.js" />
Expand Down Expand Up @@ -187,6 +188,8 @@
<Content Include="Scripts\respond.matchmedia.addListener.js" />
<Content Include="Scripts\respond.matchmedia.addListener.min.js" />
<Content Include="Scripts\respond.min.js" />
<Content Include="Scripts\CommonJS\store\store.js" />
<Content Include="Scripts\CommonJS\store\util.js" />
<Content Include="Scripts\text.js" />
<Content Include="Scripts\underscore.js" />
<Content Include="Web.config" />
Expand Down
2 changes: 2 additions & 0 deletions RequireJsNet.Examples/Scripts/CommonJS/store/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//CommonJS package format
alert('store/store.js loaded');
2 changes: 2 additions & 0 deletions RequireJsNet.Examples/Scripts/CommonJS/store/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//CommonJS package format
alert('store/util.js loaded');
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
define(['req1', "jquery"], function(req1, $) {
$(function () {
alert('jquery loaded');

require(["cart", "store", "store/util"], function (cart, store, util) {

alert('commonJS packages cart, store and util has been loaded');

});

});
});
2 changes: 2 additions & 0 deletions RequireJsNet.Examples/Scripts/cart/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//CommonJS package format
alert('cart/main.js loaded');
43 changes: 43 additions & 0 deletions RequireJsNet.Tests/ConfigMergerShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -553,5 +553,48 @@ public void UnifyBundleItemsForSameId()

CustomAssert.JsonEquals(expected, merged);
}

[Fact]
public void ReplacePackagesWithSameName()
{
var packageA1 = new RequirePackage("a", "main");
var packageA2 = new RequirePackage("a", "start", "/somewhere");

var firstConfig = ConfigurationCreators.CreateCollectionWithPackages(packageA1);
var secondConfig = ConfigurationCreators.CreateCollectionWithPackages(packageA2);

var merger = ConfigurationCreators.CreateBundleProcessingConfigMerger(firstConfig, secondConfig);
var merged = merger.GetMerged();

var expected = ConfigurationCreators.CreateEmptyCollection();
expected.Packages.PackageList = new List<RequirePackage>
{
new RequirePackage("a", "start", "/somewhere")
};

CustomAssert.JsonEquals(expected, merged);
}

[Fact]
public void AppendPackagesWithDifferentName()
{
var packageA1 = new RequirePackage("a", "main");
var packageA2 = new RequirePackage("b", "start", "/somewhere");

var firstConfig = ConfigurationCreators.CreateCollectionWithPackages(packageA1);
var secondConfig = ConfigurationCreators.CreateCollectionWithPackages(packageA2);

var merger = ConfigurationCreators.CreateBundleProcessingConfigMerger(firstConfig, secondConfig);
var merged = merger.GetMerged();

var expected = ConfigurationCreators.CreateEmptyCollection();
expected.Packages.PackageList = new List<RequirePackage>
{
new RequirePackage("a", "main"),
new RequirePackage("b", "start", "/somewhere")
};

CustomAssert.JsonEquals(expected, merged);
}
}
}
11 changes: 11 additions & 0 deletions RequireJsNet.Tests/DataCreation/ConfigurationCreators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public static ConfigurationCollection CreateEmptyCollection()
collection.Paths = new RequirePaths();
collection.Paths.PathList = new List<RequirePath>();

collection.Packages = new RequirePackages();
collection.Packages.PackageList = new List<RequirePackage>();

collection.AutoBundles = new AutoBundles();
collection.AutoBundles.Bundles = new List<AutoBundle>();

Expand All @@ -41,6 +44,14 @@ public static ConfigurationCollection CreateCollectionWithPaths(params RequirePa
return collection;
}

public static ConfigurationCollection CreateCollectionWithPackages(params RequirePackage[] packages)
{
var collection = CreateEmptyCollection();
collection.Packages = new RequirePackages();
collection.Packages.PackageList = packages.ToList();
return collection;
}

public static ConfigurationCollection CreateCollectionWithShims(params ShimEntry[] shim)
{
var collection = CreateEmptyCollection();
Expand Down
32 changes: 32 additions & 0 deletions RequireJsNet.Tests/JsonReaderShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,38 @@ public void ReadPathArray()
CustomAssert.JsonEquals(expected, config);
}

[Fact]
public void ReadPackagesArray()
{
var expectedPackages = new[]
{
new RequirePackage("cart", "main"),
new RequirePackage("store", "store")
};
var expectedCollection = ConfigurationCreators.CreateCollectionWithPackages(expectedPackages);

var config = ReadJson(new TestFileReader());

Assert.Equal(2, config.Packages.PackageList.Count);
CustomAssert.JsonEquals(expectedCollection, config);
}

[Fact]
public void SerializesPackagesInRequireJSFormat()
{
var expected = "var require = {\"baseUrl\":\"\",\"locale\":\"en\",\"urlArgs\":null,\"waitSeconds\":7,\"paths\":{},\"packages\":[{\"name\":\"cart\",\"main\":\"main\"},{\"name\":\"store\",\"main\":\"store\"}],\"shim\":{},\"map\":{}};";

var config = new RequireRendererConfiguration();
var collection = ConfigurationCreators.CreateCollectionWithPackages(
new RequirePackage("cart", "main"),
new RequirePackage("store", "store")
);
var outputConfig = RequireJsHtmlHelpers.createOutputConfigFrom(collection, config, "en");
var actual = Helpers.JavaScriptHelpers.SerializeAsVariable(outputConfig, "require");

Assert.Equal(expected, actual);
}

[Fact]
public void ReadShimWithExports()
{
Expand Down
2 changes: 2 additions & 0 deletions RequireJsNet.Tests/RequireJsNet.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<Compile Include="JsonReaderShould.cs" />
<Compile Include="ReaderFactoryShould.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RequirePackageShould.cs" />
<Compile Include="TestImplementations\TestFileReader.cs" />
</ItemGroup>
<ItemGroup>
Expand All @@ -93,6 +94,7 @@
<ItemGroup>
<None Include="packages.config" />
<EmbeddedResource Include="TestData\JsonReaderShould\ReadPathArray.json" />
<EmbeddedResource Include="TestData\JsonReaderShould\ReadPackagesArray.json" />
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
Expand Down
39 changes: 39 additions & 0 deletions RequireJsNet.Tests/RequirePackageShould.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using RequireJsNet.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace RequireJsNet.Tests
{
public class RequirePackageShould
{
[Fact]
public void InitializeWithOnlyName()
{
var name = "package1";

var actual = new RequirePackage(name);

Assert.Equal(name, actual.Name);
Assert.Equal("main", actual.Main);
Assert.Equal(null, actual.Location);
}

[Fact]
public void InitializeWithAllProperties()
{
var name = "package2";
var main = "start";
var location = "/Scripts/CommonJS/package2";

var actual = new RequirePackage(name, main, location);

Assert.Equal(name, actual.Name);
Assert.Equal(main, actual.Main);
Assert.Equal(location, actual.Location);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"packages": [
"cart",
{ "name": "store", "main": "store" }
]
}
25 changes: 25 additions & 0 deletions RequireJsNet/Configuration/ConfigMerger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public ConfigMerger(List<ConfigurationCollection> collections, ConfigLoaderOptio
this.collections = collections;
finalCollection.Paths = new RequirePaths();
finalCollection.Paths.PathList = new List<RequirePath>();
finalCollection.Packages = new RequirePackages();
finalCollection.Packages.PackageList = new List<RequirePackage>();
finalCollection.Shim = new RequireShim();
finalCollection.Shim.ShimEntries = new List<ShimEntry>();
finalCollection.Map = new RequireMap();
Expand All @@ -46,6 +48,11 @@ public ConfigurationCollection GetMerged()
MergePaths(coll);
}

if (coll.Packages != null && coll.Packages.PackageList != null)
{
MergePackages(coll);
}

if (coll.Shim != null && coll.Shim.ShimEntries != null)
{
MergeShims(coll);
Expand Down Expand Up @@ -99,6 +106,24 @@ private void MergePaths(ConfigurationCollection collection)
}
}

private void MergePackages(ConfigurationCollection collection)
{
var finalPackages = finalCollection.Packages.PackageList;
foreach (var package in collection.Packages.PackageList)
{
var existing = finalPackages.Where(r => r.Name == package.Name).FirstOrDefault();
if (existing != null)
{
existing.Main = package.Main;
existing.Location = package.Location;
}
else
{
finalPackages.Add(package);
}
}
}

private void MergeShims(ConfigurationCollection collection)
{
var finalShims = finalCollection.Shim.ShimEntries;
Expand Down
33 changes: 33 additions & 0 deletions RequireJsNet/Configuration/JsonReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ private ConfigurationCollection ProcessConfig()
var deserialized = (JObject)JsonConvert.DeserializeObject(text);
collection.FilePath = Path;
collection.Paths = GetPaths(deserialized);
collection.Packages = GetPackages(deserialized);
collection.Shim = GetShim(deserialized);
collection.Map = GetMap(deserialized);

Expand Down Expand Up @@ -133,6 +134,38 @@ private static RequirePath requirePathFrom(JProperty prop)
return result;
}

private RequirePackages GetPackages(JObject document)
{
var packages = new RequirePackages();
packages.PackageList = new List<RequirePackage>();

if (document != null && document["packages"] != null)
{
packages.PackageList = document["packages"]
.Select(r => requirePackageFrom(r))
.ToList();
}

return packages;
}

private static RequirePackage requirePackageFrom(JToken token)
{
if (token is JValue)
{
var name = ((JValue)token).Value<string>();
return new RequirePackage(name);
}
else
{
var packageObj = (JObject)token;
var name = (string)packageObj["name"];
var main = (string)packageObj["main"];
var location = (string)packageObj["location"];
return new RequirePackage(name, main, location);
}
}

private RequireShim GetShim(JObject document)
{
var shim = new RequireShim();
Expand Down
3 changes: 3 additions & 0 deletions RequireJsNet/Models/ConfigurationCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ internal class ConfigurationCollection
[JsonProperty(PropertyName = "paths")]
public RequirePaths Paths { get; set; }

[JsonProperty(PropertyName = "packages")]
public RequirePackages Packages { get; set; }

[JsonProperty(PropertyName = "shim")]
public RequireShim Shim { get; set; }

Expand Down
3 changes: 3 additions & 0 deletions RequireJsNet/Models/JsonRequireOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public class JsonRequireOutput
[JsonProperty(PropertyName = "paths")]
public Dictionary<string, IEnumerable<string>> Paths { get; set; }

[JsonProperty(PropertyName = "packages")]
public List<RequirePackage> Packages { get; set; }

[JsonProperty(PropertyName = "shim")]
public Dictionary<string, JsonRequireDeps> Shim { get; set; }

Expand Down
28 changes: 28 additions & 0 deletions RequireJsNet/Models/RequirePackage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RequireJsNet.Models
{
public class RequirePackage
{
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }

[JsonProperty(PropertyName = "main")]
public string Main { get; set; }

[JsonProperty(PropertyName = "location", NullValueHandling = NullValueHandling.Ignore)]
public string Location { get; set; }

public RequirePackage(string name, string main = "main", string location = null)
{
this.Name = name;
this.Main = main ?? "main";
this.Location = location;
}
}
}
Loading

0 comments on commit aca19ad

Please sign in to comment.