@@ -4,6 +4,10 @@ const path = require('path');
4
4
const mkdirp = require ( 'mkdirp' ) ;
5
5
const async = require ( 'async' ) ;
6
6
const loaderUtils = require ( 'loader-utils' ) ;
7
+ const pkgVersion = require ( '../package.json' ) . version ;
8
+
9
+ const defaultCacheDirectory = path . resolve ( '.cache-loader' ) ;
10
+ const ENV = process . env . NODE_ENV || 'development' ;
7
11
8
12
function loader ( ...args ) {
9
13
const callback = this . async ( ) ;
@@ -34,6 +38,7 @@ function loader(...args) {
34
38
const writeCacheFile = ( ) => {
35
39
fs . writeFile ( data . cacheFile , JSON . stringify ( {
36
40
remainingRequest : data . remainingRequest ,
41
+ cacheIdentifier : data . cacheIdentifier ,
37
42
dependencies : deps ,
38
43
contextDependencies : contextDeps ,
39
44
result : args ,
@@ -58,13 +63,19 @@ function loader(...args) {
58
63
}
59
64
60
65
function pitch ( remainingRequest , prevRequest , dataInput ) {
61
- const options = loaderUtils . getOptions ( this ) || { } ;
62
- const cacheDirectory = options . cacheDirectory || path . resolve ( '.cache-loader' ) ;
66
+ const loaderOptions = loaderUtils . getOptions ( this ) || { } ;
67
+ const defaultOptions = {
68
+ cacheDirectory : defaultCacheDirectory ,
69
+ cacheIdentifier : `cache-loader:${ pkgVersion } ${ ENV } ` ,
70
+ } ;
71
+ const options = Object . assign ( { } , defaultOptions , loaderOptions ) ;
72
+ const { cacheIdentifier, cacheDirectory } = options ;
63
73
const data = dataInput ;
64
- data . remainingRequest = remainingRequest ;
65
74
const callback = this . async ( ) ;
66
- const hash = digest ( remainingRequest ) ;
75
+ const hash = digest ( ` ${ cacheIdentifier } \n ${ remainingRequest } ` ) ;
67
76
const cacheFile = path . join ( cacheDirectory , `${ hash } .json` ) ;
77
+ data . remainingRequest = remainingRequest ;
78
+ data . cacheIdentifier = cacheIdentifier ;
68
79
data . cacheFile = cacheFile ;
69
80
fs . readFile ( cacheFile , 'utf-8' , ( readFileErr , content ) => {
70
81
if ( readFileErr ) {
@@ -79,7 +90,7 @@ function pitch(remainingRequest, prevRequest, dataInput) {
79
90
callback ( ) ;
80
91
return ;
81
92
}
82
- if ( cacheData . remainingRequest !== remainingRequest ) {
93
+ if ( cacheData . remainingRequest !== remainingRequest || cacheData . cacheIdentifier !== cacheIdentifier ) {
83
94
// in case of a hash conflict
84
95
callback ( ) ;
85
96
return ;
0 commit comments