From 0379526e20e1ff212df23e6326741aa9c06df204 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Sat, 14 Oct 2023 15:50:21 +0800 Subject: added doiuse plugin for postcss --- .../doiuse/lib/stream/SourceMapTransform.js | 74 ++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 node_modules/doiuse/lib/stream/SourceMapTransform.js (limited to 'node_modules/doiuse/lib/stream/SourceMapTransform.js') diff --git a/node_modules/doiuse/lib/stream/SourceMapTransform.js b/node_modules/doiuse/lib/stream/SourceMapTransform.js new file mode 100644 index 00000000..6295b1d3 --- /dev/null +++ b/node_modules/doiuse/lib/stream/SourceMapTransform.js @@ -0,0 +1,74 @@ +/* eslint-disable no-underscore-dangle */ +import { Transform } from 'node:stream'; + +const NEW_LINE_CHAR = '\n'.codePointAt(0); + +/** + * @typedef {Object} SourceMapTransformation + * @prop {number} line + * @prop {number} column + * @prop {string} content + */ + +export default class SourceMapTransform extends Transform { + constructor() { + super({ objectMode: true }); + } + + /** + * @type {?{location:[number,number], buffers: Buffer[]}} + * buffer for the current incoming rule. + */ + #current = null; + + /** track depth to handle rules nested in at-rules. */ + #depth = 0; + + /** track this and pass it downstream for source mapping. */ + #line = 1; + + /** track this and pass it downstream for source mapping. */ + #column = 1; + + /** + * @param {[string,Buffer]} chunk + * @param {any} encoding + * @param {Function} callback + */ + _transform([type, buf], encoding, callback) { + if ((type === 'rule_start' || type === 'atrule_start')) { + this.#depth += 1; + } + if (this.#depth > 0 && !this.#current) { + this.#current = { location: [this.#line, this.#column], buffers: [] }; + } + if (type === 'rule_end' || type === 'atrule_end') { + this.#depth -= 1; + } + + if (this.#current) { + this.#current.buffers.push(buf); + if (this.#depth === 0) { + // Push rule + this.push({ + line: this.#current.location[0], + column: this.#current.location[1], + content: Buffer.concat(this.#current.buffers).toString(), + }); + this.#current = null; + } + } + + // Update position + for (const char of buf) { + if (char === NEW_LINE_CHAR) { + this.#line += 1; + this.#column = 1; + } else { + this.#column += 1; + } + } + + callback(); + } +} -- cgit v1.2.3