Skip to content

Commit

Permalink
feat: 支持生产环境打包 cdn 加速
Browse files Browse the repository at this point in the history
  • Loading branch information
yulimchen committed Dec 3, 2023
1 parent db939f0 commit 93bbe72
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 11 deletions.
7 changes: 5 additions & 2 deletions .env.production
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# baseUrl
VITE_BASE_API = '/'
VITE_BASE_API = "/"

# 线上环境平台打包路径
VITE_PUBLIC_PATH = /
VITE_PUBLIC_PATH = "/"

# 生产环境是否启用 cdn
VITE_CDN_DEPS = "false"
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
- [x] ESLint
- [x] 首屏加载动画
- [x] 开发环境调试面板
- [ ] TODO: 生产环境 CDN 依赖
- [x] 生产环境 CDN 依赖

**主分支默认 TypeScript,如果你希望使用的是 JavaScript 语言,请切换 [js-version](https://github.com/yulimchen/vue3-h5-template/tree/js-version) 分支进行开发**

Expand Down Expand Up @@ -260,13 +260,9 @@ feat(layout): 布局完成



### - <span id="CDN">CDN 生产环境依赖(TODO)</span>
### - <span id="CDN">CDN 生产环境依赖</span>

本模板生产环境默认 CDN 加载依赖,依赖加载源见根目录 `dependencies-cdn.js` 文件。

**❗ PS.为避免打包后出现不可预估问题,请注意确保生产和开发环境的依赖版本一致!**

> 如需关闭 CDN 依赖,在根目录生产环境变量文件 `.env.production` 中修改 `VUE_APP_CDN_DEPS` 的值为 `false` ,重新打包即可。
本模板生产环境默认不开启 CDN 加载依赖,如需开启生产环境加载 CDN 依赖,在根目录生产环境变量文件 `.env.production` 中修改 `VITE_CDN_DEPS` 的值为 `true` 重新打包即可。



Expand Down
12 changes: 12 additions & 0 deletions build/cdn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { cdn } from "vite-plugin-cdn2";

export function enableCDN(isEnabled: string) {
if (isEnabled === "true") {
return cdn({
// url 可以更换为私有或其他源
// url: "https://cdn.jsdelivr.net/npm/",
url: "https://unpkg.com/",
modules: ["vue", "vue-demi", "pinia", "axios", "vant", "vue-router"]
});
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"typescript": "~5.2.2",
"unplugin-vue-components": "^0.22.12",
"vite": "^4.5.0",
"vite-plugin-cdn2": "^0.15.2",
"vite-plugin-compression": "^0.5.1",
"vite-plugin-html": "^3.2.0",
"vite-plugin-mock-dev-server": "^0.3.21",
Expand Down
117 changes: 117 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"src/**/*.tsx",
"src/**/*.vue",
"types/*.d.ts",
"vite.config.ts"
"vite.config.ts",
"build/**/*.ts",
],
"exclude": [
"node_modules",
Expand Down
5 changes: 4 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import mockDevServerPlugin from "vite-plugin-mock-dev-server";
import vueSetupExtend from "vite-plugin-vue-setup-extend";
import viteCompression from "vite-plugin-compression";
import { createHtmlPlugin } from "vite-plugin-html";
import { enableCDN } from "./build/cdn";

// 当前工作目录路径
const root: string = process.cwd();
Expand Down Expand Up @@ -47,7 +48,9 @@ export default defineConfig(({ mode }) => {
ENABLE_ERUDA: env.VITE_ENABLE_ERUDA || "false"
}
}
})
}),
// 生产环境默认不启用 CDN 加速
enableCDN(env.VITE_CDN_DEPS)
],
resolve: {
alias: {
Expand Down

11 comments on commit 93bbe72

@M69W
Copy link

@M69W M69W commented on 93bbe72 Jan 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

想请教一下这个常量的值类型改为 Boolean:

- VITE_CDN_DEPS = "false"
+ VITE_CDN_DEPS = false
...
- if (isEnabled === "true") {
+ if (isEnabled === true) {

这样会不会更好些?

@yulimchen
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

环境变量中直接读到的值都为 String,其他值类型需要做额外的转换处理

@M69W
Copy link

@M69W M69W commented on 93bbe72 Jan 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

环境变量中直接读到的值都为 String,其他值类型需要做额外的转换处理

想请教一下这个常量的值类型改为 Boolean:

- VITE_CDN_DEPS = "false"
+ VITE_CDN_DEPS = false
...
- if (isEnabled === "true") {
+ if (isEnabled === true) {

这样会不会更好些?

你说的没错,我的示例下面判断不成立

既然默认都是String,再加引号是不是更严谨?
看到一些项目的环境变量几乎不加引号
例如
https://github.com/vbenjs/vue-vben-admin/blob/main/.env.production

@M69W
Copy link

@M69W M69W commented on 93bbe72 Jan 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我提的另外一个疑惑是:
项目里两个环境变量的写法似乎并不统一:有些加了引号,有些并没加引号

@yulimchen
Copy link
Owner Author

@yulimchen yulimchen commented on 93bbe72 Jan 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其实加不加引号都一样,读出来是字符串。为了更直观知道是字符串,我手动加的而已。。像有些项目里后期做了环境变量值转换的话你上面的判断才能成立 https://github.com/pure-admin/vue-pure-admin/blob/main/build/utils.ts#L45

@M69W
Copy link

@M69W M69W commented on 93bbe72 Jan 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感谢答疑

@chenjiapenggeorge
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请教下,VITE_CDN_DEPS改为true后,打包会报错vite-plugin-cdn2: vue-demi can't find module,有遇到过这个问题么

@yulimchen
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不会,启用 cdn 打包后包含了 vue-demi 依赖

@M69W
Copy link

@M69W M69W commented on 93bbe72 Feb 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请教下,VITE_CDN_DEPS改为true后,打包会报错vite-plugin-cdn2: vue-demi can't find module,有遇到过这个问题么

项目中没有直接安装vue-demi,但是pinia用到了,所以需要在引入pinia前引入vue-demi(https://github.com/vuejs/pinia/blob/v2/packages/pinia/package.json#L77)

@chenjiapenggeorge
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请教下,VITE_CDN_DEPS改为true后,打包会报错vite-plugin-cdn2: vue-demi can't find module,有遇到过这个问题么

项目中没有直接安装vue-demi,但是pinia用到了,所以需要在引入pinia前引入vue-demi(https://github.com/vuejs/pinia/blob/v2/packages/pinia/package.json#L77)

感谢回复,项目中是有modules: ["vue", "vue-demi", "pinia", "axios", "vant", "vue-router"]引入的,请问下是还需要安装vue-demi的依赖包,然后在项目中引入吗,方便给下示例么

@yulimchen
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不需要安装 vue-demi,项目 cdn 打包并不会有缺少依赖的报错,需要提供下你代码的 playground 看看

Please sign in to comment.