{"version":3,"file":"index.mjs","sources":["src/lib/resolve.js","src/lib/get-define-elements.js","src/lib/transform-attr-values.js","src/lib/transform-custom-elements.js","src/lib/transform.js","src/index.js"],"sourcesContent":["import { readFile, stat } from 'fs';\r\nimport { join, sep } from 'path';\r\nimport fetch from 'node-fetch';\r\n\r\n/* Resolve the location of a file within `url(id)` from `cwd`\r\n/* ========================================================================== */\r\n\r\nexport default function resolve (id, rawcwd, rawcache) {\r\n\tconst cache = Object(rawcache);\r\n\r\n\t// if `id` starts with `/` then `cwd` is the filesystem root\r\n\tconst cwd = starts_with_root(id) ? '' : rawcwd;\r\n\r\n\t// resolve as a file using `cwd/id` as `file`\r\n\treturn resolve_as_fetch(id)\r\n\t.catch(() => resolve_as_file(join(cwd, id), cache))\r\n\t// otherwise, resolve as a directory using `cwd/id` as `dir`\r\n\t.catch(() => resolve_as_directory(join(cwd, id), cache))\r\n\t// otherwise, if `id` does not begin with `/`, `./`, or `../`\r\n\t.catch(() => !starts_with_relative(id)\r\n\t\t// resolve as a module using `cwd` and `id`\r\n\t\t? resolve_as_module(cwd, id, cache)\r\n\t\t: Promise.reject()\r\n\t)\r\n\t// otherwise, throw `\"HTML not found\"`\r\n\t.catch(() => Promise.reject(new Error(`HTML not found ${id} in ${cwd}`)));\r\n}\r\n\r\nfunction resolve_as_fetch (url) {\r\n\treturn starts_with_protocol(url)\r\n\t\t? fetch(url).then(\r\n\t\t\tresponse => response.text()\r\n\t\t).then(\r\n\t\t\tcontents => ({\r\n\t\t\t\tfile: url,\r\n\t\t\t\tcontents\r\n\t\t\t})\r\n\t\t)\r\n\t: Promise.reject();\r\n}\r\n\r\nfunction resolve_as_file (file, cache) {\r\n\t// resolve `file` as the file\r\n\treturn file_modified_contents(file, cache)\r\n\t// otherwise, resolve `file.html` as the file\r\n\t.catch(() => file_modified_contents(`${file}.html`, cache));\r\n}\r\n\r\nfunction resolve_as_directory (dir, cache) {\r\n\t// resolve the JSON contents of `dir/package.json` as `pkg`\r\n\treturn json_contents(dir, cache).then(\r\n\t\t// if `pkg` has a `html` field\r\n\t\tpkg => 'html' in pkg\r\n\t\t\t// resolve `dir/pkg.html` as the file\r\n\t\t\t? file_modified_contents(join(dir, pkg.html), cache)\r\n\t\t// otherwise, resolve `dir/index.html` as the file\r\n\t\t: file_modified_contents(join(dir, 'index.html'), cache)\r\n\t);\r\n}\r\n\r\nfunction resolve_as_module (cwd, id, cache) {\r\n\t// for each `dir` in the node modules directory using `cwd`\r\n\treturn node_modules_dirs(cwd).reduce(\r\n\t\t(promise, dir) => promise.catch(\r\n\t\t\t// resolve as a file using `dir/id` as `file`\r\n\t\t\t() => resolve_as_file(join(dir, id), cache)\r\n\t\t\t// otherwise, resolve as a directory using `dir/id` as `dir`\r\n\t\t\t.catch(() => resolve_as_directory(join(dir, id), cache))\r\n\t\t),\r\n\t\tPromise.reject()\r\n\t);\r\n}\r\n\r\nfunction node_modules_dirs (cwd) {\r\n\t// segments is `cwd` split by `/`\r\n\tconst segments = cwd.split(sep);\r\n\r\n\t// `count` is the length of segments\r\n\tlet count = segments.length;\r\n\r\n\t// `dirs` is an empty list\r\n\tconst dirs = [];\r\n\r\n\t// while `count` is greater than `0`\r\n\twhile (count > 0) {\r\n\t\t// if `segments[count]` is not `node_modules`\r\n\t\tif (segments[count] !== 'node_modules') {\r\n\t\t\t// push a new path to `dirs` as the `/`-joined `segments[0 - count]` and `node_modules`\r\n\t\t\tdirs.push(\r\n\t\t\t\tjoin(segments.slice(0, count).join('/') || '/', 'node_modules')\r\n\t\t\t);\r\n\t\t}\r\n\r\n\t\t// `count` is `count` minus `1`\r\n\t\t--count;\r\n\t}\r\n\r\n\t// return `dirs`\r\n\treturn dirs;\r\n}\r\n\r\n/* Additional tooling\r\n/* ========================================================================== */\r\n\r\nfunction file_contents (file, mtimeMs, cache) {\r\n\tcache[file] = new Promise(\r\n\t\t(resolvePromise, rejectPromise) => readFile(file, 'utf8', (error, contents) => error\r\n\t\t\t? rejectPromise(error)\r\n\t\t\t: resolvePromise({ file, contents })\r\n\t\t)\r\n\t);\r\n\r\n\tcache[file].mtimeMs = mtimeMs;\r\n\r\n\treturn cache[file];\r\n}\r\n\r\nfunction file_modified_contents (file, cache) {\r\n\treturn new Promise((resolvePromise, rejectPromise) => {\r\n\t\tstat(\r\n\t\t\tfile,\r\n\t\t\t(error, stats) => error\r\n\t\t\t\t? rejectPromise(error)\r\n\t\t\t: cache[file] && cache[file].mtimeMs === stats.mtimeMs\r\n\t\t\t\t? resolvePromise(cache[file])\r\n\t\t\t: resolvePromise(file_contents(file, stats.mtimeMs, cache))\r\n\t\t)\r\n\t});\r\n}\r\n\r\nfunction json_contents (dir, cache) {\r\n\tconst file = join(dir, 'package.json');\r\n\r\n\treturn file_modified_contents(file, cache).then(\r\n\t\t({ contents }) => JSON.parse(contents)\r\n\t);\r\n}\r\n\r\nfunction starts_with_protocol (id) {\r\n\treturn /^(?:\\w+:)?\\/\\/(\\S+)$/.test(id);\r\n}\r\n\r\nfunction starts_with_root (id) {\r\n\treturn /^\\//.test(id);\r\n}\r\n\r\nfunction starts_with_relative (id) {\r\n\treturn /^\\.{0,2}\\//.test(id);\r\n}\r\n","import { Result } from 'phtml';\r\nimport path from 'path';\r\nimport resolve from './resolve';\r\nimport transform from './transform';\r\n\r\n// conditionally remove <link rel=\"html\" href /> if not preserving\r\nexport default function getDefineElements (root, opts) {\r\n\tconst defines = {};\r\n\r\n\tlet promise = Promise.resolve();\r\n\r\n\t// walk the tree looking for <link rel=\"html\" href> and <define>\r\n\troot.walk(node => {\r\n\t\tconst isElement = node.type === 'element';\r\n\r\n\t\t// ignore non-elements\r\n\t\tif (!isElement) {\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\t// get the href from <link rel=\"html\" href />\r\n\t\tconst href = node.name === 'link' && node.attrs.get('rel') === 'html' && node.attrs.get('href');\r\n\r\n\t\tif (href) {\r\n\t\t\t// conditionally remove the <link> if preserving is disabled\r\n\t\t\tif (!opts.preserve) {\r\n\t\t\t\tnode.remove();\r\n\t\t\t}\r\n\r\n\t\t\t// get the current working directory for the <link>\r\n\t\t\tconst cwd = opts.cwd || path.dirname(node.source.input.from);\r\n\r\n\t\t\t// promise the contents of the href from <link>\r\n\t\t\tconst resolved = resolve(href, cwd, opts.cache);\r\n\r\n\t\t\tpromise = promise.then(\r\n\t\t\t\t() => resolved\r\n\t\t\t).then(\r\n\t\t\t\t// promise the contents of the href as an AST\r\n\t\t\t\tresult => new Result(result.contents, { ...result, from: result.file }).root\r\n\t\t\t).then(\r\n\t\t\t\t// transform the <link> AST\r\n\t\t\t\tlinkroot => transform(linkroot, opts)\r\n\t\t\t).then(\r\n\t\t\t\t// add the <link> defines to the current defines\r\n\t\t\t\tlinkdefines => {\r\n\t\t\t\t\tObject.assign(defines, linkdefines);\r\n\t\t\t\t}\r\n\t\t\t);\r\n\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\t// get the tag from <define tag />\r\n\t\tconst tag = node.name === 'define' && node.attrs.get('tag');\r\n\t\tconst isValidTag = tag && isValidTagRegExp.test(tag);\r\n\r\n\t\t// ignore invalid tags\r\n\t\tif (!isValidTag) {\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\t// add the <define> to the current defines\r\n\t\tdefines[tag] = node;\r\n\t});\r\n\r\n\t// promise the defines\r\n\treturn promise.then(\r\n\t\t() => defines\r\n\t);\r\n}\r\n\r\nconst isValidTagRegExp = /[_a-zA-Z]+[_a-zA-Z0-9-]*-[_a-zA-Z0-9-]+$/;\r\n","// transform attribute values with ${slot-X} (e.g. some-attribute=\"before-${slot-X}-after\")\r\nexport default function transformAttrValues (attrs, slots) {\r\n\tattrs.forEach(attr => {\r\n\t\tconst hasSlotValues = slotValuesRegExp.test(attr.value);\r\n\r\n\t\t// ignore attributes that don’t use ${slot-X}\r\n\t\tif (!hasSlotValues) {\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\t// replace ${slot-X} with the slot value of X\r\n\t\tattr.value = attr.value.replace(\r\n\t\t\tslotValuesRegExp,\r\n\t\t\t($0, name, fallback) => name in slots\r\n\t\t\t\t? String(slots[name])\r\n\t\t\t: fallback\r\n\t\t\t\t? fallback\r\n\t\t\t: ''\r\n\t\t);\r\n\t});\r\n}\r\n\r\n// attribute values that use ${slot-X}, where X is the slot\r\nconst slotValuesRegExp = /\\$\\{slot-([_a-zA-Z]+[_a-zA-Z0-9-]*)(?:,([^}]+))?\\}/g;\r\n","import { Element, Fragment, Text } from 'phtml';\nimport transformAttrValues from './transform-attr-values';\n\nexport default function transformCustomElements (root, opts, defines) {\n\troot.walk((node, result) => {\n\t\tconst isValidCustomElement = node.type === 'element' && node.name in defines;\n\n\t\t// ignore non-custom-elements and unknown custom elements\n\t\tif (!isValidCustomElement) {\n\t\t\treturn;\n\t\t}\n\n\t\t// leave template children unprocessed\n\t\tif (isTemplateOrTemplateChild(node)) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst defineClone = defines[node.name].clone(null, true);\n\t\tconst defineSlots = getSlotsFromDefineElement(defineClone);\n\t\tconst customSlots = getSlotsFromCustomElement(node);\n\n\t\tfor (const name in defineSlots) {\n\t\t\tif (name in customSlots) {\n\t\t\t\tdefineSlots[name].replaceWith(customSlots[name]);\n\t\t\t} else {\n\t\t\t\tdefineSlots[name].replaceWith(...defineSlots[name].nodes);\n\t\t\t}\n\t\t}\n\n\t\tdefineClone.walk(child => {\n\t\t\tif (child.type !== 'element') {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\ttransformAttrValues(child.attrs, customSlots);\n\t\t\ttransferCssClasses(node, child);\n\t\t});\n\n\t\tconst newRoot = replaceNode(node, result, opts, defineClone);\n\n\t\tif (opts.transformSlots){\n\t\t\ttransformCustomElements(newRoot, opts, defines);\n\t\t}\n\t});\n}\n\nconst transferCssClasses = (nodeFrom, nodeTo) => {\n\tif (!nodeFrom.attrs.get('class')){\n\t\treturn;\n\t}\n\n\tconst elementClasses = nodeTo.attrs.get('class')\n\t\t? nodeTo.attrs.get('class').split(' ')\n\t\t: []\n\n\tconst classes = elementClasses.concat(nodeFrom.attrs.get('class').split(' '));\n\n\tconst classSet = new Set(classes);\n\n\tconst value = Array.from(classSet.values()).join(' ');\n\n\tnodeTo.attrs.add([{ name: 'class', value }]);\n};\n\nconst replaceNode = (node, result, opts, define) => {\n    if (opts.preserve) {\n\n\t\tconst { nodes } = node;\n\n\t\t// prevent creation of empty <template></template> nodes\n\t\tif (!nodes.length) {\n\t\t\tnode.nodes.push(...define.nodes);\n\n\t\t\treturn node;\n\t\t}\n\n\t\tconst hasTemplateSiblings = nodes.some(n => n.name === 'template');\n\n\t\t/* If the template already exists then we just need to finish processing\n\t\t * the sibilings.\n\t\t *\n\t\t * This handles the scenario where there are 2 sibling slot elements and\n\t\t * are using { transformSlots: true }\n\t\t *\n\t\t * <custom-element slot=\"contents\">\n\t\t *   <span slot=\"left\">Left</span>\n\t\t *\n\t\t *   <span slot=\"right\">Right</span>\n\t\t * </custom-element>\n\t\t */\n\n\t\tif (hasTemplateSiblings) {\n\t\t\treturn node;\n\t\t}\n\n\t\tconst template = new Element({\n\t\t\tname: 'template',\n\t\t\tnodes: node.nodes,\n\t\t\tresult\n\t\t});\n\n\t\tnode.nodes.push(template, ...define.nodes);\n\n\t\treturn node;\n\t} else {\n\t\tconst { parent } = node;\n\n\t\tnode.replaceWith(...define.nodes);\n\n\t\treturn parent;\n\t}\n}\n\nconst isTemplateOrTemplateChild = node => {\n\tlet current = node;\n\n\twhile(current) {\n\t\tif (current.name === 'template') {\n\t\t\treturn true;\n\t\t}\n\n\t\tcurrent = current.parent;\n\t}\n\n\treturn false;\n}\n\nconst getSlotsFromDefineElement = node => {\n\tconst slots = {};\n\n\tnode.walk(child => {\n\t\tconst isSlot = child.type === 'element' && child.name === 'slot';\n\t\tconst name = isSlot && child.attrs.get('name');\n\n\t\tif (!name) {\n\t\t\treturn;\n\t\t}\n\n\t\tchild.attrs.remove('name');\n\n\t\tslots[name] = child;\n\t});\n\n\treturn slots;\n};\n\nconst getSlotsFromCustomElement = node => {\n\tconst slots = {};\n\n\tnode.attrs.forEach(attr => {\n\t\tconst slotMatch = attr.name.match(isSlotAttrRegExp);\n\n\t\tif (!slotMatch) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst name = slotMatch[1];\n\n\t\tslots[name] = new Text({ data: attr.value, result: node.result });\n\t});\n\n\tnode.walk(child => {\n\t\tconst isElement = child.type === 'element';\n\n\t\tif (!isElement) {\n\t\t\treturn;\n\t\t}\n\n\t\t// transform <slot name>\n\t\tconst slotElementName = child.name === 'slot' && child.attrs.get('name');\n\n\t\tif (slotElementName) {\n\t\t\tconst slotElement = new Fragment({ result: node.result });\n\n\t\t\tslotElement.append(...child.clone(null, true).nodes);\n\n\t\t\tslots[slotElementName] = slotElement;\n\n\t\t\treturn;\n\t\t}\n\n\t\t// transform <x slot>\n\t\tconst elementSlotName = child.attrs.get('slot');\n\n\t\tif (elementSlotName) {\n\t\t\tconst slotElement = child.clone(null, true);\n\n\t\t\tslotElement.attrs.remove('slot');\n\n\t\t\tslots[elementSlotName] = slotElement;\n\n\t\t\treturn;\n\t\t}\n\t});\n\n\treturn slots;\n};\n\nconst isSlotAttrRegExp = /^slot-([_a-zA-Z]+[_a-zA-Z0-9-]*)$/;\n","import getDefineElements from './get-define-elements';\r\nimport transformCustomElements from './transform-custom-elements';\r\n\r\nexport default function transform (root, opts) {\r\n\treturn getDefineElements(root, opts).then(\r\n\t\tdefines => {\r\n\t\t\ttransformCustomElements(root, opts, defines);\r\n\r\n\t\t\treturn defines;\r\n\t\t}\r\n\t);\r\n}\r\n","import phtml from 'phtml';\r\nimport transform from './lib/transform';\r\n\r\nexport default new phtml.Plugin('phtml-define', opts => root => transform(root, Object.assign({ cache: {} }, opts)));\r\n"],"names":["resolve","id","rawcwd","rawcache","cache","Object","cwd","starts_with_root","resolve_as_fetch","catch","resolve_as_file","join","resolve_as_directory","starts_with_relative","resolve_as_module","Promise","reject","Error","url","starts_with_protocol","fetch","then","response","text","contents","file","file_modified_contents","dir","json_contents","pkg","html","node_modules_dirs","reduce","promise","segments","split","sep","count","length","dirs","push","slice","file_contents","mtimeMs","resolvePromise","rejectPromise","readFile","error","stat","stats","JSON","parse","test","getDefineElements","root","opts","defines","walk","node","isElement","type","href","name","attrs","get","preserve","remove","path","dirname","source","input","from","resolved","result","Result","linkroot","transform","linkdefines","assign","tag","isValidTag","isValidTagRegExp","transformAttrValues","slots","forEach","attr","hasSlotValues","slotValuesRegExp","value","replace","$0","fallback","String","transformCustomElements","isValidCustomElement","isTemplateOrTemplateChild","defineClone","clone","defineSlots","getSlotsFromDefineElement","customSlots","getSlotsFromCustomElement","replaceWith","nodes","child","transferCssClasses","newRoot","replaceNode","transformSlots","nodeFrom","nodeTo","elementClasses","classes","concat","classSet","Set","Array","values","add","define","hasTemplateSiblings","some","n","template","Element","parent","current","isSlot","slotMatch","match","isSlotAttrRegExp","Text","data","slotElementName","slotElement","Fragment","append","elementSlotName","phtml","Plugin"],"mappings":";;;;;AAIA;;;AAGe,SAASA,OAAT,CAAkBC,EAAlB,EAAsBC,MAAtB,EAA8BC,QAA9B,EAAwC;AACtD,QAAMC,KAAK,GAAGC,MAAM,CAACF,QAAD,CAApB,CADsD;;AAItD,QAAMG,GAAG,GAAGC,gBAAgB,CAACN,EAAD,CAAhB,GAAuB,EAAvB,GAA4BC,MAAxC,CAJsD;;AAOtD,SAAOM,gBAAgB,CAACP,EAAD,CAAhB,CACNQ,KADM,CACA,MAAMC,eAAe,CAACC,IAAI,CAACL,GAAD,EAAML,EAAN,CAAL,EAAgBG,KAAhB,CADrB;AAAA,GAGNK,KAHM,CAGA,MAAMG,oBAAoB,CAACD,IAAI,CAACL,GAAD,EAAML,EAAN,CAAL,EAAgBG,KAAhB,CAH1B;AAAA,GAKNK,KALM,CAKA,MAAM,CAACI,oBAAoB,CAACZ,EAAD,CAArB;AAAA,IAEVa,iBAAiB,CAACR,GAAD,EAAML,EAAN,EAAUG,KAAV,CAFP,GAGVW,OAAO,CAAC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