-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
77 lines (72 loc) · 2.48 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
// NOTE: This came from the repo, with only slight modifications: https://github.com/nickbabcock/jomini
// NOTE: Comments are the above author's but preserved for their usefulness.
import typescript from "@rollup/plugin-typescript";
import { wasm } from "@rollup/plugin-wasm";
import path from "path";
import fs from "fs";
const outdir = (fmt, env) => {
if (env == "node") {
return `node`;
} else {
return `${fmt}${env == "slim" ? "-slim" : ""}`;
}
};
const rolls = (fmt, env) => ({
input: env !== "slim" ? "src/index.ts" : "src/index_slim.ts",
output: {
dir: `dist`,
format: fmt,
entryFileNames:
outdir(fmt, env) + `/[name].` + (fmt === "cjs" ? "cjs" : "js"),
name: "wasm_crossword_generator",
},
plugins: [
// We want to inline our wasm bundle as base64. Not needing browser users
// to fetch an additional asset is a boon as there's less room for errors
env != "slim" &&
wasm(
env == "node"
? {
maxFileSize: 0,
targetEnv: "node",
publicPath: "../",
fileName: "[name][extname]",
}
: { targetEnv: "auto-inline" }
),
typescript({
target: fmt == "es" ? "ES2022" : "ES2017",
outDir: `dist/${outdir(fmt, env)}`,
rootDir: "src",
}),
{
name: "copy-pkg",
// wasm-bindgen outputs a import.meta.url when using the web target.
// rollup will either perserve the the statement when outputting an esm,
// which will cause webpack < 5 to choke or it will output a
// "require('url')", for other output types, causing more choking. Since
// we want a downstream developer to either not worry about providing wasm
// at all, or forcing them to deal with bundling, we resolve the import to
// an empty string. This will error at runtime.
resolveImportMeta: () => `""`,
generateBundle() {
// copy the typescript definitions that wasm-bindgen creates into the
// distribution so that downstream users can benefit from documentation
// on the rust code
fs.mkdirSync(`./dist/types/pkg`, { recursive: true });
fs.copyFileSync(
path.resolve("./src/pkg/wasm_crossword_generator.d.ts"),
path.resolve(`./dist/types/pkg/wasm_crossword_generator.d.ts`)
);
},
},
],
});
export default [
rolls("umd", "fat"),
rolls("es", "fat"),
rolls("cjs", "fat"),
rolls("cjs", "node"),
rolls("es", "slim"),
rolls("cjs", "slim"),
];