private Engine SetupEngine(ResourceCollection resourceCollection, TemplatePreprocessorResource scriptResource, DocumentBuildContext context)
        {
            var rootPath    = (RelativePath)scriptResource.ResourceName;
            var engineCache = new Dictionary <string, Engine>();

            var utility = new TemplateUtility(context);

            _utilityObject = new
            {
                resolveSourceRelativePath = new Func <string, string, string>(utility.ResolveSourceRelativePath)
            };

            var engine = CreateDefaultEngine();

            var requireAction = new Func <string, object>(
                s =>
            {
                if (!s.StartsWith(RequireRelativePathPrefix))
                {
                    throw new ArgumentException($"Only relative path starting with `{RequireRelativePathPrefix}` is supported in require");
                }
                var relativePath = (RelativePath)s.Substring(RequireRelativePathPrefix.Length);
                s = relativePath.BasedOn(rootPath);

                var script = resourceCollection?.GetResource(s);
                if (string.IsNullOrWhiteSpace(script))
                {
                    return(null);
                }

                Engine cachedEngine;
                if (!engineCache.TryGetValue(s, out cachedEngine))
                {
                    cachedEngine   = CreateEngine(engine, RequireFuncVariableName);
                    engineCache[s] = cachedEngine;
                    cachedEngine.Execute(script);
                }

                return(cachedEngine.GetValue(ExportsVariableName));
            });

            engine.SetValue(RequireFuncVariableName, requireAction);
            engineCache[rootPath] = engine;
            engine.Execute(scriptResource.Content);

            var value = engine.GetValue(ExportsVariableName);

            if (value.IsObject())
            {
                var exports = value.AsObject();
                GetOptionsFunc     = GetFunc(GetOptionsFuncVariableName, exports);
                TransformModelFunc = GetFunc(TransformFuncVariableName, exports);
            }
            else
            {
                throw new InvalidPreprocessorException("Invalid 'exports' variable definition. 'exports' MUST be an object.");
            }

            return(engine);
        }
        private Engine SetupEngine(ResourceCollection resourceCollection, TemplatePreprocessorResource scriptResource, DocumentBuildContext context)
        {
            var rootPath = (TypeForwardedToRelativePath)scriptResource.ResourceName;
            var engineCache = new Dictionary<string, Engine>();

            var utility = new TemplateUtility(context);
            _utilityObject = new
            {
                resolveSourceRelativePath = new Func<string, string, string>(utility.ResolveSourceRelativePath)
            };

            var engine = CreateDefaultEngine();

            var requireAction = new Func<string, object>(
                s =>
                {
                    if (!s.StartsWith(RequireRelativePathPrefix))
                    {
                        throw new ArgumentException($"Only relative path starting with `{RequireRelativePathPrefix}` is supported in require");
                    }
                    var relativePath = (TypeForwardedToRelativePath)s.Substring(RequireRelativePathPrefix.Length);
                    s = relativePath.BasedOn(rootPath);

                    var script = resourceCollection?.GetResource(s);
                    if (string.IsNullOrWhiteSpace(script))
                    {
                        return null;
                    }

                    Engine cachedEngine;
                    if (!engineCache.TryGetValue(s, out cachedEngine))
                    {
                        cachedEngine = CreateEngine(engine, RequireFuncVariableName);
                        engineCache[s] = cachedEngine;
                        cachedEngine.Execute(script);
                    }

                    return cachedEngine.GetValue(ExportsVariableName);
                });

            engine.SetValue(RequireFuncVariableName, requireAction);
            engineCache[rootPath] = engine;
            engine.Execute(scriptResource.Content);

            var value = engine.GetValue(ExportsVariableName);
            if (value.IsObject())
            {
                var exports = value.AsObject();
                GetOptionsFunc = GetFunc(GetOptionsFuncVariableName, exports);
                TransformModelFunc = GetFunc(TransformFuncVariableName, exports);
            }
            else
            {
                throw new InvalidPreprocessorException("Invalid 'exports' variable definition. 'exports' MUST be an object.");
            }

            return engine;
        }