forked from nymea/nymea-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
310 lines (270 loc) · 9.72 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
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 'rollup-plugin-terser';
import copy from 'rollup-plugin-copy';
import del from 'del';
import * as path from 'path';
import { mdsvex } from 'mdsvex';
import autoPreprocess from 'svelte-preprocess';
import ts from '@rollup/plugin-typescript';
import typescript from 'typescript'
import hljs from 'highlight.js/lib/core';
import asciidoc from 'highlight.js/lib/languages/asciidoc';
import bash from 'highlight.js/lib/languages/bash';
import cLike from 'highlight.js/lib/languages/c-like';
import javascript from 'highlight.js/lib/languages/javascript';
import json from 'highlight.js/lib/languages/json';
import python from 'highlight.js/lib/languages/python';
import qml from 'highlight.js/lib/languages/qml';
import xml from 'highlight.js/lib/languages/xml';
import frontmatterParser from '@github-docs/frontmatter';
import pluginJson from '@rollup/plugin-json';
import image from 'svelte-image';
const staticDir = 'static'
const distDir = 'dist'
const buildDir = `${distDir}/build`
const production = !process.env.ROLLUP_WATCH;
const bundling = process.env.BUNDLING || production ? 'dynamic' : 'bundle';
const shouldPrerender = (typeof process.env.PRERENDER !== 'undefined') ? process.env.PRERENDER : !!production;
del.sync(distDir + '/**');
const escape_curlies = (str) => str.replace(/[{}]/g, c => ({ '{': '{', '}': '}' }[c]));
function createConfig({ output, inlineDynamicImports, plugins = [] }) {
hljs.debugMode();
hljs.registerLanguage('asciidoc', asciidoc);
hljs.registerLanguage('bash', bash);
hljs.registerLanguage('cLike', cLike);
hljs.registerLanguage('javascript', javascript);
hljs.registerLanguage('json', json);
hljs.registerLanguage('python', python);
hljs.registerLanguage('qml', qml);
hljs.registerLanguage('xml', xml);
const transform = inlineDynamicImports ? bundledTransform : dynamicTransform;
return {
inlineDynamicImports,
input: `src/main.ts`,
output: {
name: 'app',
sourcemap: true,
...output
},
plugins: [
copy({
targets: [
{ src: staticDir + '/**/!(__index.html)', dest: distDir },
{ src: `${staticDir}/__index.html`, dest: distDir, rename: '__app.html', transform },
{ src: '.htaccess', dest: distDir },
], copyOnce: true
}),
pluginJson(),
svelte({
extensions: ['.svelte', '.md'],
preprocess: [
autoPreprocess(),
mdsvex({
extension: '.md',
// frontmatter: {
// // marker: '-',
// parse(frontmatter, messages) {
// const schema = {
// properties: {
// id: {
// type: 'string',
// required: true,
// allowEmpty: false
// },
// title: {
// type: 'string',
// required: true,
// allowEmpty: false
// },
// type: {
// type: 'string',
// required: true,
// allowEmpty: false
// },
// beforeId: {
// type: 'string',
// required: true,
// allowEmpty: false
// }
// }
// }
// let markdown;
// if (frontmatter !== undefined) {
// markdown = `---
// ${frontmatter}
// ---`
// } else {
// markdown = `---
// ---`
// }
// try {
// // return toml.parse(frontmatter);
// const { data, errors } = frontmatterParser(markdown);
// console.log('data', data);
// if (data === undefined) {
// console.log('frontmatter', frontmatter);
// }
// return data ? data : false;
// } catch (e) {
// messages.push(
// "Parsing error on line " +
// e.line +
// ", column " +
// e.column +
// ": " +
// e.message
// );
// }
// },
// },
highlight: {
highlighter: (code, language) => {
const codeBlock = language
? hljs.highlight(language, code, true).value
: hljs.highlightAuto(code, ['asciidoc', 'bash', 'cLike', 'javascript', 'json', 'python', 'qml', 'xml']).value;
return `<pre class="code"><code class="language-${language} hljs">${escape_curlies(codeBlock)}</code></pre>`;
},
},
layout: path.join(__dirname, './src/pages/documentation/_markdown.svelte'),
}),
// image({
// optimizeAll: true, // optimize all images discovered in img tags
// // Case insensitive. Only files whose extension exist in this array will be
// // processed by the <img> tag (assuming `optimizeAll` above is true). Empty
// // the array to allow all extensions to be processed. However, only jpegs and
// // pngs are explicitly supported.
// imgTagExtensions: ['jpg', 'jpeg', 'png'],
// // Same as the above, except that this array applies to the Image Component.
// // If the images passed to your image component are unknown, it might be a
// // good idea to populate this array.
// componentExtensions: [],
// inlineBelow: 10000, // inline all images in img tags below 10kb
// compressionLevel: 8, // png quality level
// quality: 70, // jpeg/webp quality level
// tagName: "Image", // default component name
// sizes: [1200, 800, 1200], // array of sizes for srcset in pixels
// // array of screen size breakpoints at which sizes above will be applied
// breakpoints: [375, 768, 1024],
// outputDir: "g/",
// // should be ./static for Sapper and ./public for plain Svelte projects
// publicDir: "./static/",
// placeholder: "blur", // or "blur",
// // WebP options [sharp docs](https://sharp.pixelplumbing.com/en/stable/api-output/#webp)
// webpOptions: {
// quality: 75,
// lossless: false,
// force: true
// },
// webp: true,
// // Potrace options for SVG placeholder
// trace: {
// background: "#fff",
// color: "#002fa7",
// threshold: 120
// },
// // Wheter to download and optimize remote images loaded from a url
// optimizeRemote: true
// })
],
// enable run-time checks when not in production
dev: !production,
hydratable: true,
// we'll extract any component CSS out into
// a separate file — better for performance
css: css => {
css.write(`${buildDir}/bundle.css`);
}
}),
// copy({
// targets: [{ src: 'static/g', dest: 'dist' }],
// }),
ts({
typescript,
sourceMap: !production
}),
// 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/rollup-plugin-commonjs
resolve({
browser: true,
dedupe: importee => importee === 'svelte' || importee.startsWith('svelte/')
}),
commonjs(),
// If we're building for production (npm run build
// instead of npm run dev), minify
production && terser(),
...plugins
],
watch: {
clearScreen: false
}
}
}
const bundledConfig = {
inlineDynamicImports: true,
output: {
format: 'iife',
file: `${buildDir}/bundle.js`
},
plugins: [
!production && serve(),
!production && livereload(distDir)
]
}
const dynamicConfig = {
inlineDynamicImports: false,
output: {
format: 'esm',
dir: buildDir
},
plugins: [
!production && livereload(distDir),
]
}
const configs = [createConfig(bundledConfig)]
if (bundling === 'dynamic')
configs.push(createConfig(dynamicConfig))
if (shouldPrerender) [...configs].pop().plugins.push(prerender())
export default configs
function serve() {
let started = false;
return {
writeBundle() {
if (!started) {
started = true;
require('child_process').spawn('npm', ['run', 'serve'], {
stdio: ['ignore', 'inherit', 'inherit'],
shell: true
});
}
}
};
}
function prerender() {
return {
writeBundle() {
if (shouldPrerender) {
require('child_process').spawn('npm', ['run', 'export'], {
stdio: ['ignore', 'inherit', 'inherit'],
shell: true
});
}
}
}
}
function bundledTransform(contents) {
return contents.toString().replace('__SCRIPT__', `
<script defer src="/build/bundle.js" ></script>
`)
}
function dynamicTransform(contents) {
return contents.toString().replace('__SCRIPT__', `
<script type="module" defer src="https://unpkg.com/[email protected]/dist/index.mjs?module" data-main="/build/main.js"></script>
<script nomodule defer src="https://unpkg.com/dimport/nomodule" data-main="/build/main.js"></script>
`)
}