From 9a84df157eb81f72584926d35bbae010f71b85dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnaud=20Barr=C3=A9?= Date: Tue, 27 Feb 2024 23:41:20 +0100 Subject: [PATCH] Fix build watch mode [publish] --- CHANGELOG.md | 4 ++++ package.json | 2 +- src/esbuildPlugin.ts | 8 +++----- src/utils/intervalCheck.ts | 14 +++++++++----- src/vitePlugin.ts | 11 +++++++---- 5 files changed, 24 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 40f59b6..f6ca4d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +## 0.7.4 + +- Actually fix plugin usage in build watch mode + ## 0.7.3 - Align with Tailwind 3.4: https://tailwindcss.com/blog/tailwindcss-v3-4 diff --git a/package.json b/package.json index d901d48..8467788 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "description": "A bundler-first & PostCSS-independent implementation of Tailwind", "private": true, "type": "module", - "version": "0.7.3", + "version": "0.7.4", "author": "Arnaud Barré (https://github.com/ArnaudBarre)", "license": "MIT", "scripts": { diff --git a/src/esbuildPlugin.ts b/src/esbuildPlugin.ts index f198d5f..4141f3f 100644 --- a/src/esbuildPlugin.ts +++ b/src/esbuildPlugin.ts @@ -18,10 +18,7 @@ const esbuildPlugin: typeof declaration = ({ let hasBase = false; let hasUtils = false; - const utilsIntervalCheck = intervalCheck( - intervalCheckMs, - downwind.generate, - ); + let utilsIntervalCheck: ReturnType>; // Virtual entries build.onResolve({ filter: /^virtual:@downwind\// }, (args) => ({ @@ -53,7 +50,7 @@ const esbuildPlugin: typeof declaration = ({ build.onStart(() => { hasBase = false; hasUtils = false; - utilsIntervalCheck.reset(); + utilsIntervalCheck = intervalCheck(intervalCheckMs, downwind.generate); }); build.onLoad({ filter: /\.css$/ }, ({ path }) => { utilsIntervalCheck.taskRunning(); @@ -83,6 +80,7 @@ const esbuildPlugin: typeof declaration = ({ } was not found in the bundle. Downwind can't work without both virtual:@downwind/base.css and virtual:@downwind/utils.css.`, ); } + utilsIntervalCheck.clean(); }); }, }); diff --git a/src/utils/intervalCheck.ts b/src/utils/intervalCheck.ts index 26abbe1..0d0f266 100644 --- a/src/utils/intervalCheck.ts +++ b/src/utils/intervalCheck.ts @@ -2,12 +2,12 @@ export const intervalCheck = (ms: number, getValue: () => T) => { let utilsResolved = false; let scanHappenedOnce = false; let scanHappened = false; + + const timoutId = setTimeout(() => { + if (!scanHappenedOnce) throw new Error("No file to scan"); + }, 5_000); + return { - reset: () => { - utilsResolved = false; - scanHappenedOnce = false; - scanHappened = false; - }, promise: new Promise((resolve) => { const intervalId = setInterval(() => { if (!scanHappenedOnce) return; @@ -25,5 +25,9 @@ export const intervalCheck = (ms: number, getValue: () => T) => { scanHappenedOnce = true; scanHappened = true; }, + clean: () => { + if (!utilsResolved) throw new Error("Build ended without utils"); + clearTimeout(timoutId); + }, }; }; diff --git a/src/vitePlugin.ts b/src/vitePlugin.ts index 2f07e74..672caf1 100644 --- a/src/vitePlugin.ts +++ b/src/vitePlugin.ts @@ -73,9 +73,7 @@ const vitePlugin: typeof declaration = ({ }; // Build - const utilsIntervalCheck = intervalCheck(buildIntervalCheckMs, () => - downwind.generate(), - ); + let utilsIntervalCheck: ReturnType>; return [ { @@ -169,7 +167,9 @@ const vitePlugin: typeof declaration = ({ buildStart() { hasBase = false; hasUtils = false; - utilsIntervalCheck.reset(); + utilsIntervalCheck = intervalCheck(buildIntervalCheckMs, () => + downwind.generate(), + ); }, load(id) { if (id === baseModuleId) return downwind.getBase(); @@ -177,7 +177,9 @@ const vitePlugin: typeof declaration = ({ if (id === devtoolsModuleId) return ""; }, transform(code, id) { + if (id === baseModuleId) return; if (id === utilsModuleId) return; + if (id === devtoolsModuleId) return; if (cssRE.test(id)) { utilsIntervalCheck.taskRunning(); return { @@ -203,6 +205,7 @@ const vitePlugin: typeof declaration = ({ } was not found in the bundle. Downwind can't work without both virtual:@downwind/base.css and virtual:@downwind/utils.css.`, ); } + utilsIntervalCheck.clean(); }, }, ];