How to use the @open-wc/building-utils/index-html/index.js.extractResources function in @open-wc/building-utils

To help you get started, we’ve selected a few @open-wc/building-utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github open-wc / open-wc / packages / es-dev-server / src / middleware / compatibility.js View on Github external
// return cached index.html if it did not change
    const cache = cachedHTMLs.get(ctx.url);
    if (cache && cache.lastModified === lastModified) {
      ctx.body = cache.body;
      return;
    }

    // for performance we didn't drain the index.html stream for the app index above. if we end up here
    // we do need the whole body content now
    if (!indexHTMLString) {
      indexHTMLString = await getBodyAsString(ctx.body);
    }

    // extract input files from index.html
    const resources = extractResources(indexHTMLString);
    if (resources.inlineModules.length > 0) {
      throw new Error(
        `Compatibility cannot handle "inline" modules (modules without a src attribute). Place your js code in a separate file.`,
      );
    }

    if (!resources.jsModules || resources.jsModules.length === 0) {
      throw new Error(
        `Compatibility mode requires at least one 
github open-wc / open-wc / packages / es-dev-server / src / utils / transform-index-html.js View on Github external
export function getTransformedIndexHTML(cfg) {
  const polyfillModules =
    ([compatibilityModes.AUTO, compatibilityModes.ALWAYS].includes(cfg.compatibilityMode) &&
      !cfg.uaCompat.supportsEsm) ||
    cfg.compatibilityMode === compatibilityModes.MAX;

  // extract input files from index.html
  const resources = extractResources(cfg.indexHTMLString, { removeImportMaps: false });
  /** @type {Map} */
  const inlineModules = new Map();

  resources.inlineModules.forEach((content, i) => {
    inlineModules.set(`inline-module-${i}.js`, content);
  });

  const files = [
    ...resources.jsModules,
    ...[...inlineModules.keys()].map(e => `${e}?source=${encodeURIComponent(cfg.indexUrl)}`),
  ];

  if (files.length === 0) {
    return {
      indexHTML: cfg.indexHTMLString,
      inlineModules: new Map(),