-
Notifications
You must be signed in to change notification settings - Fork 1
/
rollup.config.js
137 lines (130 loc) · 3.38 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import resolve from '@rollup/plugin-node-resolve';
import babel from 'rollup-plugin-babel';
import { terser } from 'rollup-plugin-terser';
import replace from '@rollup/plugin-replace';
import url from '@rollup/plugin-url';
import hotcss from 'rollup-plugin-hot-css';
import commonjs from 'rollup-plugin-commonjs-alternate';
import refresh from 'rollup-plugin-react-refresh';
import copy from 'rollup-plugin-copy';
const appName = 'rollupReactApp';
const NODE_ENV = process.env.NODE_ENV || 'development';
const production = NODE_ENV !== 'development' && NODE_ENV !== 'test';
const development = NODE_ENV === 'development';
const outputFile = production ? '/static/js/index' : '/index.[hash]';
const publicUrl = process.env.PUBLIC_URL || 'http://localhost:9000';
const esmFile = `${outputFile}.js`;
const iifeFile = `${outputFile}.legacy.js`;
const styles = development ? '/styles.[hash].css' : 'static/assets/styles.css';
const genScripts = () => {
let scripts = `<script async type="module" src="${esmFile}"></script>`;
if (production) {
scripts += `<script nomodule src="${iifeFile}"></script>`;
}
return scripts;
};
const watch = () => ({
exclude: [
'build/favicon.ico',
'build/logo192.png',
'build/logo512.png',
'build/manifest.json',
'build/robots.txt',
'build/index.html',
],
});
const plugins = babelConf => [
copy({
targets: [
{
src: [
'public/favicon.ico',
'public/logo192.png',
'public/logo512.png',
'public/manifest.json',
'public/robots.text',
],
dest: 'build',
},
{
src: 'public/index.html',
dest: 'build',
transform: contents =>
contents
.toString()
.replace('%SCRIPTS%', genScripts())
.replace(/%PUBLIC_URL%/g, publicUrl)
.replace('%STYLES%', styles),
},
],
}),
replace({
'process.env.NODE_ENV': JSON.stringify(NODE_ENV),
}),
url(),
hotcss({
hot: development,
filename: development ? 'styles.css' : 'static/assets/styles.css',
}),
babel(babelConf),
resolve({ extensions: ['.mjs', '.js', '.jsx', '.json'] }),
commonjs({ extensions: ['.js', '.jsx'] }),
production && terser(),
development && refresh(),
];
const esm = {
input: 'src/index.js',
output: {
dir: 'build',
format: 'esm',
entryFileNames: development ? '[name].[hash].js' : 'static/js/[name].js',
assetFileNames: development ? '[name].[hash][extname]' : '[name][extname]',
sourcemap: true,
},
watch: watch(),
plugins: plugins({
exclude: 'node_modules/**',
presets: [
[
'@babel/preset-env',
{
targets: {
esmodules: true,
},
},
],
'@babel/preset-react',
],
plugins: development ? ['react-refresh/babel'] : [],
}),
};
const iife = {
input: 'src/index.js',
output: {
dir: 'build',
format: 'iife',
entryFileNames: 'static/js/[name].legacy.js',
assetFileNames: development ? '[name][hash][extname]' : '[name][extname]',
name: appName,
sourcemap: true,
},
watch: watch(),
plugins: plugins({
presets: [
[
'@babel/preset-env',
{
targets: {
browsers: ['> 0.5%'],
},
},
],
'@babel/preset-react',
],
}),
};
const config = [esm];
if (production) {
config.push(iife);
}
export default config;