webpack-sources
Version:
Source code handling classes for webpack
56 lines (50 loc) • 1.66 kB
JavaScript
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
;
const getGeneratedSourceInfo = require("./getGeneratedSourceInfo");
const splitIntoLines = require("./splitIntoLines");
/** @typedef {import("./getGeneratedSourceInfo").GeneratedSourceInfo} GeneratedSourceInfo */
/** @typedef {import("./streamChunks").OnChunk} OnChunk */
/** @typedef {import("./streamChunks").OnName} OnName */
/** @typedef {import("./streamChunks").OnSource} OnSource */
/**
* @param {string} source source
* @param {OnChunk} onChunk on chunk
* @param {OnSource} onSource on source
* @param {OnName} onName on name
* @returns {GeneratedSourceInfo} source info
*/
const streamChunksOfRawSource = (source, onChunk, onSource, onName) => {
let line = 1;
const matches = splitIntoLines(source);
/** @type {undefined | string} */
let match;
for (match of matches) {
onChunk(match, line, 0, -1, -1, -1, -1);
line++;
}
return matches.length === 0 || /** @type {string} */ (match).endsWith("\n")
? {
generatedLine: matches.length + 1,
generatedColumn: 0
}
: {
generatedLine: matches.length,
generatedColumn: /** @type {string} */ (match).length
};
};
/**
* @param {string} source source
* @param {OnChunk} onChunk on chunk
* @param {OnSource} onSource on source
* @param {OnName} onName on name
* @param {boolean} finalSource is final source
* @returns {GeneratedSourceInfo} source info
*/
module.exports = (source, onChunk, onSource, onName, finalSource) => {
return finalSource
? getGeneratedSourceInfo(source)
: streamChunksOfRawSource(source, onChunk, onSource, onName);
};