Skip to content

Commit

Permalink
Auto clear JS module cache on .cjs file change to streamline debuggin…
Browse files Browse the repository at this point in the history
…g process.
  • Loading branch information
yuatpocketgems committed Dec 14, 2020
1 parent a796d3a commit 79a66a3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions unity/Assets/Puerts/Src/Editor/CJSImporter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.IO;
using UnityEditor.Experimental.AssetImporters;
using UnityEngine;

using Puerts;

[ScriptedImporter(1, "cjs")]
public class CJSImporter : ScriptedImporter
Expand All @@ -10,5 +12,7 @@ public override void OnImportAsset(AssetImportContext ctx)
TextAsset subAsset = new TextAsset( File.ReadAllText( ctx.assetPath ) );
ctx.AddObjectToAsset("text", subAsset);
ctx.SetMainObject( subAsset );

JsEnv.ClearAllModuleCaches();
}
}
17 changes: 16 additions & 1 deletion unity/Assets/Puerts/Src/JsEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,21 @@ public TResult Eval<TResult>(string chunk, string chunkName = "chunk")
#endif
}

public void ClearModuleCache ()
{
Eval("global.clearModuleCache()");
}

public static void ClearAllModuleCaches () {
lock (jsEnvs)
{
foreach (JsEnv jsEnv in jsEnvs)
{
jsEnv.ClearModuleCache();
}
}
}

public void AddLazyStaticWrapLoader(Type type, Func<TypeRegisterInfo> lazyStaticWrapLoader)
{
#if THREAD_SAFE
Expand Down Expand Up @@ -597,4 +612,4 @@ internal void ReleasePendingJSFunctions()
}
}
}
}
}

0 comments on commit 79a66a3

Please sign in to comment.