Fix bugs in Vue watcher and client builds

This commit is contained in:
the1812 2019-11-20 15:31:52 +08:00
parent f342b622fa
commit afa0d54efa
7 changed files with 30 additions and 13 deletions

View File

@ -77,7 +77,8 @@ namespace BilibiliEvolved.Build
public ConcurrentDictionary<string, string> CachedMinFiles { get; private set; } = new ConcurrentDictionary<string, string>();
public void UpdateCachedMinFile(string url)
{
Console.WriteLine($"Update min url: {url}");
url = url.Replace(@"\", "/");
// Console.WriteLine($"Update min url: {url}");
CachedMinFiles[url] = File.ReadAllText(url);
}
}

View File

@ -33,7 +33,8 @@ namespace BilibiliEvolved.Build.Watcher
return !cache.Contains(originalFile);
};
}
private void KillExternalWatcher() {
private void KillExternalWatcher()
{
if (!externalWatcherProcess.HasExited)
{
externalWatcherProcess.Kill();
@ -58,7 +59,9 @@ namespace BilibiliEvolved.Build.Watcher
cache.SaveCache();
if (!Path.GetFileNameWithoutExtension(e.FullPath).EndsWith(".vue"))
{
File.WriteAllText(ResourceMinifier.GetMinimizedFileName(e.FullPath), Minifier.Minify(PostBuild(content)));
var minFile = ResourceMinifier.GetMinimizedFileName(e.FullPath);
File.WriteAllText(minFile, Minifier.Minify(PostBuild(content)));
builder.UpdateCachedMinFile(minFile);
if (e.Name.Contains("dark-slice"))
{
builder.BuildDarkStyles();

View File

@ -23,7 +23,9 @@ namespace BilibiliEvolved.Build.Watcher
builder.WriteInfo($"[{Name}] {e.Name} changed.");
cache.AddCache(e.FullPath);
cache.SaveCache();
File.WriteAllText(ResourceMinifier.GetMinimizedFileName(e.FullPath), Minifier.Minify(File.ReadAllText(e.FullPath)));
string minFile = ResourceMinifier.GetMinimizedFileName(e.FullPath);
File.WriteAllText(minFile, Minifier.Minify(File.ReadAllText(e.FullPath)));
builder.UpdateCachedMinFile(minFile);
if (e.Name.Contains("dark-slice")) {
builder.BuildDarkStyles();
}

View File

@ -19,6 +19,7 @@ namespace BilibiliEvolved.Build.Watcher
private Dictionary<string, TaskCompletionSource<string>> waitRequests = new Dictionary<string, TaskCompletionSource<string>>();
public Task<string> WaitForBuild(string path) {
var tcs = new TaskCompletionSource<string>();
// Console.WriteLine($"add request: {path}");
waitRequests[path] = tcs;
return tcs.Task;
}
@ -28,9 +29,10 @@ namespace BilibiliEvolved.Build.Watcher
if (File.Exists(originalFilename)) {
File.Delete(originalFilename);
}
if (waitRequests.ContainsKey(e.FullPath)) {
waitRequests[e.FullPath].SetResult(File.ReadAllText(e.FullPath));
waitRequests.Remove(e.FullPath);
// Console.WriteLine($"test filename: {originalFilename}");
if (waitRequests.ContainsKey(originalFilename)) {
waitRequests[originalFilename].SetResult(File.ReadAllText(e.FullPath));
waitRequests.Remove(originalFilename);
}
}
}
@ -40,7 +42,10 @@ namespace BilibiliEvolved.Build.Watcher
public VueWatcher() : base($"src{Path.DirectorySeparatorChar}")
{
GenericFilter = "*.vue";
}
public override void Start(ProjectBuilder builder) {
tsWatcher.Start(builder);
base.Start(builder);
}
public override void Stop() {
base.Stop();
@ -58,6 +63,7 @@ namespace BilibiliEvolved.Build.Watcher
var minFile = $"min{Path.DirectorySeparatorChar + Path.GetFileName(e.FullPath)}.min.js";
var minifier = new JavascriptMinifier();
File.WriteAllText(minFile, minifier.Minify(compiledText.ToString()));
builder.UpdateCachedMinFile(minFile);
cache.AddCache(e.FullPath);
cache.SaveCache();
}
@ -118,8 +124,8 @@ namespace BilibiliEvolved.Build.Watcher
else if (vueFile.ScriptLang == "ts" || vueFile.ScriptLang == "typescript")
{
var tsFile = path + ".ts";
var task = tsWatcher.WaitForBuild(tsFile);
File.WriteAllText(tsFile, vueFile.Script);
var task = tsWatcher.WaitForBuild(path);
var script = task.Result.Replace("export default ", "return {export:Object.assign({template},").Trim().TrimEnd(';');
compiledText.Append($"{script})}}");
}

View File

@ -85,15 +85,17 @@ namespace BilibiliEvolved.Build.Watcher
{
StopWatching();
};
while (input.ToLowerInvariant() != "q")
while (input != null && input.ToLowerInvariant() != "q")
{
input = Console.ReadLine();
}
StopWatching();
if (input != null) {
StopWatching();
}
}
private void StopWatching()
{
watchers.ForEach(w => w.Stop());
watchers.Where(w => w.IsWatching).ForEach(w => w.Stop());
builder.WriteInfo("Watcher stopped.");
Environment.Exit(0);
}

View File

@ -92,13 +92,16 @@ namespace BilibiliEvolved.Build.Watcher
// throw new ArgumentException($"The path is not relative to cwd. {nameof(fullPath)}={fullPath}, {nameof(cwd)}={cwd}", nameof(fullPath));
// }
// }
public void Start(ProjectBuilder builder)
public virtual void Start(ProjectBuilder builder)
{
if (IsWatching)
{
return;
}
IsWatching = true;
if (builder is null) {
throw new NullReferenceException("Project Builder cannot be null");
}
this.builder = builder;
if (watcher == null)
{
@ -107,7 +110,7 @@ namespace BilibiliEvolved.Build.Watcher
{
builder.GetBundleFiles();
HandleFileChange(e);
builder.UpdateCachedMinFile(ResourceMinifier.GetMinimizedFileName(e.FullPath));
// builder.UpdateCachedMinFile(ResourceMinifier.GetMinimizedFileName(e.FullPath));
ChangedFilesHistory.Add(e.FullPath);
}, HandleFileChangesPeriod);
FileSystemEventHandler handler = (s, e) =>

Binary file not shown.