Skip to content

Commit

Permalink
chore(deps): vite v6.0.0-alpha.8 (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored May 1, 2024
1 parent 29dafe0 commit fddccc4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 60 deletions.
52 changes: 31 additions & 21 deletions examples/react-server/src/features/unocss/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,49 @@
import { debounce, objectHas, tinyassert } from "@hiogawa/utils";
import vitePluginUnocss, { type UnocssVitePluginAPI } from "@unocss/vite";
import { DevEnvironment, type Plugin } from "vite";
import { DevEnvironment, type Plugin, type PluginOption } from "vite";
import { invalidateModule } from "../style/plugin";
import { createVirtualPlugin } from "../utils/plugin";

// cf.
// https://github.com/unocss/unocss/tree/47eafba27619ed26579df60fe3fdeb6122b5093c/packages/vite/src/modes/global
// https://github.com/tailwindlabs/tailwindcss/blob/719c0d488378002ff752e8dc7199c843930bb296/packages/%40tailwindcss-vite/src/index.ts

export function vitePluginSharedUnocss(): Plugin {
// TODO:
// reading `uno.config.ts` is adding more than 1 sec on startup time. is it normal?

export function vitePluginSharedUnocss(): PluginOption {
// reuse original plugin to grab internal unocss instance and transform plugins
const originPlugins = vitePluginUnocss();
const transformPlugins = originPlugins.filter((p) =>
p.name.startsWith("unocss:transformers:"),
);
const apiPlugin = originPlugins.find((p) => p.name === "unocss:api");
const originalPlugins = vitePluginUnocss();
const apiPlugin = originalPlugins.find((p) => p.name === "unocss:api");
tinyassert(apiPlugin);
const ctx = (apiPlugin.api as UnocssVitePluginAPI).getContext();

return {
// reuse unocss transform plugins with sharedDuringBuild
const transformPlugins = originalPlugins
.filter((plugin) => plugin.name.startsWith("unocss:transformers:"))
.map((plugin) => ({
...plugin,
sharedDuringBuild: true,
}));

const mainPlugin: Plugin = {
name: vitePluginSharedUnocss.name,
sharedDuringBuild: true,
create(environment) {
const plugins: Plugin[] = [...transformPlugins];

// [dev, build]
// extract tokens by intercepting transform
plugins.push({
name: vitePluginSharedUnocss.name + ":extract",
transform(code, id) {
if (ctx.filter(code, id)) {
ctx.tasks.push(ctx.extract(code, id));
}
},
});
// extract tokens by intercepting transform
transform: {
order: "post",
handler(code, id) {
if (ctx.filter(code, id)) {
ctx.tasks.push(ctx.extract(code, id));
}
},
},

create(environment) {
const plugins: Plugin[] = [];

// Following plugins are naturally applied to the environments
// Following plugins are applied only to the environments
// which import "virtual:unocss.css".
// So, even though we only need to handle "client" environment case,
// such artificial restriction is not necessary.
Expand Down Expand Up @@ -110,4 +118,6 @@ export function vitePluginSharedUnocss(): Plugin {
return plugins;
},
};

return [...transformPlugins, mainPlugin];
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"tsup": "^8.0.2",
"tsx": "^4.7.1",
"typescript": "^5.4.3",
"vite": "6.0.0-alpha.7",
"vite": "6.0.0-alpha.8",
"vitest": "^1.5.1",
"wrangler": "^3.48.0"
},
Expand Down
76 changes: 38 additions & 38 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fddccc4

Please sign in to comment.