mirror of https://github.com/webpack/webpack.git
refactor cache to be more generic and usable for other things
This commit is contained in:
parent
c453f395d1
commit
9b8d26f6e8
26
lib/Cache.js
26
lib/Cache.js
|
|
@ -13,14 +13,10 @@ const { AsyncParallelHook, AsyncSeriesBailHook, SyncHook } = require("tapable");
|
|||
class Cache {
|
||||
constructor() {
|
||||
this.hooks = {
|
||||
/** @type {AsyncSeriesBailHook<string>} */
|
||||
getModule: new AsyncSeriesBailHook(["identifier"]),
|
||||
/** @type {AsyncParallelHook<string, Module>} */
|
||||
storeModule: new AsyncParallelHook(["identifier", "module"]),
|
||||
/** @type {AsyncSeriesBailHook<string, string>} */
|
||||
getAsset: new AsyncSeriesBailHook(["identifier", "hash"]),
|
||||
/** @type {AsyncParallelHook<string, string, Source>} */
|
||||
storeAsset: new AsyncParallelHook(["identifier", "hash", "source"]),
|
||||
get: new AsyncSeriesBailHook(["identifier", "etag"]),
|
||||
/** @type {AsyncParallelHook<string, string, any>} */
|
||||
store: new AsyncParallelHook(["identifier", "etag", "data"]),
|
||||
/** @type {SyncHook} */
|
||||
beginIdle: new SyncHook([]),
|
||||
/** @type {AsyncParallelHook} */
|
||||
|
|
@ -30,20 +26,12 @@ class Cache {
|
|||
};
|
||||
}
|
||||
|
||||
getModule(identifier, callback) {
|
||||
this.hooks.getModule.callAsync(identifier, callback);
|
||||
get(identifier, etag, callback) {
|
||||
this.hooks.get.callAsync(identifier, etag, callback);
|
||||
}
|
||||
|
||||
storeModule(identifier, module, callback) {
|
||||
this.hooks.storeModule.callAsync(identifier, module, callback);
|
||||
}
|
||||
|
||||
getAsset(identifier, hash, callback) {
|
||||
this.hooks.getAsset.callAsync(identifier, hash, callback);
|
||||
}
|
||||
|
||||
storeAsset(identifier, hash, source, callback) {
|
||||
this.hooks.storeAsset.callAsync(identifier, hash, source, callback);
|
||||
store(identifier, etag, source, callback) {
|
||||
this.hooks.store.callAsync(identifier, etag, source, callback);
|
||||
}
|
||||
|
||||
beginIdle() {
|
||||
|
|
|
|||
|
|
@ -499,8 +499,8 @@ class Compilation {
|
|||
return callback(null, alreadyAddedModule);
|
||||
}
|
||||
|
||||
const cacheName = this.compilerPath + "/" + identifier;
|
||||
this.cache.getModule(cacheName, (err, cacheModule) => {
|
||||
const cacheName = `${this.compilerPath}/module/${identifier}`;
|
||||
this.cache.get(cacheName, null, (err, cacheModule) => {
|
||||
if (err) return callback(err);
|
||||
|
||||
if (cacheModule) {
|
||||
|
|
@ -598,8 +598,9 @@ class Compilation {
|
|||
if (currentProfile !== undefined) {
|
||||
currentProfile.markStoringStart();
|
||||
}
|
||||
this.cache.storeModule(
|
||||
this.compilerPath + "/" + module.identifier(),
|
||||
this.cache.store(
|
||||
`${this.compilerPath}/module/${module.identifier()}`,
|
||||
null,
|
||||
module,
|
||||
err => {
|
||||
if (currentProfile !== undefined) {
|
||||
|
|
@ -2066,10 +2067,11 @@ class Compilation {
|
|||
// TODO Workaround for https://github.com/suguru03/neo-async/issues/63
|
||||
const callback = err => process.nextTick(() => _callback(err));
|
||||
|
||||
const cacheName = this.compilerPath + "/" + fileManifest.identifier;
|
||||
const ident = fileManifest.identifier;
|
||||
const cacheName = `${this.compilerPath}/asset/${ident}`;
|
||||
const usedHash = fileManifest.hash;
|
||||
|
||||
this.cache.getAsset(cacheName, usedHash, (err, sourceFromCache) => {
|
||||
this.cache.get(cacheName, usedHash, (err, sourceFromCache) => {
|
||||
let filenameTemplate, file;
|
||||
try {
|
||||
filenameTemplate = fileManifest.filenameTemplate;
|
||||
|
|
@ -2130,7 +2132,7 @@ class Compilation {
|
|||
source,
|
||||
chunk
|
||||
});
|
||||
this.cache.storeAsset(cacheName, usedHash, source, callback);
|
||||
this.cache.store(cacheName, usedHash, source, callback);
|
||||
} catch (err) {
|
||||
this.errors.push(
|
||||
new ChunkRenderError(chunk, file || filenameTemplate, err)
|
||||
|
|
|
|||
|
|
@ -15,34 +15,20 @@ class MemoryCachePlugin {
|
|||
* @returns {void}
|
||||
*/
|
||||
apply(compiler) {
|
||||
/** @type {Map<string, Module>} */
|
||||
const moduleCache = new Map();
|
||||
/** @type {Map<string, { hash: string, source: Source }>} */
|
||||
const assetCache = new Map();
|
||||
compiler.cache.hooks.storeModule.tap(
|
||||
/** @type {Map<string, { etag: string, data: any }>} */
|
||||
const cache = new Map();
|
||||
compiler.cache.hooks.store.tap(
|
||||
"MemoryCachePlugin",
|
||||
(identifier, module) => {
|
||||
moduleCache.set(identifier, module);
|
||||
(identifier, etag, data) => {
|
||||
cache.set(identifier, { etag, data });
|
||||
}
|
||||
);
|
||||
compiler.cache.hooks.getModule.tap("MemoryCachePlugin", identifier => {
|
||||
return moduleCache.get(identifier);
|
||||
compiler.cache.hooks.get.tap("MemoryCachePlugin", (identifier, etag) => {
|
||||
const cacheEntry = cache.get(identifier);
|
||||
if (cacheEntry !== undefined && cacheEntry.etag === etag) {
|
||||
return cacheEntry.data;
|
||||
}
|
||||
});
|
||||
compiler.cache.hooks.storeAsset.tap(
|
||||
"MemoryCachePlugin",
|
||||
(identifier, hash, source) => {
|
||||
assetCache.set(identifier, { hash, source });
|
||||
}
|
||||
);
|
||||
compiler.cache.hooks.getAsset.tap(
|
||||
"MemoryCachePlugin",
|
||||
(identifier, hash) => {
|
||||
const cacheEntry = assetCache.get(identifier);
|
||||
if (cacheEntry !== undefined && cacheEntry.hash === hash) {
|
||||
return cacheEntry.source;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
module.exports = MemoryCachePlugin;
|
||||
|
|
|
|||
Loading…
Reference in New Issue