UNPKG

@storm-stack/core

Version:

A build toolkit and runtime used by Storm Software in TypeScript applications

1 lines 4.94 kB
{"version":3,"sources":["../../src/lib/utilities/source-map.ts"],"names":["dmp","Diff","generateSourceMap","id","source","transpiled","diff","diff_main","toString","diff_cleanupSemantic","offset","index","length","type","text","textLength","prependLeft","next","at","replaceText","firstNonWhitespaceIndexOfText","search","offsetStart","Math","max","update","remove","hasChanged","code","map","generateMap","file","includeContent"],"mappings":";;;;;;;;;AAwBA,IAAMA,GAAAA,GAAM,IAAIC,qBAAAA,EAAAA;AAUT,SAASC,iBAAAA,CACdC,EAAAA,EACAC,MAAAA,EACAC,UAAAA,EAAmB;AAEnB,EAAA,IAAI,CAACA,UAAAA,EAAY;AACf,IAAA;AACF,EAAA;AAEA,EAAA,MAAMC,OAAON,GAAAA,CAAIO,SAAAA,CAAUH,MAAAA,CAAOI,QAAAA,IAAYH,UAAAA,CAAAA;AAC9CL,EAAAA,GAAAA,CAAIS,qBAAqBH,IAAAA,CAAAA;AAEzB,EAAA,IAAII,MAAAA,GAAS,CAAA;AAEb,EAAA,KAAA,IAASC,KAAAA,GAAQ,CAAA,EAAGA,KAAAA,GAAQL,IAAAA,CAAKM,QAAQD,KAAAA,EAAAA,EAAS;AAChD,IAAA,IAAIL,IAAAA,CAAKK,KAAAA,CAAAA,EAAQ;AACf,MAAA,MAAM,CAACE,IAAAA,EAAMC,IAAAA,CAAAA,GAAQR,KAAKK,KAAAA,CAAAA;AAC1B,MAAA,MAAMI,aAAaD,IAAAA,CAAKF,MAAAA;AAExB,MAAA,QAAQC,IAAAA;AACN,QAAA,KAAK,CAAA,EAAG;AACNH,UAAAA,MAAAA,IAAUK,UAAAA;AACV,UAAA;AACF,QAAA;AACA,QAAA,KAAK,CAAA,EAAG;AACNX,UAAAA,MAAAA,CAAOY,WAAAA,CAAYN,QAAQI,IAAAA,CAAAA;AAC3B,UAAA;AACF,QAAA;AACA,QAAA,KAAK,EAAA,EAAI;AACP,UAAA,MAAMG,IAAAA,GAAOX,IAAAA,CAAKY,EAAAA,CAAGP,KAAAA,GAAQ,CAAA,CAAA;AAE7B,UAAA,IAAIM,IAAAA,IAAQA,IAAAA,CAAK,CAAA,CAAA,KAAO,CAAA,EAAG;AACzB,YAAA,MAAME,WAAAA,GAAcF,KAAK,CAAA,CAAA;AAEzB,YAAA,MAAMG,6BAAAA,GAAgCN,IAAAA,CAAKO,MAAAA,CAAO,IAAA,CAAA;AAClD,YAAA,MAAMC,WAAAA,GACJZ,MAAAA,GAASa,IAAAA,CAAKC,GAAAA,CAAIJ,+BAA+B,CAAA,CAAA;AAEnDhB,YAAAA,MAAAA,CAAOqB,MAAAA,CAAOH,WAAAA,EAAaZ,MAAAA,GAASK,UAAAA,EAAYI,WAAAA,CAAAA;AAChDR,YAAAA,KAAAA,IAAS,CAAA;UACX,CAAA,MAAO;AACLP,YAAAA,MAAAA,CAAOsB,MAAAA,CAAOhB,MAAAA,EAAQA,MAAAA,GAASK,UAAAA,CAAAA;AACjC,UAAA;AAEAL,UAAAA,MAAAA,IAAUK,UAAAA;AAEV,UAAA;AACF,QAAA;AACF;AACF,IAAA;AACF,EAAA;AAEA,EAAA,IAAI,CAACX,MAAAA,CAAOuB,UAAAA,EAAU,EAAI;AACxB,IAAA;AACF,EAAA;AAEA,EAAA,OAAO;AACLC,IAAAA,IAAAA,EAAMxB,OAAOI,QAAAA,EAAQ;AACrBqB,IAAAA,GAAAA,EAAKzB,OAAO0B,WAAAA,CAAY;MACtB1B,MAAAA,EAAQD,EAAAA;AACR4B,MAAAA,IAAAA,EAAM,GAAG5B,EAAAA,CAAAA,IAAAA,CAAAA;MACT6B,cAAAA,EAAgB;KAClB;AACF,GAAA;AACF;AAhEgB9B,wBAAAA,CAAAA,iBAAAA,EAAAA,mBAAAA,CAAAA","file":"chunk-XX6K67MA.cjs","sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Storm Stack\n\n This code was released as part of the Storm Stack project. Storm Stack\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/storm-stack.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/storm-stack\n Documentation: https://docs.stormsoftware.com/projects/storm-stack\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\n/* eslint-disable ts/no-unsafe-call */\n\nimport Diff from \"diff-match-patch\";\nimport type MagicString from \"magic-string\";\nimport type { CompilerResult } from \"../../types/compiler\";\n\nconst dmp = new Diff();\n\n/**\n * Generate code with source map.\n *\n * @param id - The file name.\n * @param source - The source code.\n * @param transpiled - The transpiled code.\n * @returns The compiler result.\n */\nexport function generateSourceMap(\n id: string,\n source: MagicString,\n transpiled?: string\n): CompilerResult {\n if (!transpiled) {\n return;\n }\n\n const diff = dmp.diff_main(source.toString(), transpiled);\n dmp.diff_cleanupSemantic(diff);\n\n let offset = 0;\n\n for (let index = 0; index < diff.length; index++) {\n if (diff[index]) {\n const [type, text] = diff[index]!;\n const textLength = text.length;\n\n switch (type) {\n case 0: {\n offset += textLength;\n break;\n }\n case 1: {\n source.prependLeft(offset, text);\n break;\n }\n case -1: {\n const next = diff.at(index + 1);\n\n if (next && next[0] === 1) {\n const replaceText = next[1];\n\n const firstNonWhitespaceIndexOfText = text.search(/\\S/);\n const offsetStart =\n offset + Math.max(firstNonWhitespaceIndexOfText, 0);\n\n source.update(offsetStart, offset + textLength, replaceText);\n index += 1;\n } else {\n source.remove(offset, offset + textLength);\n }\n\n offset += textLength;\n\n break;\n }\n }\n }\n }\n\n if (!source.hasChanged()) {\n return;\n }\n\n return {\n code: source.toString(),\n map: source.generateMap({\n source: id,\n file: `${id}.map`,\n includeContent: true\n })\n };\n}\n"]}