@route-resource-preload/webpack-plugin
Version:
Focus on improving the first screen loading speed of applications and providing the best user experience.
106 lines (105 loc) • 4 kB
JavaScript
const x = "route-resource-preload-manifest.json", z = "webpack-route-resource-preload";
function b(i) {
return /\.js$/i.test(i) ? "script" : /\.(svg|jpe?g|png|webp)$/i.test(i) ? "image" : /\.(woff2?|otf|ttf|eot)$/i.test(i) ? "font" : /\.css$/i.test(i) ? "style" : !1;
}
function v(i) {
let s = "";
return i.forEach((d) => {
s && (s += ", "), s += `<${d.href}>; rel=preload; as=${d.type}`, /^(font|script)$/.test(d.type) && (s += "; crossorigin=anonymous");
}), [{ key: "Link", value: s }];
}
function O(i) {
if (typeof i == "function")
return i;
if (typeof i == "object")
return (s) => i[s];
}
class G {
constructor(s) {
const { assets: d, headers: k, modulePreloadMap: g, mfPreloadMap: E, assetPreloadMap: P, exclude: h } = s || {}, { filename: J = x, minify: K = !0, publicPath: y = "", inline: R = !0 } = s || {};
let { routes: p } = s || {};
if (!p && !g && !E && !P)
throw new Error("One of routes/modulePreloadMap/mfPreloadMap/assetPreloadMap mapping is required");
const M = g ? Object.keys(g) : [], N = E ? Object.keys(E) : [], A = P ? Object.keys(P) : [];
try {
!p && M.length && (p = (c) => {
for (var f = 0; f < M.length; f++) {
const l = M[f];
if (!g[l])
throw new Error(`404: ${l} Not Found, please check your router.`);
if (g[l].includes(c))
return l;
}
return c;
});
} catch (c) {
throw new Error(c);
}
p || (p = (c) => c);
const S = O(p), j = O(k) || k === !0 && v, F = O(d) || b;
this.run = (c) => {
const f = /* @__PURE__ */ new Map(), l = {}, o = {};
try {
let w = function(e) {
if (R) {
const r = o["*"].find((u) => u.type === "script"), n = y || "/", a = r.href.replace(n, ""), m = c.assets[a];
if (m) {
let u = `window.__routerResourcePreloadManifest=${JSON.stringify(e)};`;
u += m.source(), c.assets[a] = {
size: () => u.length,
source: () => u
};
}
}
const t = JSON.stringify(e, null, K ? 0 : 2);
c.assets[J] = {
size: () => t.length,
source: () => t
};
};
const L = c.getStats().toJson(), { chunks: _, modules: q } = L;
_.forEach((e) => {
const { id: t, files: r, origins: n, entry: a } = e, m = n[0].request, u = m && !a ? S(m) : "*";
u && f.set(t, {
assets: new Set(r),
pattern: u
});
}), q.forEach((e) => {
e.assets.forEach((t) => {
e.chunks.forEach((r) => {
const n = f.get(r);
n && (n.assets.add(t), f.set(r, n));
});
});
}), f.forEach((e) => {
let t = o[e.pattern] = o[e.pattern] || [];
e.assets.forEach((r) => {
let n = F(r), a = y.endsWith("/") ? y + r : `${y}/${r}`;
n && (!h || !h.test(a)) && t.push({ type: n, href: a });
});
});
const $ = Object.keys(o);
N.length && $.length && N.forEach((e) => {
$.forEach((t) => {
E[e].includes(t) && (!h || !h.test(t)) && (o[e] instanceof Array || (o[e] = []), o[e].push({ type: "mf", href: t }), delete o[t]);
});
}), A.length && $.length && A.forEach((e) => {
var r, n;
const t = (n = (r = P[e]) == null ? void 0 : r.filter((a) => !h || !h.test(a))) == null ? void 0 : n.map((a) => ({ type: F(a), href: a }));
o[e] && t instanceof Array ? o[e] = o[e].concat(t) : o[e] = t;
}), j ? ($.forEach((e) => {
const t = o[e], r = j(t, e, o) || [];
l[e] = { files: t, headers: r };
}), w(l)) : w(o);
} catch (w) {
c.errors.push(new Error(w));
}
};
}
apply(s) {
s.hooks !== void 0 ? s.hooks.emit.tap(z, this.run) : s.plugin("emit", this.run);
}
}
export {
G as default
};