How to use CDN #6846
Answered
by
erlangera
PeterlitsZo
asked this question in
Q&A
How to use CDN
#6846
-
|
Hi everyone. I am going to let my Firstly I try Then I find this document (rollup's output paths), it looks great and I try to set Thanks if you help or not help me 👍 UPDATES:
|
Beta Was this translation helpful? Give feedback.
Answered by
erlangera
Sep 9, 2022
Replies: 1 comment 8 replies
-
|
I find two ways to use cdn in vite, wish can help you and others.
// vite.config.js
build: {
rollupOptions: {
external: ['vue'],
output: {
paths: {
vue: 'https://unpkg.com/vue@3/dist/vue.esm-browser.js'
}
}
}
}
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite App CDN IIFE</title>
<!-- vite-plugin-html inject -->
<%- cdnScript %>
</head>
<body>
<div id="app"></div>
<script type="module" src="./src/main.js"></script>
</body>
</html>// vite.config.js
import { fileURLToPath, URL } from "node:url";
import vue from "@vitejs/plugin-vue";
import { createHtmlPlugin } from "vite-plugin-html";
export default {
resolve: {
alias: {
"@": fileURLToPath(new URL("./src", import.meta.url)),
},
},
plugins: [
vue(),
createHtmlPlugin({
inject: {
data: {
cdnScript:
process.env.NODE_ENV === "production"
? '<script src="https://unpkg.com/vue@3"></script>'
: "",
},
},
}),
],
build: {
rollupOptions: {
external: ["vue"],
output: {
format: "iife",
globals: {
vue: "Vue",
},
},
},
},
}; |
Beta Was this translation helpful? Give feedback.
8 replies
Answer selected by
PeterlitsZo
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I find two ways to use cdn in vite, wish can help you and others.
esmformat output, you can useexternalandoutput.pathsto include CDN module. Note that the CDN js must be a esm format js. Andimport ** from 'moduleName'will be replace withimport ** from 'CDN_url'example:
externalandoutput.globalsto include CDN module. Note that the CDN js must export module to a global variable.example: