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 ConcurrentDictionary<string, string> CachedMinFiles { get; private set; } = new ConcurrentDictionary<string, string>();
public void UpdateCachedMinFile(string url) 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); CachedMinFiles[url] = File.ReadAllText(url);
} }
} }

View File

@ -33,7 +33,8 @@ namespace BilibiliEvolved.Build.Watcher
return !cache.Contains(originalFile); return !cache.Contains(originalFile);
}; };
} }
private void KillExternalWatcher() { private void KillExternalWatcher()
{
if (!externalWatcherProcess.HasExited) if (!externalWatcherProcess.HasExited)
{ {
externalWatcherProcess.Kill(); externalWatcherProcess.Kill();
@ -58,7 +59,9 @@ namespace BilibiliEvolved.Build.Watcher
cache.SaveCache(); cache.SaveCache();
if (!Path.GetFileNameWithoutExtension(e.FullPath).EndsWith(".vue")) 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")) if (e.Name.Contains("dark-slice"))
{ {
builder.BuildDarkStyles(); builder.BuildDarkStyles();

View File

@ -23,7 +23,9 @@ namespace BilibiliEvolved.Build.Watcher
builder.WriteInfo($"[{Name}] {e.Name} changed."); builder.WriteInfo($"[{Name}] {e.Name} changed.");
cache.AddCache(e.FullPath); cache.AddCache(e.FullPath);
cache.SaveCache(); 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")) { if (e.Name.Contains("dark-slice")) {
builder.BuildDarkStyles(); builder.BuildDarkStyles();
} }

View File

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

View File

@ -85,15 +85,17 @@ namespace BilibiliEvolved.Build.Watcher
{ {
StopWatching(); StopWatching();
}; };
while (input.ToLowerInvariant() != "q") while (input != null && input.ToLowerInvariant() != "q")
{ {
input = Console.ReadLine(); input = Console.ReadLine();
} }
if (input != null) {
StopWatching(); StopWatching();
} }
}
private void StopWatching() private void StopWatching()
{ {
watchers.ForEach(w => w.Stop()); watchers.Where(w => w.IsWatching).ForEach(w => w.Stop());
builder.WriteInfo("Watcher stopped."); builder.WriteInfo("Watcher stopped.");
Environment.Exit(0); 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)); // 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) if (IsWatching)
{ {
return; return;
} }
IsWatching = true; IsWatching = true;
if (builder is null) {
throw new NullReferenceException("Project Builder cannot be null");
}
this.builder = builder; this.builder = builder;
if (watcher == null) if (watcher == null)
{ {
@ -107,7 +110,7 @@ namespace BilibiliEvolved.Build.Watcher
{ {
builder.GetBundleFiles(); builder.GetBundleFiles();
HandleFileChange(e); HandleFileChange(e);
builder.UpdateCachedMinFile(ResourceMinifier.GetMinimizedFileName(e.FullPath)); // builder.UpdateCachedMinFile(ResourceMinifier.GetMinimizedFileName(e.FullPath));
ChangedFilesHistory.Add(e.FullPath); ChangedFilesHistory.Add(e.FullPath);
}, HandleFileChangesPeriod); }, HandleFileChangesPeriod);
FileSystemEventHandler handler = (s, e) => FileSystemEventHandler handler = (s, e) =>

Binary file not shown.