-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathrollup.config.js
180 lines (166 loc) · 6.26 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const fs = require('fs');
const path = require('path');
const commonjs = require('@rollup/plugin-commonjs');
const copy = require('rollup-plugin-copy');
const license = require('rollup-plugin-license');
const nodeGlobals = require('rollup-plugin-node-globals');
const nodeResolve = require('@rollup/plugin-node-resolve');
const nodePolyfills = require('rollup-plugin-node-polyfills');
const replace = require('rollup-plugin-re');
const typescript = require('@rollup/plugin-typescript');
const { generateCrossPlatformPath } = require('../../utils/build');
const packageJson = require('./package.json');
const json = require('@rollup/plugin-json');
const wasm = require('@rollup/plugin-wasm');
const PRODUCT_NAME = 'e3kit';
const FORMAT = {
CJS: 'cjs',
ES: 'es',
UMD: 'umd',
};
const CRYPTO_TYPE = {
WASM: 'wasm',
ASMJS: 'asmjs',
};
const TARGET = {
BROWSER: 'browser',
WORKER: 'worker',
};
const sourcePath = path.join(__dirname, 'src');
const outputPath = path.join(__dirname, 'dist');
const getModulePath = (request) => {
const resolvePaths = require.resolve.paths(request);
for (let resolvePath of resolvePaths) {
const modulePath = path.join(resolvePath, request);
if (fs.existsSync(modulePath)) {
return modulePath;
}
}
throw new Error(`Module '${request}' was not found`);
};
const getCryptoEntryPointName = (target, cryptoType, format) =>
`${target}${cryptoType === CRYPTO_TYPE.ASMJS ? '.asmjs' : ''}.${format}.js`;
const createEntry = (target, cryptoType, format) => {
const foundationModuleName = 'virgil-crypto';
const foundationPath = getModulePath(foundationModuleName);
const foundationEntryPoint = generateCrossPlatformPath(
foundationModuleName,
'dist',
getCryptoEntryPointName(target, cryptoType, FORMAT.ES),
);
const foundationWasmPath = generateCrossPlatformPath(
foundationPath,
'dist',
`libfoundation.${target}.wasm`,
);
const pythiaModuleName = '@virgilsecurity/pythia-crypto';
const pythiaPath = getModulePath(pythiaModuleName);
const pythiaEntryPoint = generateCrossPlatformPath(
pythiaModuleName,
'dist',
getCryptoEntryPointName(target, cryptoType, FORMAT.ES),
);
const pythiaWasmPath = generateCrossPlatformPath(
pythiaPath,
'dist',
`libpythia.${target}.wasm`,
);
return {
external: format !== FORMAT.UMD ? [foundationEntryPoint, pythiaEntryPoint] : [],
input: path.join(sourcePath, 'index.ts'),
output: {
format,
file: path.join(outputPath, getCryptoEntryPointName(target, cryptoType, format)),
name: 'E3kit',
},
onwarn: function (warning) {
// Skip certain warnings
// should intercept ... but doesn't in some rollup versions
if (warning.code === 'THIS_IS_UNDEFINED') {
return;
}
// console.warn everything else
console.warn(warning.message);
},
plugins: [
replace({
replaces: {
'process.env.__VIRGIL_PRODUCT_NAME__': JSON.stringify(PRODUCT_NAME),
'process.env.__VIRGIL_PRODUCT_VERSION__': JSON.stringify(packageJson.version),
'process.env.NODE_DEBUG': JSON.stringify(''),
},
patterns: [
{
match: /node_modules\/level-js\/(index|iterator)\.js/,
test: "var setImmediate = require('./util/immediate')",
replace: "var setImmediate = require('immediate')",
},
{
match: /node_modules\/level-js\/util\/clear\.js/,
test: "var setImmediate = require('./immediate')",
replace: "var setImmediate = require('immediate')",
},
{
match: /node_modules\/level-js\/util\/immediate-browser.js/,
test: "module.exports = require('immediate')",
replace: 'module.exports = {}',
},
{
match: /(index|EThree)\.ts$/,
test: foundationModuleName,
replace: foundationEntryPoint,
},
{
match: /EThree\.ts$/,
test: pythiaModuleName,
replace: pythiaEntryPoint,
},
],
}),
nodePolyfills(),
nodeResolve({ browser: true, preferBuiltins: false }),
commonjs(),
json(),
license({
banner: {
content: {
file: path.join(__dirname, '..', '..', 'LICENSE'),
},
},
}),
wasm({
publicPath: outputPath,
sync: [foundationWasmPath, pythiaWasmPath],
}),
copy({
targets: [
{
src: foundationWasmPath,
dest: outputPath,
},
{
src: pythiaWasmPath,
dest: outputPath,
},
],
}),
typescript({
tsconfig: './tsconfig.json',
}),
],
};
};
module.exports = [
createEntry(TARGET.BROWSER, CRYPTO_TYPE.ASMJS, FORMAT.CJS),
createEntry(TARGET.BROWSER, CRYPTO_TYPE.WASM, FORMAT.CJS),
createEntry(TARGET.BROWSER, CRYPTO_TYPE.ASMJS, FORMAT.ES),
createEntry(TARGET.BROWSER, CRYPTO_TYPE.WASM, FORMAT.ES),
createEntry(TARGET.BROWSER, CRYPTO_TYPE.ASMJS, FORMAT.UMD),
createEntry(TARGET.BROWSER, CRYPTO_TYPE.WASM, FORMAT.UMD),
createEntry(TARGET.WORKER, CRYPTO_TYPE.ASMJS, FORMAT.CJS),
createEntry(TARGET.WORKER, CRYPTO_TYPE.WASM, FORMAT.CJS),
createEntry(TARGET.WORKER, CRYPTO_TYPE.ASMJS, FORMAT.ES),
createEntry(TARGET.WORKER, CRYPTO_TYPE.WASM, FORMAT.ES),
createEntry(TARGET.WORKER, CRYPTO_TYPE.ASMJS, FORMAT.UMD),
createEntry(TARGET.WORKER, CRYPTO_TYPE.WASM, FORMAT.UMD),
];