UNPKG

vite-plugin-neutralino

Version:

A Vite plugin for seamless integration with Neutralinojs.

1 lines 4.31 kB
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { type ResolvedConfig, type Plugin, type HtmlTagDescriptor } from 'vite';\r\nimport fs from 'node:fs/promises';\r\nimport path from 'node:path';\r\n\r\ninterface AuthInfo {\r\n nlPort: number;\r\n}\r\n\r\n/**\r\n * Plugin options.\r\n */\r\ninterface Options {\r\n /**\r\n * Custom root path for the Neutralinojs project.\r\n * @default config.root\r\n */\r\n rootPath?: string;\r\n}\r\n\r\n/**\r\n * Gets the script tag for development mode by reading the auth info from the .tmp directory.\r\n * @param root - The project root directory.\r\n * @returns A promise that resolves to the script tag or null if not applicable.\r\n */\r\nasync function getDevScriptTag(root: string): Promise<HtmlTagDescriptor | null> {\r\n const authInfoPath = path.join(root, '.tmp', 'auth_info.json');\r\n try {\r\n const authInfoFile = await fs.readFile(authInfoPath, { encoding: 'utf-8' });\r\n const authInfo: AuthInfo = JSON.parse(authInfoFile);\r\n const port = Number.isInteger(authInfo.nlPort) ? authInfo.nlPort : 0;\r\n\r\n if (port > 0) {\r\n return {\r\n tag: 'script',\r\n attrs: {\r\n src: `http://localhost:${port}/__neutralino_globals.js`,\r\n },\r\n injectTo: 'head-prepend',\r\n };\r\n }\r\n \r\n console.warn('[vite-plugin-neutralino] Invalid Neutralino port, script not injected.');\r\n return null;\r\n } catch (error) {\r\n console.warn(`[vite-plugin-neutralino] Could not read Neutralino auth file: ${authInfoPath}, script not injected. Please make sure the Neutralino app is running.`);\r\n return null;\r\n }\r\n}\r\n\r\n/**\r\n * Gets the script tag for production mode.\r\n * @returns The script tag for production.\r\n */\r\nfunction getProdScriptTag(): HtmlTagDescriptor {\r\n return {\r\n tag: 'script',\r\n attrs: {\r\n src: '__neutralino_globals.js',\r\n },\r\n injectTo: 'head-prepend',\r\n };\r\n}\r\n\r\nconst neutralino = (options: Options = {}): Plugin => {\r\n let config: ResolvedConfig;\r\n\r\n return {\r\n name: 'vite-plugin-neutralino',\r\n configResolved(resolvedConfig) {\r\n config = resolvedConfig;\r\n },\r\n async transformIndexHtml() {\r\n try {\r\n if (config.mode === 'production') {\r\n return [getProdScriptTag()];\r\n }\r\n\r\n if (config.mode === 'development') {\r\n const root = options.rootPath || config.root;\r\n const devTag = await getDevScriptTag(root);\r\n return devTag ? [devTag] : [];\r\n }\r\n\r\n return [];\r\n } catch (error) {\r\n console.error('[vite-plugin-neutralino] An unexpected error occurred:', error);\r\n return [];\r\n }\r\n },\r\n };\r\n};\r\n\r\nexport default neutralino;"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,sBAAe;AACf,uBAAiB;AAsBjB,eAAe,gBAAgB,MAAiD;AAC5E,QAAM,eAAe,iBAAAA,QAAK,KAAK,MAAM,QAAQ,gBAAgB;AAC7D,MAAI;AACA,UAAM,eAAe,MAAM,gBAAAC,QAAG,SAAS,cAAc,EAAE,UAAU,QAAQ,CAAC;AAC1E,UAAM,WAAqB,KAAK,MAAM,YAAY;AAClD,UAAM,OAAO,OAAO,UAAU,SAAS,MAAM,IAAI,SAAS,SAAS;AAEnE,QAAI,OAAO,GAAG;AACV,aAAO;AAAA,QACH,KAAK;AAAA,QACL,OAAO;AAAA,UACH,KAAK,oBAAoB,IAAI;AAAA,QACjC;AAAA,QACA,UAAU;AAAA,MACd;AAAA,IACJ;AAEA,YAAQ,KAAK,wEAAwE;AACrF,WAAO;AAAA,EACX,SAAS,OAAO;AACZ,YAAQ,KAAK,iEAAiE,YAAY,wEAAwE;AAClK,WAAO;AAAA,EACX;AACJ;AAMA,SAAS,mBAAsC;AAC3C,SAAO;AAAA,IACH,KAAK;AAAA,IACL,OAAO;AAAA,MACH,KAAK;AAAA,IACT;AAAA,IACA,UAAU;AAAA,EACd;AACJ;AAEA,IAAM,aAAa,CAAC,UAAmB,CAAC,MAAc;AAClD,MAAI;AAEJ,SAAO;AAAA,IACH,MAAM;AA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