pundle-loader-js
Version:
Javascript loader for Pundle
114 lines (94 loc) • 4.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
exports.getName = getName;
exports.getParsedReplacement = getParsedReplacement;
exports.processEnsure = processEnsure;
exports.processImport = processImport;
exports.incrementSourceMapLines = incrementSourceMapLines;
var _babelTypes = require('babel-types');
var t = _interopRequireWildcard(_babelTypes);
var _sourceMap = require('source-map');
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function getName(obj) {
if (typeof obj.name === 'string') {
return obj.name;
}
var chunks = [];
if (_typeof(obj.object) === 'object') {
chunks.push(getName(obj.object));
}
if (_typeof(obj.property) === 'object') {
chunks.push(getName(obj.property));
}
return chunks.join('.');
}
var STRING_REGEX = /^"[^"]*"$/;
function getParsedReplacement(rawValue) {
var parsedValue = void 0;
if (STRING_REGEX.test(rawValue)) {
parsedValue = t.stringLiteral(JSON.parse(rawValue));
} else if (typeof rawValue === 'number') {
parsedValue = t.numericLiteral(rawValue);
} else {
parsedValue = t.identifier(rawValue);
}
return parsedValue;
}
function processEnsure(context, file, chunks, path) {
var _path$node$arguments = _slicedToArray(path.node.arguments, 3),
nodeEntry = _path$node$arguments[0],
nodeCallback = _path$node$arguments[1],
nodeName = _path$node$arguments[2];
var chunk = context.getChunk(null, nodeName && nodeName.type === 'StringLiteral' ? nodeName.value : null);
nodeEntry.elements.forEach(function (element) {
chunk.addImport(context.getImportRequest(element.value, file.filePath));
});
if (nodeCallback && nodeCallback.params.length) {
var nodeCallbackParam = nodeCallback.params[0];
path.scope.traverse(nodeCallback, {
CallExpression: function CallExpression(_ref) {
var node = _ref.node,
scope = _ref.scope;
if (getName(node.callee) === nodeCallbackParam.name && !scope.getBinding(nodeCallbackParam.name)) {
var argument = node.arguments[0];
var request = context.getImportRequest(argument.value, file.filePath);
chunk.addImport(request);
node.arguments[0].value = request.id.toString();
}
}
});
}
path.node.arguments[0] = t.stringLiteral(chunk.getId().toString());
path.node.arguments[2] = nodeCallback;
chunks.push(chunk);
}
function processImport(context, file, chunks, path) {
var chunk = context.getChunk();
var argument = path.node.arguments[0];
if (!argument || argument.type !== 'StringLiteral') {
return;
}
var importRequest = context.getImportRequest(argument.value, file.filePath);
chunk.addImport(importRequest);
path.replaceWith(t.callExpression(t.identifier('require.import'), [t.stringLiteral(chunk.getId().toString()), t.stringLiteral(importRequest.id.toString())]));
chunks.push(chunk);
}
function incrementSourceMapLines(sourceMap, filePath, contents, linesToIncrement) {
var newMap = new _sourceMap.SourceMapGenerator();
var entryMap = new _sourceMap.SourceMapConsumer(sourceMap);
for (var i = 0, length = entryMap._generatedMappings.length; i < length; i++) {
var mapping = entryMap._generatedMappings[i];
if (mapping.source === null) continue;
newMap.addMapping({
source: filePath,
original: { line: mapping.originalLine, column: mapping.originalColumn },
generated: { line: mapping.generatedLine + linesToIncrement, column: mapping.generatedColumn }
});
}
newMap.setSourceContent(filePath, contents);
return newMap.toJSON();
}