diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index e40617f310..6663a4988e 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -34,10 +34,8 @@ jobs: run: yarn install env: CI: true - - name: Build icons - run: ./node_modules/.bin/gulp gen-icons-json - - name: Build translations - run: ./node_modules/.bin/gulp build-translations + - name: Build resources + run: ./node_modules/.bin/gulp gen-icons-json build-translations gather-gallery-demos - name: Run eslint run: ./node_modules/.bin/eslint '{**/src,src}/**/*.{js,ts,html}' --ignore-path .gitignore - name: Run tsc diff --git a/.gitignore b/.gitignore index c3c6849cdb..0fcd9b68bf 100644 --- a/.gitignore +++ b/.gitignore @@ -24,7 +24,7 @@ dist .vscode/* !.vscode/extensions.json -# Cast dev settings +# Cast dev settings src/cast/dev_const.ts # Secrets diff --git a/build-scripts/gulp/gallery.js b/build-scripts/gulp/gallery.js index ec430118e5..194a6e532d 100644 --- a/build-scripts/gulp/gallery.js +++ b/build-scripts/gulp/gallery.js @@ -1,7 +1,10 @@ // Run demo develop mode const gulp = require("gulp"); +const fs = require("fs"); +const path = require("path"); const env = require("../env"); +const paths = require("../paths"); require("./clean.js"); require("./translations.js"); @@ -12,6 +15,31 @@ require("./service-worker.js"); require("./entry-html.js"); require("./rollup.js"); +gulp.task("gather-gallery-demos", async function gatherDemos() { + const files = await fs.promises.readdir( + path.resolve(paths.gallery_dir, "src/demos") + ); + + let content = "export const DEMOS = {\n"; + + for (const file of files) { + const demoId = path.basename(file, ".ts"); + const demoPath = "../src/demos/" + demoId; + content += ` "${demoId}": () => import("${demoPath}"),\n`; + } + + content += "};"; + + const galleryBuild = path.resolve(paths.gallery_dir, "build"); + + fs.mkdirSync(galleryBuild, { recursive: true }); + fs.writeFileSync( + path.resolve(galleryBuild, "import-demos.ts"), + content, + "utf-8" + ); +}); + gulp.task( "develop-gallery", gulp.series( @@ -20,7 +48,11 @@ gulp.task( }, "clean-gallery", "translations-enable-merge-backend", - gulp.parallel("gen-icons-json", "build-translations"), + gulp.parallel( + "gen-icons-json", + "build-translations", + "gather-gallery-demos" + ), "copy-static-gallery", "gen-index-gallery-dev", env.useRollup() ? "rollup-dev-server-gallery" : "webpack-dev-server-gallery" @@ -35,7 +67,11 @@ gulp.task( }, "clean-gallery", "translations-enable-merge-backend", - gulp.parallel("gen-icons-json", "build-translations"), + gulp.parallel( + "gen-icons-json", + "build-translations", + "gather-gallery-demos" + ), "copy-static-gallery", env.useRollup() ? "rollup-prod-gallery" : "webpack-prod-gallery", "gen-index-gallery-prod" diff --git a/gallery/src/ha-gallery.js b/gallery/src/ha-gallery.js index 3352048e38..dfb07486d8 100644 --- a/gallery/src/ha-gallery.js +++ b/gallery/src/ha-gallery.js @@ -11,9 +11,7 @@ import { PolymerElement } from "@polymer/polymer/polymer-element"; import "../../src/components/ha-card"; import "../../src/managers/notification-manager"; import "../../src/styles/polymer-ha-style"; - -// eslint-disable-next-line no-undef -const DEMOS = require.context("./demos", true, /^(.*\.(ts$))[^.]*$/im); +import { DEMOS } from "../build/import-demos"; const fixPath = (path) => path.substr(2, path.length - 5); @@ -163,7 +161,7 @@ class HaGallery extends PolymerElement { }, _demos: { type: Array, - value: DEMOS.keys().map(fixPath), + value: Object.keys(DEMOS), }, _lovelaceDemos: { type: Array, @@ -210,7 +208,7 @@ class HaGallery extends PolymerElement { while (root.lastChild) root.removeChild(root.lastChild); if (demo) { - DEMOS(`./${demo}.ts`); + DEMOS[demo](); const el = document.createElement(demo); root.appendChild(el); }