-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.ts
174 lines (163 loc) · 4.19 KB
/
rollup.config.ts
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
import babel from "rollup-plugin-babel";
import serve from "rollup-plugin-serve";
import livereload from "rollup-plugin-livereload";
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import builtins from "rollup-plugin-node-builtins";
import globals from "rollup-plugin-node-globals";
import replace from "@rollup/plugin-replace";
import React from "react";
import ReactDOM from "react-dom";
import fs from "fs";
import path from "path";
const findFilesInDir = (dir) => {
return fs
.readdirSync(dir)
.filter((el) => path.extname(el) === ".tsx")
.map((el) => path.basename(el, path.extname(el)));
};
const pages = findFilesInDir("src/pages");
if (!pages.length) {
throw new Error(`No sources found in pages`);
}
export default (commandLineArgs) => {
console.log({ commandLineArgs });
const isBundle = commandLineArgs.bundle;
const devPage = commandLineArgs.x;
// clear args
delete commandLineArgs.bundle;
delete commandLineArgs.x;
bootstrap({
isBundle,
devPage,
});
// Prod config
if (isBundle) {
return pages.map((page) => {
return {
input: `dist/pages/${page}.js`,
output: {
file: `dist/bundle/${page}.js`,
format: "iife",
sourcemap: true,
},
plugins: [
resolve({
preferBuiltins: true,
}),
babel({
exclude: "node_modules/**",
presets: ["@babel/preset-env", "@babel/preset-react"],
}),
commonjs({
namedExports: {
"react-dom": Object.keys(ReactDOM),
react: Object.keys(React),
},
}),
replace({
"process.env.NODE_ENV": JSON.stringify("production"),
preventAssignment: true,
}),
globals(),
builtins(),
!isBundle
? serve({
open: true,
verbose: true,
contentBase: ["", "public"],
host: "localhost",
port: "3000",
})
: null,
!isBundle ? livereload({ watch: "dist" }) : null,
],
};
});
}
// Dev config
// if nod dev page provided
if (devPage === true) {
throw new Error("Please provide a page to watch");
}
// if the page is not exist
if (!pages.includes(devPage)) {
throw new Error(
"This page seems to be not exist. Please provide a valid page to watch"
);
}
return {
input: `dist/pages/${devPage}.js`,
output: {
file: `dist/bundle/${devPage}.js`,
format: "iife",
sourcemap: true,
},
plugins: [
resolve({
preferBuiltins: true,
}),
babel({
exclude: "node_modules/**",
presets: ["@babel/preset-env", "@babel/preset-react"],
}),
commonjs({
namedExports: {
"react-dom": Object.keys(ReactDOM),
react: Object.keys(React),
},
}),
replace({
"process.env.NODE_ENV": JSON.stringify("production"),
preventAssignment: true,
}),
globals(),
builtins(),
serve({
open: true,
verbose: true,
contentBase: ["dist"],
host: "localhost",
port: "3000",
}),
livereload({ watch: "dist" }),
],
};
};
// Bootstrap index.html
const bootstrap = (props) => {
if (props.isBundle) {
fs.writeFileSync(
"dist/index.html",
`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>React - Rollup Test</title>
</head>
<body>
<noscript> You need to enable JavaScript to run this app. </noscript>
<div id="root"></div>
</body>
</html>`
);
} else {
fs.writeFileSync(
"dist/index.html",
`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>React - Rollup Test</title>
</head>
<body>
<noscript> You need to enable JavaScript to run this app. </noscript>
<div id="root"></div>
<script src="./bundle/${props.devPage}.js" ></script>
</body>
</html>`
);
}
};