Skip to content

Commit

Permalink
refactor: vite plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Jan 28, 2024
1 parent 6f66787 commit aac2c30
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 73 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@
"@types/react-dom": "^18.2.15",
"core-js": "^3.33.2",
"dprint": "^0.45.0",
"gen_dep_tag": "^0.0.3",
"lightningcss": "1.23.0",
"oxlint": "^0.1.0",
"rollup-plugin-externals": "^0.0.1",
"rollup-plugin-swc3": "^0.11.0",
"toggle-switch-css": "^1.0.3",
"typescript": "^5.2.2",
"vite": "^5.0.0",
"vite-plugin-external-globals": "^0.4.0"
"vite": "^5.0.0"
}
}
2 changes: 1 addition & 1 deletion plugins/sw-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default function(): Plugin {
let is_prod = false;
return {
name: "sw-plugin",
config(_, env) {
configResolved(env) {
is_prod = env.command === "build";
},
transformIndexHtml(html) {
Expand Down
71 changes: 12 additions & 59 deletions pnpm-lock.yaml

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

42 changes: 31 additions & 11 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { presets, tagBuilder } from "gen_dep_tag";
import { execSync } from "node:child_process";
import { readdirSync } from "node:fs";
import { readFile } from "node:fs/promises";
import { join } from "node:path";
import { externals } from "rollup-plugin-externals";
import { swc } from "rollup-plugin-swc3";
import { defineConfig } from "vite";
import externalGlobals, { libPreset } from "vite-plugin-external-globals";
import { defineConfig, type HtmlTagDescriptor } from "vite";
import pkg from "./package.json" assert { type: "json" };
import sw_plugin from "./plugins/sw-plugin";

Expand Down Expand Up @@ -35,23 +36,29 @@ const langMap = await Promise.all(
}),
);

const tag = tagBuilder({ sri: true });

export default defineConfig({
clearScreen: false,
json: {
namedExports: false,
},
plugins: [
swc(),
externalGlobals({
apply: "build",
injectTo: "body",
integrity: true,
crossorigin: "anonymous",
entry: [
libPreset("react"),
libPreset("react-dom"),
],
externals({
react: "React",
"react-dom": "ReactDOM",
}),
{
name: "html-cdn-codegen",
apply: "build",
transformIndexHtml(html) {
return {
html,
tags: [presets.react, presets["react-dom"]].map(tag).map(htmlTag),
};
},
},
sw_plugin(),
],
base: "./",
Expand Down Expand Up @@ -83,3 +90,16 @@ export default defineConfig({
},
},
});

function htmlTag(meta: ReturnType<typeof tag>): HtmlTagDescriptor {
const { url, integrity } = meta;
return {
tag: "script",
attrs: {
src: url,
integrity,
crossorigin: "anonymous",
},
injectTo: "head",
};
}

1 comment on commit aac2c30

@vercel
Copy link

@vercel vercel bot commented on aac2c30 Jan 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lrc-maker – ./

lrc-maker-git-master-magic-akari.vercel.app
lrc-maker-magic-akari.vercel.app
lrc-maker.vercel.app

Please sign in to comment.