Skip to content

Commit a18adae

Browse files
committed
v.3.2 - added .vue file caching
1 parent 271963c commit a18adae

File tree

5 files changed

+41
-24
lines changed

5 files changed

+41
-24
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,13 @@ The really cool part about in-line templates is that they can include ASP.NET We
8181
</vue:Component>
8282
```
8383

84-
## When to use in-line vs. .vue files?
84+
## .vue files vs. in-line templates
8585

86-
If you need to include server rendered data inside the Vue template (see above), then in-line is the only option.
86+
If you need to include server rendered data inside the Vue template with `<%...%>` tags (see above), then an in-line template is the only option.
8787

88-
Otherwise, the easiest way is to start in-line. Then at some point when the page becomes too big, it is easy to copy the content of each component/app control to separate (.vue) file.
88+
Otherwise, the easiest way is to start with in-line templates. Then at some point when the page becomes too big, it is easy to copy the content of each component/app control to separate (.vue) file.
89+
90+
Note: When using .vue files, the JavaScript output is cached between requests (involved .vue files including component imports are monitored for changes). This means that for the first request, using .vue files will be slower than in-line templates because the files(s) have to be loaded from disc. But for subsequent requests, .vue files will be faster than in-line templates because the template processing is already done.
8991

9092

9193
## Web Forms controls

sample-web-site/Bin/VueJSWebForms.dll

0 Bytes
Binary file not shown.

src/Module1.vb

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Friend Module Module1
99
If content.Length = 0 Then Throw New Exception("Template is empty")
1010
If content.StartsWith("<template>", StringComparison.InvariantCultureIgnoreCase) Then
1111
If Not String.IsNullOrEmpty(prpOptions) Then Throw New Exception("Control cannot have 'Options' property when content starts with <template>")
12-
Return "(" & VueFilesToJS.CompileText(content, ctx.Server.MapPath("~/"), ctx.Request.Url.AbsolutePath, prpsquashWS) & ")()"
12+
Return "(" & VueFilesToJS.Compile(ctx.Server.MapPath("~/"), ctx.Request.Url.AbsolutePath, prpsquashWS, content) & ")()"
1313
Else
1414
prpOptions = If(prpOptions, "").Trim
1515
If prpOptions.Length = 0 Then prpOptions = "{}"
@@ -19,12 +19,26 @@ Friend Module Module1
1919
"template:" & VueFilesToJS.JSStringEncode(content) & "," &
2020
prpOptions.Substring(1)
2121
End If
22+
End If
23+
24+
REM .vue File
25+
If content.Length > 0 Then Throw New Exception("Control cannot have content when used with 'File' property")
26+
If Not String.IsNullOrEmpty(prpOptions) Then Throw New Exception("Control cannot both 'File' and 'Options' properties")
27+
28+
'If Not useCache Then Return "(" & VueFilesToJS.Compile(ctx.Server.MapPath("~/"), prpFile, prpsquashWS) & ")()"
29+
30+
Dim f As String
31+
Dim CacheKey = "VueJSWebForm:" & ctx.Request.MapPath(prpFile)
32+
Dim obj = ctx.Cache.Get(CacheKey)
33+
If obj Is Nothing Then
34+
Dim FileList As New List(Of String)
35+
f = VueFilesToJS.Compile(ctx.Server.MapPath("~/"), prpFile, prpsquashWS, Nothing, AddressOf FileList.Add)
36+
ctx.Cache.Add(CacheKey, f, New Web.Caching.CacheDependency(FileList.ToArray), Web.Caching.Cache.NoAbsoluteExpiration, Web.Caching.Cache.NoSlidingExpiration, Web.Caching.CacheItemPriority.Normal, Nothing)
2237
Else
23-
REM .vue File
24-
If content.Length > 0 Then Throw New Exception("Control cannot have content when used with 'File' property")
25-
If Not String.IsNullOrEmpty(prpOptions) Then Throw New Exception("Control cannot both 'File' and 'Options' properties")
26-
Return "(" & VueFilesToJS.Compile(ctx.Server.MapPath("~/"), prpFile, prpsquashWS) & ")()"
38+
f = DirectCast(obj, String)
2739
End If
40+
41+
Return "(" & f & ")()"
2842
End Function
2943

3044
End Module

src/My Project/AssemblyInfo.vb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices
3131
' by using the '*' as shown below:
3232
' <Assembly: AssemblyVersion("1.0.*")>
3333

34-
<Assembly: AssemblyVersion("3.1.1.0")>
35-
<Assembly: AssemblyFileVersion("3.1.1.0")>
34+
<Assembly: AssemblyVersion("3.2.0.0")>
35+
<Assembly: AssemblyFileVersion("3.2.0.0")>

src/VueFilesToJS.vb

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,20 @@
22
Private Components As New Dictionary(Of String, Component)
33
Private WsRoot As String
44
Private SquashWS As Boolean
5+
Private FileReadCallback As Action(Of String) = Nothing
56

6-
Public Shared Function Compile(wsRootPath As String, vueFile As String, Optional squashWS As Boolean = True) As String
7+
Public Shared Function Compile(wsRootPath As String, sourceFile As String, Optional squashWS As Boolean = True, Optional rootFileContent As String = Nothing, Optional fileReadCallback As Action(Of String) = Nothing) As String
78
If wsRootPath.EndsWith("\") Then wsRootPath = wsRootPath.Substring(0, wsRootPath.Length - 1)
8-
Dim inst = New VueFilesToJS With {.WsRoot = wsRootPath, .SquashWS = squashWS}
9-
Return inst.ProcRoot(vueFile, AddressOf My.Computer.FileSystem.ReadAllText)
10-
End Function
11-
12-
Public Shared Function CompileText(txt As String, wsRootPath As String, vueFile As String, Optional squashWS As Boolean = True) As String
13-
If wsRootPath.EndsWith("\") Then wsRootPath = wsRootPath.Substring(0, wsRootPath.Length - 1)
14-
Dim inst = New VueFilesToJS With {.WsRoot = wsRootPath, .SquashWS = squashWS}
15-
Return inst.ProcRoot(vueFile, Function(fn) txt)
9+
Dim inst = New VueFilesToJS With {.WsRoot = wsRootPath, .SquashWS = squashWS, .FileReadCallback = fileReadCallback}
10+
Return inst.ProcRoot(sourceFile, rootFileContent)
1611
End Function
1712

1813
Private Sub New()
1914
REM private constructor so only Compile function can create instance
2015
End Sub
2116

22-
Private Function ProcRoot(vueFile As String, loadFile As Func(Of String, String)) As String
23-
Dim res = ParseVueFile(vueFile, Nothing, loadFile)
17+
Private Function ProcRoot(vueFile As String, FileContent As String) As String
18+
Dim res = ParseVueFile(vueFile, Nothing, FileContent)
2419

2520
Dim sb As New System.Text.StringBuilder
2621
sb.AppendLine("function() {")
@@ -38,10 +33,16 @@
3833
End Function
3934

4035
'wsroot = "c:\web-sites\tjek" (no ending \)
41-
Private Function ParseVueFile(vueFile As String, fromComp As Component, loadFile As Func(Of String, String)) As ParseVueFileResult
36+
Private Function ParseVueFile(vueFile As String, fromComp As Component, Optional fileContent As String = Nothing) As ParseVueFileResult
4237
Dim cp = ResolvePath(vueFile)
4338
Dim vfWin = WsRoot & cp.Replace("/", "\") & JustFN(vueFile)
44-
Dim x = loadFile(vfWin).Trim
39+
Dim x As String
40+
If fileContent Is Nothing Then
41+
If FileReadCallback IsNot Nothing Then FileReadCallback(vfWin)
42+
x = My.Computer.FileSystem.ReadAllText(vfWin).Trim
43+
Else
44+
x = fileContent.Trim
45+
End If
4546

4647
If Not x.EndsWith("</script>") Then Throw New Exception(".vue file '" & vueFile & "' does not end with </script>")
4748
x = x.Substring(0, x.Length - 9).Trim
@@ -171,7 +172,7 @@
171172
c = New Component With {.Name = compName, .File = vueFile}
172173
If parentComp IsNot Nothing Then parentComp.SubComps.Add(c)
173174
Components.Add(compName, c)
174-
c.pvfr = ParseVueFile(vueFile, c, AddressOf My.Computer.FileSystem.ReadAllText)
175+
c.pvfr = ParseVueFile(vueFile, c)
175176
End Sub
176177

177178
Private Class Component

0 commit comments

Comments
 (0)