All files / owid-grapher/site webpackUtils.tsx

46.91% Statements 38/81
33.33% Branches 1/3
50% Functions 1/2
46.91% Lines 38/81

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 661x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x                         5x 5x 5x 1x 1x 1x                                                               1x
import * as fs from "fs-extra" // todo: this should not be here.
import urljoin from "url-join"
import * as path from "path"
import { ENV } from "../settings/serverSettings"
 
const WEBPACK_DEV_URL = process.env.WEBPACK_DEV_URL ?? "http://localhost:8090"
const WEBPACK_OUTPUT_PATH =
    process.env.WEBPACK_OUTPUT_PATH ?? path.join(__dirname + "/../", "webpack")
 
let manifest: { [key: string]: string }
export const webpackUrl = (
    assetName: string,
    baseUrl = "",
    isProduction = ENV === "production"
) => {
    if (isProduction) {
        // Read the real asset name from the manifest in case it has a hashed filename
        if (!manifest)
            manifest = JSON.parse(
                fs
                    .readFileSync(
                        path.join(WEBPACK_OUTPUT_PATH, "manifest.json")
                    )
                    .toString("utf8")
            )
        if (baseUrl) return urljoin(baseUrl, "/assets", manifest[assetName])
        else return urljoin("/", "assets", manifest[assetName])
    }
 
    return urljoin(WEBPACK_DEV_URL, assetName)
}
 
export const bakeEmbedSnippet = (
    baseUrl: string
) => `const embedSnippet = () => {
const link = document.createElement('link')
link.type = 'text/css'
link.rel = 'stylesheet'
link.href = '${webpackUrl("commons.css", baseUrl)}'
document.head.appendChild(link)

let loadedScripts = 0;
const checkReady = () => {
    loadedScripts++
    if (loadedScripts === 4)
        window.MultiEmbedderSingleton.embedAll()
}

const coreScripts = [
    'https://polyfill.io/v3/polyfill.min.js?features=es6,fetch,URL,IntersectionObserver,IntersectionObserverEntry',
    '${webpackUrl("commons.js", baseUrl)}',
    '${webpackUrl("vendors.js", baseUrl)}',
    '${webpackUrl("owid.js", baseUrl)}'
]

coreScripts.forEach(url => {
    const script = document.createElement('script')
    script.type = 'text/javascript'
    script.onload = checkReady
    script.src = url
    document.head.appendChild(script)
})
}
embedSnippet()
`