Skip to content

How to set up config for dynamic imports when entry file and chunks are loaded from a CDN? #18097

Answered by jongeyer
jongeyer asked this question in Q&A
Discussion options

You must be logged in to vote

Aha! So I was not using renderBuildUrl. But then as I was reading the docs on that more carefully I realized that's exactly what I needed. It took some trial and error with the code outlined here, but looks like I got it working for both JS and CSS chunks.

// vite.config
experimental: {
    renderBuiltUrl(filename, { hostType }) {
      if (hostType === 'js') {
        return {
          runtime: `window.__toCdnUrl(${JSON.stringify(filename)})`
        }
      } else {
        return { relative: true }
      }
    }
  },

then in my proxy module (to get the CDN URL at runtime)

const loaderPathFile = 'assets/gen-lp-loader.js'
const loaderSrc = document.querySelector('#MainContent script').…

Replies: 2 comments 6 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
6 replies
@sapphi-red
Comment options

@jongeyer
Comment options

@sapphi-red
Comment options

@jongeyer
Comment options

Answer selected by jongeyer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants