From bcce9705ed40644cf8ed20324a00056e8c9e8790 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Sat, 23 Jan 2021 13:34:54 -0500 Subject: [PATCH] feat(html): ensure /public imports are passed to `urlToBuiltUrl` ...and any local non-module ` + if (!assetRefs.length && node.children.length) { + js += `\nimport "${id}?html-proxy&index=${inlineModuleIndex}.js"` + shouldRemove = true + } + } + + for (const assetRef of assetRefs) { + const url = assetRef.value?.content + if (!url || isExcludedUrl(url)) { + continue } - if (isModule) { - inlineModuleIndex++ - if (url && !isExcludedUrl(url)) { - // - js += `\nimport "${id}?html-proxy&index=${inlineModuleIndex}.js"` + } + // CSS references + else if (node.tag === 'link' && isCSSRequest(url)) { + js += `\nimport ${JSON.stringify(url)}` shouldRemove = true } } - } - // For asset references in index.html, also generate an import - // statement for each - this will be handled by the asset plugin - const assetAttrs = assetAttrsConfig[node.tag] - if (assetAttrs) { - for (const p of node.props) { - if ( - p.type === NodeTypes.ATTRIBUTE && - p.value && - assetAttrs.includes(p.name) - ) { - const url = p.value.content - if (!isExcludedUrl(url)) { - if (node.tag === 'link' && isCSSRequest(url)) { - // CSS references, convert to import - js += `\nimport ${JSON.stringify(url)}` - shouldRemove = true - } else { - assetUrls.push(p) - } - } else if (checkPublicFile(url, config.root)) { - s.overwrite( - p.value.loc.start.offset, - p.value.loc.end.offset, - config.base + url.slice(1) - ) - } + // for each encountered asset url, rewrite original html so that it + // references the post-build location. + if (!shouldRemove) { + const builtUrl = await urlToBuiltUrl(url, id, config, this) + if (url !== builtUrl) { + const { loc } = assetRef.value! + s.overwrite( + loc.start.offset, + loc.end.offset, + JSON.stringify(builtUrl) + ) } } } + // removed tags will be injected at the end if (shouldRemove) { - // remove the script tag from the html. we are going to inject new - // ones in the end. s.remove(node.loc.start.offset, node.loc.end.offset) } - }) - - // for each encountered asset url, rewrite original html so that it - // references the post-build location. - for (const attr of assetUrls) { - const value = attr.value! - const url = await urlToBuiltUrl(value.content, id, config, this) - s.overwrite( - value.loc.start.offset, - value.loc.end.offset, - JSON.stringify(url) - ) } processedHtml.set(id, s.toString())