Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// script (external or inline)
if (asset.type === 'script') {
const scriptInBody = (asset.options && asset.options.target === 'body') || false
const scriptTag = HASTBuilers.createHTMLNode('script')
HASTUtils.addAttributeToNode(scriptTag, 'type', 'text/javascript')
if (assetPath) {
HASTUtils.addAttributeToNode(scriptTag, 'src', assetPath)
if (asset.options && asset.options.defer) {
HASTUtils.addBooleanAttributeToNode(scriptTag, 'defer')
}
if (asset.options && asset.options.async) {
HASTUtils.addBooleanAttributeToNode(scriptTag, 'async')
}
} else if (asset.content) {
HASTUtils.addTextNode(scriptTag, asset.content)
}
if (scriptInBody) {
HASTUtils.addChildNode(bodyNode, scriptTag)
} else {
HASTUtils.addChildNode(headNode, scriptTag)
}
}
// icon
if (asset.type === 'icon' && assetPath) {
const iconTag = HASTBuilers.createHTMLNode('link')
HASTUtils.addAttributeToNode(iconTag, 'rel', 'shortcut icon')
HASTUtils.addAttributeToNode(iconTag, 'href', assetPath)
if (asset.options && asset.options.iconType) {
HASTUtils.addAttributeToNode(iconTag, 'type', asset.options.iconType)
HASTUtils.addAttributeToNode(linkTag, 'href', assetPath)
HASTUtils.addChildNode(headNode, linkTag)
}
// link stylesheet (external css, font)
if ((asset.type === 'style' || asset.type === 'font') && assetPath) {
const linkTag = HASTBuilers.createHTMLNode('link')
HASTUtils.addAttributeToNode(linkTag, 'rel', 'stylesheet')
HASTUtils.addAttributeToNode(linkTag, 'href', assetPath)
HASTUtils.addChildNode(headNode, linkTag)
}
// inline style
if (asset.type === 'style' && asset.content) {
const styleTag = HASTBuilers.createHTMLNode('style')
HASTUtils.addTextNode(styleTag, asset.content)
HASTUtils.addChildNode(headNode, styleTag)
}
// script (external or inline)
if (asset.type === 'script') {
const scriptInBody = (asset.options && asset.options.target === 'body') || false
const scriptTag = HASTBuilers.createHTMLNode('script')
HASTUtils.addAttributeToNode(scriptTag, 'type', 'text/javascript')
if (assetPath) {
HASTUtils.addAttributeToNode(scriptTag, 'src', assetPath)
if (asset.options && asset.options.defer) {
HASTUtils.addBooleanAttributeToNode(scriptTag, 'defer')
}
if (asset.options && asset.options.async) {
HASTUtils.addBooleanAttributeToNode(scriptTag, 'async')
}
// Nuxt has an internal templating so requires an override
if (appRootOverride) {
HASTUtils.addTextNode(bodyNode, appRootOverride)
} else {
const appRootNode = HASTBuilers.createHTMLNode('div')
HASTUtils.addAttributeToNode(appRootNode, 'id', 'app')
HASTUtils.addChildNode(bodyNode, appRootNode)
}
if (settings.language) {
HASTUtils.addAttributeToNode(htmlNode, 'lang', settings.language)
}
if (settings.title) {
const titleTag = HASTBuilers.createHTMLNode('title')
HASTUtils.addTextNode(titleTag, settings.title)
HASTUtils.addChildNode(headNode, titleTag)
}
/* For frameworks that need to inject and point out the generated build files
or adding some script tags in head or body */
if (customTags.length > 0) {
customTags.forEach((tag: CustomTag) => {
const { targetTag, tagName, attributes, content } = tag
const targetNode = targetTag === 'head' ? headNode : bodyNode
const createdNode = HASTBuilers.createHTMLNode(tagName)
if (content) {
HASTUtils.addTextNode(createdNode, content)
}
if (attributes && attributes.length > 0) {
const createHTMLEntryFileChunks = (uidl: ProjectUIDL, options: EntryFileOptions) => {
const { assetsPrefix = '', appRootOverride, customHeadContent, customTags } = options
const { settings, meta, assets, manifest } = uidl.globals
const htmlNode = HASTBuilers.createHTMLNode('html')
const headNode = HASTBuilers.createHTMLNode('head')
const bodyNode = HASTBuilers.createHTMLNode('body')
HASTUtils.addChildNode(htmlNode, headNode)
HASTUtils.addChildNode(htmlNode, bodyNode)
// Vue and React use a standard <div id="app"> in the body tag.
// Nuxt has an internal templating so requires an override
if (appRootOverride) {
HASTUtils.addTextNode(bodyNode, appRootOverride)
} else {
const appRootNode = HASTBuilers.createHTMLNode('div')
HASTUtils.addAttributeToNode(appRootNode, 'id', 'app')
HASTUtils.addChildNode(bodyNode, appRootNode)
}
if (settings.language) {
HASTUtils.addAttributeToNode(htmlNode, 'lang', settings.language)
}
if (settings.title) {
const titleTag = HASTBuilers.createHTMLNode('title')
HASTUtils.addTextNode(titleTag, settings.title)
HASTUtils.addChildNode(headNode, titleTag)
}
</div>
const iconTag = HASTBuilers.createHTMLNode('link')
HASTUtils.addAttributeToNode(iconTag, 'rel', 'shortcut icon')
HASTUtils.addAttributeToNode(iconTag, 'href', assetPath)
if (asset.options && asset.options.iconType) {
HASTUtils.addAttributeToNode(iconTag, 'type', asset.options.iconType)
}
if (asset.options && asset.options.iconSizes) {
HASTUtils.addAttributeToNode(iconTag, 'sizes', asset.options.iconSizes)
}
HASTUtils.addChildNode(headNode, iconTag)
}
})
if (customHeadContent) {
HASTUtils.addTextNode(headNode, customHeadContent)
}
const chunks: Record = {
[FileType.HTML]: [
{
name: 'doctype',
type: ChunkType.STRING,
fileType: FileType.HTML,
content: '',
linkAfter: [],
},
{
name: 'html-node',
type: ChunkType.HAST,
fileType: FileType.HTML,
content: htmlNode,
customTags.forEach((tag: CustomTag) => {
const { targetTag, tagName, attributes, content } = tag
const targetNode = targetTag === 'head' ? headNode : bodyNode
const createdNode = HASTBuilers.createHTMLNode(tagName)
if (content) {
HASTUtils.addTextNode(createdNode, content)
}
if (attributes && attributes.length > 0) {
attributes.forEach((attribute: Attribute) => {
const { attributeKey, attributeValue } = attribute
if (attributeValue) {
HASTUtils.addAttributeToNode(createdNode, attributeKey, attributeValue)
} else {
HASTUtils.addBooleanAttributeToNode(createdNode, attributeKey)
}
})
}
HASTUtils.addChildNode(targetNode, createdNode)
})
}