Skip to content

Commit

Permalink
initial prep for devtools in vpk
Browse files Browse the repository at this point in the history
  • Loading branch information
LankyMoose committed Apr 6, 2024
1 parent 3347a4d commit 3d1f6ab
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions packages/vite-plugin-kaioken/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,23 @@ const defaultEsBuildOptions: ESBuildOptions = {
include: ["**/*.tsx", "**/*.ts", "**/*.jsx", "**/*.js"],
}

export default function (): Plugin {
export interface KaiokenPluginOptions {
enableDevtools?: boolean
}

const devtoolsScript = `
import {contexts} from "kaioken/dist/globals.js";
console.log("kaioken:devtools", contexts);
`

export default function (
opts: KaiokenPluginOptions = {
enableDevtools: true,
}
): Plugin {
let isProduction = false
let isBuild = false
let hasInjectedDevtools = false

return {
name: "vite-plugin-kaioken",
Expand Down Expand Up @@ -50,7 +64,11 @@ export default function (): Plugin {
},
transform(code, id) {
if (isProduction || isBuild) return
if (!/\.(tsx|jsx)$/.test(id)) return
if (opts.enableDevtools && !hasInjectedDevtools) {
code += devtoolsScript
hasInjectedDevtools = true
}
if (!/\.(tsx|jsx)$/.test(id)) return { code }
const ast = this.parse(code)
try {
const componentNames = findExportedComponentNames(ast.body as AstNode[])
Expand Down

0 comments on commit 3d1f6ab

Please sign in to comment.