-
Notifications
You must be signed in to change notification settings - Fork 83
/
rollup.config.mjs
135 lines (126 loc) · 3.14 KB
/
rollup.config.mjs
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
import { defineConfig } from 'rollup'
import terser from "@rollup/plugin-terser";
import pkg from "./package.json" with { type: "json" };
import dts from "rollup-plugin-dts";
// import typescript from "@rollup/plugin-typescript";
import typescript from "rollup-plugin-typescript2";
import replace from "@rollup/plugin-replace";
import copy from "rollup-plugin-copy";
import { wasm } from "@rollup/plugin-wasm";
import fs from "fs";
const LIBRARY_NAME = "WasmerSDK"; // Change with your library's name
const EXTERNAL = [
"./pkg/"
]; // Indicate which modules should be treated as external
const GLOBALS = {}; // https://rollupjs.org/guide/en/#outputglobals
const entries = [
'src-js/index.ts',
'src-js/node.ts',
'src-js/unpkg.ts',
'src-js/wasm-inlined.ts',
'src-js/worker.js',
]
const banner = `/*!
* ${pkg.name}
* ${pkg.description}
*
* @version v${pkg.version}
* @author ${pkg.author}
* @homepage ${pkg.homepage}
* @repository ${pkg.repository.url}
* @license ${pkg.license}
*/`;
const makeConfig = (env = "development", plugins = []) => {
const config = {
input: entries,
external: EXTERNAL,
output: {
banner,
dir: 'dist',
format: 'esm',
entryFileNames: '[name].mjs',
},
plugins: [
typescript({
// rootDir: "./src-js",
}),
wasmPlugin(),
// wasm({
// maxFileSize: 100 * 1024 * 1024,
// }),
copy({
targets: [
{
src: ["pkg/wasmer_js_bg.wasm", "pkg/wasmer_js_bg.wasm.d.ts"],
dest: "dist",
},
],
}),
replace({
values: {
"globalThis.wasmUrl": `"https://unpkg.com/${pkg.name}@${pkg.version}/dist/wasmer_js_bg.wasm"`,
"globalThis.workerUrl": `"https://unpkg.com/${pkg.name}@${pkg.version}/dist/index.mjs"`,
},
preventAssignment: true,
}),
...plugins,
],
external: EXTERNAL,
};
const tsConfig = {
input: entries,
output: {
dir: 'dist',
format: 'esm',
entryFileNames: f => `${f.name.replace(/src-js[\\/]/, '')}.d.mts`,
},
plugins: [
dts({
respectExternal: true,
})
],
external: EXTERNAL,
};
return [config, tsConfig];
};
export default commandLineArgs => {
let env =
commandLineArgs.environment === "BUILD:production" ? "production" : null;
let plugins = [];
if (env === "production") {
plugins.push(
terser({
output: {
comments: /^!/,
},
}),
);
}
const configs = makeConfig(env, plugins);
return configs;
};
/**
* @returns {import('rollup').Plugin} Plugin
*/
export function wasmPlugin() {
return {
name: 'wasm',
async load(id) {
if (!id.endsWith('.wasm'))
return
const binary = await fs.readFileSync(id)
const base64 = binary.toString('base64')
return `
var isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null;
const src = ${JSON.stringify(base64)};
if (isNode) {
buf = Buffer.from(src, 'base64');
}
else {
buf = Uint8Array.from(atob(src), c => c.charCodeAt(0));
}
export default buf;
`
},
}
}