UNPKG

@web/dev-server-core

Version:
52 lines 1.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseDynamicImport = void 0; const REGEXP_WHITESPACE = /\s/; function getDynamicImportStartAndEnd(str, codeStart) { let start = -1; let end = -1; for (let i = 0; i < str.length; i += 1) { const isWhitespace = REGEXP_WHITESPACE.test(str[i]); if (start === -1) { if (!isWhitespace) { start = i; } } if (isWhitespace) { if (end === -1) { // we hit the end of a word, set the previous index as the last end = i - 1; } } else if (i === str.length - 1) { // we are at the end of the string if (end === -1) { end = i; } } else { // we're in a word or we hit a new word end = -1; } } return { dynamicStart: codeStart + start, dynamicEnd: codeStart + end + 1 }; } function parseDynamicImport(code, start, end) { const rawDynamicImport = code.substring(start, end); const { dynamicStart, dynamicEnd } = getDynamicImportStartAndEnd(rawDynamicImport, start); const importString = code.substring(dynamicStart, dynamicEnd); const importSpecifier = importString.substring(1, importString.length - 1); const startSymbol = code[dynamicStart]; const stringLiteral = [`\``, "'", '"'].includes(startSymbol); const concatenatedString = startSymbol === `\`` || importSpecifier.includes("'") || importSpecifier.includes('"'); return { importString, importSpecifier: stringLiteral ? importSpecifier : importString, stringLiteral, concatenatedString, dynamicStart, dynamicEnd, }; } exports.parseDynamicImport = parseDynamicImport; //# sourceMappingURL=parseDynamicImport.js.map