UNPKG

webpack-preprocessor-loader

Version:
147 lines (146 loc) 3.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.reader = void 0; const head_index = { '/': { 2: ['/*', '//'], max_length: 2, }, '{': { 3: ['{/*'], max_length: 3, }, '<': { 4: ['<!--'], max_length: 4, }, }; const open_close_pair = { '/*': '*/', '//': '\n', '{/*': '*/}', '<!--': '-->', }; function getPlainTextResult(raw) { return { is_comment: false, raw, block: '', c_open: null, c_close: null, }; } function* reader(content) { const len = content.length; if (len === 0) { return null; } let is_eof = false; let ptr = -1; let char = ''; let block = ''; let raw = ''; let current_open = ''; let current_close = ''; let in_new_comment = false; let skip_to_next_line = false; while (++ptr < len) { is_eof = ptr === len - 1; char = content[ptr]; raw = raw.concat(char); if (skip_to_next_line) { if (isNewLine(char)) { yield getPlainTextResult(raw); raw = ''; skip_to_next_line = false; } } else if (!in_new_comment) { if (isWhitespace(char)) { continue; } if (isNewLine(char)) { yield getPlainTextResult(raw); raw = ''; continue; } const open = getCommentOpen(char, ptr, content); if (!open) { skip_to_next_line = true; } else { current_open = open; current_close = open_close_pair[open]; in_new_comment = true; raw = raw.slice(0, -1).concat(current_open); block = ''; ptr += open.length - 1; } } else { const look_ahead = content.slice(ptr, ptr + current_close.length); if (look_ahead === current_close) { let tail = ''; let offset = 0; if (!isNewLine(current_close)) { [tail, offset] = getContentTillNextNewline(content, ptr + current_close.length); } yield { is_comment: true, raw: raw.slice(0, -1).concat(look_ahead, tail), block, c_open: current_open, c_close: current_close, }; raw = ''; in_new_comment = false; ptr += look_ahead.length + offset - 1; } else { block = block.concat(char); } } /** * NOTE: * If file ends without a new-line char, * the last line will be directly omitted. * * See Issue: https://github.com/afterwind-io/preprocessor-loader/issues/4 */ if (is_eof) { yield getPlainTextResult(raw); break; } } return null; } exports.reader = reader; function isWhitespace(char) { return char === ' ' || char === '\t'; } function isNewLine(char) { return char === '\n'; } function getCommentOpen(char, ptr, content) { const index = head_index[char]; if (!index) { return null; } let len = 0; while (len++ < index.max_length) { if (!index[len]) { continue; } const matches = index[len]; const open = content.slice(ptr, ptr + len); if (matches.includes(open)) { return open; } continue; } return null; } function getContentTillNextNewline(content, ptr) { const index = content.slice(ptr).indexOf('\n') + 1; return [content.slice(ptr, ptr + index), index]; }