forked from gdg-x/hoverboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config-1729522513300.mjs
192 lines (188 loc) · 6.01 KB
/
rollup.config-1729522513300.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import json from '@rollup/plugin-json';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import terser from '@rollup/plugin-terser';
import typescript from '@rollup/plugin-typescript';
import { rollupPluginHTML } from '@web/rollup-plugin-html';
import fs from 'fs';
import copy from 'rollup-plugin-copy';
import livereload from 'rollup-plugin-livereload';
import { generateSW } from 'rollup-plugin-workbox';
import n from 'nunjucks';
/* eslint-env node */
const { BUILD_ENV, NODE_ENV, ROLLUP_WATCH } = process.env;
const production = NODE_ENV === 'production';
const watch = !!ROLLUP_WATCH;
const buildTarget = BUILD_ENV ? BUILD_ENV : production ? 'production' : 'development';
const webVitalsPolyfill = fs.readFileSync('./node_modules/web-vitals/dist/polyfill.js').toString();
const getConfigPath = () => {
const path = `./config/${buildTarget}.json`;
if (!fs.existsSync(path)) {
throw new Error(`
ERROR: Config path '${path}' does not exists.
Please, use production|development.json files or add a configuration file at '${path}'.
`);
}
console.log(`File path ${path} selected as config...`);
return path;
};
const getData = () => {
const settingsFiles = [
'./public/data/resources.json',
'./public/data/settings.json',
getConfigPath(),
];
const combineSettings = (currentData, path) => {
const settingsData = JSON.parse(fs.readFileSync(path).toString());
return {
...currentData,
...settingsData,
};
};
return settingsFiles.reduce(combineSettings, {
NODE_ENV: NODE_ENV || 'production',
webVitalsPolyfill,
});
};
const cleanupData = (data) => {
if (!data.image.startsWith('http')) {
data.image = `${data.url}${data.image}`;
}
return data;
};
const data = cleanupData(getData());
const nunjucks = n.configure({ throwOnUndefined: true });
const compileTemplate = (template) => nunjucks.renderString(template, data);
const compileBufferTemplate = (body) => compileTemplate(body.toString());
/* eslint-env node */
const ONE_WEEK = 60 * 60 * 24 * 7;
// Firebase Reserved URLs https://firebase.google.com/docs/hosting/reserved-urls
const FIREBASE_RESERVED_URLS = /^\/__\/.*/;
const FIREBASE_COFIG_URL = '/__/firebase/init.json';
const STATIC_EXPIRATION = {
maxAgeSeconds: ONE_WEEK,
maxEntries: 200,
};
const workboxConfig = {
mode: 'debug', // TODO: Remove mode
swDest: 'dist/service-worker.js',
navigateFallback: '/index.html',
navigateFallbackDenylist: [FIREBASE_RESERVED_URLS],
skipWaiting: true,
clientsClaim: true,
offlineGoogleAnalytics: true,
globDirectory: 'dist',
globPatterns: ['**/*.{html,js,css,json,svg,md}'],
runtimeCaching: [
{
urlPattern: /\/images\/.*/,
handler: 'NetworkFirst',
options: {
cacheName: 'images-cache',
expiration: STATIC_EXPIRATION,
},
},
{
urlPattern: /https:\/\/maps\.googleapis\.com\/maps.*/,
handler: 'NetworkFirst',
options: {
cacheName: 'google-maps-cache',
expiration: STATIC_EXPIRATION,
},
},
{
urlPattern: FIREBASE_COFIG_URL,
handler: 'NetworkFirst',
options: {
cacheName: 'firebase-cache',
expiration: {
maxAgeSeconds: ONE_WEEK,
maxEntries: 10,
},
},
},
{
urlPattern: /https:\/\/firebasestorage\.googleapis\.com\/.*/,
handler: 'NetworkFirst',
options: {
cacheName: 'firebase-storage-cache',
expiration: STATIC_EXPIRATION,
},
},
{
urlPattern: /https:\/\/storage\.googleapis\.com\/.*/,
handler: 'NetworkFirst',
options: {
cacheName: 'google-storage-cache',
cacheableResponse: {
statuses: [0, 200],
},
expiration: STATIC_EXPIRATION,
},
},
],
};
/* eslint-env node */
const config = [
{
input: 'src/firebase-messaging-sw.ts',
treeshake: production,
output: {
file: 'dist/firebase-messaging-sw.js',
sourcemap: production,
},
plugins: [
nodeResolve(),
typescript({
noEmitOnError: true,
sourceMap: production,
}),
production && terser(),
],
},
{
treeshake: production,
output: {
dir: 'dist',
entryFileNames: production ? '[name]-[hash].js' : '[name].js',
chunkFileNames: production ? '[name]-[hash].js' : '[name].js',
sourcemap: production,
},
plugins: [
nodeResolve(),
json(),
typescript({
noEmitOnError: true,
sourceMap: production,
}),
rollupPluginHTML({
input: {
html: compileBufferTemplate(fs.readFileSync('index.html')),
},
extractAssets: false,
minify: production,
}),
copy({
targets: [
{
src: 'public/*',
dest: 'dist',
},
{
src: 'public/manifest.json',
dest: 'dist',
transform: compileBufferTemplate,
},
{
src: 'public/data/*.md',
dest: 'dist/data',
transform: compileBufferTemplate,
},
],
}),
production && generateSW(workboxConfig),
production && terser(),
watch && livereload({ watch: 'dist' }),
],
},
];
export { config as default };