-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrollup.config.js
53 lines (48 loc) · 2.25 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
import { rollupPluginHTML } from '@web/rollup-plugin-html';
import { terser } from 'rollup-plugin-terser';
import { importMetaAssets } from '@web/rollup-plugin-import-meta-assets';
import { readFileSync, rmdirSync, existsSync } from 'fs';
import { nodeResolve } from '@rollup/plugin-node-resolve';
const outdir = '../dist';
if (existsSync(outdir)) {
rmdirSync(outdir, { recursive: true });
}
export default {
input: './index.html',
output: { dir: outdir, entryFileNames: "app.[hash].js" },
plugins: [nodeResolve(), (() => ({
transform(code, id) {
let urlMatch;
do {
urlMatch = code.match(/(new URL\([^),]+\,\s*import.meta.url\s*\))/);
if (urlMatch) {
const urlWithAbsolutePath = urlMatch[1].replace('import.meta.url', `'file://${id}'`);
const func = new Function('return ' + urlWithAbsolutePath);
const resolvedUrl = func();
const pathname = resolvedUrl.pathname;
if (pathname.endsWith('.html')) {
const datauri = `data:text/html;base64,${readFileSync(pathname).toString('base64')}`;
code = code.replace(urlMatch[0], `new URL('${datauri}')`);
} else if (pathname.endsWith('.css')) {
const datauri = `data:text/css;base64,${readFileSync(pathname).toString('base64')}`;
code = code.replace(urlMatch[0], `new URL('${datauri}')`);
} else if (pathname.endsWith('.js')) {
const datauri = `data:text/javascript;base64,${readFileSync(pathname).toString('base64')}`;
code = code.replace(urlMatch[0], `new URL('${datauri}')`);
} else {
console.log('skipping', urlMatch[1]);
urlMatch = null;
}
}
} while (urlMatch);
return {
code: code
}
}
}))(), importMetaAssets(), rollupPluginHTML({
include: '**/*.html', minify: true,
transformHtml: (html) => {
return html.replace(/<script type=\"importmap\">[^<]+<\/script>/g, "");
}
}), terser()],
};