Skip to content

Commit

Permalink
Fix build watch mode [publish]
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre committed Feb 27, 2024
1 parent 1d47b6a commit 9a84df1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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": {
Expand Down
8 changes: 3 additions & 5 deletions src/esbuildPlugin.ts
Expand Up @@ -18,10 +18,7 @@ const esbuildPlugin: typeof declaration = ({
let hasBase = false;
let hasUtils = false;

const utilsIntervalCheck = intervalCheck(
intervalCheckMs,
downwind.generate,
);
let utilsIntervalCheck: ReturnType<typeof intervalCheck<string>>;

// Virtual entries
build.onResolve({ filter: /^virtual:@downwind\// }, (args) => ({
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
});
},
});
14 changes: 9 additions & 5 deletions src/utils/intervalCheck.ts
Expand Up @@ -2,12 +2,12 @@ export const intervalCheck = <T>(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<T>((resolve) => {
const intervalId = setInterval(() => {
if (!scanHappenedOnce) return;
Expand All @@ -25,5 +25,9 @@ export const intervalCheck = <T>(ms: number, getValue: () => T) => {
scanHappenedOnce = true;
scanHappened = true;
},
clean: () => {
if (!utilsResolved) throw new Error("Build ended without utils");
clearTimeout(timoutId);
},
};
};
11 changes: 7 additions & 4 deletions src/vitePlugin.ts
Expand Up @@ -73,9 +73,7 @@ const vitePlugin: typeof declaration = ({
};

// Build
const utilsIntervalCheck = intervalCheck(buildIntervalCheckMs, () =>
downwind.generate(),
);
let utilsIntervalCheck: ReturnType<typeof intervalCheck<string>>;

return [
{
Expand Down Expand Up @@ -169,15 +167,19 @@ const vitePlugin: typeof declaration = ({
buildStart() {
hasBase = false;
hasUtils = false;
utilsIntervalCheck.reset();
utilsIntervalCheck = intervalCheck(buildIntervalCheckMs, () =>
downwind.generate(),
);
},
load(id) {
if (id === baseModuleId) return downwind.getBase();
if (id === utilsModuleId) return utilsIntervalCheck.promise;
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 {
Expand All @@ -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();
},
},
];
Expand Down

0 comments on commit 9a84df1

Please sign in to comment.