-
-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f8c812
commit 3649b3d
Showing
11 changed files
with
438 additions
and
491 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import os from 'os'; | ||
|
||
import cacache from 'cacache'; | ||
import findCacheDir from 'find-cache-dir'; | ||
import serialize from 'serialize-javascript'; | ||
|
||
export default class Webpack4Cache { | ||
constructor(compilation, options) { | ||
this.options = options; | ||
this.cacheDir = | ||
options.cache === true | ||
? Webpack4Cache.getCacheDirectory() | ||
: options.cache; | ||
} | ||
|
||
static getCacheDirectory() { | ||
return findCacheDir({ name: 'terser-webpack-plugin' }) || os.tmpdir(); | ||
} | ||
|
||
isEnabled() { | ||
return !!this.cacheDir; | ||
} | ||
|
||
get(task) { | ||
// eslint-disable-next-line no-param-reassign | ||
task.cacheIdent = task.cacheIdent || serialize(task.cacheKeys); | ||
|
||
return cacache | ||
.get(this.cacheDir, task.cacheIdent) | ||
.then(({ data }) => JSON.parse(data)); | ||
} | ||
|
||
store(task, data) { | ||
return cacache.put(this.cacheDir, task.cacheIdent, JSON.stringify(data)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import crypto from 'crypto'; | ||
|
||
// eslint-disable-next-line import/extensions,import/no-unresolved | ||
import getLazyHashedEtag from 'webpack/lib/cache/getLazyHashedEtag'; | ||
import serialize from 'serialize-javascript'; | ||
|
||
export default class Cache { | ||
constructor(compilation, options) { | ||
this.options = options; | ||
this.compilation = compilation; | ||
} | ||
|
||
isEnabled() { | ||
return !!this.compilation.cache; | ||
} | ||
|
||
createCacheIdent(task) { | ||
const cacheKeys = crypto | ||
.createHash('md4') | ||
.update(serialize(task.cacheKeys)) | ||
.digest('hex'); | ||
|
||
return `${this.compilation.compilerPath}/TerserWebpackPlugin/${cacheKeys}/${task.file}`; | ||
} | ||
|
||
get(task) { | ||
// eslint-disable-next-line no-param-reassign | ||
task.cacheIdent = task.cacheIdent || this.createCacheIdent(task); | ||
// eslint-disable-next-line no-param-reassign | ||
task.cacheETag = task.cacheETag || getLazyHashedEtag(task.asset); | ||
|
||
return new Promise((resolve, reject) => { | ||
this.compilation.cache.get( | ||
task.cacheIdent, | ||
task.cacheETag, | ||
(err, result) => { | ||
if (err) { | ||
reject(err); | ||
} else if (result) { | ||
resolve(result); | ||
} else { | ||
reject(); | ||
} | ||
} | ||
); | ||
}); | ||
} | ||
|
||
store(task, data) { | ||
return new Promise((resolve, reject) => { | ||
this.compilation.cache.store( | ||
task.cacheIdent, | ||
task.cacheETag, | ||
data, | ||
(err) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(data); | ||
} | ||
} | ||
); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.