[6a3a178] | 1 | {"version":3,"file":"index.es.mjs","sources":["lib/get-comma.js","lib/get-image.js","lib/get-media.js","lib/handle-invalidation.js","lib/process-image-set.js","index.js"],"sourcesContent":["// return whether a node is a valid comma\nexport default node => Object(node).type === 'comma';\n","const imageSetFunctionMatchRegExp = /^(-webkit-)?image-set$/i\n\n// return a valid image\nexport default node =>\n\t// <url> | <image()> | <cross-fade()> | <gradient>\n\t// the image-set() function can not be nested inside of itself\n\tObject(node).type === 'func' && /^(cross-fade|image|(repeating-)?(conic|linear|radial)-gradient|url)$/i.test(node.value) && !(\n\t\tnode.parent.parent && node.parent.parent.type === 'func' && imageSetFunctionMatchRegExp.test(node.parent.parent.value)\n\t)\n\t? String(node)\n: Object(node).type === 'string'\n\t? node.value\n: false;\n","import postcss from 'postcss';\n\nconst dpiRatios = { dpcm: 2.54, dpi: 1, dppx: 96, x: 96 };\n\n// return a valid @media rule\nexport default (node, mediasByDpr) => {\n\tif (Object(node).type === 'number' && node.unit in dpiRatios) {\n\t\t// calculate min-device-pixel-ratio and min-resolution\n\t\tconst dpi = Number(node.value) * dpiRatios[node.unit.toLowerCase()];\n\t\tconst dpr = Math.floor(dpi / dpiRatios.x * 100) / 100;\n\n\t\tif (dpi in mediasByDpr) {\n\t\t\treturn false;\n\t\t} else {\n\t\t\tconst media = mediasByDpr[dpi] = postcss.atRule({\n\t\t\t\tname: 'media',\n\t\t\t\tparams: `(-webkit-min-device-pixel-ratio: ${dpr}), (min-resolution: ${dpi}dpi)`\n\t\t\t});\n\n\t\t\treturn media;\n\t\t}\n\t} else {\n\t\treturn false;\n\t}\n};\n","export default (opts, message, word) => {\n\tif (opts.oninvalid === 'warn') {\n\t\topts.decl.warn(opts.result, message, { word: String(word) });\n\t} else if (opts.oninvalid === 'throw') {\n\t\tthrow opts.decl.error(message, { word: String(word) });\n\t}\n};\n","import getComma from './get-comma';\nimport getImage from './get-image';\nimport getMedia from './get-media';\nimport handleInvalidation from './handle-invalidation';\n\nexport default (imageSetOptionNodes, decl, opts) => {\n\tconst parent = decl.parent;\n\tconst mediasByDpr = {};\n\n\tlet length = imageSetOptionNodes.length;\n\tlet index = -1;\n\n\twhile (index < length) {\n\t\tconst [comma, value, media] = [\n\t\t\tindex < 0 ? true : getComma(imageSetOptionNodes[index]),\n\t\t\tgetImage(imageSetOptionNodes[index + 1]),\n\t\t\tgetMedia(imageSetOptionNodes[index + 2], mediasByDpr)\n\t\t];\n\n\t\t// handle invalidations\n\t\tif (!comma) {\n\t\t\treturn handleInvalidation(opts, 'unexpected comma', imageSetOptionNodes[index]);\n\t\t} else if (!value) {\n\t\t\treturn handleInvalidation(opts, 'unexpected image', imageSetOptionNodes[index + 1]);\n\t\t} else if (!media) {\n\t\t\treturn handleInvalidation(opts, 'unexpected resolution', imageSetOptionNodes[index + 2]);\n\t\t}\n\n\t\t// prepare @media { decl: <image> }\n\t\tconst parentClone = parent.clone().removeAll();\n\t\tconst declClone = decl.clone({ value });\n\n\t\tparentClone.append(declClone);\n\t\tmedia.append(parentClone);\n\n\t\tindex += 3\n\t}\n\n\tconst medias = Object.keys(mediasByDpr).sort((a, b) => a - b).map(params => mediasByDpr[params]);\n\n\t// conditionally prepend previous siblings\n\tif (medias.length) {\n\t\tconst firstDecl = medias[0].nodes[0].nodes[0];\n\n\t\tif (medias.length === 1) {\n\t\t\tdecl.value = firstDecl.value\n\t\t} else {\n\t\t\tconst siblings = parent.nodes;\n\t\t\tconst previousSiblings = siblings.slice(0, siblings.indexOf(decl)).concat(firstDecl);\n\n\t\t\tif (previousSiblings.length) {\n\t\t\t\tconst parentClone = parent.cloneBefore().removeAll();\n\n\t\t\t\tparentClone.append(previousSiblings);\n\t\t\t}\n\n\t\t\t// prepend any @media { decl: <image> } rules\n\t\t\tparent.before(medias.slice(1));\n\n\t\t\t// conditionally remove the current rule\n\t\t\tif (!opts.preserve) {\n\t\t\t\tdecl.remove();\n\n\t\t\t\t// and then conditionally remove its parent\n\t\t\t\tif (!parent.nodes.length) {\n\t\t\t\t\tparent.remove();\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n","import postcss from 'postcss';\nimport valueParser from 'postcss-values-parser';\nimport processImageSet from './lib/process-image-set';\n\nconst imageSetValueMatchRegExp = /(^|[^\\w-])(-webkit-)?image-set\\(/\nconst imageSetFunctionMatchRegExp = /^(-webkit-)?image-set$/i\n\nexport default postcss.plugin('postcss-image-set-function', opts => {\n\t// prepare options\n\tconst preserve = 'preserve' in Object(opts) ? Boolean(opts.preserve) : true;\n\tconst oninvalid = 'oninvalid' in Object(opts) ? opts.oninvalid : 'ignore';\n\n\treturn (root, result) => {\n\t\t// for every declaration\n\t\troot.walkDecls(decl => {\n\t\t\tconst { value } = decl;\n\n\t\t\t// if a declaration likely uses an image-set() function\n\t\t\tif (imageSetValueMatchRegExp.test(value)) {\n\t\t\t\tconst valueAST = valueParser(value).parse();\n\n\t\t\t\t// process every image-set() function\n\t\t\t\tvalueAST.walkType('func', node => {\n\t\t\t\t\tif (imageSetFunctionMatchRegExp.test(node.value)) {\n\t\t\t\t\t\tprocessImageSet(\n\t\t\t\t\t\t\tnode.nodes.slice(1, -1),\n\t\t\t\t\t\t\tdecl,\n\t\t\t\t\t\t\t{ decl, oninvalid, preserve, result }\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}\n});\n"],"names":["node","Object","type","imageSetFunctionMatchRegExp","test","value","parent","String","dpiRatios","dpcm","dpi","dppx","x","mediasByDpr","unit","Number","toLowerCase","dpr","Math","floor","media","postcss","atRule","name","params","opts","message","word","oninvalid","decl","warn","result","error","imageSetOptionNodes","length","index","getComma","getImage","getMedia","comma","handleInvalidation","parentClone","clone","removeAll","declClone","append","medias","keys","sort","a","b","map","firstDecl","nodes","siblings","previousSiblings","slice","indexOf","concat","cloneBefore","before","preserve","remove","imageSetValueMatchRegExp","plugin","Boolean","root","walkDecls","valueAST","valueParser","parse","walkType","processImageSet"],"mappings":";;;AAAA;AACA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