{"version":3,"file":"preserve-import-attributes-D4gHeo2H.cjs","sources":["../src/internal/preserve-import-attributes.ts"],"sourcesContent":["import { type Plugin } from 'vite';\n\nexport const importRegexBothAssertAndWith =\n\t/(import\\s+?(?:(?:[\\s\\w*,{}]*\\s+from\\s+?)|)(?:(?:\".*?\")|(?:'.*?'))\\s*?(?:;|$|))(\\s*(?:assert|with)\\s*{.*}\\s*;?)/g;\n\nexport const importRegexAssertOnly =\n\t/(import\\s+?(?:(?:[\\s\\w*,{}]*\\s+from\\s+?)|)(?:(?:\".*?\")|(?:'.*?'))\\s*?(?:;|$|))(\\s*assert\\s*{.*}\\s*;?)/g;\n\nexport const importRegexWithOnly =\n\t/(import\\s+?(?:(?:[\\s\\w*,{}]*\\s+from\\s+?)|)(?:(?:\".*?\")|(?:'.*?'))\\s*?(?:;|$|))(\\s*with\\s*{.*}\\s*;?)/g;\n\nconst importWithAssertRegex = (keeping: ImportAttributeKinds) =>\n\tkeeping === 'both' || keeping === true\n\t\t? importRegexBothAssertAndWith\n\t\t: keeping === 'with'\n\t\t\t? importRegexWithOnly\n\t\t\t: importRegexAssertOnly;\n\nexport interface ImportStatementsWithAsserts {\n\timportStatement: string;\n\tassertStatement: string;\n}\n\nexport type ImportAttributeKinds = true | 'both' | 'assert' | 'with';\n\nexport const preserveImportAttributes = (keeping: ImportAttributeKinds = 'assert') => {\n\tconst importsWithAssertsFileMap = new Map<string, ImportStatementsWithAsserts[]>();\n\tconst regex = importWithAssertRegex(keeping);\n\treturn {\n\t\tname: 'preserve-import-attributes',\n\t\tenforce: 'post',\n\t\ttransform: (code, id) => {\n\t\t\timportsWithAssertsFileMap.set(\n\t\t\t\tid,\n\t\t\t\t[...code.matchAll(regex)]\n\t\t\t\t\t.map(([_, importStatement, assertStatement]) =>\n\t\t\t\t\t\timportStatement && assertStatement\n\t\t\t\t\t\t\t? { importStatement, assertStatement }\n\t\t\t\t\t\t\t: undefined,\n\t\t\t\t\t)\n\t\t\t\t\t.filter((s): s is ImportStatementsWithAsserts => !!s),\n\t\t\t);\n\t\t},\n\t\tgenerateBundle(_options, bundle) {\n\t\t\tfor (const file of Object.values(bundle)) {\n\t\t\t\tif (file.type === 'chunk') {\n\t\t\t\t\tfor (const moduleId of file.moduleIds) {\n\t\t\t\t\t\tconst map = importsWithAssertsFileMap.get(moduleId);\n\t\t\t\t\t\tif (map) {\n\t\t\t\t\t\t\tfor (const { importStatement, assertStatement } of map) {\n\t\t\t\t\t\t\t\tfile.code = file.code.replaceAll(\n\t\t\t\t\t\t\t\t\timportStatement,\n\t\t\t\t\t\t\t\t\timportStatement + assertStatement,\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t} as Plugin;\n};\n"],"names":[],"mappings":";;AAEO,MAAM,+BACZ;AAEM,MAAM,wBACZ;AAEM,MAAM,sBACZ;AAED,MAAM,wBAAwB,CAAC,YAC9B,YAAY,UAAU,YAAY,OAC/B,+BACA,YAAY,SACX,sBACA;AASQ,MAAA,2BAA2B,CAAC,UAAgC,aAAa;AAC/E,QAAA,gDAAgC,IAA2C;AAC3E,QAAA,QAAQ,sBAAsB,OAAO;AACpC,SAAA;AAAA,IACN,MAAM;AAAA,IACN,SAAS;AAAA,IACT,WAAW,CAAC,MAAM,OAAO;AACE,gCAAA;AAAA,QACzB;AAAA,QACA,CAAC,GAAG,KAAK,SAAS,KAAK,CAAC,EACtB;AAAA,UAAI,CAAC,CAAC,GAAG,iBAAiB,eAAe,MACzC,mBAAmB,kBAChB,EAAE,iBAAiB,oBACnB;AAAA,UAEH,OAAO,CAAC,MAAwC,CAAC,CAAC,CAAC;AAAA,MACtD;AAAA,IACD;AAAA,IACA,eAAe,UAAU,QAAQ;AAChC,iBAAW,QAAQ,OAAO,OAAO,MAAM,GAAG;AACrC,YAAA,KAAK,SAAS,SAAS;AACf,qBAAA,YAAY,KAAK,WAAW;AAChC,kBAAA,MAAM,0BAA0B,IAAI,QAAQ;AAClD,gBAAI,KAAK;AACR,yBAAW,EAAE,iBAAiB,gBAAgB,KAAK,KAAK;AAClD,qBAAA,OAAO,KAAK,KAAK;AAAA,kBACrB;AAAA,kBACA,kBAAkB;AAAA,gBACnB;AAAA,cAAA;AAAA,YACD;AAAA,UACD;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAAA,EAEF;AACD;;;;;"}