forked from HunterXuan/wx-tfjs-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
121 lines (114 loc) · 4.21 KB
/
rollup.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import path from 'path';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import builtins from 'rollup-plugin-node-builtins';
import esbuild from 'rollup-plugin-esbuild';
import { terser } from 'rollup-plugin-terser';
import alias from '@rollup/plugin-alias';
import multiInput from 'rollup-plugin-multi-input';
const p = s => path.resolve(__dirname, s);
const useCustom = process.argv.includes('--custom');
const isDev = process.argv.includes('-w') || process.argv.includes('--watch');
function codeTransform() {
return {
transform(code, file) {
// 注入环境变量
code = code.replace(`import.meta.CUSTOM`, `${useCustom}`);
// 改为国内地址
code = code
.replace(/fromTFHub:/g, 'fromTFHub_:')
.replace(
/https:\/\/tfhub.dev\/mediapipe\/tfjs-model\/handdetector\/1\/default\/1/g,
'https://ai.flypot.cn/proxy/tfjs-models/tfhub-tfjs-modules/mediapipe/tfjs-model/handdetector/1/default/1/model.json'
)
.replace(
/https:\/\/tfhub.dev\/mediapipe\/tfjs-model\/handskeleton\/1\/default\/1\/anchors.json\?tfjs-format=file/g,
'https://ai.flypot.cn/proxy/tfjs-models/tfhub-tfjs-modules/mediapipe/tfjs-model/handskeleton/1/default/1/anchors.json',
)
.replace(
/https:\/\/tfhub.dev\/mediapipe\/tfjs-model\/handskeleton\/1\/default\/1/g,
'https://ai.flypot.cn/proxy/tfjs-models/tfhub-tfjs-modules/mediapipe/tfjs-model/handskeleton/1/default/1/model.json',
)
.replace(
/https:\/\/tfhub.dev\/mediapipe\/tfjs-model\/facemesh\/1\/default\/1/g,
'https://ai.flypot.cn/proxy/tfjs-models/tfhub-tfjs-modules/mediapipe/tfjs-model/facemesh/1/default/1/model.json',
)
.replace(
/https:\/\/tfhub.dev\/mediapipe\/tfjs-model\/iris\/1\/default\/2/g,
'https://ai.flypot.cn/proxy/tfjs-models/tfhub-tfjs-modules/mediapipe/tfjs-model/iris/1/default/2/model.json',
)
.replace(
/https:\/\/tfhub.dev\/tensorflow\/tfjs-model\/blazeface\/1\/default\/1/g,
'https://www.gstaticcnapps.cn/tfhub-tfjs-modules/tensorflow/tfjs-model/blazeface/1/default/1/model.json'
)
.replace(
/https:\/\/tfhub.dev\/google\/imagenet\/mobilenet_v1_025_224\/classification\/1/g,
'https://www.gstaticcnapps.cn/tfhub-tfjs-modules/google/imagenet/mobilenet_v1_025_224/classification/1/model.json'
)
.replace(
/https:\/\/storage.googleapis.com\//g,
'https://www.gstaticcnapps.cn/'
);
// body-pix
code = code.replace(/function getInputSize\(e\){/g, 'function getInputSize(e){return [e.shape[0], e.shape[1]];').replace('webgl2', 'webgl');
return { code };
},
};
}
const aliasPlugin = useCustom
? alias({
entries: [
{
find: /@tensorflow\/tfjs$/,
replacement: p('./custom_tfjs/custom_tfjs.js'),
},
{
find: /@tensorflow\/tfjs-core$/,
replacement: p('./custom_tfjs/custom_tfjs_core.js'),
},
{
find: '@tensorflow/tfjs-core/dist/ops/ops_for_converter',
replacement: p('./custom_tfjs/custom_ops_for_converter.js'),
},
],
})
: null;
export default [
{
input: [
'miniprogram/**/*.ts',
],
treeshake: true,
output: {
format: 'cjs',
dir: 'miniprogram/',
chunkFileNames: 'chunks/[name].js',
entryFileNames: (chunkInfo) => {
return chunkInfo.facadeModuleId.replace(p('./miniprogram'), '').replace('.ts', '.js').substring(1);
},
},
plugins: [
multiInput(),
codeTransform(),
aliasPlugin,
builtins(),
resolve({
extensions: ['.ts', '.js'],
preferBuiltins: false,
mainFields: ['jsnext:main', 'jsnext', 'module', 'main'],
}),
commonjs({ include: ['node_modules/**'] }),
esbuild({
sourceMap: false,
minify: !isDev,
target: 'es2018',
legalComments: 'none',
}),
terser({
output: { comments: false },
mangle: !isDev,
compress: !isDev,
}),
],
},
];