Skip to content

Commit 59d2055

Browse files
committed
use createCachedData when possible
1 parent 311fa7e commit 59d2055

2 files changed

Lines changed: 41 additions & 17 deletions

File tree

src/main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,8 @@ function getNodeCachedDir() {
268268
}
269269

270270
jsFlags() {
271-
return this.value ? '--nolazy' : undefined;
271+
// return this.value ? '--nolazy' : undefined;
272+
return undefined;
272273
}
273274

274275
ensureExists() {

src/vs/loader.js

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ var AMDLoader;
179179
};
180180
Utilities.forEachProperty = function (obj, callback) {
181181
if (obj) {
182-
var key = undefined;
182+
var key = void 0;
183183
for (key in obj) {
184184
if (obj.hasOwnProperty(key)) {
185185
callback(key, obj[key]);
@@ -327,8 +327,8 @@ var AMDLoader;
327327
return options;
328328
};
329329
ConfigurationOptionsUtil.mergeConfigurationOptions = function (overwrite, base) {
330-
if (overwrite === undefined) { overwrite = null; }
331-
if (base === undefined) { base = null; }
330+
if (overwrite === void 0) { overwrite = null; }
331+
if (base === void 0) { base = null; }
332332
var result = AMDLoader.Utilities.recursiveClone(base || {});
333333
// Merge known properties and overwrite the unknown ones
334334
AMDLoader.Utilities.forEachProperty(overwrite, function (key, value) {
@@ -660,6 +660,7 @@ var AMDLoader;
660660
this._env = env;
661661
this._didInitialize = false;
662662
this._didPatchNodeRequire = false;
663+
this._hasCreateCachedData = false;
663664
}
664665
NodeScriptLoader.prototype._init = function (nodeRequire) {
665666
if (this._didInitialize) {
@@ -671,6 +672,8 @@ var AMDLoader;
671672
this._vm = nodeRequire('vm');
672673
this._path = nodeRequire('path');
673674
this._crypto = nodeRequire('crypto');
675+
// check for `createCachedData`-api
676+
this._hasCreateCachedData = typeof (new this._vm.Script('').createCachedData) === 'function';
674677
};
675678
// patch require-function of nodejs such that we can manually create a script
676679
// from cached data. this is done by overriding the `Module._compile` function
@@ -711,15 +714,15 @@ var AMDLoader;
711714
options.cachedData = that._fs.readFileSync(cachedDataPath);
712715
}
713716
catch (e) {
714-
options.produceCachedData = true;
717+
options.produceCachedData = !that._hasCreateCachedData;
715718
}
716719
var script = new that._vm.Script(wrapper, options);
717720
var compileWrapper = script.runInThisContext(options);
718721
var dirname = that._path.dirname(filename);
719722
var require = makeRequireFunction(this);
720723
var args = [this.exports, require, this, filename, dirname, process, _commonjsGlobal, Buffer];
721724
var result = compileWrapper.apply(this.exports, args);
722-
that._processCachedData(moduleManager, script, cachedDataPath);
725+
that._processCachedData(moduleManager, script, wrapper, cachedDataPath, !options.cachedData);
723726
return result;
724727
};
725728
};
@@ -778,15 +781,15 @@ var AMDLoader;
778781
}
779782
else {
780783
var cachedDataPath_1 = _this._getCachedDataPath(opts.nodeCachedData.seed, opts.nodeCachedData.path, scriptSrc);
781-
_this._fs.readFile(cachedDataPath_1, function (err, cachedData) {
784+
_this._fs.readFile(cachedDataPath_1, function (_err, cachedData) {
782785
// create script options
783786
var options = {
784787
filename: vmScriptSrc,
785-
produceCachedData: typeof cachedData === 'undefined',
788+
produceCachedData: !_this._hasCreateCachedData && typeof cachedData === 'undefined',
786789
cachedData: cachedData
787790
};
788791
var script = _this._loadAndEvalScript(moduleManager, scriptSrc, vmScriptSrc, contents, options, recorder, callback, errorback);
789-
_this._processCachedData(moduleManager, script, cachedDataPath_1);
792+
_this._processCachedData(moduleManager, script, contents, cachedDataPath_1, !options.cachedData);
790793
});
791794
}
792795
});
@@ -820,7 +823,7 @@ var AMDLoader;
820823
var basename = this._path.basename(filename).replace(/\.js$/, '');
821824
return this._path.join(basedir, basename + "-" + hash + ".code");
822825
};
823-
NodeScriptLoader.prototype._processCachedData = function (moduleManager, script, cachedDataPath) {
826+
NodeScriptLoader.prototype._processCachedData = function (moduleManager, script, contents, cachedDataPath, createCachedData) {
824827
var _this = this;
825828
if (script.cachedDataRejected) {
826829
// data rejected => delete cache file
@@ -838,13 +841,12 @@ var AMDLoader;
838841
});
839842
}
840843
});
841-
}, moduleManager.getConfig().getOptionsLiteral().nodeCachedData.writeDelay);
844+
}, moduleManager.getConfig().getOptionsLiteral().nodeCachedData.writeDelay / 2);
842845
}
843846
else if (script.cachedDataProduced) {
844847
// data produced => tell outside world
845848
moduleManager.getConfig().getOptionsLiteral().nodeCachedData.onData(undefined, {
846-
path: cachedDataPath,
847-
length: script.cachedData.length
849+
path: cachedDataPath
848850
});
849851
// data produced => write cache file
850852
NodeScriptLoader._runSoon(function () {
@@ -859,6 +861,27 @@ var AMDLoader;
859861
});
860862
}, moduleManager.getConfig().getOptionsLiteral().nodeCachedData.writeDelay);
861863
}
864+
else if (this._hasCreateCachedData && createCachedData) {
865+
// NEW world
866+
// data produced => tell outside world
867+
moduleManager.getConfig().getOptionsLiteral().nodeCachedData.onData(undefined, {
868+
path: cachedDataPath
869+
});
870+
// soon'ish create and save cached data
871+
NodeScriptLoader._runSoon(function () {
872+
var data = script.createCachedData(contents);
873+
_this._fs.writeFile(cachedDataPath, data, function (err) {
874+
if (!err) {
875+
return;
876+
}
877+
moduleManager.getConfig().getOptionsLiteral().nodeCachedData.onData({
878+
errorCode: 'writeFile',
879+
path: cachedDataPath,
880+
detail: err
881+
});
882+
});
883+
}, moduleManager.getConfig().getOptionsLiteral().nodeCachedData.writeDelay * 2);
884+
}
862885
};
863886
NodeScriptLoader._runSoon = function (callback, minTimeout) {
864887
var timeout = minTimeout + Math.ceil(Math.random() * minTimeout);
@@ -1067,7 +1090,7 @@ var AMDLoader;
10671090
AMDLoader.PluginDependency = PluginDependency;
10681091
var ModuleManager = (function () {
10691092
function ModuleManager(env, scriptLoader, defineFunc, requireFunc, loaderAvailableTimestamp) {
1070-
if (loaderAvailableTimestamp === undefined) { loaderAvailableTimestamp = 0; }
1093+
if (loaderAvailableTimestamp === void 0) { loaderAvailableTimestamp = 0; }
10711094
this._env = env;
10721095
this._scriptLoader = scriptLoader;
10731096
this._loaderAvailableTimestamp = loaderAvailableTimestamp;
@@ -1186,7 +1209,7 @@ var AMDLoader;
11861209
*/
11871210
ModuleManager.prototype.defineModule = function (strModuleId, dependencies, callback, errorback, stack, moduleIdResolver) {
11881211
var _this = this;
1189-
if (moduleIdResolver === undefined) { moduleIdResolver = new ModuleIdResolver(strModuleId); }
1212+
if (moduleIdResolver === void 0) { moduleIdResolver = new ModuleIdResolver(strModuleId); }
11901213
var moduleId = this._moduleIdProvider.getModuleId(strModuleId);
11911214
if (this._modules2[moduleId]) {
11921215
if (!this._config.isDuplicateMessageIgnoredFor(strModuleId)) {
@@ -1245,7 +1268,7 @@ var AMDLoader;
12451268
* @return The exports of module 'id'
12461269
*/
12471270
ModuleManager.prototype.synchronousRequire = function (_strModuleId, moduleIdResolver) {
1248-
if (moduleIdResolver === undefined) { moduleIdResolver = new ModuleIdResolver(_strModuleId); }
1271+
if (moduleIdResolver === void 0) { moduleIdResolver = new ModuleIdResolver(_strModuleId); }
12491272
var dependency = this._normalizeDependency(_strModuleId, moduleIdResolver);
12501273
var m = this._modules2[dependency.id];
12511274
if (!m) {
@@ -1637,7 +1660,7 @@ var AMDLoader;
16371660
jQuery: true
16381661
};
16391662
var _requireFunc_config = function (params, shouldOverwrite) {
1640-
if (shouldOverwrite === undefined) { shouldOverwrite = false; }
1663+
if (shouldOverwrite === void 0) { shouldOverwrite = false; }
16411664
moduleManager.configure(params, shouldOverwrite);
16421665
};
16431666
var RequireFunc = function () {

0 commit comments

Comments
 (0)