|
| 1 | +svelte = require('svelte/compiler') |
| 2 | + |
| 3 | +/** |
| 4 | + * @typedef Options |
| 5 | + * @property {{ [tag: string]: string[] }} tags |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * @typedef {import('svelte/types/compiler/interfaces').TemplateNode} TemplateNode |
| 10 | + */ |
| 11 | + |
| 12 | +/** |
| 13 | + * @param {Options} [options] |
| 14 | + * @returns {import('svelte/types/compiler/preprocess/types').PreprocessorGroup} |
| 15 | + */ |
| 16 | +function imagePreprocess(options = {}) { |
| 17 | + const imports = {} |
| 18 | + const tags = options.tags || { |
| 19 | + img: ['src', 'srcset', 'data-src', 'data-srcset'], |
| 20 | + source: ['src', 'srcset', 'data-src', 'data-srcset'], |
| 21 | + } |
| 22 | + return { |
| 23 | + markup: ({ content, filename }) => { |
| 24 | + const preparedContent = content |
| 25 | + .replace(/<style[^>]*>[\w\W]+<\/style>/g, (match) => ' '.repeat(match.length)) |
| 26 | + .replace(/<script[^>]*>[\w\W]+<\/script>/g, (match) => ' '.repeat(match.length)) |
| 27 | + let ast |
| 28 | + try { |
| 29 | + ast = svelte.parse(preparedContent) |
| 30 | + } catch (e) { |
| 31 | + console.error(e, 'Error parsing content') |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + /** @type {TemplateNode[]} */ |
| 36 | + const matches = [] |
| 37 | + svelte.walk(ast, { |
| 38 | + enter: (node) => { |
| 39 | + if (!['Element', 'Fragment', 'InlineComponent'].includes(node.type)) { |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + if (tags[node.name]) { |
| 44 | + matches.push({ node, attributes: tags[node.name] }) |
| 45 | + } |
| 46 | + }, |
| 47 | + }) |
| 48 | + |
| 49 | + const dependencies = [] |
| 50 | + const code = matches.reduce( |
| 51 | + /** |
| 52 | + * @param {{content: string, offset: number}} processed |
| 53 | + * @param {{node: TemplateNode, attributes: string[]} match |
| 54 | + * @param {number} index |
| 55 | + */ |
| 56 | + (processed, match, index) => { |
| 57 | + const attributes = (match.node.attributes || []).filter( |
| 58 | + (a) => a.type === 'Attribute' && match.attributes.includes(a.name) |
| 59 | + ) |
| 60 | + if ( |
| 61 | + attributes.length === 0 || |
| 62 | + (match.node.attributes || []).find( |
| 63 | + (a) => a.name === 'rel' && a.value[0].data === 'external' |
| 64 | + ) |
| 65 | + ) { |
| 66 | + return processed |
| 67 | + } |
| 68 | + |
| 69 | + let { content, offset } = processed |
| 70 | + |
| 71 | + for (const attribute of attributes) { |
| 72 | + if (attribute.value[0]?.type === 'Text') { |
| 73 | + const value = attribute.value[0] |
| 74 | + if (value.data.startsWith('http')) continue |
| 75 | + |
| 76 | + const symbol = `__IMAGE_${index}__` |
| 77 | + const replacement = `{${symbol}}` |
| 78 | + |
| 79 | + if (!imports[filename]) imports[filename] = {} |
| 80 | + imports[filename][symbol] = value.data |
| 81 | + |
| 82 | + dependencies.push(value.data) |
| 83 | + |
| 84 | + content = |
| 85 | + content.substring(0, value.start + offset) + |
| 86 | + replacement + |
| 87 | + content.substring(value.end + offset) |
| 88 | + |
| 89 | + offset += replacement.length - value.data.length |
| 90 | + } |
| 91 | + } |
| 92 | + return { content, offset } |
| 93 | + }, |
| 94 | + { content, offset: 0 } |
| 95 | + ).content |
| 96 | + |
| 97 | + return { code, dependencies } |
| 98 | + }, |
| 99 | + script: ({ content, attributes, filename }) => { |
| 100 | + if (!attributes.context) { |
| 101 | + const localImports = Object.entries(imports[filename] || {}) |
| 102 | + if (localImports.length > 0) { |
| 103 | + const dependencies = localImports.map(([symbol, path]) => path) |
| 104 | + const code = |
| 105 | + localImports |
| 106 | + .map(([symbol, path]) => `import ${symbol} from "${path}"`) |
| 107 | + .join('\n') + |
| 108 | + '\n' + |
| 109 | + content |
| 110 | + return { code, dependencies } |
| 111 | + } |
| 112 | + } |
| 113 | + }, |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +module.exports = imagePreprocess |
0 commit comments