Skip to content

Commit e033f1e

Browse files
committed
cache parsed source
1 parent 513a1f1 commit e033f1e

2 files changed

Lines changed: 26 additions & 16 deletions

File tree

‎lib/buildDeps.js‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,17 @@ function addModule(depTree, context, modu, options, reason, finalCallback) {
127127
options.events.emit("module", modu, filename);
128128

129129
if(options.cache) {
130-
options.cache.get(filenameWithLoaders, function(err, source) {
130+
options.cache.get(filenameWithLoaders, function(err, cachedData) {
131131
if(err) return readFile();
132132
modu.fromCache = true;
133-
processJs(null, source);
133+
cachedData = JSON.parse(cachedData);
134+
processParsedJs(cachedData.source, cachedData.deps);
134135
});
135136
} else
136137
readFile();
137138

138139
// Read the file and process it with loaders
140+
// [this step is cached]
139141
var cacheEntry;
140142
function readFile() {
141143
cacheEntry = options.cache && options.cache.create(filenameWithLoaders);
@@ -153,23 +155,36 @@ function addModule(depTree, context, modu, options, reason, finalCallback) {
153155
});
154156
}
155157

156-
// process the finished javascript code
158+
// process the result delivered from loaders or direct from file
157159
// for inclusion into the result
158160
// (static code analysis for requires and other stuff)
159-
function processJs(err, source) {
161+
// [this step is cached]
162+
function processJs(err, resultBuffers, cacheable) {
160163
if(err) {
161164
callback(err);
162165
return;
163166
}
164-
if(cacheEntry && cacheEntry.saved)
165-
modu.toCache = true;
167+
var source = resultBuffers[0].toString("utf-8")
166168
var deps;
167169
try {
168170
deps = parse(source, options.parse);
169171
} catch(e) {
170172
callback("File \"" + filenameWithLoaders + "\" parsing failed: " + e);
171173
return;
172174
}
175+
176+
if(cacheable && cacheEntry) {
177+
modu.toCache = true;
178+
cacheEntry.save(JSON.stringify({
179+
deps: deps,
180+
source: source
181+
}));
182+
}
183+
return processParsedJs(source, deps);
184+
}
185+
186+
// process the final parsed javascript code
187+
function processParsedJs(source, deps) {
173188
modu.requires = deps.requires || [];
174189
modu.asyncs = deps.asyncs || [];
175190
modu.contexts = deps.contexts || [];

‎lib/execLoaders.js‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,15 @@ var path = require("path");
1313
* @param loaders {string[]} the absolute filenames of the loaders
1414
* @param filenames {string[]} the filenames of "contents"
1515
* @param contents {Buffer[]} read contents
16+
* @param cacheEntry {CacheEntry} the cache entry to add dependencies
1617
* @param options {object} the options of the module system
17-
* @param callback {function} (err, resultingJavascriptCode)
18+
* @param callback {function} (err, arrayOfResultBuffers, allowCaching)
1819
*/
1920
module.exports = function(context, request, loaders, filenames, contents, cacheEntry, options, callback) {
2021
var loaderFunctions, cacheable = true;
2122
if(loaders.length === 0) {
2223
// if no loaders are used, the file content is the resulting code
23-
var result = contents[0].toString("utf-8");
24-
if(cacheEntry)
25-
cacheEntry.save(result);
26-
callback(null, result);
24+
callback(null, contents, true);
2725
} else {
2826
// try to load all loaders
2927
// TODO this doesn't reload the loader if it has changed
@@ -155,7 +153,7 @@ module.exports = function(context, request, loaders, filenames, contents, cacheE
155153
// ups. loader throwed an exeception
156154
if(!done) {
157155
done = true;
158-
callback("Loader throwed exeception: " + (typeof e === "object" && e.stack ? e.stack : e));
156+
callback(new Error("Loader throwed exeception: " + (typeof e === "object" && e.stack ? e.stack : e)));
159157
} else {
160158
// loader is already "done", so we cannot use the callback function
161159
// for better debugging we print the error on the console
@@ -165,10 +163,7 @@ module.exports = function(context, request, loaders, filenames, contents, cacheE
165163
return;
166164
}
167165
} else {
168-
var result = args[0].toString("utf-8");
169-
if(cacheEntry && cacheable)
170-
cacheEntry.save(result);
171-
callback(null, result);
166+
callback(null, args, cacheable);
172167
}
173168
}
174169
}

0 commit comments

Comments
 (0)