smartinject/dist/index.js

80 lines
5.9 KiB
JavaScript
Raw Normal View History

2017-02-28 22:40:13 +00:00
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
require("typings-global");
let Module = require('module');
2017-07-30 13:53:58 +00:00
const path = require("path");
2017-02-28 22:40:13 +00:00
const sourceMap = require("source-map-support");
const through2 = require("through2");
sourceMap.install({
retrieveFile: function (path) {
if (cache[path]) {
return cache[path].contents.toString();
}
2017-07-28 18:44:24 +00:00
else if (cache[path + '.js']) {
return cache[path + '.js'].contents.toString();
}
2017-02-28 22:40:13 +00:00
}
});
let cache = {};
let originalLoader = Module._extensions['.js'];
Module._extensions['.js'] = function (module, filename) {
let file = cache[filename];
2017-07-28 18:44:24 +00:00
let file2 = cache[filename + '.js'];
2017-02-28 22:40:13 +00:00
if (file) {
module._compile(file.contents.toString(), filename);
}
2017-07-28 18:44:24 +00:00
else if (file2) {
module._compile(file2.contents.toString(), filename);
}
2017-02-28 22:40:13 +00:00
else {
originalLoader.apply(this, arguments);
}
};
let originalModuleResolve = Module._resolveFilename;
Module._resolveFilename = function (request, parent, isMain) {
let resolvedRequest;
2017-07-30 15:21:36 +00:00
if (parent && /^\./.test(request)) {
let resolvedDir = path.parse(parent.filename).dir;
resolvedRequest = path.join('/', resolvedDir, request);
}
else {
resolvedRequest = request;
}
2017-07-30 15:21:36 +00:00
// uncomment for testing
/* if (/[yourTestFilenameHere.js]/.test(request)) {
console.log('resolvedRequest:' + resolvedRequest)
console.log(parent)
} */
2017-07-30 13:53:58 +00:00
let file = cache[resolvedRequest];
let file2 = cache[resolvedRequest + '.js'];
2017-07-28 18:44:24 +00:00
if (file || file2) {
2017-07-30 15:44:27 +00:00
return resolvedRequest;
2017-02-28 22:40:13 +00:00
}
else {
return originalModuleResolve.apply(this, arguments);
}
};
exports.gulpPipe = function () {
return through2.obj(function (file, enc, cb) {
cache[file.path] = file;
return cb(null, file);
});
};
exports.injectFileArray = (fileArray) => __awaiter(this, void 0, void 0, function* () {
for (let fileObject of fileArray) {
2017-07-30 15:44:27 +00:00
/* if (/[yourTestFilenameHere.js]/.test(request)) {
// console.log('injected:' + fileObject.path)
} */
2017-02-28 22:40:13 +00:00
cache[fileObject.path] = fileObject;
}
return fileArray;
});
2017-07-30 15:44:27 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7O0FBQUEsMEJBQXVCO0FBRXZCLElBQUksTUFBTSxHQUFHLE9BQU8sQ0FBQyxRQUFRLENBQUMsQ0FBQTtBQUM5Qiw2QkFBNkI7QUFDN0IsZ0RBQWdEO0FBQ2hELHFDQUFxQztBQUtyQyxTQUFTLENBQUMsT0FBTyxDQUFDO0lBQ2hCLFlBQVksRUFBRSxVQUFVLElBQUk7UUFDMUIsRUFBRSxDQUFDLENBQUMsS0FBSyxDQUFDLElBQUksQ0FBQyxDQUFDLENBQUMsQ0FBQztZQUNoQixNQUFNLENBQUMsS0FBSyxDQUFDLElBQUksQ0FBQyxDQUFDLFFBQVEsQ0FBQyxRQUFRLEVBQUUsQ0FBQTtRQUN4QyxDQUFDO1FBQUMsSUFBSSxDQUFDLEVBQUUsQ0FBQyxDQUFDLEtBQUssQ0FBQyxJQUFJLEdBQUcsS0FBSyxDQUFDLENBQUMsQ0FBQyxDQUFDO1lBQy9CLE1BQU0sQ0FBQyxLQUFLLENBQUMsS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