@taro-minify-pack/plugin-remote-assets 0.1.4-alpha.2 → 0.1.4-alpha.3
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/index.d.ts +0 -1
- package/dist/index.js +6 -5
- package/dist/index.js.map +1 -1
- package/dist/path-transform/path-transform.d.ts +1 -1
- package/dist/path-transform/path-transform.js +1 -1
- package/dist/path-transform/path-transform.js.map +1 -1
- package/dist/types.d.ts +2 -13
- package/dist/utils.d.ts +3 -10
- package/dist/utils.js +1 -24
- package/dist/utils.js.map +1 -1
- package/package.json +3 -4
- package/src/index.ts +6 -6
- package/src/path-transform/path-transform.ts +1 -1
- package/src/types.ts +3 -16
- package/src/utils.ts +3 -24
- package/dist/upload-adapter/define-adapter.d.ts +0 -2
- package/dist/upload-adapter/define-adapter.js +0 -6
- package/dist/upload-adapter/define-adapter.js.map +0 -1
- package/dist/upload-adapter/index.d.ts +0 -2
- package/dist/upload-adapter/index.js +0 -19
- package/dist/upload-adapter/index.js.map +0 -1
- package/dist/upload-adapter/upload-adapter-ali-oss.d.ts +0 -6
- package/dist/upload-adapter/upload-adapter-ali-oss.js +0 -66
- package/dist/upload-adapter/upload-adapter-ali-oss.js.map +0 -1
- package/dist/upload-assets/index.d.ts +0 -8
- package/dist/upload-assets/index.js +0 -39
- package/dist/upload-assets/index.js.map +0 -1
- package/src/upload-adapter/define-adapter.ts +0 -3
- package/src/upload-adapter/index.ts +0 -2
- package/src/upload-adapter/upload-adapter-ali-oss.ts +0 -48
- package/src/upload-assets/index.ts +0 -34
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { RemoteAssetPluginOpt } from './types';
|
|
2
2
|
import { IPluginContext } from '@tarojs/service';
|
|
3
3
|
export * from './types';
|
|
4
|
-
export * from './upload-adapter';
|
|
5
4
|
declare const _default: (ctx: IPluginContext, pluginOpts: RemoteAssetPluginOpt) => void;
|
|
6
5
|
export default _default;
|
package/dist/index.js
CHANGED
|
@@ -27,20 +27,21 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
29
|
const path_1 = __importDefault(require("path"));
|
|
30
|
-
const
|
|
30
|
+
const helper_1 = require("@taro-minify-pack/helper");
|
|
31
31
|
const utils_1 = require("./utils");
|
|
32
32
|
const path_transform_1 = require("./path-transform/path-transform");
|
|
33
33
|
__exportStar(require("./types"), exports);
|
|
34
|
-
|
|
35
|
-
const cacheFilePath = path_1.default.resolve(process.cwd(), 'node_modules', '.cache/remote-assets-cache.json');
|
|
34
|
+
const cacheFilePath = path_1.default.resolve(process.cwd(), 'node_modules', '.cache/remote-assets.json');
|
|
36
35
|
exports.default = (ctx, pluginOpts) => {
|
|
37
36
|
const transform = (0, path_transform_1.pathTransform)({ cacheFilePath, pathAlias: pluginOpts.pathAlias || {} });
|
|
38
37
|
ctx.onBuildStart(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
39
38
|
const { assetsDirPath, uploader } = pluginOpts;
|
|
40
39
|
const cacheData = (0, utils_1.getCacheData)(cacheFilePath);
|
|
41
|
-
const
|
|
40
|
+
const cacheAssetUniqueKeys = Object.values(cacheData).map(item => item.uniqueKey);
|
|
41
|
+
const processAssets = (assets) => assets.filter(item => !cacheAssetUniqueKeys.includes(item.uniqueKey));
|
|
42
|
+
const remoteAssetInfoList = yield (0, helper_1.uploadAssets)({ assetsDirPath, upload: uploader, processAssets });
|
|
42
43
|
const remoteAssetInfoMap = remoteAssetInfoList.reduce((result, item) => {
|
|
43
|
-
return Object.assign(Object.assign({}, result), { [item.localPath]: item
|
|
44
|
+
return Object.assign(Object.assign({}, result), { [item.localPath]: item });
|
|
44
45
|
}, cacheData);
|
|
45
46
|
(0, utils_1.saveCacheData)(cacheFilePath, remoteAssetInfoMap);
|
|
46
47
|
}));
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,gDAAuB;AAGvB,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,gDAAuB;AAGvB,qDAAuE;AACvE,mCAAqD;AACrD,oEAA+D;AAE/D,0CAAuB;AAEvB,MAAM,aAAa,GAAG,cAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,cAAc,EAAE,2BAA2B,CAAC,CAAA;AAE9F,kBAAe,CAAC,GAAmB,EAAE,UAAgC,EAAE,EAAE;IACvE,MAAM,SAAS,GAAG,IAAA,8BAAa,EAAC,EAAE,aAAa,EAAE,SAAS,EAAE,UAAU,CAAC,SAAS,IAAI,EAAE,EAAE,CAAC,CAAA;IAEzF,GAAG,CAAC,YAAY,CAAC,GAAS,EAAE;QAC1B,MAAM,EAAE,aAAa,EAAE,QAAQ,EAAE,GAAG,UAAU,CAAA;QAC9C,MAAM,SAAS,GAAG,IAAA,oBAAY,EAAC,aAAa,CAAC,CAAA;QAC7C,MAAM,oBAAoB,GAAG,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;QACjF,MAAM,aAAa,GAAG,CAAC,MAAwB,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,oBAAoB,CAAC,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAA;QACzH,MAAM,mBAAmB,GAAG,MAAM,IAAA,qBAAY,EAAC,EAAE,aAAa,EAAE,MAAM,EAAE,QAAQ,EAAE,aAAa,EAAE,CAAC,CAAA;QAClG,MAAM,kBAAkB,GAAG,mBAAmB,CAAC,MAAM,CAAC,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EAAE;YAChG,uCAAY,MAAM,KAAE,CAAC,GAAG,CAAC,EAAE,IAAI,MAAM,CAAC,IAAI,GAAG,EAAE,CAAC,IAAE;QACpD,CAAC,EAAE,EAAE,CAAC,CAAA;QAEN,MAAM,SAAS,GAAG,CAAC,GAAG,EAAE;YACtB,MAAM,cAAc,GAAG,MAAM,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA;YAChG,IAAI,cAAc;gBAAE,OAAO,IAAI,CAAC,OAAO,CAAC,eAAe,CAAC,cAAc,CAAC,EAAE,GAAG,CAAC,SAAS,CAAC,cAAc,CAAC,CAAC,CAAA;YACvG,OAAO,IAAI,CAAA;QACb,CAAC,CAAC,EAAE,CAAA;QACJ,IAAI,CAAC,kBAAkB,CAAC,SAAS,CAAC;YAAE,OAAO,IAAI,CAAA;QAC/C,OAAO,kBAAkB,CAAC,SAAS,CAAC,CAAC,SAAS,CAAA;IAChD,CAAC,CAAA;AACH,CAAC,CAAA;AAhBY,QAAA,aAAa,iBAgBzB"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,18 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
localPath: string;
|
|
3
|
-
uniqueKey: string;
|
|
4
|
-
ext: string;
|
|
5
|
-
}
|
|
6
|
-
export interface RemoteAssetInfo {
|
|
7
|
-
ext: string;
|
|
8
|
-
localPath: string;
|
|
9
|
-
uniqueKey: string;
|
|
10
|
-
remoteUrl: string;
|
|
11
|
-
}
|
|
12
|
-
export type Uploader = (opt: LocalAssetInfo) => Promise<RemoteAssetInfo>;
|
|
13
|
-
export type UploaderAdapter<T = any> = (opt: T) => Uploader;
|
|
1
|
+
import { RemoteAssetInfo, Uploader } from '@taro-minify-pack/helper';
|
|
14
2
|
export interface RemoteAssetPluginOpt {
|
|
15
3
|
assetsDirPath: string;
|
|
16
4
|
pathAlias?: Record<string, string>;
|
|
17
5
|
uploader?: Uploader;
|
|
18
6
|
}
|
|
7
|
+
export type CacheData = Record<string, RemoteAssetInfo>;
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,11 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export declare const travelFiles: (dir: string) => string[];
|
|
5
|
-
/**
|
|
6
|
-
* 生成文件 key
|
|
7
|
-
*/
|
|
8
|
-
export declare const generateFileUniqueKey: (filePath: string) => string;
|
|
9
|
-
export declare const getCacheData: (cacheFilePath: string) => any;
|
|
10
|
-
export declare const saveCacheData: (cacheFilePath: string, cacheData: any) => void;
|
|
1
|
+
import { CacheData } from './types';
|
|
2
|
+
export declare const getCacheData: (cacheFilePath: string) => CacheData;
|
|
3
|
+
export declare const saveCacheData: (cacheFilePath: string, cacheData: CacheData) => void;
|
|
11
4
|
export declare const noop: () => void;
|
package/dist/utils.js
CHANGED
|
@@ -3,32 +3,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.noop = exports.saveCacheData = exports.getCacheData =
|
|
6
|
+
exports.noop = exports.saveCacheData = exports.getCacheData = void 0;
|
|
7
7
|
const fs_1 = __importDefault(require("fs"));
|
|
8
8
|
const path_1 = __importDefault(require("path"));
|
|
9
|
-
const md5_1 = __importDefault(require("md5"));
|
|
10
|
-
/**
|
|
11
|
-
* 递归查找文件
|
|
12
|
-
*/
|
|
13
|
-
const travelFiles = (dir) => {
|
|
14
|
-
const files = fs_1.default.readdirSync(dir);
|
|
15
|
-
return files.reduce((result, file) => {
|
|
16
|
-
const filePath = path_1.default.join(dir, file);
|
|
17
|
-
if (!fs_1.default.statSync(filePath).isDirectory())
|
|
18
|
-
return [...result, filePath];
|
|
19
|
-
return [...result, ...(0, exports.travelFiles)(filePath)];
|
|
20
|
-
}, []);
|
|
21
|
-
};
|
|
22
|
-
exports.travelFiles = travelFiles;
|
|
23
|
-
/**
|
|
24
|
-
* 生成文件 key
|
|
25
|
-
*/
|
|
26
|
-
const generateFileUniqueKey = (filePath) => {
|
|
27
|
-
const { dir, base } = path_1.default.parse(filePath);
|
|
28
|
-
const buffer = fs_1.default.readFileSync(`${dir}${path_1.default.sep}${base}`);
|
|
29
|
-
return (0, md5_1.default)(buffer);
|
|
30
|
-
};
|
|
31
|
-
exports.generateFileUniqueKey = generateFileUniqueKey;
|
|
32
9
|
const getCacheData = (cacheFilePath) => {
|
|
33
10
|
try {
|
|
34
11
|
fs_1.default.accessSync(cacheFilePath);
|
package/dist/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;;;;AAAA,4CAAmB;AACnB,gDAAuB;
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;;;;AAAA,4CAAmB;AACnB,gDAAuB;AAGhB,MAAM,YAAY,GAAG,CAAC,aAAqB,EAAa,EAAE;IAC/D,IAAI,CAAC;QACH,YAAE,CAAC,UAAU,CAAC,aAAa,CAAC,CAAA;QAC5B,OAAO,IAAI,CAAC,KAAK,CAAC,YAAE,CAAC,YAAY,CAAC,aAAa,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAA;IAC9D,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,EAAE,CAAA;IACX,CAAC;AACH,CAAC,CAAA;AAPY,QAAA,YAAY,gBAOxB;AAEM,MAAM,aAAa,GAAG,CAAC,aAAqB,EAAE,SAAoB,EAAE,EAAE;IAC3E,MAAM,YAAY,GAAG,cAAI,CAAC,OAAO,CAAC,aAAa,CAAC,CAAA;IAChD,IAAI,CAAC,YAAE,CAAC,UAAU,CAAC,YAAY,CAAC;QAAE,YAAE,CAAC,SAAS,CAAC,YAAY,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAA;IACjF,YAAE,CAAC,aAAa,CAAC,aAAa,EAAE,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,CAAA;AAC5D,CAAC,CAAA;AAJY,QAAA,aAAa,iBAIzB;AAEM,MAAM,IAAI,GAAG,GAAG,EAAE,GAAE,CAAC,CAAA;AAAf,QAAA,IAAI,QAAW"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@taro-minify-pack/plugin-remote-assets",
|
|
3
|
-
"version": "0.1.4-alpha.
|
|
3
|
+
"version": "0.1.4-alpha.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -26,9 +26,9 @@
|
|
|
26
26
|
"@babel/types": "^7.26.10",
|
|
27
27
|
"@supercharge/promise-pool": "^3.2.0",
|
|
28
28
|
"ali-oss": "^6.23.0",
|
|
29
|
-
"md5": "^2.3.0",
|
|
30
29
|
"mini-css-extract-plugin": "^2.9.2",
|
|
31
|
-
"postcss": "^8.5.3"
|
|
30
|
+
"postcss": "^8.5.3",
|
|
31
|
+
"@taro-minify-pack/helper": "0.1.4-alpha.3"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@tarojs/service": "^4.0.9",
|
|
@@ -37,7 +37,6 @@
|
|
|
37
37
|
"@types/babel__generator": "^7.6.8",
|
|
38
38
|
"@types/babel__template": "^7.4.4",
|
|
39
39
|
"@types/babel__traverse": "^7.20.6",
|
|
40
|
-
"@types/md5": "^2.3.5",
|
|
41
40
|
"@types/node": "^18.19.130",
|
|
42
41
|
"@types/webpack-sources": "^3.2.3",
|
|
43
42
|
"@typescript-eslint/eslint-plugin": "^8.26.1",
|
package/src/index.ts
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
import path from 'path'
|
|
2
2
|
import { RemoteAssetPluginOpt } from '@/types'
|
|
3
3
|
import { IPluginContext } from '@tarojs/service'
|
|
4
|
-
import { uploadAssets } from '
|
|
4
|
+
import { LocalAssetInfo, uploadAssets } from '@taro-minify-pack/helper'
|
|
5
5
|
import { getCacheData, saveCacheData } from './utils'
|
|
6
6
|
import { pathTransform } from '@/path-transform/path-transform'
|
|
7
7
|
|
|
8
8
|
export * from '@/types'
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const cacheFilePath = path.resolve(process.cwd(), 'node_modules', '.cache/remote-assets-cache.json')
|
|
10
|
+
const cacheFilePath = path.resolve(process.cwd(), 'node_modules', '.cache/remote-assets.json')
|
|
13
11
|
|
|
14
12
|
export default (ctx: IPluginContext, pluginOpts: RemoteAssetPluginOpt) => {
|
|
15
13
|
const transform = pathTransform({ cacheFilePath, pathAlias: pluginOpts.pathAlias || {} })
|
|
@@ -17,9 +15,11 @@ export default (ctx: IPluginContext, pluginOpts: RemoteAssetPluginOpt) => {
|
|
|
17
15
|
ctx.onBuildStart(async () => {
|
|
18
16
|
const { assetsDirPath, uploader } = pluginOpts
|
|
19
17
|
const cacheData = getCacheData(cacheFilePath)
|
|
20
|
-
const
|
|
18
|
+
const cacheAssetUniqueKeys = Object.values(cacheData).map(item => item.uniqueKey)
|
|
19
|
+
const processAssets = (assets: LocalAssetInfo[]) => assets.filter(item => !cacheAssetUniqueKeys.includes(item.uniqueKey))
|
|
20
|
+
const remoteAssetInfoList = await uploadAssets({ assetsDirPath, upload: uploader, processAssets })
|
|
21
21
|
const remoteAssetInfoMap = remoteAssetInfoList.reduce((result, item) => {
|
|
22
|
-
return { ...result, [item.localPath]: item
|
|
22
|
+
return { ...result, [item.localPath]: item }
|
|
23
23
|
}, cacheData)
|
|
24
24
|
saveCacheData(cacheFilePath, remoteAssetInfoMap)
|
|
25
25
|
})
|
package/src/types.ts
CHANGED
|
@@ -1,22 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
localPath: string,
|
|
3
|
-
uniqueKey: string,
|
|
4
|
-
ext: string
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export interface RemoteAssetInfo {
|
|
8
|
-
ext: string,
|
|
9
|
-
localPath: string,
|
|
10
|
-
uniqueKey: string,
|
|
11
|
-
remoteUrl: string
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export type Uploader = (opt: LocalAssetInfo) => Promise<RemoteAssetInfo>
|
|
15
|
-
|
|
16
|
-
export type UploaderAdapter<T = any> = (opt: T) => Uploader
|
|
1
|
+
import { RemoteAssetInfo, Uploader } from '@taro-minify-pack/helper'
|
|
17
2
|
|
|
18
3
|
export interface RemoteAssetPluginOpt {
|
|
19
4
|
assetsDirPath: string,
|
|
20
5
|
pathAlias?: Record<string, string>
|
|
21
6
|
uploader?: Uploader,
|
|
22
7
|
}
|
|
8
|
+
|
|
9
|
+
export type CacheData = Record<string, RemoteAssetInfo>
|
package/src/utils.ts
CHANGED
|
@@ -1,29 +1,8 @@
|
|
|
1
1
|
import fs from 'fs'
|
|
2
2
|
import path from 'path'
|
|
3
|
-
import
|
|
3
|
+
import { CacheData } from '@/types'
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
* 递归查找文件
|
|
7
|
-
*/
|
|
8
|
-
export const travelFiles = (dir: string): string[] => {
|
|
9
|
-
const files = fs.readdirSync(dir)
|
|
10
|
-
return files.reduce<string[]>((result, file) => {
|
|
11
|
-
const filePath = path.join(dir, file)
|
|
12
|
-
if (!fs.statSync(filePath).isDirectory()) return [...result, filePath]
|
|
13
|
-
return [...result, ...travelFiles(filePath)]
|
|
14
|
-
}, [])
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* 生成文件 key
|
|
19
|
-
*/
|
|
20
|
-
export const generateFileUniqueKey = (filePath: string) => {
|
|
21
|
-
const { dir, base } = path.parse(filePath)
|
|
22
|
-
const buffer = fs.readFileSync(`${dir}${path.sep}${base}`)
|
|
23
|
-
return md5(buffer)
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export const getCacheData = (cacheFilePath:string): any => {
|
|
5
|
+
export const getCacheData = (cacheFilePath: string): CacheData => {
|
|
27
6
|
try {
|
|
28
7
|
fs.accessSync(cacheFilePath)
|
|
29
8
|
return JSON.parse(fs.readFileSync(cacheFilePath).toString())
|
|
@@ -32,7 +11,7 @@ export const getCacheData = (cacheFilePath:string): any => {
|
|
|
32
11
|
}
|
|
33
12
|
}
|
|
34
13
|
|
|
35
|
-
export const saveCacheData = (cacheFilePath:string, cacheData:
|
|
14
|
+
export const saveCacheData = (cacheFilePath: string, cacheData: CacheData) => {
|
|
36
15
|
const cacheDirPath = path.dirname(cacheFilePath)
|
|
37
16
|
if (!fs.existsSync(cacheDirPath)) fs.mkdirSync(cacheDirPath, { recursive: true })
|
|
38
17
|
fs.writeFileSync(cacheFilePath, JSON.stringify(cacheData))
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.defineUploaderAdapter = void 0;
|
|
4
|
-
const defineUploaderAdapter = (adapter) => adapter;
|
|
5
|
-
exports.defineUploaderAdapter = defineUploaderAdapter;
|
|
6
|
-
//# sourceMappingURL=define-adapter.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"define-adapter.js","sourceRoot":"","sources":["../../src/upload-adapter/define-adapter.ts"],"names":[],"mappings":";;;AAEO,MAAM,qBAAqB,GAAG,CAAI,OAA2B,EAAE,EAAE,CAAC,OAAO,CAAA;AAAnE,QAAA,qBAAqB,yBAA8C"}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./upload-adapter-ali-oss"), exports);
|
|
18
|
-
__exportStar(require("./define-adapter"), exports);
|
|
19
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/upload-adapter/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2DAAwC;AACxC,mDAAgC"}
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
12
|
-
var t = {};
|
|
13
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
14
|
-
t[p] = s[p];
|
|
15
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
16
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
17
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
18
|
-
t[p[i]] = s[p[i]];
|
|
19
|
-
}
|
|
20
|
-
return t;
|
|
21
|
-
};
|
|
22
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
23
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.aliOssUploadAdapter = void 0;
|
|
27
|
-
const define_adapter_1 = require("./define-adapter");
|
|
28
|
-
const ali_oss_1 = __importDefault(require("ali-oss"));
|
|
29
|
-
exports.aliOssUploadAdapter = (0, define_adapter_1.defineUploaderAdapter)((opt) => {
|
|
30
|
-
const { customDomain, bucketDir = '/' } = opt, aliOssOpt = __rest(opt, ["customDomain", "bucketDir"]);
|
|
31
|
-
const client = new ali_oss_1.default(aliOssOpt);
|
|
32
|
-
const isRemoteAssetExist = (...args) => __awaiter(void 0, void 0, void 0, function* () {
|
|
33
|
-
try {
|
|
34
|
-
yield client.head(...args);
|
|
35
|
-
return true;
|
|
36
|
-
}
|
|
37
|
-
catch (error) {
|
|
38
|
-
if (error.code === 'NoSuchKey')
|
|
39
|
-
return false;
|
|
40
|
-
throw error;
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
const uploadToAliOss = (...args) => __awaiter(void 0, void 0, void 0, function* () { return client.put(...args); });
|
|
44
|
-
const getRemoteAssetPath = (localAssetInfo) => {
|
|
45
|
-
const { uniqueKey, ext } = localAssetInfo;
|
|
46
|
-
return `${bucketDir}/${uniqueKey}${ext}`;
|
|
47
|
-
};
|
|
48
|
-
const getRemoteAssetUrl = (localAssetInfo) => {
|
|
49
|
-
const { uniqueKey, ext } = localAssetInfo;
|
|
50
|
-
if (customDomain)
|
|
51
|
-
return `${customDomain}/${uniqueKey}${ext}`;
|
|
52
|
-
const remotePath = getRemoteAssetPath(localAssetInfo);
|
|
53
|
-
return client.signatureUrl(remotePath);
|
|
54
|
-
};
|
|
55
|
-
return (localAssetInfo) => __awaiter(void 0, void 0, void 0, function* () {
|
|
56
|
-
const { localPath } = localAssetInfo;
|
|
57
|
-
const remoteAssetPath = getRemoteAssetPath(localAssetInfo);
|
|
58
|
-
const remoteAssetUrl = getRemoteAssetUrl(localAssetInfo);
|
|
59
|
-
const isExist = yield isRemoteAssetExist(remoteAssetPath);
|
|
60
|
-
if (isExist)
|
|
61
|
-
return Object.assign(Object.assign({}, localAssetInfo), { remoteUrl: remoteAssetUrl });
|
|
62
|
-
yield uploadToAliOss(remoteAssetPath, localPath);
|
|
63
|
-
return Object.assign(Object.assign({}, localAssetInfo), { remoteUrl: remoteAssetUrl });
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
//# sourceMappingURL=upload-adapter-ali-oss.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"upload-adapter-ali-oss.js","sourceRoot":"","sources":["../../src/upload-adapter/upload-adapter-ali-oss.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,qDAAwD;AAExD,sDAA4B;AAOf,QAAA,mBAAmB,GAAG,IAAA,sCAAqB,EAAC,CAAC,GAA2B,EAAE,EAAE;IACvF,MAAM,EAAE,YAAY,EAAE,SAAS,GAAG,GAAG,KAAmB,GAAG,EAAjB,SAAS,UAAK,GAAG,EAArD,6BAA+C,CAAM,CAAA;IAE3D,MAAM,MAAM,GAAG,IAAI,iBAAM,CAAC,SAAS,CAAC,CAAA;IAEpC,MAAM,kBAAkB,GAAG,CAAO,GAAG,IAAgC,EAAE,EAAE;QACvE,IAAI,CAAC;YACH,MAAM,MAAM,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,CA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,cAAc,KAAE,SAAS,EAAE,cAAc,IAAE;IACzD,CAAC,CAAA,CAAA;AACH,CAAC,CAAC,CAAA"}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { Uploader } from '../types';
|
|
2
|
-
interface UploaderOpt {
|
|
3
|
-
assetsDirPath?: string;
|
|
4
|
-
cacheData?: Record<string, any>;
|
|
5
|
-
upload?: Uploader;
|
|
6
|
-
}
|
|
7
|
-
export declare const uploadAssets: (opt: UploaderOpt) => Promise<import("../types").RemoteAssetInfo[]>;
|
|
8
|
-
export {};
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.uploadAssets = void 0;
|
|
16
|
-
const promise_pool_1 = require("@supercharge/promise-pool");
|
|
17
|
-
const utils_1 = require("../utils");
|
|
18
|
-
const path_1 = __importDefault(require("path"));
|
|
19
|
-
const uploadAssets = (opt) => __awaiter(void 0, void 0, void 0, function* () {
|
|
20
|
-
const { assetsDirPath, cacheData, upload } = opt;
|
|
21
|
-
if (!assetsDirPath || !upload)
|
|
22
|
-
return [];
|
|
23
|
-
const uploadedAssetFileNames = Object.values(cacheData || {}).map(item => path_1.default.basename(new URL(item).pathname));
|
|
24
|
-
const assetsPaths = (0, utils_1.travelFiles)(assetsDirPath);
|
|
25
|
-
const assetsInfos = assetsPaths.reduce((result, localPath) => {
|
|
26
|
-
const uniqueKey = (0, utils_1.generateFileUniqueKey)(localPath);
|
|
27
|
-
const { ext } = path_1.default.parse(localPath);
|
|
28
|
-
const isUploaded = uploadedAssetFileNames.includes(`${uniqueKey}${ext}`);
|
|
29
|
-
if (isUploaded)
|
|
30
|
-
return result;
|
|
31
|
-
return [...result, { localPath, ext, uniqueKey }];
|
|
32
|
-
}, []);
|
|
33
|
-
const { results: remoteAssetInfoList } = yield promise_pool_1.PromisePool.withConcurrency(2)
|
|
34
|
-
.for(assetsInfos)
|
|
35
|
-
.process((localAssetInfo) => __awaiter(void 0, void 0, void 0, function* () { return upload(localAssetInfo); }));
|
|
36
|
-
return remoteAssetInfoList.filter(Boolean);
|
|
37
|
-
});
|
|
38
|
-
exports.uploadAssets = uploadAssets;
|
|
39
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/upload-assets/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,4DAAuD;AAEvD,mCAA4D;AAC5D,gDAAuB;AAQhB,MAAM,YAAY,GAAG,CAAO,GAAgB,EAAE,EAAE;IACrD,MAAM,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,GAAG,CAAA;IAEhD,IAAI,CAAC,aAAa,IAAI,CAAC,MAAM;QAAE,OAAO,EAAE,CAAA;IAExC,MAAM,sBAAsB,GAAG,MAAM,CAAC,MAAM,CAAC,SAAS,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,cAAI,CAAC,QAAQ,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAA;IAEhH,MAAM,WAAW,GAAG,IAAA,mBAAW,EAAC,aAAa,CAAC,CAAA;IAE9C,MAAM,WAAW,GAAG,WAAW,CAAC,MAAM,CAAmB,CAAC,MAAM,EAAE,SAAS,EAAE,EAAE;QAC7E,MAAM,SAAS,GAAG,IAAA,6BAAqB,EAAC,SAAS,CAAC,CAAA;QAClD,MAAM,EAAE,GAAG,EAAE,GAAG,cAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAA;QACrC,MAAM,UAAU,GAAG,sBAAsB,CAAC,QAAQ,CAAC,GAAG,SAAS,GAAG,GAAG,EAAE,CAAC,CAAA;QACxE,IAAI,UAAU;YAAE,OAAO,MAAM,CAAA;QAC7B,OAAO,CAAC,GAAG,MAAM,EAAE,EAAE,SAAS,EAAE,GAAG,EAAE,SAAS,EAAE,CAAC,CAAA;IACnD,CAAC,EAAE,EAAE,CAAC,CAAA;IAEN,MAAM,EAAE,OAAO,EAAE,mBAAmB,EAAE,GAAG,MAAM,0BAAW,CAAC,eAAe,CAAC,CAAC,CAAC;SAC1E,GAAG,CAAC,WAAW,CAAC;SAChB,OAAO,CAAC,CAAO,cAAc,EAAE,EAAE,kDAAC,OAAA,MAAM,CAAC,cAAc,CAAC,CAAA,GAAA,CAAC,CAAA;IAE5D,OAAO,mBAAmB,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;AAC5C,CAAC,CAAA,CAAA;AAtBY,QAAA,YAAY,gBAsBxB"}
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { defineUploaderAdapter } from './define-adapter'
|
|
2
|
-
import { LocalAssetInfo } from '@/types'
|
|
3
|
-
import AliOss from 'ali-oss'
|
|
4
|
-
|
|
5
|
-
export interface AliOssUploadAdapterOpt extends AliOss.Options {
|
|
6
|
-
customDomain?: string
|
|
7
|
-
bucketDir?: string
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export const aliOssUploadAdapter = defineUploaderAdapter((opt: AliOssUploadAdapterOpt) => {
|
|
11
|
-
const { customDomain, bucketDir = '/', ...aliOssOpt } = opt
|
|
12
|
-
|
|
13
|
-
const client = new AliOss(aliOssOpt)
|
|
14
|
-
|
|
15
|
-
const isRemoteAssetExist = async (...args: Parameters<AliOss['head']>) => {
|
|
16
|
-
try {
|
|
17
|
-
await client.head(...args)
|
|
18
|
-
return true
|
|
19
|
-
} catch (error: any) {
|
|
20
|
-
if (error.code === 'NoSuchKey') return false
|
|
21
|
-
throw error
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const uploadToAliOss = async (...args: Parameters<AliOss['put']>) => client.put(...args)
|
|
26
|
-
|
|
27
|
-
const getRemoteAssetPath = (localAssetInfo: LocalAssetInfo) => {
|
|
28
|
-
const { uniqueKey, ext } = localAssetInfo
|
|
29
|
-
return `${bucketDir}/${uniqueKey}${ext}`
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const getRemoteAssetUrl = (localAssetInfo: LocalAssetInfo) => {
|
|
33
|
-
const { uniqueKey, ext } = localAssetInfo
|
|
34
|
-
if (customDomain) return `${customDomain}/${uniqueKey}${ext}`
|
|
35
|
-
const remotePath = getRemoteAssetPath(localAssetInfo)
|
|
36
|
-
return client.signatureUrl(remotePath)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return async (localAssetInfo: LocalAssetInfo) => {
|
|
40
|
-
const { localPath } = localAssetInfo
|
|
41
|
-
const remoteAssetPath = getRemoteAssetPath(localAssetInfo)
|
|
42
|
-
const remoteAssetUrl = getRemoteAssetUrl(localAssetInfo)
|
|
43
|
-
const isExist = await isRemoteAssetExist(remoteAssetPath)
|
|
44
|
-
if (isExist) return { ...localAssetInfo, remoteUrl: remoteAssetUrl }
|
|
45
|
-
await uploadToAliOss(remoteAssetPath, localPath)
|
|
46
|
-
return { ...localAssetInfo, remoteUrl: remoteAssetUrl }
|
|
47
|
-
}
|
|
48
|
-
})
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { PromisePool } from '@supercharge/promise-pool'
|
|
2
|
-
import { LocalAssetInfo, Uploader } from '@/types'
|
|
3
|
-
import { generateFileUniqueKey, travelFiles } from '@/utils'
|
|
4
|
-
import path from 'path'
|
|
5
|
-
|
|
6
|
-
interface UploaderOpt {
|
|
7
|
-
assetsDirPath?: string,
|
|
8
|
-
cacheData?:Record<string, any>
|
|
9
|
-
upload?: Uploader
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export const uploadAssets = async (opt: UploaderOpt) => {
|
|
13
|
-
const { assetsDirPath, cacheData, upload } = opt
|
|
14
|
-
|
|
15
|
-
if (!assetsDirPath || !upload) return []
|
|
16
|
-
|
|
17
|
-
const uploadedAssetFileNames = Object.values(cacheData || {}).map(item => path.basename(new URL(item).pathname))
|
|
18
|
-
|
|
19
|
-
const assetsPaths = travelFiles(assetsDirPath)
|
|
20
|
-
|
|
21
|
-
const assetsInfos = assetsPaths.reduce<LocalAssetInfo[]>((result, localPath) => {
|
|
22
|
-
const uniqueKey = generateFileUniqueKey(localPath)
|
|
23
|
-
const { ext } = path.parse(localPath)
|
|
24
|
-
const isUploaded = uploadedAssetFileNames.includes(`${uniqueKey}${ext}`)
|
|
25
|
-
if (isUploaded) return result
|
|
26
|
-
return [...result, { localPath, ext, uniqueKey }]
|
|
27
|
-
}, [])
|
|
28
|
-
|
|
29
|
-
const { results: remoteAssetInfoList } = await PromisePool.withConcurrency(2)
|
|
30
|
-
.for(assetsInfos)
|
|
31
|
-
.process(async (localAssetInfo) => upload(localAssetInfo))
|
|
32
|
-
|
|
33
|
-
return remoteAssetInfoList.filter(Boolean)
|
|
34
|
-
}
|