@cnguu/vite-plugin-uni-cdn 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/README.md +65 -0
- package/dist/index.cjs +28 -21
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +28 -21
- package/dist/package.json +55 -0
- package/package.json +1 -1
package/dist/README.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# vite-plugin-uni-cdn
|
|
2
|
+
|
|
3
|
+
Vite 插件,在 UniApp 中替换静态资源链接为 CDN 链接
|
|
4
|
+
|
|
5
|
+
[](https://github.com/cnguu/vite-plugin-uni-cdn/releases)
|
|
6
|
+
[](https://github.com/cnguu/vite-plugin-uni-cdn/blob/main/LICENSE)
|
|
7
|
+
|
|
8
|
+
## 安装
|
|
9
|
+
|
|
10
|
+
```shell
|
|
11
|
+
pnpm i -D @cnguu/vite-plugin-uni-cdn
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## 使用
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
// vite.config.ts
|
|
18
|
+
import UniCdn from '@cnguu/vite-plugin-uni-cdn'
|
|
19
|
+
import Uni from '@dcloudio/vite-plugin-uni'
|
|
20
|
+
import { defineConfig } from 'vite'
|
|
21
|
+
|
|
22
|
+
export default defineConfig({
|
|
23
|
+
plugins: [
|
|
24
|
+
UniCdn({
|
|
25
|
+
cdn: 'https://cdn.jsdelivr.net/gh/cnguu/vite-plugin-uni-cdn@main/packages/playground/src/static/cdn',
|
|
26
|
+
sourceDir: 'src/static/cdn',
|
|
27
|
+
}),
|
|
28
|
+
Uni(),
|
|
29
|
+
],
|
|
30
|
+
})
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
> 目前只支持绝对路径,如 `/static/xxx/xxx.png`
|
|
34
|
+
|
|
35
|
+
## 参数
|
|
36
|
+
|
|
37
|
+
### `cdn`
|
|
38
|
+
|
|
39
|
+
- cdn 地址
|
|
40
|
+
- 默认 `''`
|
|
41
|
+
|
|
42
|
+
### `sourceDir`
|
|
43
|
+
|
|
44
|
+
- 替换资源目录,不在该目录下的资源不会被替换
|
|
45
|
+
- 默认 `'static/cdn'`
|
|
46
|
+
|
|
47
|
+
### `include`
|
|
48
|
+
|
|
49
|
+
- 扫描白名单 GLOB 格式
|
|
50
|
+
- 默认 `['**/*.{vue,css,scss,sass,less,styl}']`
|
|
51
|
+
|
|
52
|
+
### `exclude`
|
|
53
|
+
|
|
54
|
+
- 扫描黑名单 GLOB 格式
|
|
55
|
+
- 默认 `['**/node_modules/**', '**/uni_modules/**', '**/dist/**', '**/unpackage/**']`
|
|
56
|
+
|
|
57
|
+
### `deleteOutputFiles`
|
|
58
|
+
|
|
59
|
+
- 是否删除替换资源目录对应的输出目录
|
|
60
|
+
- 默认 `true`
|
|
61
|
+
|
|
62
|
+
### `verbose`
|
|
63
|
+
|
|
64
|
+
- 是否输出命令行信息
|
|
65
|
+
- 默认 `true`
|
package/dist/index.cjs
CHANGED
|
@@ -1060,7 +1060,8 @@ function createConsola(options = {}) {
|
|
|
1060
1060
|
}
|
|
1061
1061
|
const consola = createConsola();
|
|
1062
1062
|
|
|
1063
|
-
|
|
1063
|
+
const PLUGIN_NAME = "vite-plugin-uni-cdn";
|
|
1064
|
+
function createLogger(verbose) {
|
|
1064
1065
|
const prefix = chalk.blue.bold(`
|
|
1065
1066
|
[${PLUGIN_NAME}]`);
|
|
1066
1067
|
return {
|
|
@@ -1087,37 +1088,43 @@ function createLogger(PLUGIN_NAME, verbose) {
|
|
|
1087
1088
|
function escapeRegExp(str) {
|
|
1088
1089
|
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
1089
1090
|
}
|
|
1091
|
+
function isInvalidOriginalPath(originalPath) {
|
|
1092
|
+
return originalPath.startsWith("http") || originalPath.startsWith("data:");
|
|
1093
|
+
}
|
|
1094
|
+
function codeReplaceMatch(assetDir, cdnBasePath, logger, match, quote, originalPath, css = false) {
|
|
1095
|
+
if (isInvalidOriginalPath(originalPath)) {
|
|
1096
|
+
return match;
|
|
1097
|
+
}
|
|
1098
|
+
let relativePath = originalPath.startsWith(assetDir) ? originalPath.slice(assetDir.length) : originalPath;
|
|
1099
|
+
if (!relativePath.startsWith("/")) {
|
|
1100
|
+
relativePath = `/${relativePath}`;
|
|
1101
|
+
}
|
|
1102
|
+
const outputFileName = `${cdnBasePath}${relativePath}`;
|
|
1103
|
+
logger.pathReplace(originalPath, outputFileName);
|
|
1104
|
+
if (css) {
|
|
1105
|
+
return `url(${quote || ""}${outputFileName}${quote || ""})`;
|
|
1106
|
+
}
|
|
1107
|
+
return `${quote}${outputFileName}${quote}`;
|
|
1108
|
+
}
|
|
1090
1109
|
function replaceStaticToCdn(code, assetDir, cdnBasePath, logger) {
|
|
1091
1110
|
const escapedStaticPrefix = escapeRegExp(assetDir);
|
|
1092
|
-
|
|
1111
|
+
let transformed = code.replace(new RegExp(
|
|
1093
1112
|
`url\\(\\s*(['"]?)(${escapedStaticPrefix}[^'")\\s]+)\\1\\s*\\)`,
|
|
1094
1113
|
"g"
|
|
1095
|
-
)
|
|
1096
|
-
let transformed = code.replace(cssUrlRE, (match, quote, originalPath) => {
|
|
1114
|
+
), (match, quote, originalPath) => {
|
|
1097
1115
|
try {
|
|
1098
|
-
|
|
1099
|
-
return match;
|
|
1100
|
-
}
|
|
1101
|
-
const outputFileName = `${cdnBasePath}${originalPath}`;
|
|
1102
|
-
logger.pathReplace(originalPath, outputFileName);
|
|
1103
|
-
return `url(${quote || ""}${outputFileName}${quote || ""})`;
|
|
1116
|
+
return codeReplaceMatch(assetDir, cdnBasePath, logger, match, quote, originalPath, true);
|
|
1104
1117
|
} catch (error) {
|
|
1105
1118
|
logger.error(`\u5904\u7406 CSS \u5931\u8D25`, error);
|
|
1106
1119
|
return match;
|
|
1107
1120
|
}
|
|
1108
1121
|
});
|
|
1109
|
-
|
|
1122
|
+
transformed = transformed.replace(new RegExp(
|
|
1110
1123
|
`(['"])(${escapedStaticPrefix}[^'"]*)\\1`,
|
|
1111
1124
|
"g"
|
|
1112
|
-
)
|
|
1113
|
-
transformed = transformed.replace(stringRE, (match, quote, originalPath) => {
|
|
1125
|
+
), (match, quote, originalPath) => {
|
|
1114
1126
|
try {
|
|
1115
|
-
|
|
1116
|
-
return match;
|
|
1117
|
-
}
|
|
1118
|
-
const outputFileName = `${cdnBasePath}${originalPath}`;
|
|
1119
|
-
logger.pathReplace(originalPath, outputFileName);
|
|
1120
|
-
return `${quote}${outputFileName}${quote}`;
|
|
1127
|
+
return codeReplaceMatch(assetDir, cdnBasePath, logger, match, quote, originalPath);
|
|
1121
1128
|
} catch (error) {
|
|
1122
1129
|
logger.error(`\u5904\u7406\u5B57\u7B26\u4E32\u5931\u8D25`, error);
|
|
1123
1130
|
return match;
|
|
@@ -1125,8 +1132,8 @@ function replaceStaticToCdn(code, assetDir, cdnBasePath, logger) {
|
|
|
1125
1132
|
});
|
|
1126
1133
|
return transformed;
|
|
1127
1134
|
}
|
|
1135
|
+
|
|
1128
1136
|
function UniCdn(opt) {
|
|
1129
|
-
const PLUGIN_NAME = "vite-plugin-uni-cdn";
|
|
1130
1137
|
const defaultOption = {
|
|
1131
1138
|
cdn: "",
|
|
1132
1139
|
sourceDir: "static/cdn",
|
|
@@ -1140,7 +1147,7 @@ function UniCdn(opt) {
|
|
|
1140
1147
|
if (!cdnBasePath || !options.sourceDir) {
|
|
1141
1148
|
return { name: PLUGIN_NAME };
|
|
1142
1149
|
}
|
|
1143
|
-
const logger = createLogger(
|
|
1150
|
+
const logger = createLogger(options.verbose ?? true);
|
|
1144
1151
|
const filter = vite.createFilter(options.include, options.exclude);
|
|
1145
1152
|
let isSrc = false;
|
|
1146
1153
|
let projectRoot = "";
|
package/dist/index.d.cts
CHANGED
|
@@ -6,7 +6,7 @@ interface VitePluginUniCdnOption {
|
|
|
6
6
|
*/
|
|
7
7
|
cdn?: string;
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
9
|
+
* 替换资源目录,不在该目录下的资源不会被替换
|
|
10
10
|
*/
|
|
11
11
|
sourceDir?: string;
|
|
12
12
|
/**
|
|
@@ -26,7 +26,7 @@ interface VitePluginUniCdnOption {
|
|
|
26
26
|
*/
|
|
27
27
|
verbose?: boolean;
|
|
28
28
|
}
|
|
29
|
+
|
|
29
30
|
declare function UniCdn(opt: VitePluginUniCdnOption): Plugin;
|
|
30
31
|
|
|
31
32
|
export = UniCdn;
|
|
32
|
-
export type { VitePluginUniCdnOption };
|
package/dist/index.d.mts
CHANGED
|
@@ -6,7 +6,7 @@ interface VitePluginUniCdnOption {
|
|
|
6
6
|
*/
|
|
7
7
|
cdn?: string;
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
9
|
+
* 替换资源目录,不在该目录下的资源不会被替换
|
|
10
10
|
*/
|
|
11
11
|
sourceDir?: string;
|
|
12
12
|
/**
|
|
@@ -26,7 +26,7 @@ interface VitePluginUniCdnOption {
|
|
|
26
26
|
*/
|
|
27
27
|
verbose?: boolean;
|
|
28
28
|
}
|
|
29
|
+
|
|
29
30
|
declare function UniCdn(opt: VitePluginUniCdnOption): Plugin;
|
|
30
31
|
|
|
31
32
|
export { UniCdn as default };
|
|
32
|
-
export type { VitePluginUniCdnOption };
|
package/dist/index.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ interface VitePluginUniCdnOption {
|
|
|
6
6
|
*/
|
|
7
7
|
cdn?: string;
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
9
|
+
* 替换资源目录,不在该目录下的资源不会被替换
|
|
10
10
|
*/
|
|
11
11
|
sourceDir?: string;
|
|
12
12
|
/**
|
|
@@ -26,7 +26,7 @@ interface VitePluginUniCdnOption {
|
|
|
26
26
|
*/
|
|
27
27
|
verbose?: boolean;
|
|
28
28
|
}
|
|
29
|
+
|
|
29
30
|
declare function UniCdn(opt: VitePluginUniCdnOption): Plugin;
|
|
30
31
|
|
|
31
32
|
export = UniCdn;
|
|
32
|
-
export type { VitePluginUniCdnOption };
|
package/dist/index.mjs
CHANGED
|
@@ -1053,7 +1053,8 @@ function createConsola(options = {}) {
|
|
|
1053
1053
|
}
|
|
1054
1054
|
const consola = createConsola();
|
|
1055
1055
|
|
|
1056
|
-
|
|
1056
|
+
const PLUGIN_NAME = "vite-plugin-uni-cdn";
|
|
1057
|
+
function createLogger(verbose) {
|
|
1057
1058
|
const prefix = chalk.blue.bold(`
|
|
1058
1059
|
[${PLUGIN_NAME}]`);
|
|
1059
1060
|
return {
|
|
@@ -1080,37 +1081,43 @@ function createLogger(PLUGIN_NAME, verbose) {
|
|
|
1080
1081
|
function escapeRegExp(str) {
|
|
1081
1082
|
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
1082
1083
|
}
|
|
1084
|
+
function isInvalidOriginalPath(originalPath) {
|
|
1085
|
+
return originalPath.startsWith("http") || originalPath.startsWith("data:");
|
|
1086
|
+
}
|
|
1087
|
+
function codeReplaceMatch(assetDir, cdnBasePath, logger, match, quote, originalPath, css = false) {
|
|
1088
|
+
if (isInvalidOriginalPath(originalPath)) {
|
|
1089
|
+
return match;
|
|
1090
|
+
}
|
|
1091
|
+
let relativePath = originalPath.startsWith(assetDir) ? originalPath.slice(assetDir.length) : originalPath;
|
|
1092
|
+
if (!relativePath.startsWith("/")) {
|
|
1093
|
+
relativePath = `/${relativePath}`;
|
|
1094
|
+
}
|
|
1095
|
+
const outputFileName = `${cdnBasePath}${relativePath}`;
|
|
1096
|
+
logger.pathReplace(originalPath, outputFileName);
|
|
1097
|
+
if (css) {
|
|
1098
|
+
return `url(${quote || ""}${outputFileName}${quote || ""})`;
|
|
1099
|
+
}
|
|
1100
|
+
return `${quote}${outputFileName}${quote}`;
|
|
1101
|
+
}
|
|
1083
1102
|
function replaceStaticToCdn(code, assetDir, cdnBasePath, logger) {
|
|
1084
1103
|
const escapedStaticPrefix = escapeRegExp(assetDir);
|
|
1085
|
-
|
|
1104
|
+
let transformed = code.replace(new RegExp(
|
|
1086
1105
|
`url\\(\\s*(['"]?)(${escapedStaticPrefix}[^'")\\s]+)\\1\\s*\\)`,
|
|
1087
1106
|
"g"
|
|
1088
|
-
)
|
|
1089
|
-
let transformed = code.replace(cssUrlRE, (match, quote, originalPath) => {
|
|
1107
|
+
), (match, quote, originalPath) => {
|
|
1090
1108
|
try {
|
|
1091
|
-
|
|
1092
|
-
return match;
|
|
1093
|
-
}
|
|
1094
|
-
const outputFileName = `${cdnBasePath}${originalPath}`;
|
|
1095
|
-
logger.pathReplace(originalPath, outputFileName);
|
|
1096
|
-
return `url(${quote || ""}${outputFileName}${quote || ""})`;
|
|
1109
|
+
return codeReplaceMatch(assetDir, cdnBasePath, logger, match, quote, originalPath, true);
|
|
1097
1110
|
} catch (error) {
|
|
1098
1111
|
logger.error(`\u5904\u7406 CSS \u5931\u8D25`, error);
|
|
1099
1112
|
return match;
|
|
1100
1113
|
}
|
|
1101
1114
|
});
|
|
1102
|
-
|
|
1115
|
+
transformed = transformed.replace(new RegExp(
|
|
1103
1116
|
`(['"])(${escapedStaticPrefix}[^'"]*)\\1`,
|
|
1104
1117
|
"g"
|
|
1105
|
-
)
|
|
1106
|
-
transformed = transformed.replace(stringRE, (match, quote, originalPath) => {
|
|
1118
|
+
), (match, quote, originalPath) => {
|
|
1107
1119
|
try {
|
|
1108
|
-
|
|
1109
|
-
return match;
|
|
1110
|
-
}
|
|
1111
|
-
const outputFileName = `${cdnBasePath}${originalPath}`;
|
|
1112
|
-
logger.pathReplace(originalPath, outputFileName);
|
|
1113
|
-
return `${quote}${outputFileName}${quote}`;
|
|
1120
|
+
return codeReplaceMatch(assetDir, cdnBasePath, logger, match, quote, originalPath);
|
|
1114
1121
|
} catch (error) {
|
|
1115
1122
|
logger.error(`\u5904\u7406\u5B57\u7B26\u4E32\u5931\u8D25`, error);
|
|
1116
1123
|
return match;
|
|
@@ -1118,8 +1125,8 @@ function replaceStaticToCdn(code, assetDir, cdnBasePath, logger) {
|
|
|
1118
1125
|
});
|
|
1119
1126
|
return transformed;
|
|
1120
1127
|
}
|
|
1128
|
+
|
|
1121
1129
|
function UniCdn(opt) {
|
|
1122
|
-
const PLUGIN_NAME = "vite-plugin-uni-cdn";
|
|
1123
1130
|
const defaultOption = {
|
|
1124
1131
|
cdn: "",
|
|
1125
1132
|
sourceDir: "static/cdn",
|
|
@@ -1133,7 +1140,7 @@ function UniCdn(opt) {
|
|
|
1133
1140
|
if (!cdnBasePath || !options.sourceDir) {
|
|
1134
1141
|
return { name: PLUGIN_NAME };
|
|
1135
1142
|
}
|
|
1136
|
-
const logger = createLogger(
|
|
1143
|
+
const logger = createLogger(options.verbose ?? true);
|
|
1137
1144
|
const filter = createFilter(options.include, options.exclude);
|
|
1138
1145
|
let isSrc = false;
|
|
1139
1146
|
let projectRoot = "";
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cnguu/vite-plugin-uni-cdn",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.0.1",
|
|
5
|
+
"description": "Vite 插件,在 UniApp 中替换资源链接为 CDN 链接",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://github.com/cnguu/vite-plugin-uni-cdn",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/cnguu/vite-plugin-uni-cdn.git"
|
|
11
|
+
},
|
|
12
|
+
"bugs": "https://github.com/cnguu/vite-plugin-uni-cdn/issues",
|
|
13
|
+
"keywords": [
|
|
14
|
+
"vite",
|
|
15
|
+
"plugin",
|
|
16
|
+
"uni",
|
|
17
|
+
"uniapp",
|
|
18
|
+
"cdn"
|
|
19
|
+
],
|
|
20
|
+
"maintainers": [
|
|
21
|
+
{
|
|
22
|
+
"name": "cnguu",
|
|
23
|
+
"email": "gu642779596@gmail.com",
|
|
24
|
+
"url": "https://github.com/cnguu"
|
|
25
|
+
}
|
|
26
|
+
],
|
|
27
|
+
"sideEffects": false,
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"import": {
|
|
31
|
+
"types": "./index.d.ts",
|
|
32
|
+
"default": "./dist/index.mjs"
|
|
33
|
+
},
|
|
34
|
+
"require": {
|
|
35
|
+
"types": "./index.d.ts",
|
|
36
|
+
"default": "./dist/index.cjs"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"main": "./dist/index.cjs",
|
|
41
|
+
"module": "./dist/index.mjs",
|
|
42
|
+
"types": "./index.d.ts",
|
|
43
|
+
"files": [
|
|
44
|
+
"dist",
|
|
45
|
+
"index.d.ts"
|
|
46
|
+
],
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "unbuild",
|
|
49
|
+
"debug": "cross-env NODE_ENV=development unbuild --sourcemap",
|
|
50
|
+
"stub": "unbuild --stub"
|
|
51
|
+
},
|
|
52
|
+
"peerDependencies": {
|
|
53
|
+
"vite": "^5.0.0"
|
|
54
|
+
}
|
|
55
|
+
}
|
package/package.json
CHANGED