jscc
Version:
Tiny and powerful preprocessor for conditional comments and replacement of compile-time variables in text files
37 lines • 1.52 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
const magic_string_1 = __importDefault(require("magic-string"));
const parseChunks = require("./parse-chunks");
const ParseHelper = require("./parse-helper");
const Parser = require("./parser");
/**
* Parse the received buffer and returns an object with the parsed code and its
* sourceMap, if required by `props` and the buffer has changed.
*
* @param source Source code
* @param props Parsed user options
*/
const parseBuffer = function _parseBuffer(source, props) {
// Add a MagicString instance to the props and create the helpers.
props.magicStr = new magic_string_1.default(source);
const helper = new ParseHelper(source, props);
// Parse the buffer chunk by chunk and get the changed status.
const changes = parseChunks(new Parser(props), source, helper);
// In the result, if the buffer did not change return `source` untouched.
const result = {
code: changes ? props.magicStr.toString() : source,
};
// If required, add the source map, in `null` if there were no changes.
if (props.sourceMap) {
result.map = changes ? props.magicStr.generateMap({
source: props.values._FILE || undefined,
includeContent: props.mapContent,
hires: props.mapHires,
}) : null;
}
return result;
};
module.exports = parseBuffer;
//# sourceMappingURL=parse-buffer.js.map