Skip to content

Commit

Permalink
build: added explicit auto-injection of /src/main.js into index.html …
Browse files Browse the repository at this point in the history
…when building with vite
  • Loading branch information
ErikBjare committed May 6, 2024
1 parent 14043b2 commit 10ed9b3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
<link rel="icon" type="image/png" href="/logo.png">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<!-- Verify with https://csp-evaluator.withgoogle.com/ -->
<!-- TODO: fix CSP (should depend on prod/dev mode, as pre-vite )-->
<meta http-equiv="Content-Security-Policy" content="default-src 'self' <%= htmlWebpackPlugin.options.templateParameters.cspDefaultSrc %> https://api.github.com/repos/ActivityWatch/activitywatch/releases/latest; img-src 'self' data:; font-src 'self' data:; style-src 'self' 'unsafe-inline'; object-src 'none'; script-src 'self' 'unsafe-eval'">
</head>
<body>
<noscript>
<strong>We're sorry but the ActivityWatch Web UI doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
<!-- We can build the app with either Vite or Webpack, they inject in different ways -->
<!-- Vite will auto-inject here: VITE_AUTOINJECT -->
</body>
</html>
19 changes: 19 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,29 @@ export default defineConfig(({ mode }) => {
};
};

// Auto-injects /src/main.js into index.html on a new line after the one which has VITE_AUTOINJECT
const autoInject = () => {
return {
name: 'html-transform',
transformIndexHtml(html) {
const pattern = /<!--.*VITE_AUTOINJECT.*-->/;
// check if the pattern exists in the html, if not, throw error
if (!pattern.test(html)) {
throw new Error(`Could not find pattern ${pattern} in the html file`);
}
return html.replace(
pattern,
'<!-- Vite injected: --><script type="module" src="/src/main.js"></script>'
);
},
};
};

// Return the configuration
return {
plugins: [
setCsp(),
autoInject(),
vue(),
VitePWA({
devOptions: {
Expand Down

1 comment on commit 10ed9b3

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are screenshots of this commit:

Screenshots using aw-server v0.12.3b11 (click to expand)

Screenshots using aw-server-rust master (click to expand)

Screenshots using aw-server-rust v0.12.3b11 (click to expand)

Please sign in to comment.