-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
117 lines (106 loc) · 2.4 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
import dts from "rollup-plugin-dts";
import esbuild from "rollup-plugin-esbuild";
import json from "@rollup/plugin-json";
import alias from "@rollup/plugin-alias";
import postcss from "rollup-plugin-postcss";
import postcssUrl from "postcss-url";
import autoprefixer from "autoprefixer";
import packageConfig from "./package.json";
import { resolve, join } from "path";
import { readFileSync } from "fs";
import { outputFile } from "fs-extra";
const projectRootDir = resolve(__dirname);
const typesFiles = packageConfig.types;
const input = "src/main.ts";
/**
* With this rollup config we get three outputs to this package, an es bundle that
* bring tree shaking to the lib, a cjs bundle and the library type definitions
*/
const createAlias = (alias, path) => ({
find: alias,
replacement: resolve(projectRootDir, path),
});
const commonPlugins = [
alias({
entries: [
createAlias("src", "src"),
createAlias("core", "src/core"),
createAlias("fonts", "src/fonts"),
createAlias("icons", "src/icons"),
createAlias("flavors", "src/flavors"),
],
}),
];
const esbuildPluginConfig = esbuild({
jsx: "transform", // default, or 'preserve'
jsxFactory: "React.createElement",
jsxFragment: "React.Fragment",
});
const commonOutputConfig = {
sourcemap: true,
esModule: true,
freeze: false,
};
const external = ["react", "react-dom"];
const cjsBundle = {
input,
output: [
{
file: packageConfig.main,
format: "cjs",
...commonOutputConfig,
},
],
plugins: [
postcss({
extract: "styles.css",
plugins: [
autoprefixer(),
postcssUrl({
url: (asset) => {
const oldPath = asset.relativePath;
const newPath = oldPath.replace("../", "").replace("./", "");
const oldFile = readFileSync(asset.absolutePath);
outputFile(join("dist", newPath), oldFile);
return `./${newPath}`;
},
}),
],
}),
json(),
esbuildPluginConfig,
...commonPlugins,
],
external,
};
const esBundle = {
input,
output: [
{
dir: "es",
format: "es",
preserveModules: true,
...commonOutputConfig,
},
],
plugins: [
postcss({ inject: false }),
json(),
esbuildPluginConfig,
...commonPlugins,
],
external,
};
const typedefsBundle = {
input,
output: [
{
dir: "types",
format: "es",
preserveModules: true,
},
],
plugins: [dts(), postcss({ inject: false }), json(), ...commonPlugins],
external,
};
export default [cjsBundle, esBundle, typedefsBundle];