-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
rollup.config.js
214 lines (212 loc) · 10.1 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
209
210
211
212
213
214
// rollup.config.js
import resolve from '@rollup/plugin-node-resolve';
import babel from '@rollup/plugin-babel';
import { transformAsync } from '@babel/core';
import commonjs from '@rollup/plugin-commonjs';
import replace from '@rollup/plugin-replace';
import copy from 'rollup-plugin-copy';
import terser from '@rollup/plugin-terser';
import { minify } from 'terser';
// import styles from "@ironkinoko/rollup-plugin-styles";
const config = {
// The entry point for the bundler
input: 'www/js/app.js',
output: {
format: 'iife',
name: 'KiwixJSBundle'
},
treeshake: 'recommended',
plugins: [
babel({
exclude: 'node_modules/**',
babelHelpers: 'bundled'
}),
// Needed to get rid of residual "requires" left in the code by Babel...
commonjs(),
// Resolves references to node_modules packages
resolve({
browser: true
}),
// styles({
// // mode: 'extract',
// modules: true
// }),
replace({
// Prevent a fatal error in IE11 (bug with the URL constructor polyfill)
'document.baseURI': "document.location.href.replace(/[^/]*$/, '')",
// Redirect the libzim Worker loader to the new location
'js/lib/libzim': 'js/libzim',
'js/lib/darkreader.min.js': 'js/darkreader.min.js',
preventAssignment: true
}),
copy({
targets: [{
src: ['www/js/lib/*dec-wasm.wasm', 'www/js/lib/libzim-asm.js', 'www/js/lib/libzim-wasm.*', 'www/js/lib/darkreader.min.js', 'www/js/lib/webpHeroBundle*',
'node_modules/jquery/dist/jquery.slim.min.*', '!www/js/lib/libzim-wasm.dev*'],
dest: 'dist/www/js'
},
{ src: ['archives', 'images', 'index.html', 'manifest.json', 'package.json', 'LICENSE', 'CHANGELOG.md', 'README.md', '*.appxmanifest', '*.pfx', '*.cjs', 'Package.StoreAssociation.xml'], dest: 'dist' }
],
flatten: true
})
]
};
if (process.env.BUILD === 'production') {
// Production (minified) build
config.plugins.push(terser());
config.plugins.push(
// Copy static files and binary (WASM/ASM) files that need to be loaded relative to the bundle
copy({
targets: [
{
src: ['www/**', '!www/js/app.js', '!www/js/lib', '!www/index.html'],
dest: 'dist/www',
expandDirectories: true,
onlyFiles: true
},
{
src: ['KiwixWebApp*.jsproj'],
dest: 'dist',
// Modify the jsproj included files
transform: (contents, filename) => contents.toString()
// Replace the entry point with the bundle
.replace(/(www[\\/]js[\\/])app.js/, '$1bundle.min.js')
// Remove all the lib files that will be included in the bundle
.replace(/(?:<Content Include=)?["']www[\\/]js[\\/]lib[\\/]cache[\s\S]+zimfile.js["'](?:\s*\/>|,)\s*/, '')
// Replace any references to node_modules
.replace(/node_modules[\\/].*dist[\\/]((?:js|css)[\\/])?/g, function (m, p1) {
p1 = p1 || 'js/';
return 'www/' + p1;
})
// Alter remaining lib references
.replace(/([\\/])js[\\/]lib/g, '$1js')
// Remove unneeded ASM/WASM binaries
.replace(/['"]www[\\/]js[\\/].*dec.*js['"],\s*/g, '')
},
{
src: ['service-worker.js'],
dest: 'dist',
// Modify the Service Worker precache files
transform: async (contents, filename) => {
const result = await minify(
contents.toString()
// Replace the entry point with the bundle
.replace(/(www[\\/]js[\\/])app.js/, '$1bundle.min.js')
// Remove all the lib files that will be included in the bundle
.replace(/(?:<Content Include=)?["']www[\\/]js[\\/]lib[\\/]cache[\s\S]+zimfile.js["'](?:\s*\/>|,)\s*/, '')
// Replace any references to node_modules
.replace(/node_modules[\\/].*dist[\\/]((?:js|css)[\\/])?/g, function (m, p1) {
p1 = p1 || 'js/';
return 'www/' + p1;
})
// Alter remaining lib references
.replace(/([\\/])js[\\/]lib/g, '$1js')
// Remove unneeded ASM/WASM binaries
.replace(/['"]www[\\/]js[\\/].*dec.*js['"],\s*/g, '')
);
return result.code;
}
},
{
src: ['replayWorker.js'],
dest: 'dist',
transform: async (contents, filename) => {
const code = contents.toString();
// Now minify the replayWorker
const minified = await minify(code);
// How to transform with babel (tested to make a difference with Firefox 68+)
const result = await transformAsync(minified.code, {
filename,
presets: [
[
'@babel/preset-env',
{
targets: {
edge: '18',
firefox: '60',
chrome: '67',
safari: '11.1'
},
modules: false,
spec: true
}
]
]
});
return result.code;
}
},
{
src: 'www/index.html',
dest: 'dist/www',
// Link the html to the new bundle entry point
transform: (contents, filename) => contents.toString()
// Uncomment the bundle link
.replace(/<!--\s(<script type="text\/javascript.*bundle.js.*)\s-->/, '$1')
.replace(/bundle\.js/, 'bundle.min.js')
// Comment out the old app.js link
.replace(/(<script type="module.*app.js.*)/, '<!-- $1 -->')
// Redirect jQuery and bootstrap
.replace(/(<script\s.*src=").*jquery.slim.min.js/, '$1js/jquery.slim.min.js')
// .replace(/(<script\s.*src=").*bootstrap.bundle.min.js/, '$1js/bootstrap.bundle.min.js')
// .replace(/(<link\s.*href=").*bootstrap.min.css/, '$1css/bootstrap.min.css')
}
],
flatten: false
})
)
} else {
// Normal (Uniminified) build
config.plugins.push(
// Copy static files and binary (WASM/ASM) files that need to be loaded relative to the bundle
copy({
targets: [
{
src: ['www/**', '!www/js/app.js', '!www/js/lib', '!www/index.html'],
dest: 'dist/www',
expandDirectories: true,
onlyFiles: true
},
{
src: ['service-worker.js', 'KiwixWebApp*.jsproj'],
dest: 'dist',
// Modify the Service Worker precache files
transform: (contents, filename) => contents.toString()
// Replace the entry point with the bundle
.replace(/(www[\\/]js[\\/])app.js/, '$1bundle.js')
// Remove all the lib files that will be included in the bundle
.replace(/(?:<Content Include=)?["']www[\\/]js[\\/]lib[\\/]cache[\s\S]+zimfile.js["'](?:\s*\/>|,)\s*/, '')
// Replace any references to node_modules
.replace(/node_modules[\\/].*dist[\\/]((?:js|css)[\\/])?/g, function (m, p1) {
p1 = p1 || 'js/';
return 'www/' + p1;
})
// Alter remaining lib references
.replace(/([\\/])js[\\/]lib/g, '$1js')
// Remove unneeded ASM/WASM binaries
.replace(/['"]www[\\/]js[\\/].*dec.*js['"],\s*/g, '')
},
{
src: ['replayWorker.js'],
dest: 'dist'
},
{
src: 'www/index.html',
dest: 'dist/www',
// Link the html to the new bundle entry point
transform: (contents, filename) => contents.toString()
// Uncomment the bundle link
.replace(/<!--\s(<script type="text\/javascript.*bundle.js.*)\s-->/, '$1')
// Comment out the old app.js link
.replace(/(<script type="module.*app.js.*)/, '<!-- $1 -->')
// Redirect jQuery and bootstrap
.replace(/(<script\s.*src=").*jquery.slim.min.js/, '$1js/jquery.slim.min.js')
// .replace(/(<script\s.*src=").*bootstrap.bundle.min.js/, '$1js/bootstrap.bundle.min.js')
// .replace(/(<link\s.*href=").*bootstrap.min.css/, '$1css/bootstrap.min.css')
}
],
flatten: false
})
)
}
export default config;