next-with-linaria
Version:
Linaria support for Next.js App Router
22 lines • 1.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.insertImportStatement = insertImportStatement;
function insertImportStatement(content, importStatement) {
const importRegex = /^\s*(?:import\s+[^;]+?\s+from\s+["'][^"']+["'];|import\s*["'][^"']+["'];)/gm;
const importMatches = [...content.matchAll(importRegex)];
if (importMatches.length > 0) {
const lastImport = importMatches[importMatches.length - 1];
const insertPosition = lastImport.index + lastImport[0].length;
return `${content.slice(0, insertPosition)}\n${importStatement}${content.slice(insertPosition)}`;
}
const directiveRegex = /^(?:(?:\s*["']use \w+["'];?\s*\n?)+)/;
const directiveMatch = content.match(directiveRegex);
if (directiveMatch) {
const endOfDirectives = directiveMatch[0].length;
const needsNewline = !content.slice(0, endOfDirectives).endsWith('\n');
const separator = needsNewline ? '\n' : '';
return `${content.slice(0, endOfDirectives)}${separator}${importStatement}\n${content.slice(endOfDirectives)}`;
}
return `${importStatement}\n${content}`;
}
//# sourceMappingURL=insert-import.js.map