From 455d1bbb92133a42ede0eeb02debcd6977d9617e Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 17 Jun 2021 11:06:16 -0700 Subject: [PATCH] Hash entrypoint to avoid caching --- build-scripts/rollup/manifest-plugin.js | 22 +++++++++++++++ raw_package/__init__.py | 5 ++++ rollup.config.js | 36 ++++++++++++++++--------- 3 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 build-scripts/rollup/manifest-plugin.js diff --git a/build-scripts/rollup/manifest-plugin.js b/build-scripts/rollup/manifest-plugin.js new file mode 100644 index 00000000..e5b55566 --- /dev/null +++ b/build-scripts/rollup/manifest-plugin.js @@ -0,0 +1,22 @@ +export default function (userOptions = {}) { + return { + name: "manifest", + generateBundle(outputOptions, bundle) { + const manifest = {}; + + for (const chunk of Object.values(bundle)) { + if (!chunk.isEntry) { + continue; + } + manifest[chunk.name] = chunk.fileName; + } + + this.emitFile({ + type: "asset", + source: JSON.stringify(manifest, undefined, 2), + name: "manifest.json", + fileName: "manifest.json", + }); + }, + }; +} diff --git a/raw_package/__init__.py b/raw_package/__init__.py index 0da9d71b..307c755c 100644 --- a/raw_package/__init__.py +++ b/raw_package/__init__.py @@ -1,7 +1,12 @@ """ESPHome dashboard.""" from pathlib import Path +import json def where(): """Return path to the frontend.""" return Path(__file__).parent + +def entrypoint(): + manifest = json.loads((where() / "static/js/esphome/manifest.json").read_text()) + return manifest['index'] diff --git a/rollup.config.js b/rollup.config.js index caf41402..70239f85 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -2,27 +2,37 @@ import { nodeResolve } from "@rollup/plugin-node-resolve"; import { terser } from "rollup-plugin-terser"; import json from "@rollup/plugin-json"; import typescript from "@rollup/plugin-typescript"; +import manifest from "./build-scripts/rollup/manifest-plugin"; +const isProdBuild = process.env.NODE_ENV === "production"; + +/** + * @type { import("rollup").MergedRollupOptions } + */ const config = { input: "src/index.ts", output: { dir: "esphome_dashboard/static/js/esphome/", format: "module", + entryFileNames: isProdBuild ? "[name]-[hash].js" : "[name].js", + chunkFileNames: isProdBuild ? "c.[hash].js" : "[name].js", + assetFileNames: isProdBuild ? "a.[hash].js" : "[name].js", }, preserveEntrySignatures: false, - plugins: [typescript(), nodeResolve(), json()], + plugins: [ + typescript(), + nodeResolve(), + json(), + manifest(), + isProdBuild && + terser({ + ecma: 2019, + toplevel: true, + output: { + comments: false, + }, + }), + ].filter(Boolean), }; -if (process.env.NODE_ENV === "production") { - config.plugins.push( - terser({ - ecma: 2019, - toplevel: true, - output: { - comments: false, - }, - }) - ); -} - export default config;