Skip to content

Commit

Permalink
chore: finish proxy vite server adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
ErKeLost committed Oct 5, 2024
1 parent f87e4c7 commit fcec298
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 263 deletions.
2 changes: 1 addition & 1 deletion examples/vue3/farm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default defineConfig({
imports: ["vue", VueRouterAutoImports],
}),
vue(),
// Inspector(),
Inspector(),
// compressionMiddleware(),
// myCustomPlugin(),
// createHtmlPlugin({
Expand Down
3 changes: 1 addition & 2 deletions examples/vue3/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import HelloWorld from "./components/HelloWorld.vue";
// console.log(import.meta.env);
import { bbb } from "./indexa.ts";
import { ref } from "vue";
bbb();
fetch("https://wallhaven.fun/api/wallhaven/w/yx6e9l").then((res) => res.json());
// .then((data) => console.log(data))
Expand All @@ -16,7 +15,7 @@ window.onbeforeunload = function () {
);
};
const aa = ref(9299)
const aa = ref(92922299)
console.log(aa.value);
</script>
Expand Down
177 changes: 0 additions & 177 deletions packages/core/src/plugin/js/new-vite-server-adapter.ts

This file was deleted.

115 changes: 32 additions & 83 deletions packages/core/src/plugin/js/vite-server-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,43 @@
// import { watch } from 'chokidar';
import { FSWatcher } from 'chokidar';
// import { Server } from '../../index.js';
import { Server, Server as httpServer } from '../../server/index.js';
// import WsServer from '../../server/ws.js';
import { Server } from '../../server/index.js';
import { CompilationContext, ViteModule } from '../type.js';
import { throwIncompatibleError } from './utils.js';

// TODO type error refactor vite adaptor
export class ViteDevServerAdapter {
moduleGraph: ViteModuleGraphAdapter;
config: any;
pluginName: string;
// printUrls: any;
resolvedUrls: any;
serverOptions: any;
watcher: FSWatcher;
middlewares: any;
ws: any;
httpServer: httpServer;
private _server: any;

private _config: any;
private _moduleGraphAdapter: any;
[key: string]: any;
[key: symbol]: any;
constructor(pluginName: string, config: any, server: any) {
this.moduleGraph = createViteModuleGraphAdapter(pluginName);
this.config = config;
this.pluginName = pluginName;
// watcher is not used in Farm vite plugin for now
// it's only for compatibility
// this.watcher = watch(config.root);

this._server = server;

this.watcher = server.watcher.getInternalWatcher();

this.middlewares = server.middlewares;

// this.printUrls = server.printUrls;

this.resolvedUrls = server.resolvedUrls;

this.serverOptions = server.serverOptions;

this.ws = server.ws;

this.httpServer = server.httpServer;
}

get printUrls() {
return this._server.printUrls;
}

set printUrls(value) {
this._server.printUrls = value;
this._config = config;
this._moduleGraphAdapter = createViteModuleGraphAdapter(pluginName);

return new Proxy(this, {
get: (_target, prop) => {
switch (prop) {
case 'moduleGraph':
return this._moduleGraphAdapter;
case 'watcher':
return this._server.watcher.getInternalWatcher();
case 'middlewares':
return this._server.middlewares;
case 'config':
return this._config;
default: {
const value = this._server[prop];
return typeof value === 'function'
? value.bind(this._server)
: value;
}
}
},
set: (_target, prop, value) => {
this._server[prop] = value;
return true;
}
});
}
}

Expand Down Expand Up @@ -154,12 +141,6 @@ export function createViteDevServerAdapter(
}

throwIncompatibleError(pluginName, 'viteDevServer', allowedKeys, key);
},
set(target, key, value) {
const handler =
handleSetOperation[key as keyof typeof handleSetOperation] ||
handleSetOperation.default;
return handler(target, key, value, server);
}
}
);
Expand Down Expand Up @@ -196,35 +177,3 @@ export function createViteModuleGraphAdapter(pluginName: string) {

return proxy;
}

type SetOperationHandler<T> = (
target: T,
key: string | symbol,
value: any,
server: Server
) => boolean;

type HandleSetOperationType<T> = {
[K in keyof T]?: SetOperationHandler<T>;
} & {
default: SetOperationHandler<T>;
};

export const handleSetOperation: HandleSetOperationType<ViteDevServerAdapter> =
{
printUrls: (target, _key, value, server) => {
// target.printUrls = function() {
// value.call(server);
// };
if (_key === 'printUrls') {
server[_key] = value.bind(server);
} else {
(target as any)[_key] = value;
}
return true;
},
default: (target, key, value) => {
(target as any)[key] = value;
return true;
}
};

0 comments on commit fcec298

Please sign in to comment.