-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
208 lines (178 loc) · 5.53 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import svelte from 'rollup-plugin-svelte';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import livereload from 'rollup-plugin-livereload';
import terser from 'terser'; // using the basic terser since we need to run this after processing the build files
import fs from 'fs-extra'; // used in connection with terser to update the fileVersion string in our build files
import 'dotenv/config'; // used to pull our app constants from the .env file
// cache files offline
import workbox from 'rollup-plugin-workbox-build'
// wpg - used to copy the bootstrap installed via NPM to our public folder
import copy from 'rollup-plugin-copy';
// used to replace instances of 'caching versions' in our build files
import replace from 'replace-in-file';
const production = !process.env.ROLLUP_WATCH;
const fileVersion = process.env.FILE_VERSION; //'v0.1.3323'; // change this when we want to update the file cache constant (__cVersion__)
export default {
input: 'src/main.js',
output: {
sourcemap: true,
format: 'iife',
name: 'app',
file: 'public/build/bundle.js'
},
plugins: [
// clear out the staging directory
updateStagingDir(),
runCodeVersionReplaceOnStaging(),
svelte({
// enable run-time checks when not in production
dev: !production,
// we'll extract any component CSS out into
// a separate file - better for performance
css: css => {
css.write('public/build/bundle.css');
}
}),
//throwOut(), // use if we want to stop the code at some point
// using the rollup-plugin-copy module to copy our bootstrap module code from the modules directory to our build directory
// as well as copy the service worker, manifest, idb handler, and images
copy({
targets: [{
src: 'staging/bs4.4.1.css',
dest: 'public/'
},
{
src: 'staging/sw.js',
dest: 'public/'
},
{
src: 'staging/index.html',
dest: 'public/'
},
{
src: 'staging/manifest.json',
dest: 'public/'
},
{
src: 'staging/images/*',
dest: 'public/images/'
},
{
src: 'staging/codeVersion.json',
dest: 'public/'
}
]
}),
// If you have external dependencies installed from
// npm, you'll most likely need these plugins. In
// some cases you'll need additional configuration -
// consult the documentation for details:
// https://github.com/rollup/plugins/tree/master/packages/commonjs
resolve({
browser: true,
dedupe: ['svelte']
}),
commonjs(),
// cache files
workbox({
mode: 'injectManifest',
options: {
swSrc: 'staging/sw.js',
swDest: 'public/sw.js',
globDirectory: 'public',
globPatterns: [
'**/*.{js,css,png,map}',
'./manifest.json',
'./images/**',
'./bs4.4.1.css',
'./index.html'
]
}
}),
// In dev mode, call `npm run start` once
// the bundle has been generated
!production && serve(),
// Watch the `public` directory and refresh the
// browser on changes when not in production
!production && livereload('public'),
// If we're building for production (npm run build
// instead of npm run dev), minify
production && serve(true)
],
watch: {
clearScreen: false
}
};
// we need to clear and update the staging directory syncronously
function updateStagingDir() {
console.log('Clear staging directory');
try {
fs.emptyDirSync('staging');
console.log('Empty staging directory');
} catch (err) {
throw "err";
}
try {
fs.copySync('src', 'staging');
console.log('Copied src to staging');
} catch (err) {
throw "err";
}
console.log('Updated staging directory');
}
function runCodeVersionReplaceOnStaging() {
// this function simply replaces text in our staging files to help clear the file cache when the code is built
const replaceResults = replace.sync({
files: [
'staging/codeVersion.json',
'staging/sw.js',
'staging/App.svelte',
'staging/index.html',
'staging/main.js',
'staging/manifest.json'
],
from: /__cVersion__/g,
to: fileVersion,
});
// var code = fs.readFileSync("staging/index.html", "utf8");
// console.log(code);
console.log(replaceResults);
}
// function throwOut() {
// throw 'end this code run';
// }
// default to starting dev build (prodBuild=true for production build)
function serve(prodBuild=false){
let started = false;
return {
writeBundle() {
if (!started) {
started = true;
if (!prodBuild) {
require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
stdio: ['ignore', 'inherit', 'inherit'],
shell: true
});
}
// *************** DO NOT EDIT UNLESS YOU UNDERSTAND WHAT IT DOES ********************
// this function is important to replace constants in our build files to set application settings from the .env file
replace.sync({
files: [
'public/sw.js',
'public/manifest.json',
'public/build/bundle.js',
'public/build/bundle.js.map'
],
from: [/__cVersion__/g, /__ARRAY_DB_TABLES__/g, /__INDEXEDDB_NAME__/g, /__ARRAY_DB_CORE_TABLES__/g],
to: [fileVersion, process.env.ARRAY_DB_TABLES, process.env.INDEXEDDB_NAME, process.env.ARRAY_DB_CORE_TABLES],
});
// ***********************************************************************************
if (prodBuild) {
var code = fs.readFileSync("public/build/bundle.js", "utf8");
fs.writeFileSync("public/build/bundle.js", terser.minify(code).code, "utf8");
}
}
}
};
}