-
Notifications
You must be signed in to change notification settings - Fork 6
/
rollup.config.mjs
97 lines (87 loc) · 2.64 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
import { defineConfig } from "rollup";
import copy from "rollup-plugin-copy";
import { extname } from "node:path";
let destination = "node_modules/dataclass";
export default defineConfig({
input: ["modules/Data.js", "modules/runtime.js"],
output: [
{
dir: destination,
format: "cjs",
entryFileNames: ({ name }) => (name === "Data" ? "dataclass.js" : "[name].js"),
},
{
dir: destination,
format: "esm",
entryFileNames: ({ name }) => (name === "Data" ? "dataclass.module.js" : "[name].module.js"),
},
],
plugins: [
copy({
targets: [
{ src: ["typings/*", "LICENSE"], dest: destination },
{
src: "typings/*",
dest: destination,
rename: (name, extension) =>
name.endsWith(".d") || name.endsWith(".js")
? `${name.slice(0, name.lastIndexOf("."))}.module${extname(name)}.${extension}`
: `${name}.module.${extension}`,
},
{ src: "README.md", dest: destination, transform: generateReadme },
{ src: "package.json", dest: destination, transform: generatePkg },
],
}),
],
});
function generatePkg(contents) {
let pkg = JSON.parse(contents.toString());
return JSON.stringify(
{
name: pkg.name,
version: pkg.version,
description: pkg.description,
author: pkg.author,
license: pkg.license,
homepage: pkg.homepage,
repository: pkg.repository,
main: pkg.main,
module: pkg.module,
exports: {
".": "./dataclass.module.js",
"./runtime": "./runtime.module.js",
},
types: pkg.types,
sideEffects: pkg.sideEffects,
files: pkg.files,
keywords: pkg.keywords,
},
null,
2,
);
}
function generateReadme() {
return `
The library brings flexibility and usefulness of data classes from Kotlin, Scala, or Python to TypeScript and JavaScript.
Read full docs [on the homepage](https://dataclass.js.org).
\`\`\`javascript
import { Data } from "dataclass";
// 1. easily describe your data classes using just language features
class User extends Data {
name: string = "Anon";
age: number = 0;
}
// 2. instantiate classes while type systems ensure correctness
let user = User.create({ name: "Liza", age: 23 });
// > User { name: "Liza", age: 23 }
// 3. make changes while benefiting from immutable values
let updated = user.copy({ name: "Ann" });
// > User { name: "Ann", age: 23 }
updated = updated.copy({ name: "Liza" });
// > User { name: "Liza", age: 23 }
// 4. compare objects by their value, not reference
console.log(user === updated, user.equals(updated));
// > false, true
\`\`\`
`.trimStart();
}