Skip to content

Commit

Permalink
autoBundler normalizes dependency paths in bundle requires (placehold…
Browse files Browse the repository at this point in the history
…er fix for #45)
  • Loading branch information
CezarCretu committed Jan 5, 2015
1 parent ea38427 commit 7535c16
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 46 deletions.
7 changes: 6 additions & 1 deletion RequireJsNet.Compressor/AutoDependency/ScriptProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void Process()
{
EnsureHasRange(x.ParentNode.Node, lines);
EnsureHasRange(x.DependencyArrayNode, lines);
EnsureHasRange(x.ModuleDefinitionNode, lines);
EnsureHasRange(x.ModuleDefinitionNode, lines);
EnsureHasRange(x.ModuleIdentifierNode, lines);
EnsureHasRange(x.SingleDependencyNode, lines);
var arguments = x.ParentNode.Node.As<CallExpression>().Arguments;
Expand Down Expand Up @@ -102,6 +102,11 @@ private TransformationCollection GetTransformations(VisitorResult result, List<R
}
else
{
foreach (var reqCall in result.RequireCalls.Where(r => r.DependencyArrayNode != null))
{
trans.Add(NormalizeDepsTransformation.Create(reqCall));
}

// if there are no define calls but there is at least one require module call, transform that into a define call
if (!result.RequireCalls.Where(r => r.Type == RequireCallType.Define).Any())
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// RequireJS.NET
// Copyright VeriTech.io
// http://veritech.io
// Dual licensed under the MIT and GPL licenses:
// http://www.opensource.org/licenses/mit-license.php
// http://www.gnu.org/licenses/gpl.html

using System;
using System.Linq;
using Jint.Parser.Ast;
using RequireJsNet.Compressor.Parsing;
using RequireJsNet.Helpers;

namespace RequireJsNet.Compressor.Transformations
{
internal class NormalizeDepsTransformation : IRequireTransformation
{
public RequireCall RequireCall { get; set; }

public static NormalizeDepsTransformation Create(RequireCall requireCall)
{
return new NormalizeDepsTransformation
{
RequireCall = requireCall
};
}

public void Execute(ref string script)
{
var dependencies =
RequireCall.DependencyArrayNode.Elements.Where(r => r.Type == SyntaxNodes.Literal).Cast<Literal>();

foreach (var dependency in dependencies)
{
var range = new int[] {dependency.Range[0] + 1, dependency.Range[1] - 1};

var preDeps = script.Substring(0, range[0]);
var postDeps = script.Substring(range[1], script.Length - range[1]);
var deps = script.Substring(range[0], range[1] - range[0]).ToModuleName();
script = preDeps + deps + postDeps;
}
}

public int[] GetAffectedRange()
{
return RequireCall.DependencyArrayNode.Range;
}
}
}
2 changes: 1 addition & 1 deletion RequireJsNet.Compressor/RequireJsNet.Compressor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
<Compile Include="Tasks\CssCompressorTask.cs" />
<Compile Include="Tasks\JavaScriptCompressorTask.cs" />
<Compile Include="Tasks\RequireCompressorTask.cs" />
<Compile Include="AutoDependency\Transformations\DepsToLowerTransformation.cs" />
<Compile Include="AutoDependency\Transformations\NormalizeDepsTransformation.cs" />
<Compile Include="AutoDependency\Transformations\ToDefineTransformation.cs" />
<Compile Include="AutoDependency\Transformations\AddIdentifierTransformation.cs" />
<Compile Include="AutoDependency\Transformations\IRequireTransformation.cs" />
Expand Down
3 changes: 0 additions & 3 deletions RequireJsNet.Examples/RequireJS.complex.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@
"include": [
{
"directory": "\\controllers\\Root\\"
},
{
"file": "jquery-1.10.2"
}
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
require(["controllers/root/home/complexLoad", "underscore"], function() {
require(["Controllers/Root/Home/complexLoad", "underscore"], function() {
_.each([1], alert);
});
2 changes: 2 additions & 0 deletions RequireJsNet/Helpers/PathHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public static string GetRequirePath(this string path)

public static string ToModuleName(this string relativePath)
{
var initial = relativePath;
relativePath = initial.Replace('/', Path.DirectorySeparatorChar);
var directory = Path.GetDirectoryName(relativePath);
var file = relativePath.EndsWith(".js") ? Path.GetFileNameWithoutExtension(relativePath)
: Path.GetFileName(relativePath);
Expand Down

0 comments on commit 7535c16

Please sign in to comment.