From 97bb92135851c2f486afb3aa5b2c08b7a1ccbcc3 Mon Sep 17 00:00:00 2001 From: Herman Lederer Date: Thu, 11 Aug 2022 14:08:57 +0300 Subject: [PATCH 1/2] Fix script --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4569367..02e0505 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "scripts": { "prepack": "pnpm run build && cp .gitignore .npmignore && cat .npmpack >> .npmignore", "postpack": "rm .npmignore", - "dev": "pnpm run build -- --watch", + "dev": "pnpm run build --watch", "build": "tsup", "test": "jest", "lint": "eslint ." From 40d86bd5908f4893181726e6c1ef81d322201b32 Mon Sep 17 00:00:00 2001 From: Herman Lederer Date: Thu, 11 Aug 2022 14:09:28 +0300 Subject: [PATCH 2/2] Introduce a build-time error --- README.md | 4 +- src/plugin/index.ts | 95 ++++++++++++++++++++++++++------------------- 2 files changed, 58 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index c6d927a..45001e7 100644 --- a/README.md +++ b/README.md @@ -94,12 +94,12 @@ interface UserOptions { ## Usage -The relay server only works in dev mode but the abstraction module can still be included in production, it is up to you to ensure it is not. There is no significant consequewnces to inclusing the abstractions layer in production since it will just make requests to a an inactive API endpoint, however, it is unnecessary code that should not be shipped to users. +The relay server only works in dev mode but the abstraction module can still be imported in production, it is up to you to ensure it is not. The abstraction layer is not buildable in production since it relies on fs relay server port, which does not exist at buid time. Importhing the abstraction layer at build time will casue an error. This plugin runs a relay server that allows the browser to communicate with node. There are two ways to interact with the relay server: - use the abstraction API (**recommended**), -- fetch network requests (not supported). +- fetch network requests (tinkering only). ### Abstraction API diff --git a/src/plugin/index.ts b/src/plugin/index.ts index 3337ca2..bcc7458 100644 --- a/src/plugin/index.ts +++ b/src/plugin/index.ts @@ -2,7 +2,7 @@ import type { Plugin } from 'vite'; import FsServer from './server'; import { UserOptions, resolveOptions } from './Options'; -function VitePluginFs(userOptiuons: UserOptions = {}): Plugin { +function VitePluginFs(userOptiuons: UserOptions = {}): Plugin[] { const options = resolveOptions(userOptiuons); const virtualModuleId = 'virtual:fs'; @@ -12,57 +12,74 @@ function VitePluginFs(userOptiuons: UserOptions = {}): Plugin { let isProd = true; // Assume prod unless otherwise is certain - return { - name: 'vite-plugin-fs', + return [ + // Serve-time plugin + { + name: 'vite-plugin-fs', - apply: 'serve', + apply: 'serve', - config(_, env) { - if (env.mode === 'development') { - isProd = false; - } + config(_, env) { + if (env.mode === 'development') { + isProd = false; + } - return { - build: { - rollupOptions: { - external: [virtualModuleId], + return { + build: { + rollupOptions: { + external: [virtualModuleId], + }, }, - }, - optimizeDeps: { - exclude: [virtualModuleId], - }, - }; - }, + optimizeDeps: { + exclude: [virtualModuleId], + }, + }; + }, - async buildStart() { - if (!isProd) { - server = new FsServer(options); - await server.start(); - } - }, + async buildStart() { + if (!isProd) { + server = new FsServer(options); + await server.start(); + } + }, - resolveId(id) { - if (id === virtualModuleId) { - return resolvedVirtualModuleId; - } + resolveId(id) { + if (id === virtualModuleId) { + return resolvedVirtualModuleId; + } - return null; - }, + return null; + }, - load(id) { - if (typeof server?.activePort === 'number') { - if (id === resolvedVirtualModuleId) { - return `export const activePort = ${server.activePort}`; + load(id) { + if (typeof server?.activePort === 'number') { + if (id === resolvedVirtualModuleId) { + return `export const activePort = ${server.activePort}`; + } } - } - return null; + return null; + }, + + closeBundle() { + server?.stop(); + }, }, + // Build time plugin + { + name: 'vite-plugin-fs', + + apply: 'build', + + resolveId(id) { + if (id === virtualModuleId) { + this.error('virtual:fs imported in production code, that is not allowed. It is likely caused by importing vite-plugin-fs/browser. Please find a way not to do that, making a vite plugin is a great start.'); + } - closeBundle() { - server?.stop(); + return null; + }, }, - }; + ]; } export default VitePluginFs;