Skip to content

Commit a380ebb

Browse files
authored
Move the vite plugin over to unplugin using the bundler core (#4)
Move vite plugin to use the bundler core package as it's base.
1 parent e921113 commit a380ebb

File tree

12 files changed

+342
-137
lines changed

12 files changed

+342
-137
lines changed

bundlers/vite/src/App.tsx

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import { useState } from 'react'
2-
import reactLogo from './assets/react.svg'
3-
import viteLogo from '/vite.svg'
4-
import './App.css'
1+
import { useState, lazy, Suspense } from "react";
2+
import reactLogo from "./assets/react.svg";
3+
import viteLogo from "/vite.svg";
4+
import "./App.css";
5+
6+
const IndexedLazyComponent = lazy(() => import("./IndexedLazyComponent"));
7+
const LazyComponent = lazy(() => import("./LazyComponent/LazyComponent"));
58

69
function App() {
7-
const [count, setCount] = useState(0)
10+
const [count, setCount] = useState(0);
811

912
return (
1013
<>
@@ -28,8 +31,14 @@ function App() {
2831
<p className="read-the-docs">
2932
Click on the Vite and React logos to learn more
3033
</p>
34+
<Suspense fallback={null}>
35+
<IndexedLazyComponent />
36+
</Suspense>
37+
<Suspense fallback={null}>
38+
<LazyComponent />
39+
</Suspense>
3140
</>
32-
)
41+
);
3342
}
3443

35-
export default App
44+
export default App;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function IndexedLazyComponent() {
2+
return (
3+
<div>
4+
<h1>Indexed Lazy Component</h1>
5+
</div>
6+
);
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from "./IndexedLazyComponent";
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { lazy, Suspense } from "react";
2+
3+
const IndexedLazyComponent = lazy(() => import("../IndexedLazyComponent"));
4+
5+
export default function LazyComponent() {
6+
return (
7+
<div>
8+
<h1>Lazy Component</h1>
9+
<Suspense fallback={null}>
10+
<IndexedLazyComponent />
11+
</Suspense>
12+
</div>
13+
);
14+
}

bundlers/vite/vite.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { defineConfig } from "vite";
22
import react from "@vitejs/plugin-react";
3-
import { viteStatsPlugin } from "@codecov/vite-plugin";
3+
import { codecovVitePlugin } from "@codecov/vite-plugin";
44

55
// https://vitejs.dev/config/
66
export default defineConfig({
7-
plugins: [react(), viteStatsPlugin()],
7+
plugins: [react(), codecovVitePlugin({ enableBundleAnalysis: true })],
88
});

packages/bundler-plugin-core/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"dist"
2323
],
2424
"scripts": {
25-
"typecheck": "tsc",
25+
"type-check": "tsc --noEmit",
2626
"build": "unbuild",
2727
"dev": "unbuild --stub && node --watch-path=src dist/index.mjs",
2828
"clean": "rm -rf dist .turbo node_modules",
@@ -35,13 +35,17 @@
3535
"author": "",
3636
"license": "Apache-2.0",
3737
"dependencies": {
38-
"unplugin": "^1.5.0"
38+
"unplugin": "^1.5.0",
39+
"zod": "^3.22.4"
3940
},
4041
"devDependencies": {
4142
"@types/node": "^20.8.6",
4243
"typescript": "^5.0.4",
4344
"unbuild": "^2.0.0"
4445
},
46+
"peerDependencies": {
47+
"zod": "^3.22.4"
48+
},
4549
"volta": {
4650
"extends": "../../package.json"
4751
},
Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,64 @@
1-
import { createUnplugin } from "unplugin";
1+
import { type UnpluginOptions, createUnplugin } from "unplugin";
2+
import {
3+
type BundleAnalysisUploadPlugin,
4+
type Asset,
5+
type Chunk,
6+
type Module,
7+
type Options,
8+
type Output,
9+
} from "./types.js";
210

3-
export const unplugin = createUnplugin((_options) => {
4-
return {
5-
name: "unplugin-stats",
6-
};
7-
});
11+
import { jsonSchema } from "./schemas.js";
12+
import { z } from "zod";
13+
interface CodecovUnpluginFactoryOptions {
14+
bundleAnalysisUploadPlugin: BundleAnalysisUploadPlugin;
15+
}
16+
17+
export function codecovUnpluginFactory({
18+
bundleAnalysisUploadPlugin,
19+
}: CodecovUnpluginFactoryOptions) {
20+
return createUnplugin<Options, true>((userOptions, _unpluginMetaContext) => {
21+
const plugins: UnpluginOptions[] = [];
22+
23+
if (userOptions?.enableBundleAnalysis) {
24+
const statsFileName = z
25+
.string()
26+
.endsWith(".json")
27+
.optional()
28+
.parse(userOptions?.statsFileName);
29+
30+
const output: Output = {
31+
version: "1",
32+
};
33+
34+
let startTime = NaN;
35+
const { pluginVersion, version, ...pluginOpts } =
36+
bundleAnalysisUploadPlugin({
37+
output,
38+
statsFileName,
39+
});
40+
41+
plugins.push({
42+
...pluginOpts,
43+
buildStart() {
44+
startTime = Date.now();
45+
output.version = version;
46+
output.plugin = {
47+
name: pluginOpts.name,
48+
version: pluginVersion,
49+
};
50+
output.builtAt = startTime;
51+
},
52+
buildEnd(this) {
53+
const duration = Date.now() - startTime;
54+
output.duration = duration;
55+
},
56+
});
57+
}
58+
59+
return plugins;
60+
});
61+
}
62+
63+
export type { BundleAnalysisUploadPlugin, Asset, Chunk, Module, Options };
64+
export { jsonSchema };
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { z } from "zod";
2+
3+
const literalSchema = z.union([z.string(), z.number(), z.boolean(), z.null()]);
4+
5+
type Literal = z.infer<typeof literalSchema>;
6+
7+
type Json = Literal | { [key: string]: Json } | Json[];
8+
9+
export const jsonSchema: z.ZodType<Json> = z.lazy(() =>
10+
z.union([literalSchema, z.array(jsonSchema), z.record(jsonSchema)]),
11+
);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { type UnpluginOptions } from "unplugin";
2+
3+
export interface Dependency {
4+
name: string;
5+
version: string;
6+
}
7+
8+
export interface Asset {
9+
name: string;
10+
size: number;
11+
fileName?: string;
12+
}
13+
14+
export interface Chunk {
15+
id: string;
16+
preliminaryFileName: string;
17+
entry: boolean;
18+
initial: boolean;
19+
files: string[];
20+
names: string[];
21+
}
22+
23+
export interface Module {
24+
name: string;
25+
size?: number;
26+
chunks: (string | number)[];
27+
chunkPreliminaryFileNames: string[];
28+
}
29+
30+
export interface Output {
31+
version?: string;
32+
bundler?: {
33+
name: string;
34+
version: string;
35+
};
36+
outputPath?: string;
37+
builtAt?: number;
38+
duration?: number;
39+
assets?: Asset[];
40+
chunks?: Chunk[];
41+
modules?: Module[];
42+
plugin?: {
43+
name: string;
44+
version: string;
45+
};
46+
}
47+
48+
export interface BundleAnalysisUploadPluginArgs {
49+
output: Output;
50+
statsFileName?: string;
51+
}
52+
53+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
54+
export interface Options {
55+
statsFileName?: string;
56+
enableBundleAnalysis?: boolean;
57+
}
58+
59+
export type BundleAnalysisUploadPlugin = (
60+
args: BundleAnalysisUploadPluginArgs,
61+
) => UnpluginOptions & {
62+
pluginVersion: string;
63+
version: string;
64+
};

packages/vite-plugin/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"dist"
2323
],
2424
"scripts": {
25-
"typecheck": "tsc",
25+
"type-check": "tsc --noEmit",
2626
"build": "unbuild",
2727
"dev": "unbuild --stub && node --watch-path=src dist/index.mjs",
2828
"clean": "rm -rf dist .turbo node_modules",
@@ -45,5 +45,9 @@
4545
},
4646
"engines": {
4747
"node": ">=20.0.0"
48+
},
49+
"dependencies": {
50+
"@codecov/bundler-plugin-core": "workspace:^",
51+
"unplugin": "^1.5.0"
4852
}
4953
}

0 commit comments

Comments
 (0)