skypager-project-types-electron-app
Version:
skypager electron app project type
94 lines (76 loc) • 2.51 kB
JavaScript
const mapValues = require('lodash/mapValues')
const scriptFixer = `
(function hackModuleInHtmlForVirtualMachines () {
if (typeof module === 'undefined' || typeof __dirname === 'undefined' || typeof __filename === 'undefined') return
const path = require('path')
// All modules except the main module should return
if (!module.filename || path.isAbsolute(module.filename)) return
// Only the main module should go here
// The root of a normal path: '' or 'Z:'
const root = process.cwd().split(path.sep).shift()
// Virtual machines may have 'Shared Folders' as the root
const VMRoot = __dirname.split(path.sep).shift()
__dirname = root + __dirname.slice(VMRoot.length)
__filename = root + __filename.slice(VMRoot.length)
module.filename = __filename
if (module.paths) {
const VMPrefixLen = path.resolve(VMRoot).length
module.paths.forEach((p, i) => {
module.paths[i] = root + p.slice(VMPrefixLen)
})
}
})()
`
module.exports = function(params = {}) {
const {
stylesheets = [],
headScripts = [],
dllScripts = [],
content = '',
htmlClass = '',
bodyClass = '',
bodyId = '',
chunks = [],
headTop = '',
headBottom = '',
bodyTop = '',
bodyBottom = '',
initialState = null
} = params
const publicPath = params.publicPath || ''
const dllPublicPath = params.dllPublicPath || ''
const styleTags = stylesheets.map(href =>
`<link class='template-stylesheet' rel="stylesheet" href="${publicPath}${href}" />`
)
const dllTags = dllScripts.map(href =>
`<script class='dll' src="${dllPublicPath}${href}"></script>`
)
const scriptTags = headScripts.map(href =>
`<script class='head' src="${publicPath}${href}"></script>`
)
const chunkTags = chunks.map(href =>
`<script class='chunk' src="${publicPath}${href}"></script>`
)
const injectState = typeof initialState === 'object'
? `try { window.__INITIAL_STATE__ = ${JSON.stringify(initialState)}; } catch(error) { console.log('Error injecting initial state', error) }`
: ''
return (`
<html lang="en" class="${htmlClass}">
<head>
<meta charset="UTF-8" />
<script> ${scriptFixer} </script>
${headTop}
${dllTags.join("\n")}
${styleTags.join("\n")}
${scriptTags.join("\n")}
${headBottom}
<script>${injectState}</script>
</head>
<body id="${bodyId}" class="${bodyClass}">
${bodyTop}
<div id="app">${content}</div>
${chunkTags.join("\n")}
${bodyBottom}
</body>
</html>`).trim()
}