mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-01-26 19:09:52 +00:00
## Summary Marks the `comfy-api-plugin` Vite plugin as build-time only by adding `apply: 'build'`. This prevents the plugin's transform from running during development (`vite dev`), improving dev server startup time and avoiding unnecessary processing when the plugin's output is not needed in development mode. Also updates `build/tsconfig.json` to use `moduleResolution: "bundler"` which is the recommended setting for Vite projects. ## Changes - **build/plugins/comfyAPIPlugin.ts**: Add `apply: 'build'` to restrict plugin to production builds - **build/tsconfig.json**: Update `moduleResolution` from `"node"` to `"bundler"` ## Testing - `pnpm typecheck` passes - `pnpm build` produces correct output ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-8224-fix-Mark-comfy-api-plugin-as-build-time-only-2ef6d73d36508145a48ae849087fbad7) by [Unito](https://www.unito.io)
120 lines
3.3 KiB
TypeScript
120 lines
3.3 KiB
TypeScript
import path from 'path'
|
|
import type { Plugin } from 'vite'
|
|
|
|
interface ShimResult {
|
|
code: string
|
|
exports: string[]
|
|
}
|
|
|
|
const SKIP_WARNING_FILES = new Set(['scripts/app', 'scripts/api'])
|
|
|
|
/** Files that will be removed in v1.34 */
|
|
const DEPRECATED_FILES = ['scripts/ui', 'extensions/core/groupNode'] as const
|
|
|
|
function getWarningMessage(
|
|
fileKey: string,
|
|
shimFileName: string
|
|
): string | null {
|
|
if (SKIP_WARNING_FILES.has(fileKey)) {
|
|
return null
|
|
}
|
|
|
|
const isDeprecated = DEPRECATED_FILES.some((deprecatedPath) =>
|
|
fileKey.startsWith(deprecatedPath)
|
|
)
|
|
|
|
if (isDeprecated) {
|
|
return `[ComfyUI Deprecated] Importing from "${shimFileName}" is deprecated and will be removed in v1.34.`
|
|
}
|
|
|
|
return `[ComfyUI Notice] "${shimFileName}" is an internal module, not part of the public API. Future updates may break this import.`
|
|
}
|
|
|
|
function isLegacyFile(id: string): boolean {
|
|
return (
|
|
id.endsWith('.ts') &&
|
|
(id.includes('src/extensions/core') || id.includes('src/scripts'))
|
|
)
|
|
}
|
|
|
|
function transformExports(code: string, id: string): ShimResult {
|
|
const moduleName = getModuleName(id)
|
|
const exports: string[] = []
|
|
let newCode = code
|
|
|
|
// Regex to match different types of exports
|
|
const regex =
|
|
/export\s+(const|let|var|function|class|async function)\s+([a-zA-Z$_][a-zA-Z\d$_]*)(\s|\()/g
|
|
let match
|
|
|
|
while ((match = regex.exec(code)) !== null) {
|
|
const name = match[2]
|
|
// All exports should be bind to the window object as new API endpoint.
|
|
if (exports.length == 0) {
|
|
newCode += `\nwindow.comfyAPI = window.comfyAPI || {};`
|
|
newCode += `\nwindow.comfyAPI.${moduleName} = window.comfyAPI.${moduleName} || {};`
|
|
}
|
|
newCode += `\nwindow.comfyAPI.${moduleName}.${name} = ${name};`
|
|
exports.push(
|
|
`export const ${name} = window.comfyAPI.${moduleName}.${name};\n`
|
|
)
|
|
}
|
|
|
|
return {
|
|
code: newCode,
|
|
exports
|
|
}
|
|
}
|
|
|
|
function getModuleName(id: string): string {
|
|
// Simple example to derive a module name from the file path
|
|
const parts = id.split('/')
|
|
const fileName = parts[parts.length - 1]
|
|
return fileName.replace(/\.\w+$/, '') // Remove file extension
|
|
}
|
|
|
|
export function comfyAPIPlugin(isDev: boolean): Plugin {
|
|
return {
|
|
name: 'comfy-api-plugin',
|
|
apply: 'build',
|
|
transform(code: string, id: string) {
|
|
if (isDev) return null
|
|
|
|
if (isLegacyFile(id)) {
|
|
const result = transformExports(code, id)
|
|
|
|
if (result.exports.length > 0) {
|
|
const projectRoot = process.cwd()
|
|
const relativePath = path
|
|
.relative(path.join(projectRoot, 'src'), id)
|
|
.replace(/\\/g, '/')
|
|
const shimFileName = relativePath.replace(/\.ts$/, '.js')
|
|
|
|
let shimContent = `// Shim for ${relativePath}\n`
|
|
|
|
const fileKey = relativePath.replace(/\.ts$/, '')
|
|
const warningMessage = getWarningMessage(fileKey, shimFileName)
|
|
|
|
if (warningMessage) {
|
|
// It will only display once because it is at the root of the file.
|
|
shimContent += `console.warn('${warningMessage}');\n`
|
|
}
|
|
|
|
shimContent += result.exports.join('')
|
|
|
|
this.emitFile({
|
|
type: 'asset',
|
|
fileName: shimFileName,
|
|
source: shimContent
|
|
})
|
|
}
|
|
|
|
return {
|
|
code: result.code,
|
|
map: null // If you're not modifying the source map, return null
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|