Skip to content

Commit

Permalink
Improve single-file mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
lapo-luchini committed May 7, 2024
1 parent 8e1cfed commit ea72cc2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ document.body.innerText = ASN1.decode(Hex.decode('06032B6570')).content();
Local usage
--------------------

Since unfortunately ESM modules are not working on `file:` protocol due to [CORS issues](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#other_differences_between_modules_and_standard_scripts), there is a bundled [single-file version working locally](https://asn1js.eu/index-local.html). It doesn't work online (due to [CSP](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) restrictions about inline content) but can be saved locally and opened in a browser. (known bug: dark mode is currently broken in this mode)
Since unfortunately ESM modules are not working on `file:` protocol due to [CORS issues](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#other_differences_between_modules_and_standard_scripts), there is a bundled [single-file version working locally](https://asn1js.eu/index-local.html). It doesn't work online (due to [CSP](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) restrictions about inline content) but can be saved locally and opened in a browser. ([known bug](https://github.com/lapo-luchini/asn1js/issues/89): dark mode is currently broken in this mode)

ISC license
-----------
Expand Down
16 changes: 12 additions & 4 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,26 @@ import { DomUtils } from 'htmlparser2';

const removeNodes = [ 'theme-select', 'rowExamples' ];

const preventSVGEmit = () => {
return {
generateBundle(opts, bundle) {
for (const key in bundle)
if (key.endsWith('.svg'))
delete bundle[key];
},
};
};

export default defineConfig({
plugins: [
preventSVGEmit(),
pluginDom({
applyOnMode: true, // all modes
handler: node => {
if (removeNodes.includes(node.attribs.id))
DomUtils.removeElement(node);
else if (node.name == 'link' && node.attribs.rel == 'icon') {
//node.attribs.href = 'data:image/svg+xml,' + encodeURI(fs.readFileSync('favicon.svg', 'ascii').replace(/^([^<]+|<[^s]|<s[^v]|<sv[^g])+/, '').trim());
else if (node.name == 'link' && node.attribs.rel == 'icon')
node.attribs.href = 'data:image/svg+xml;base64,' + btoa(fs.readFileSync('favicon.svg', 'ascii').replace(/^([^<]+|<[^s]|<s[^v]|<sv[^g])+/, '').trim());
console.log(node.attribs.href);
}
},
}),
viteSingleFile(),
Expand Down

0 comments on commit ea72cc2

Please sign in to comment.