@taro-minify-pack/plugin-remote-assets 0.0.1-alpha.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/.eslintrc.js +21 -0
- package/LICENSE +21 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +65 -0
- package/dist/index.js.map +1 -0
- package/dist/path-transform/path-transform-babel.d.ts +3 -0
- package/dist/path-transform/path-transform-babel.js +28 -0
- package/dist/path-transform/path-transform-babel.js.map +1 -0
- package/dist/path-transform/path-transform-postcss.d.ts +3 -0
- package/dist/path-transform/path-transform-postcss.js +20 -0
- package/dist/path-transform/path-transform-postcss.js.map +1 -0
- package/dist/path-transform/path-transform.d.ts +6 -0
- package/dist/path-transform/path-transform.js +23 -0
- package/dist/path-transform/path-transform.js.map +1 -0
- package/dist/types.d.ts +18 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/upload-adapter/define-adapter.d.ts +2 -0
- package/dist/upload-adapter/define-adapter.js +6 -0
- package/dist/upload-adapter/define-adapter.js.map +1 -0
- package/dist/upload-adapter/index.d.ts +2 -0
- package/dist/upload-adapter/index.js +19 -0
- package/dist/upload-adapter/index.js.map +1 -0
- package/dist/upload-adapter/upload-adapter-ali-oss.d.ts +6 -0
- package/dist/upload-adapter/upload-adapter-ali-oss.js +67 -0
- package/dist/upload-adapter/upload-adapter-ali-oss.js.map +1 -0
- package/dist/upload-assets/index.d.ts +7 -0
- package/dist/upload-assets/index.js +34 -0
- package/dist/upload-assets/index.js.map +1 -0
- package/dist/utils.d.ts +11 -0
- package/dist/utils.js +48 -0
- package/dist/utils.js.map +1 -0
- package/package.json +56 -0
- package/src/index.ts +50 -0
- package/src/path-transform/path-transform-babel.ts +34 -0
- package/src/path-transform/path-transform-postcss.ts +28 -0
- package/src/path-transform/path-transform.ts +24 -0
- package/src/types.ts +22 -0
- package/src/upload-adapter/define-adapter.ts +3 -0
- package/src/upload-adapter/index.ts +2 -0
- package/src/upload-adapter/upload-adapter-ali-oss.ts +49 -0
- package/src/upload-assets/index.ts +27 -0
- package/src/utils.ts +39 -0
- package/tsconfig.json +24 -0
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
env: {
|
|
3
|
+
node: true,
|
|
4
|
+
es6: true
|
|
5
|
+
},
|
|
6
|
+
extends: [
|
|
7
|
+
'standard',
|
|
8
|
+
'plugin:@typescript-eslint/recommended'
|
|
9
|
+
],
|
|
10
|
+
parser: '@typescript-eslint/parser',
|
|
11
|
+
plugins: ['@typescript-eslint'],
|
|
12
|
+
rules: {
|
|
13
|
+
'@typescript-eslint/no-empty-function': 'off',
|
|
14
|
+
'@typescript-eslint/no-explicit-any': 'off',
|
|
15
|
+
'@typescript-eslint/no-var-requires': 'off',
|
|
16
|
+
'@typescript-eslint/no-non-null-assertion': 'off',
|
|
17
|
+
'@typescript-eslint/ban-ts-comment': 'off',
|
|
18
|
+
'@typescript-eslint/no-unused-vars': 'off',
|
|
19
|
+
'no-use-before-define': 'off'
|
|
20
|
+
}
|
|
21
|
+
}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 yu.pan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { RemoteAssetPluginOpt } from './types';
|
|
2
|
+
import { IPluginContext } from '@tarojs/service';
|
|
3
|
+
export { RemoteAssetPluginOpt } from './types';
|
|
4
|
+
export * from './upload-adapter';
|
|
5
|
+
declare const _default: (ctx: IPluginContext, pluginOpts: RemoteAssetPluginOpt) => void;
|
|
6
|
+
export default _default;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
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
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
17
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
18
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
19
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
20
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
21
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
22
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
const path_1 = __importDefault(require("path"));
|
|
30
|
+
const upload_assets_1 = require("./upload-assets");
|
|
31
|
+
const utils_1 = require("./utils");
|
|
32
|
+
const path_transform_1 = require("./path-transform/path-transform");
|
|
33
|
+
__exportStar(require("./upload-adapter"), exports);
|
|
34
|
+
const cacheFilePath = path_1.default.resolve(__dirname, 'remote-assets-cache.json');
|
|
35
|
+
exports.default = (ctx, pluginOpts) => {
|
|
36
|
+
const transform = (0, path_transform_1.pathTransform)({ cacheFilePath, pathAlias: pluginOpts.pathAlias || {} });
|
|
37
|
+
ctx.onBuildStart(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
38
|
+
const { assetsDirPath, uploader } = pluginOpts;
|
|
39
|
+
const remoteAssetInfoList = yield (0, upload_assets_1.uploadAssets)({ assetsDirPath, upload: uploader });
|
|
40
|
+
const remoteAssetInfoMap = remoteAssetInfoList.reduce((result, item) => {
|
|
41
|
+
return Object.assign(Object.assign({}, result), { [item.localPath]: item.remoteUrl });
|
|
42
|
+
}, {});
|
|
43
|
+
(0, utils_1.saveCacheData)(cacheFilePath, remoteAssetInfoMap);
|
|
44
|
+
}));
|
|
45
|
+
ctx.modifyRunnerOpts((curRunnerOpts) => {
|
|
46
|
+
const { postcss: curPostcssOpts } = curRunnerOpts.opts;
|
|
47
|
+
console.log(curPostcssOpts, path_1.default.resolve(__dirname, './path-transform/path-transform-postcss'));
|
|
48
|
+
curRunnerOpts.opts.postcss = Object.assign(Object.assign({}, curPostcssOpts), { [path_1.default.resolve(__dirname, './path-transform/path-transform-postcss')]: {
|
|
49
|
+
enabled: true,
|
|
50
|
+
config: { transform }
|
|
51
|
+
} });
|
|
52
|
+
});
|
|
53
|
+
ctx.modifyWebpackChain(({ chain }) => {
|
|
54
|
+
const babelPluginPathTransformPath = path_1.default.resolve(__dirname, './path-transform/path-transform-babel');
|
|
55
|
+
chain.module
|
|
56
|
+
.rule('script')
|
|
57
|
+
.use('babelLoader')
|
|
58
|
+
.options({
|
|
59
|
+
plugins: [
|
|
60
|
+
[babelPluginPathTransformPath, { transform }]
|
|
61
|
+
]
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
};
|
|
65
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,gDAAuB;AAGvB,mDAA8C;AAC9C,mCAAuC;AACvC,oEAA+D;AAI/D,mDAAgC;AAEhC,MAAM,aAAa,GAAG,cAAI,CAAC,OAAO,CAAC,SAAS,EAAE,0BAA0B,CAAC,CAAA;AAEzE,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,GAA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C,CAAC,CAAA;AACJ,CAAC,CAAA"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const template_1 = __importDefault(require("@babel/template"));
|
|
7
|
+
const utils_1 = require("../utils");
|
|
8
|
+
module.exports = () => {
|
|
9
|
+
return {
|
|
10
|
+
visitor: {
|
|
11
|
+
ImportDeclaration(importDeclarationAstPath, state) {
|
|
12
|
+
var _a;
|
|
13
|
+
if ((_a = state.file.opts.filename) === null || _a === void 0 ? void 0 : _a.includes('node_modules'))
|
|
14
|
+
return;
|
|
15
|
+
const { transform = utils_1.noop } = state.opts;
|
|
16
|
+
const { node } = importDeclarationAstPath;
|
|
17
|
+
const { value } = node.source;
|
|
18
|
+
const fileUrl = transform(value) || '';
|
|
19
|
+
if (!fileUrl || fileUrl === value)
|
|
20
|
+
return;
|
|
21
|
+
const [specifier] = node.specifiers;
|
|
22
|
+
const assignExpression = template_1.default.ast(`const ${specifier.local.name} = '${fileUrl}';`);
|
|
23
|
+
importDeclarationAstPath.replaceWith(assignExpression);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
//# sourceMappingURL=path-transform-babel.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"path-transform-babel.js","sourceRoot":"","sources":["../../src/path-transform/path-transform-babel.ts"],"names":[],"mappings":";;;;;AAEA,+DAAsC;AACtC,mCAA8B;AAM9B,MAAM,CAAC,OAAO,GAAG,GAAe,EAAE;IAChC,OAAO;QACL,OAAO,EAAE;YACP,iBAAiB,CAAE,wBAAqD,EAAE,KAAiB;;gBACzF,IAAI,MAAA,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,0CAAE,QAAQ,CAAC,cAAc,CAAC;oBAAE,OAAM;gBAE9D,MAAM,EAAE,SAAS,GAAG,YAAI,EAAE,GAAG,KAAK,CAAC,IAA0B,CAAA;gBAE7D,MAAM,EAAE,IAAI,EAAE,GAAG,wBAAwB,CAAA;gBAEzC,MAAM,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC,MAAM,CAAA;gBAE7B,MAAM,OAAO,GAAG,SAAS,CAAC,KAAK,CAAC,IAAI,EAAE,CAAA;gBAEtC,IAAI,CAAC,OAAO,IAAI,OAAO,KAAK,KAAK;oBAAE,OAAM;gBAEzC,MAAM,CAAC,SAAS,CAAC,GAAG,IAAI,CAAC,UAAU,CAAA;gBAEnC,MAAM,gBAAgB,GAAG,kBAAQ,CAAC,GAAG,CAAC,SAAS,SAAS,CAAC,KAAK,CAAC,IAAI,OAAO,OAAO,IAAI,CAAC,CAAA;gBAEtF,wBAAwB,CAAC,WAAW,CAAC,gBAA6B,CAAC,CAAA;YACrE,CAAC;SACF;KACF,CAAA;AACH,CAAC,CAAA"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const utils_1 = require("../utils");
|
|
4
|
+
const urlRegexp = /url\(['"]([^'"]*)['"]\)/;
|
|
5
|
+
module.exports = (opt) => {
|
|
6
|
+
return {
|
|
7
|
+
postcssPlugin: 'assets-path-transform',
|
|
8
|
+
Declaration(decl) {
|
|
9
|
+
if (!urlRegexp.test(decl.value))
|
|
10
|
+
return;
|
|
11
|
+
const [_, filePath] = decl.value.match(urlRegexp);
|
|
12
|
+
const { transform = utils_1.noop } = opt;
|
|
13
|
+
const transformedFilePath = transform(filePath) || '';
|
|
14
|
+
if (!transformedFilePath || transformedFilePath === filePath)
|
|
15
|
+
return;
|
|
16
|
+
decl.value = `url(${transformedFilePath})`;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
//# sourceMappingURL=path-transform-postcss.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"path-transform-postcss.js","sourceRoot":"","sources":["../../src/path-transform/path-transform-postcss.ts"],"names":[],"mappings":";;AACA,mCAA8B;AAE9B,MAAM,SAAS,GAAG,yBAAyB,CAAA;AAM3C,MAAM,CAAC,OAAO,GAAG,CAAC,GAA2B,EAAkB,EAAE;IAC/D,OAAO;QACL,aAAa,EAAE,uBAAuB;QAEtC,WAAW,CAAE,IAAI;YACf,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC;gBAAE,OAAM;YAEvC,MAAM,CAAC,CAAC,EAAE,QAAQ,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,SAAS,CAAE,CAAA;YAElD,MAAM,EAAE,SAAS,GAAG,YAAI,EAAE,GAAG,GAAG,CAAA;YAEhC,MAAM,mBAAmB,GAAG,SAAS,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAA;YAErD,IAAI,CAAC,mBAAmB,IAAI,mBAAmB,KAAK,QAAQ;gBAAE,OAAM;YAEpE,IAAI,CAAC,KAAK,GAAG,OAAO,mBAAmB,GAAG,CAAA;QAC5C,CAAC;KACF,CAAA;AACH,CAAC,CAAA"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.pathTransform = void 0;
|
|
4
|
+
const utils_1 = require("../utils");
|
|
5
|
+
const pathTransform = (opt) => {
|
|
6
|
+
return (path) => {
|
|
7
|
+
const remoteAssetInfoMap = (0, utils_1.getCacheData)(opt.cacheFilePath) || {};
|
|
8
|
+
const pathAliasRegMap = Object.keys(opt.pathAlias).reduce((result, key) => {
|
|
9
|
+
return Object.assign(Object.assign({}, result), { [key]: new RegExp(`^${key}`) });
|
|
10
|
+
}, {});
|
|
11
|
+
const localPath = (() => {
|
|
12
|
+
const matchPathAlias = Object.keys(pathAliasRegMap).find(key => pathAliasRegMap[key].test(path));
|
|
13
|
+
if (matchPathAlias)
|
|
14
|
+
return path.replace(pathAliasRegMap[matchPathAlias], opt.pathAlias[matchPathAlias]);
|
|
15
|
+
return path;
|
|
16
|
+
})();
|
|
17
|
+
if (!remoteAssetInfoMap[localPath])
|
|
18
|
+
return localPath;
|
|
19
|
+
return remoteAssetInfoMap[localPath];
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
exports.pathTransform = pathTransform;
|
|
23
|
+
//# sourceMappingURL=path-transform.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"path-transform.js","sourceRoot":"","sources":["../../src/path-transform/path-transform.ts"],"names":[],"mappings":";;;AAAA,mCAAsC;AAO/B,MAAM,aAAa,GAAG,CAAC,GAAQ,EAAE,EAAE;IACxC,OAAO,CAAC,IAAY,EAAE,EAAE;QACtB,MAAM,kBAAkB,GAAG,IAAA,oBAAY,EAAC,GAAG,CAAC,aAAa,CAAC,IAAI,EAAE,CAAA;QAEhE,MAAM,eAAe,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,MAAM,CAAyB,CAAC,MAAM,EAAE,GAAG,EAAE,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,SAAS,CAAA;QACpD,OAAO,kBAAkB,CAAC,SAAS,CAAC,CAAA;IACtC,CAAC,CAAA;AACH,CAAC,CAAA;AAhBY,QAAA,aAAa,iBAgBzB"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface LocalAssetInfo {
|
|
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;
|
|
14
|
+
export interface RemoteAssetPluginOpt {
|
|
15
|
+
assetsDirPath: string;
|
|
16
|
+
pathAlias?: Record<string, string>;
|
|
17
|
+
uploader?: Uploader;
|
|
18
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,6 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -0,0 +1,19 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/upload-adapter/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2DAAwC;AACxC,mDAAgC"}
|
|
@@ -0,0 +1,67 @@
|
|
|
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
|
+
const path_1 = __importDefault(require("path"));
|
|
30
|
+
exports.aliOssUploadAdapter = (0, define_adapter_1.defineUploaderAdapter)((opt) => {
|
|
31
|
+
const { customDomain, bucketDir = '/' } = opt, aliOssOpt = __rest(opt, ["customDomain", "bucketDir"]);
|
|
32
|
+
const client = new ali_oss_1.default(aliOssOpt);
|
|
33
|
+
const isRemoteAssetExist = (...args) => __awaiter(void 0, void 0, void 0, function* () {
|
|
34
|
+
try {
|
|
35
|
+
yield client.head(...args);
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
if (error.code === 'NoSuchKey')
|
|
40
|
+
return false;
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
const uploadToAliOss = (...args) => __awaiter(void 0, void 0, void 0, function* () { return client.put(...args); });
|
|
45
|
+
const getRemoteAssetPath = (localAssetInfo) => {
|
|
46
|
+
const { uniqueKey, ext } = localAssetInfo;
|
|
47
|
+
return path_1.default.join(bucketDir, `${uniqueKey}${ext}`);
|
|
48
|
+
};
|
|
49
|
+
const getRemoteAssetUrl = (localAssetInfo) => {
|
|
50
|
+
const { uniqueKey, ext } = localAssetInfo;
|
|
51
|
+
if (customDomain)
|
|
52
|
+
return path_1.default.join(customDomain, `${uniqueKey}${ext}`);
|
|
53
|
+
const remotePath = getRemoteAssetPath(localAssetInfo);
|
|
54
|
+
return client.signatureUrl(remotePath);
|
|
55
|
+
};
|
|
56
|
+
return (localAssetInfo) => __awaiter(void 0, void 0, void 0, function* () {
|
|
57
|
+
const { localPath } = localAssetInfo;
|
|
58
|
+
const remoteAssetPath = getRemoteAssetPath(localAssetInfo);
|
|
59
|
+
const remoteAssetUrl = getRemoteAssetUrl(localAssetInfo);
|
|
60
|
+
const isExist = yield isRemoteAssetExist(remoteAssetPath);
|
|
61
|
+
if (isExist)
|
|
62
|
+
return Object.assign(Object.assign({}, localAssetInfo), { remoteUrl: remoteAssetUrl });
|
|
63
|
+
yield uploadToAliOss(remoteAssetPath, localPath);
|
|
64
|
+
return Object.assign(Object.assign({}, localAssetInfo), { remoteUrl: remoteAssetUrl });
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
//# sourceMappingURL=upload-adapter-ali-oss.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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;AAC5B,g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,YAAY,CAAC,UAAU,CAAC,CAAA;IACxC,CAAC,CAAA;IAED,OAAO,CAAO,cAA8B,EAAE,EAAE;QAC9C,MAAM,EAAE,SAAS,EAAE,GAAG,cAAc,CAAA;QACpC,MAAM,eAAe,GAAG,kBAAkB,CAAC,cAAc,CAAC,CAAA;QAC1D,MAAM,cAAc,GAAG,iBAAiB,CAAC,cAAc,CAAC,CAAA;QACxD,MAAM,OAAO,GAAG,MAAM,kBAAkB,CAAC,eAAe,CAAC,CAAA;QACzD,IAAI,OAAO;YAAE,uCAAY,cAAc,KAAE,SAAS,EAAE,cAAc,IAAE;QACpE,MAAM,cAAc,CAAC,eAAe,EAAE,SAAS,CAAC,CAAA;QAChD,uCAAY,cAAc,KAAE,SAAS,EAAE,cAAc,IAAE;IACzD,CAAC,CAAA,CAAA;AACH,CAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1,34 @@
|
|
|
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, upload } = opt;
|
|
21
|
+
if (!assetsDirPath || !upload)
|
|
22
|
+
return [];
|
|
23
|
+
const assetsPaths = (0, utils_1.travelFiles)(assetsDirPath);
|
|
24
|
+
const { results: remoteAssetInfoList } = yield promise_pool_1.PromisePool.withConcurrency(2)
|
|
25
|
+
.for(assetsPaths)
|
|
26
|
+
.process((localPath) => __awaiter(void 0, void 0, void 0, function* () {
|
|
27
|
+
const uniqueKey = (0, utils_1.generateFileUniqueKey)(localPath);
|
|
28
|
+
const { ext } = path_1.default.parse(localPath);
|
|
29
|
+
return upload({ localPath, uniqueKey, ext });
|
|
30
|
+
}));
|
|
31
|
+
return remoteAssetInfoList;
|
|
32
|
+
});
|
|
33
|
+
exports.uploadAssets = uploadAssets;
|
|
34
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/upload-assets/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,4DAAuD;AAEvD,mCAA4D;AAC5D,gDAAuB;AAOhB,MAAM,YAAY,GAAG,CAAO,GAAgB,EAAE,EAAE;IACrD,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,GAAG,GAAG,CAAA;IAErC,IAAI,CAAC,aAAa,IAAI,CAAC,MAAM;QAAE,OAAO,EAAE,CAAA;IAExC,MAAM,WAAW,GAAG,IAAA,mBAAW,EAAC,aAAa,CAAC,CAAA;IAE9C,MAAM,EAAE,OAAO,EAAE,mBAAmB,EAAE,GAAG,MAAM,0BAAW,CAAC,eAAe,CAAC,CAAC,CAAC;SAC1E,GAAG,CAAC,WAAW,CAAC;SAChB,OAAO,CAAC,CAAO,SAAS,EAAE,EAAE;QAC3B,MAAM,SAAS,GAAG,IAAA,6BAAqB,EAAC,SAAS,CAAC,CAAA;QAClD,MAAM,EAAE,GAAG,EAAE,GAAG,cAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAA;QACrC,OAAO,MAAM,CAAC,EAAE,SAAS,EAAE,SAAS,EAAE,GAAG,EAAE,CAAC,CAAA;IAC9C,CAAC,CAAA,CAAC,CAAA;IAEJ,OAAO,mBAAmB,CAAA;AAC5B,CAAC,CAAA,CAAA;AAhBY,QAAA,YAAY,gBAgBxB"}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
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;
|
|
11
|
+
export declare const noop: () => void;
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.noop = exports.saveCacheData = exports.getCacheData = exports.generateFileUniqueKey = exports.travelFiles = void 0;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
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
|
+
const getCacheData = (cacheFilePath) => {
|
|
33
|
+
try {
|
|
34
|
+
fs_1.default.accessSync(cacheFilePath);
|
|
35
|
+
return JSON.parse(fs_1.default.readFileSync(cacheFilePath).toString());
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
exports.getCacheData = getCacheData;
|
|
42
|
+
const saveCacheData = (cacheFilePath, cacheData) => {
|
|
43
|
+
fs_1.default.writeFileSync(cacheFilePath, JSON.stringify(cacheData));
|
|
44
|
+
};
|
|
45
|
+
exports.saveCacheData = saveCacheData;
|
|
46
|
+
const noop = () => { };
|
|
47
|
+
exports.noop = noop;
|
|
48
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;;;;AAAA,4CAAmB;AACnB,gDAAuB;AACvB,8CAAqB;AAErB;;GAEG;AACI,MAAM,WAAW,GAAG,CAAC,GAAW,EAAY,EAAE;IACnD,MAAM,KAAK,GAAG,YAAE,CAAC,WAAW,CAAC,GAAG,CAAC,CAAA;IACjC,OAAO,KAAK,CAAC,MAAM,CAAW,CAAC,MAAM,EAAE,IAAI,E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CAAC,CAAA;AAC5D,CAAC,CAAA;AAFY,QAAA,aAAa,iBAEzB;AAEM,MAAM,IAAI,GAAG,GAAG,EAAE,GAAE,CAAC,CAAA;AAAf,QAAA,IAAI,QAAW"}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@taro-minify-pack/plugin-remote-assets",
|
|
3
|
+
"version": "0.0.1-alpha.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"homepage": "https://github.com/panyu97py/taro-minify-pack",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/panyu97py/taro-minify-pack.git"
|
|
14
|
+
},
|
|
15
|
+
"author": "yu.pan",
|
|
16
|
+
"license": "ISC",
|
|
17
|
+
"peerDependencies": {
|
|
18
|
+
"@tarojs/service": "^3.0.0"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@babel/core": "^7.26.10",
|
|
22
|
+
"@babel/generator": "^7.26.10",
|
|
23
|
+
"@babel/parser": "^7.26.10",
|
|
24
|
+
"@babel/template": "^7.26.9",
|
|
25
|
+
"@babel/traverse": "^7.26.10",
|
|
26
|
+
"@babel/types": "^7.26.10",
|
|
27
|
+
"@supercharge/promise-pool": "^3.2.0",
|
|
28
|
+
"ali-oss": "^6.23.0",
|
|
29
|
+
"md5": "^2.3.0",
|
|
30
|
+
"mini-css-extract-plugin": "^2.9.2",
|
|
31
|
+
"postcss": "^8.5.3"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@tarojs/service": "^4.0.9",
|
|
35
|
+
"@types/ali-oss": "^6.16.13",
|
|
36
|
+
"@types/babel__core": "^7.20.5",
|
|
37
|
+
"@types/babel__generator": "^7.6.8",
|
|
38
|
+
"@types/babel__template": "^7.4.4",
|
|
39
|
+
"@types/babel__traverse": "^7.20.6",
|
|
40
|
+
"@types/md5": "^2.3.5",
|
|
41
|
+
"@types/node": "^18.19.130",
|
|
42
|
+
"@types/webpack-sources": "^3.2.3",
|
|
43
|
+
"@typescript-eslint/eslint-plugin": "^8.26.1",
|
|
44
|
+
"@typescript-eslint/parser": "^8.26.1",
|
|
45
|
+
"eslint": "^8.19.0",
|
|
46
|
+
"eslint-config-standard": "^17.0.0",
|
|
47
|
+
"eslint-plugin-import": "^2.26.0",
|
|
48
|
+
"eslint-plugin-n": "^15.2.3",
|
|
49
|
+
"eslint-plugin-promise": "^6.0.0",
|
|
50
|
+
"tsc-alias": "^1.8.16",
|
|
51
|
+
"typescript": "^5.8.2"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "tsc && tsc-alias"
|
|
55
|
+
}
|
|
56
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import path from 'path'
|
|
2
|
+
import { RemoteAssetPluginOpt } from '@/types'
|
|
3
|
+
import { IPluginContext } from '@tarojs/service'
|
|
4
|
+
import { uploadAssets } from './upload-assets'
|
|
5
|
+
import { saveCacheData } from './utils'
|
|
6
|
+
import { pathTransform } from '@/path-transform/path-transform'
|
|
7
|
+
|
|
8
|
+
export { RemoteAssetPluginOpt } from '@/types'
|
|
9
|
+
|
|
10
|
+
export * from './upload-adapter'
|
|
11
|
+
|
|
12
|
+
const cacheFilePath = path.resolve(__dirname, 'remote-assets-cache.json')
|
|
13
|
+
|
|
14
|
+
export default (ctx: IPluginContext, pluginOpts: RemoteAssetPluginOpt) => {
|
|
15
|
+
const transform = pathTransform({ cacheFilePath, pathAlias: pluginOpts.pathAlias || {} })
|
|
16
|
+
|
|
17
|
+
ctx.onBuildStart(async () => {
|
|
18
|
+
const { assetsDirPath, uploader } = pluginOpts
|
|
19
|
+
const remoteAssetInfoList = await uploadAssets({ assetsDirPath, upload: uploader })
|
|
20
|
+
const remoteAssetInfoMap = remoteAssetInfoList.reduce((result, item) => {
|
|
21
|
+
return { ...result, [item.localPath]: item.remoteUrl }
|
|
22
|
+
}, {})
|
|
23
|
+
saveCacheData(cacheFilePath, remoteAssetInfoMap)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
ctx.modifyRunnerOpts((curRunnerOpts) => {
|
|
27
|
+
const { postcss: curPostcssOpts } = curRunnerOpts.opts
|
|
28
|
+
console.log(curPostcssOpts, path.resolve(__dirname, './path-transform/path-transform-postcss'))
|
|
29
|
+
curRunnerOpts.opts.postcss = {
|
|
30
|
+
...curPostcssOpts,
|
|
31
|
+
[path.resolve(__dirname, './path-transform/path-transform-postcss')]: {
|
|
32
|
+
enabled: true,
|
|
33
|
+
config: { transform }
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
ctx.modifyWebpackChain(({ chain }) => {
|
|
39
|
+
const babelPluginPathTransformPath = path.resolve(__dirname, './path-transform/path-transform-babel')
|
|
40
|
+
|
|
41
|
+
chain.module
|
|
42
|
+
.rule('script')
|
|
43
|
+
.use('babelLoader')
|
|
44
|
+
.options({
|
|
45
|
+
plugins: [
|
|
46
|
+
[babelPluginPathTransformPath, { transform }]
|
|
47
|
+
]
|
|
48
|
+
})
|
|
49
|
+
})
|
|
50
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { NodePath, PluginItem, PluginPass } from '@babel/core'
|
|
2
|
+
import type { ImportDeclaration, Statement } from '@babel/types'
|
|
3
|
+
import template from '@babel/template'
|
|
4
|
+
import { noop } from '@/utils'
|
|
5
|
+
|
|
6
|
+
export interface AssetsTransformOpt {
|
|
7
|
+
transform?: (filePath: string) => string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
module.exports = (): PluginItem => {
|
|
11
|
+
return {
|
|
12
|
+
visitor: {
|
|
13
|
+
ImportDeclaration (importDeclarationAstPath: NodePath<ImportDeclaration>, state: PluginPass) {
|
|
14
|
+
if (state.file.opts.filename?.includes('node_modules')) return
|
|
15
|
+
|
|
16
|
+
const { transform = noop } = state.opts as AssetsTransformOpt
|
|
17
|
+
|
|
18
|
+
const { node } = importDeclarationAstPath
|
|
19
|
+
|
|
20
|
+
const { value } = node.source
|
|
21
|
+
|
|
22
|
+
const fileUrl = transform(value) || ''
|
|
23
|
+
|
|
24
|
+
if (!fileUrl || fileUrl === value) return
|
|
25
|
+
|
|
26
|
+
const [specifier] = node.specifiers
|
|
27
|
+
|
|
28
|
+
const assignExpression = template.ast(`const ${specifier.local.name} = '${fileUrl}';`)
|
|
29
|
+
|
|
30
|
+
importDeclarationAstPath.replaceWith(assignExpression as Statement)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { AcceptedPlugin } from 'postcss'
|
|
2
|
+
import { noop } from '@/utils'
|
|
3
|
+
|
|
4
|
+
const urlRegexp = /url\(['"]([^'"]*)['"]\)/
|
|
5
|
+
|
|
6
|
+
export interface AssetsPathTransformOpt {
|
|
7
|
+
transform?: (filePath: string) => string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
module.exports = (opt: AssetsPathTransformOpt): AcceptedPlugin => {
|
|
11
|
+
return {
|
|
12
|
+
postcssPlugin: 'assets-path-transform',
|
|
13
|
+
|
|
14
|
+
Declaration (decl) {
|
|
15
|
+
if (!urlRegexp.test(decl.value)) return
|
|
16
|
+
|
|
17
|
+
const [_, filePath] = decl.value.match(urlRegexp)!
|
|
18
|
+
|
|
19
|
+
const { transform = noop } = opt
|
|
20
|
+
|
|
21
|
+
const transformedFilePath = transform(filePath) || ''
|
|
22
|
+
|
|
23
|
+
if (!transformedFilePath || transformedFilePath === filePath) return
|
|
24
|
+
|
|
25
|
+
decl.value = `url(${transformedFilePath})`
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { getCacheData } from '@/utils'
|
|
2
|
+
|
|
3
|
+
interface Opt{
|
|
4
|
+
cacheFilePath: string
|
|
5
|
+
pathAlias: Record<string, string>
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export const pathTransform = (opt: Opt) => {
|
|
9
|
+
return (path: string) => {
|
|
10
|
+
const remoteAssetInfoMap = getCacheData(opt.cacheFilePath) || {}
|
|
11
|
+
|
|
12
|
+
const pathAliasRegMap = Object.keys(opt.pathAlias).reduce<Record<string, RegExp>>((result, key) => {
|
|
13
|
+
return { ...result, [key]: new RegExp(`^${key}`) }
|
|
14
|
+
}, {})
|
|
15
|
+
|
|
16
|
+
const localPath = (() => {
|
|
17
|
+
const matchPathAlias = Object.keys(pathAliasRegMap).find(key => pathAliasRegMap[key].test(path))
|
|
18
|
+
if (matchPathAlias) return path.replace(pathAliasRegMap[matchPathAlias], opt.pathAlias[matchPathAlias])
|
|
19
|
+
return path
|
|
20
|
+
})()
|
|
21
|
+
if (!remoteAssetInfoMap[localPath]) return localPath
|
|
22
|
+
return remoteAssetInfoMap[localPath]
|
|
23
|
+
}
|
|
24
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export interface LocalAssetInfo {
|
|
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
|
|
17
|
+
|
|
18
|
+
export interface RemoteAssetPluginOpt {
|
|
19
|
+
assetsDirPath: string,
|
|
20
|
+
pathAlias?: Record<string, string>
|
|
21
|
+
uploader?: Uploader,
|
|
22
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { defineUploaderAdapter } from './define-adapter'
|
|
2
|
+
import { LocalAssetInfo } from '@/types'
|
|
3
|
+
import AliOss from 'ali-oss'
|
|
4
|
+
import path from 'path'
|
|
5
|
+
|
|
6
|
+
export interface AliOssUploadAdapterOpt extends AliOss.Options {
|
|
7
|
+
customDomain?: string
|
|
8
|
+
bucketDir?: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const aliOssUploadAdapter = defineUploaderAdapter((opt: AliOssUploadAdapterOpt) => {
|
|
12
|
+
const { customDomain, bucketDir = '/', ...aliOssOpt } = opt
|
|
13
|
+
|
|
14
|
+
const client = new AliOss(aliOssOpt)
|
|
15
|
+
|
|
16
|
+
const isRemoteAssetExist = async (...args: Parameters<AliOss['head']>) => {
|
|
17
|
+
try {
|
|
18
|
+
await client.head(...args)
|
|
19
|
+
return true
|
|
20
|
+
} catch (error: any) {
|
|
21
|
+
if (error.code === 'NoSuchKey') return false
|
|
22
|
+
throw error
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const uploadToAliOss = async (...args: Parameters<AliOss['put']>) => client.put(...args)
|
|
27
|
+
|
|
28
|
+
const getRemoteAssetPath = (localAssetInfo: LocalAssetInfo) => {
|
|
29
|
+
const { uniqueKey, ext } = localAssetInfo
|
|
30
|
+
return path.join(bucketDir, `${uniqueKey}${ext}`)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const getRemoteAssetUrl = (localAssetInfo: LocalAssetInfo) => {
|
|
34
|
+
const { uniqueKey, ext } = localAssetInfo
|
|
35
|
+
if (customDomain) return path.join(customDomain, `${uniqueKey}${ext}`)
|
|
36
|
+
const remotePath = getRemoteAssetPath(localAssetInfo)
|
|
37
|
+
return client.signatureUrl(remotePath)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return async (localAssetInfo: LocalAssetInfo) => {
|
|
41
|
+
const { localPath } = localAssetInfo
|
|
42
|
+
const remoteAssetPath = getRemoteAssetPath(localAssetInfo)
|
|
43
|
+
const remoteAssetUrl = getRemoteAssetUrl(localAssetInfo)
|
|
44
|
+
const isExist = await isRemoteAssetExist(remoteAssetPath)
|
|
45
|
+
if (isExist) return { ...localAssetInfo, remoteUrl: remoteAssetUrl }
|
|
46
|
+
await uploadToAliOss(remoteAssetPath, localPath)
|
|
47
|
+
return { ...localAssetInfo, remoteUrl: remoteAssetUrl }
|
|
48
|
+
}
|
|
49
|
+
})
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { PromisePool } from '@supercharge/promise-pool'
|
|
2
|
+
import { Uploader } from '@/types'
|
|
3
|
+
import { generateFileUniqueKey, travelFiles } from '@/utils'
|
|
4
|
+
import path from 'path'
|
|
5
|
+
|
|
6
|
+
interface UploaderOpt {
|
|
7
|
+
assetsDirPath?: string,
|
|
8
|
+
upload?: Uploader
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const uploadAssets = async (opt: UploaderOpt) => {
|
|
12
|
+
const { assetsDirPath, upload } = opt
|
|
13
|
+
|
|
14
|
+
if (!assetsDirPath || !upload) return []
|
|
15
|
+
|
|
16
|
+
const assetsPaths = travelFiles(assetsDirPath)
|
|
17
|
+
|
|
18
|
+
const { results: remoteAssetInfoList } = await PromisePool.withConcurrency(2)
|
|
19
|
+
.for(assetsPaths)
|
|
20
|
+
.process(async (localPath) => {
|
|
21
|
+
const uniqueKey = generateFileUniqueKey(localPath)
|
|
22
|
+
const { ext } = path.parse(localPath)
|
|
23
|
+
return upload({ localPath, uniqueKey, ext })
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
return remoteAssetInfoList
|
|
27
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import fs from 'fs'
|
|
2
|
+
import path from 'path'
|
|
3
|
+
import md5 from 'md5'
|
|
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 => {
|
|
27
|
+
try {
|
|
28
|
+
fs.accessSync(cacheFilePath)
|
|
29
|
+
return JSON.parse(fs.readFileSync(cacheFilePath).toString())
|
|
30
|
+
} catch (error) {
|
|
31
|
+
return null
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const saveCacheData = (cacheFilePath:string, cacheData:any) => {
|
|
36
|
+
fs.writeFileSync(cacheFilePath, JSON.stringify(cacheData))
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const noop = () => {}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"baseUrl": "./",
|
|
4
|
+
"target": "ES2015",
|
|
5
|
+
"module": "commonjs",
|
|
6
|
+
"strict": true,
|
|
7
|
+
"sourceMap": true,
|
|
8
|
+
"declaration": true,
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"moduleResolution": "node",
|
|
11
|
+
"outDir": "./dist/",
|
|
12
|
+
"skipLibCheck": true,
|
|
13
|
+
/* Skip type checking of declaration files. */
|
|
14
|
+
"forceConsistentCasingInFileNames": true,
|
|
15
|
+
"jsx": "react",
|
|
16
|
+
"lib": ["esnext", "dom"],
|
|
17
|
+
"types": ["@types/node"],
|
|
18
|
+
"paths": {
|
|
19
|
+
"@/*": ["src/*"],
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"include": ["src"],
|
|
23
|
+
|
|
24
|
+
}
|