@sentry/nextjs
Version:
Official Sentry SDK for Next.js
108 lines (105 loc) • 3.28 kB
JavaScript
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
function findInjectionIndexAfterDirectives(userCode) {
let index = 0;
let afterLastDirective;
while (index < userCode.length) {
const char = userCode[index];
if (char && /\s/.test(char)) {
index++;
continue;
}
if (userCode.startsWith("//", index)) {
const newlineIndex = userCode.indexOf("\n", index + 2);
index = newlineIndex === -1 ? userCode.length : newlineIndex + 1;
continue;
}
if (userCode.startsWith("/*", index)) {
const commentEndIndex = userCode.indexOf("*/", index + 2);
if (commentEndIndex === -1) {
return afterLastDirective ?? 0;
}
index = commentEndIndex + 2;
continue;
}
if (char === '"' || char === "'") {
const stringEnd = findStringLiteralEnd(userCode, index);
if (stringEnd === null) {
return afterLastDirective ?? index;
}
const terminatorEnd = findDirectiveTerminator(userCode, stringEnd);
if (terminatorEnd === null) {
return afterLastDirective ?? index;
}
afterLastDirective = terminatorEnd;
index = terminatorEnd;
continue;
}
return afterLastDirective ?? index;
}
return afterLastDirective ?? index;
}
function findStringLiteralEnd(userCode, startIndex) {
const quote = userCode[startIndex];
let index = startIndex + 1;
while (index < userCode.length) {
const char = userCode[index];
if (char === "\\") {
index += 2;
continue;
}
if (char === quote) {
return index + 1;
}
if (char === "\n" || char === "\r") {
return null;
}
index++;
}
return null;
}
function findDirectiveTerminator(userCode, startIndex) {
let index = startIndex;
while (index < userCode.length) {
const char = userCode[index];
if (char === ";") {
return index + 1;
}
if (char === "\n" || char === "\r" || char === "}") {
return index;
}
if (char && /\s/.test(char)) {
index++;
continue;
}
if (userCode.startsWith("//", index)) {
return index;
}
if (userCode.startsWith("/*", index)) {
const commentEndIndex = userCode.indexOf("*/", index + 2);
if (commentEndIndex === -1) {
return null;
}
const comment = userCode.slice(index + 2, commentEndIndex);
if (comment.includes("\n") || comment.includes("\r")) {
return index;
}
index = commentEndIndex + 2;
continue;
}
return null;
}
return index;
}
function valueInjectionLoader(userCode) {
const { values } = "getOptions" in this ? this.getOptions() : this.query;
this.cacheable(false);
const injectedCode = (
// eslint-disable-next-line prefer-template
";" + Object.entries(values).map(([key, value]) => `globalThis["${key}"] = ${JSON.stringify(value)};`).join("")
);
const injectionIndex = findInjectionIndexAfterDirectives(userCode);
return `${userCode.slice(0, injectionIndex)}${injectedCode}${userCode.slice(injectionIndex)}`;
}
exports.default = valueInjectionLoader;
exports.findInjectionIndexAfterDirectives = findInjectionIndexAfterDirectives;
//# sourceMappingURL=valueInjectionLoader.js.map