Skip to content
This repository was archived by the owner on Oct 1, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions src/compile-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,13 @@ export default class CompileCache {
*
* @param {Function} fetcher A method which conforms to the description above.
*
* @param {string} appRoot (optional)The top-level directory for your application to consturct hierarchy for map files.
* If not specified, all maps are stored as flattened under given sourcemap path.
*
* @return {Promise<Object>} An Object which has the same fields as the
* {@link get} method return result.
*/
async getOrFetch(filePath, fetcher) {
async getOrFetch(filePath, fetcher, appRoot = null) {
let cacheResult = await this.get(filePath);
let anyDependenciesChanged = await this.haveAnyDependentFilesChanged(cacheResult);

Expand All @@ -209,7 +212,7 @@ export default class CompileCache {
const map = result.sourceMaps;
if (map) {
d(`source map for ${filePath} found, saving it to ${this.getSourceMapPath()}`);
await this.saveSourceMap(cacheResult.hashInfo, filePath, map);
await this.saveSourceMap(cacheResult.hashInfo, filePath, appRoot, map);
}
}

Expand Down Expand Up @@ -295,7 +298,7 @@ export default class CompileCache {
fs.writeFileSync(target, buf);
}

getOrFetchSync(filePath, fetcher) {
getOrFetchSync(filePath, fetcher, appRoot = null) {
let cacheResult = this.getSync(filePath);
if (cacheResult.code || cacheResult.binaryData) return cacheResult;

Expand All @@ -309,18 +312,19 @@ export default class CompileCache {
const map = result.sourceMaps;
if (map) {
d(`source map for ${filePath} found, saving it to ${this.getSourceMapPath()}`);
this.saveSourceMapSync(cacheResult.hashInfo, filePath, map);
this.saveSourceMapSync(cacheResult.hashInfo, filePath, appRoot, map);
}

result.hashInfo = cacheResult.hashInfo;
return result;
}

buildSourceMapTarget(hashInfo, filePath) {
const fileName = path.basename(filePath);
const mapFileName = fileName.replace(path.extname(fileName), '.js.map');

buildSourceMapTarget(hashInfo, filePath, appRoot) {
const mapPath = appRoot ? path.relative(appRoot, filePath) : filePath;
const mapFileName = mapPath.replace(path.extname(filePath), '.js.map');
const target = path.join(this.getSourceMapPath(), mapFileName);

mkdirp.sync(path.dirname(target));
d(`Sourcemap target is: ${target}`);

return target;
Expand All @@ -332,18 +336,21 @@ export default class CompileCache {
* @param {Object} hashInfo The hash information returned from getHashForPath
*
* @param {string} filePath Path to original file to construct sourcemap file name

*
* @param {string} appRoot The top-level directory for your application to consturct hierarchy for map files.
* If not specified, all maps are stored as flattened under given sourcemap path.
*
* @param {string} sourceMap Sourcemap data as string
*
* @memberOf CompileCache
*/
async saveSourceMap(hashInfo, filePath, sourceMap) {
const target = this.buildSourceMapTarget(hashInfo, filePath);
async saveSourceMap(hashInfo, filePath, appRoot, sourceMap) {
const target = this.buildSourceMapTarget(hashInfo, filePath, appRoot);
await pfs.writeFile(target, sourceMap, 'utf-8');
}

saveSourceMapSync(hashInfo, filePath, sourceMap) {
const target = this.buildSourceMapTarget(hashInfo, filePath);
saveSourceMapSync(hashInfo, filePath, appRoot, sourceMap) {
const target = this.buildSourceMapTarget(hashInfo, filePath, appRoot);
fs.writeFileSync(target, sourceMap, 'utf-8');
}

Expand Down
17 changes: 11 additions & 6 deletions src/compiler-host.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,13 @@ export default class CompilerHost {
* alternate fallback is the compiler for
* 'text/plain', which is guaranteed to be
* present.
* @param {string} sourceMapPath (optional) The directory to store sourcemap separately
* if compiler option enabled to emit.
* Default to cachePath if not specified.
*
* @return {Promise<CompilerHost>} A read-only CompilerHost
*/
static async createFromConfiguration(rootCacheDir, appRoot, compilersByMimeType, fallbackCompiler=null) {
static async createFromConfiguration(rootCacheDir, appRoot, compilersByMimeType, fallbackCompiler = null, sourceMapPath = null) {
let target = path.join(rootCacheDir, 'compiler-info.json.gz');
let buf = await pfs.readFile(target);
let info = JSON.parse(await pzlib.gunzip(buf));
Expand All @@ -164,7 +167,7 @@ export default class CompilerHost {
compilersByMimeType[x].compilerOptions = cur.compilerOptions;
});

return new CompilerHost(rootCacheDir, compilersByMimeType, fileChangeCache, false, fallbackCompiler);
return new CompilerHost(rootCacheDir, compilersByMimeType, fileChangeCache, false, fallbackCompiler, sourceMapPath);
}


Expand Down Expand Up @@ -306,7 +309,8 @@ export default class CompilerHost {
let cache = this.cachesForCompilers.get(compiler);
return await cache.getOrFetch(
filePath,
(filePath, hashInfo) => this.compileUncached(filePath, hashInfo, compiler));
(filePath, hashInfo) => this.compileUncached(filePath, hashInfo, compiler),
this.appRoot);
}

/**
Expand Down Expand Up @@ -421,7 +425,7 @@ export default class CompilerHost {
return new CompilerHost(rootCacheDir, compilers, fileChangeCache, true, fallbackCompiler);
}

static createFromConfigurationSync(rootCacheDir, appRoot, compilersByMimeType, fallbackCompiler=null) {
static createFromConfigurationSync(rootCacheDir, appRoot, compilersByMimeType, fallbackCompiler=null, sourceMapPath = null) {
let target = path.join(rootCacheDir, 'compiler-info.json.gz');
let buf = fs.readFileSync(target);
let info = JSON.parse(zlib.gunzipSync(buf));
Expand All @@ -433,7 +437,7 @@ export default class CompilerHost {
compilersByMimeType[x].compilerOptions = cur.compilerOptions;
});

return new CompilerHost(rootCacheDir, compilersByMimeType, fileChangeCache, false, fallbackCompiler);
return new CompilerHost(rootCacheDir, compilersByMimeType, fileChangeCache, false, fallbackCompiler, sourceMapPath);
}

saveConfigurationSync() {
Expand Down Expand Up @@ -545,7 +549,8 @@ export default class CompilerHost {
let cache = this.cachesForCompilers.get(compiler);
return cache.getOrFetchSync(
filePath,
(filePath, hashInfo) => this.compileUncachedSync(filePath, hashInfo, compiler));
(filePath, hashInfo) => this.compileUncachedSync(filePath, hashInfo, compiler),
this.appRoot);
}

compileUncachedSync(filePath, hashInfo, compiler) {
Expand Down
2 changes: 1 addition & 1 deletion src/config-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export function createCompilerHostFromConfiguration(info) {
compilers[x].compilerOptions = opts;
});

let ret = new CompilerHost(rootCacheDir, compilers, fileChangeCache, false, compilers['text/plain']);
let ret = new CompilerHost(rootCacheDir, compilers, fileChangeCache, false, compilers['text/plain'], sourceMapPath);

// NB: It's super important that we guarantee that the configuration is saved
// out, because we'll need to re-read it in the renderer process
Expand Down