/// <summary> /// Transforms a JSX file. Results of the JSX to JavaScript transformation are cached. /// </summary> /// <param name="filename">Name of the file to load</param> /// <param name="useHarmony"><c>true</c> if support for es6 syntax should be rewritten.</param> /// <returns>JavaScript</returns> public string TransformJsxFile(string filename, bool?useHarmony = null) { var fullPath = _fileSystem.MapPath(filename); // 1. Check in-memory cache return(_cache.GetOrInsert( key: string.Format(JSX_CACHE_KEY, filename), slidingExpiration: TimeSpan.FromMinutes(30), cacheDependencyFiles: new[] { fullPath }, getData: () => { // 2. Check on-disk cache var contents = _fileSystem.ReadAsString(filename); var hash = _fileCacheHash.CalculateHash(contents); var cacheFilename = GetJsxOutputPath(filename); if (_fileSystem.FileExists(cacheFilename)) { var cacheContents = _fileSystem.ReadAsString(cacheFilename); if (_fileCacheHash.ValidateHash(cacheContents, hash)) { // Cache is valid :D return cacheContents; } } // 3. Not cached, perform the transformation return TransformJsxWithHeader(contents, hash, useHarmony); } )); }
/// <summary> /// Transforms a JSX file to regular JavaScript and also returns a source map to map the /// compiled source to the original version. Results of the JSX to JavaScript /// transformation are cached. /// </summary> /// <param name="filename">Name of the file to load</param> /// <param name="forceGenerateSourceMap"> /// <c>true</c> to re-transform the file if a cached version with no source map is available /// </param> /// <param name="useHarmony"><c>true</c> if support for ES6 syntax should be enabled</param> /// <param name="stripTypes"> /// Whether Flow types should be stripped out. Defaults to the value set in the site /// configuration. /// </param> /// <returns>JavaScript and source map</returns> public virtual JavaScriptWithSourceMap TransformJsxFileWithSourceMap( string filename, bool forceGenerateSourceMap = false, bool?useHarmony = null, bool?stripTypes = null ) { var cacheKey = string.Format(JSX_CACHE_KEY, filename); // 1. Check in-memory cache. We need to invalidate any in-memory cache if there's no // source map cached and forceGenerateSourceMap is true. var cached = _cache.Get <JavaScriptWithSourceMap>(cacheKey); var cacheIsValid = cached != null && (!forceGenerateSourceMap || cached.SourceMap != null); if (cacheIsValid) { return(cached); } // 2. Check on-disk cache var contents = _fileSystem.ReadAsString(filename); var hash = _fileCacheHash.CalculateHash(contents); var output = LoadJsxFromFileCache(filename, hash, forceGenerateSourceMap); if (output == null) { // 3. Not cached, perform the transformation try { output = TransformJsxWithHeader(filename, contents, hash, useHarmony, stripTypes); } catch (JsxException ex) { // Add the filename to the error message throw new JsxException(string.Format( "In file \"{0}\": {1}", filename, ex.Message ), ex); } } // Cache the result from above (either disk cache or live transformation) to memory var fullPath = _fileSystem.MapPath(filename); _cache.Set( cacheKey, output, slidingExpiration: TimeSpan.FromMinutes(30), cacheDependencyFiles: new[] { fullPath } ); return(output); }