Skip to content

Commit

Permalink
Merge pull request #9 from HermanLederer/feature/prod-warnings
Browse files Browse the repository at this point in the history
Feature/prod warnings
  • Loading branch information
StarLederer authored Aug 11, 2022
2 parents 16ae9df + 40d86bd commit 0aa03b0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 42 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 ."
Expand Down
95 changes: 56 additions & 39 deletions src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;

0 comments on commit 0aa03b0

Please sign in to comment.