import { defineNuxtModule, extendViteConfig, createResolver, addPlugin, addTemplate } from '@nuxt/kit'; const execSrcReplacements = (src, replacements) => { for (const replacement of replacements) { if ((typeof replacement.from === "string" || replacement.from instanceof RegExp) === false) { throw new TypeError( "[vite-plugin-replace]: The replacement option 'from' is not of type 'string' or 'RegExp'." ); } else if ((typeof replacement.to === "string" || replacement.to instanceof Function) === false) { throw new TypeError( "[vite-plugin-replace]: The replacement option 'to' is not of type 'string' or 'Function'" ); } else src = src.replace(replacement.from, replacement.to); } return src; }; const replaceCodePlugin = (config) => { if (config === void 0) { config = { replacements: [] }; } else if ((typeof config === "object" || config !== null) === false) { throw new TypeError( "[vite-plugin-replace]: The configuration is not of type 'Object'." ); } else if (Array.isArray(config.replacements) === false) { throw new TypeError( "[vite-plugin-replace]: The configuration option 'replacement' is not of type 'Array'." ); } return { name: "transform-file", enforce: "pre", transform: function(src) { return { code: execSrcReplacements(src, config.replacements), map: null }; } }; }; const isNotLazy = (text, options, kind) => text.includes("data-not-lazy") || options && !options[kind]; const replaceAttrs = (text, tag, attrs, directiveOnly) => { if (text.includes("devtools")) return text; if (!directiveOnly && tag) { const regex = new RegExp(`<${tag}`); text = text.replace(regex, `<${tag} v-lazy-load `); } for (const attr of attrs) { const regex = new RegExp(`${attr}=`, "g"); text = text.replace(regex, `data-${attr}=`); } return text; }; const module = defineNuxtModule({ meta: { name: "nuxt-lazy-load", configKey: "lazyLoad" }, defaults: { images: true, videos: true, audios: true, iframes: true, native: false, directiveOnly: false, defaultImage: false, loadingClass: "isLoading", loadedClass: "isLoaded", appendClass: "lazyLoad", observerConfig: {} }, setup(options, nuxt) { extendViteConfig((config) => { if (!config?.vue?.template?.transformAssetUrls) config.vue.template.transformAssetUrls = {}; config.vue.template.transformAssetUrls["img"] = [ "src", "data-src", "srcset", "data-srcset", "data-flickity-lazyload" ]; config.vue.template.transformAssetUrls["video"] = ["src", "data-src", "poster", "data-poster"]; config.vue.template.transformAssetUrls["source"] = ["src", "data-src", "srcset", "data-srcset"]; config.vue.template.transformAssetUrls["audio"] = ["src", "data-src"]; let replacements = []; if (!options.native) { replacements = [ { from: /]*?>/g, to: (match) => isNotLazy(match, options, "images") ? match : replaceAttrs(match, "img", ["src", "srcset"], options.directiveOnly) }, { from: /]*?>/g, to: (match) => isNotLazy(match) ? match : replaceAttrs(match, null, ["src", "srcset"], options.directiveOnly) }, { from: /]*?>/g, to: (match) => isNotLazy(match, options, "videos") ? match.replace("]*?>/g, to: (match) => isNotLazy(match, options, "images") ? match.replace("]*?>/g, to: (match) => isNotLazy(match, options, "audios") ? match.replace("]*?>/g, to: (match) => isNotLazy(match, options, "iframes") ? match.replace("]*?>/g, to: (match) => isNotLazy(match, options, "images") ? match : match.replace("]*?>/g, to: (match) => isNotLazy(match, options, "videos") ? match : match.replace("]*?>/g, to: (match) => isNotLazy(match, options, "audios") ? match : match.replace("]*?>/g, to: (match) => isNotLazy(match, options, "iframes") ? match : match.replace(" `export const options = ${JSON.stringify(options, void 0, 2)}` }); } } }); export { module as default };