Skip to content

Commit

Permalink
Merge branch 'v2-dev' of github.com:farm-fe/farm into v2-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
wumingliang committed Dec 23, 2024
2 parents 3451eb9 + 3643f58 commit 7642c9c
Show file tree
Hide file tree
Showing 20 changed files with 205 additions and 194 deletions.
2 changes: 1 addition & 1 deletion examples/refactor-react/farm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default defineConfig({
// cacheDir: "node_modules/.adny",
// },
output: {
// publicPath: "/aaa/",
publicPath: "/aaa/",
},
},
server: {
Expand Down
2 changes: 1 addition & 1 deletion examples/vue3/farm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export default defineConfig({
// enabled: true,
// }),
// compressionMiddleware(),
myCustomPlugin(),
// myCustomPlugin(),
// createHtmlPlugin({
// minify: true,
// /**
Expand Down
Binary file added examples/vue3/public/aaa.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 11 additions & 29 deletions examples/vue3/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ fetch("https://wallhaven.fun/api/wallhaven/w/yx6e9l").then((res) => res.json());
// .then((data) => console.log(data))
// .catch((err) => console.error(err));+
window.onbeforeunload = function () {
fetch("http://localhost:3000", { method: "post" });
navigator.sendBeacon(
"http://localhost:3000",
JSON.stringify({ key: "value" })
);
};
// window.onbeforeunload = function () {
// fetch("http://localhost:3000", { method: "post" });
// navigator.sendBeacon(
// "http://localhost:3000",
// JSON.stringify({ key: "value" })
// );
// };
// const aa = ref(92922299)
// console.log(aa.value);
Expand All @@ -24,33 +24,15 @@ window.onbeforeunload = function () {
<template>
<div>
<a href="https://farmfe.org/" target="_blank">
<img src="./assets/logo.png" class="logo" alt="Farm logo" /> </a
>ww222222222222222
<img src="./assets/logo.png" class="logo" alt="Farm logo" /> </a>
<a href="https://vuejs.org/" target="_blank">
<img src="./assets/vue.svg" class="logo vue" alt="Vue logo" />
</a>
<div h-full text-center flex select-none all:transition-400>
<div ma>
<div text-5xl fw100 animate-bounce-alt animate-count-infinite animate-duration-1s>
UnoCSS
</div>
<div op30 text-lg fw300 m1>
The instant on-demand Atomic CSS engine.
</div>
<div m2 flex justify-center text-2xl op30 hover="op80">
<a
i-carbon-logo-github
text-inherit
href="https://github.com/unocss/unocss"
target="_blank"
></a>
<div>
<img width="500" src="/aaa.jpg" alt="">

</div>
</div>
</div>
<div absolute bottom-5 right-0 left-0 text-center op30 fw300>
on-demand · instant · fully customizable
</div>
</div>
<HelloWorld msg="Farm + Vue" />
</template>

Expand Down
2 changes: 2 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,14 @@
"@types/connect": "^3.4.38",
"@types/cors": "^2.8.17",
"@types/debug": "^4.1.12",
"@types/escape-html": "^1.0.4",
"@types/etag": "^1.8.3",
"@types/figlet": "^1.5.5",
"@types/fs-extra": "^11.0.1",
"@types/http-proxy": "^1.17.14",
"@types/mime-types": "^2.1.2",
"@types/ws": "^8.5.8",
"escape-html": "^1.0.3",
"react-refresh": "^0.14.0"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface UserServerConfig {
// whether to serve static assets in spa mode, default to true
appType?: 'spa' | 'mpa' | 'custom';
middlewares?: DevServerMiddleware[];
middlewareMode?: boolean | string;
middlewareMode?: boolean;
writeToDisk?: boolean;
/** Preview server config */
preview?: UserPreviewServerConfig;
Expand Down
15 changes: 7 additions & 8 deletions packages/core/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ import { initPublicFiles } from '../utils/publicDir.js';
import { arrayEqual, isObject, normalizePath } from '../utils/share.js';

import {
adaptorViteMiddleware,
hmrPingMiddleware,
htmlFallbackMiddleware,
lazyCompilationMiddleware,
notFoundMiddleware,
proxyMiddleware,
publicMiddleware,
publicPathMiddleware,
resourceMiddleware
resourceMiddleware,
staticMiddleware
} from './middlewares/index.js';

import type * as http from 'node:http';
Expand Down Expand Up @@ -538,12 +538,14 @@ export class Server extends httpServer {
: null) || this.httpServer;

this.middlewares.use(
proxyMiddleware(this, middlewareServer as HttpServer)
proxyMiddleware(this, middlewareServer as HttpServer, proxy)
);
}

if (this.publicPath !== '/') {
this.middlewares.use(publicPathMiddleware(this));
this.middlewares.use(
publicPathMiddleware(this, this.serverOptions.middlewareMode)
);
}

if (fs.existsSync(this.publicDir as PathLike)) {
Expand All @@ -554,9 +556,7 @@ export class Server extends httpServer {
this.middlewares.use(lazyCompilationMiddleware(this));
}

if (this.resolvedUserConfig.vitePlugins?.length) {
this.middlewares.use(adaptorViteMiddleware(this));
}
this.middlewares.use(staticMiddleware(this));

this.middlewares.use(resourceMiddleware(this));

Expand All @@ -566,7 +566,6 @@ export class Server extends httpServer {

if (appType === 'spa' || appType === 'mpa') {
this.middlewares.use(htmlFallbackMiddleware(this));

this.middlewares.use(notFoundMiddleware());
}
}
Expand Down
45 changes: 0 additions & 45 deletions packages/core/src/server/middlewares/adaptorVite.ts

This file was deleted.

4 changes: 1 addition & 3 deletions packages/core/src/server/middlewares/htmlFallback.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import path from 'node:path';

import { cleanUrl, commonFsUtils, removeSlash } from '../../utils/index.js';
import { cleanUrl, removeSlash } from '../../utils/index.js';

import type Connect from 'connect';
import type { Server } from '../index.js';
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/server/middlewares/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export * from './lazyCompilation.js';
export * from './notFound.js';
export * from './proxy.js';
export * from './resource.js';
export * from './publicResource.js';
export * from './publicPath.js';
export * from './hmrPing.js';
export * from './adaptorVite.js';
export * from './static.js';
5 changes: 1 addition & 4 deletions packages/core/src/server/middlewares/lazyCompilation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
VIRTUAL_FARM_DYNAMIC_IMPORT_SUFFIX,
bold,
cyan,
formatExecutionTime,
getDynamicResources,
green
} from '../../index.js';
Expand Down Expand Up @@ -63,9 +62,7 @@ export function lazyCompilationMiddleware(
return next();
}

// TODO 取的对象不对 writeToDisk
// if (isNodeEnvironment || resolvedUserConfig.writeToDisk) {
if (isNodeEnvironment) {
if (isNodeEnvironment || resolvedUserConfig.server.writeToDisk) {
compiler.writeResourcesToDisk();
}

Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/server/middlewares/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import httpProxy from 'http-proxy';

import { ResolvedUserConfig } from '../../config/types.js';
import { isDevServer } from '../../utils/index.js';

import type * as http from 'node:http';
import type * as net from 'node:net';
import type Server from 'http-proxy';

import type Connect from 'connect';
import { CommonServerOptions } from '../http.js';
import type { Server as DevServer, HttpServer } from '../index.js';
import { PreviewServer } from '../preview.js';

Expand All @@ -23,15 +24,14 @@ export interface ProxyOptions extends httpProxy.ServerOptions {

export function proxyMiddleware(
app: DevServer | PreviewServer,
middlewareServer: HttpServer
middlewareServer: HttpServer,
config: NonNullable<CommonServerOptions['proxy']>
): Connect.NextHandleFunction {
const isDev = isDevServer(app);
const { resolvedUserConfig } = app;
const serverOptions = isDev ? app.serverOptions : app.previewServerOptions;

const proxies: Record<string, [Server, ProxyOptions]> = {};
Object.keys(serverOptions.proxy).forEach((context) => {
let opts = serverOptions.proxy[context];
Object.keys(config).forEach((context) => {
let opts = config[context];
if (!opts) {
return;
}
Expand Down
8 changes: 3 additions & 5 deletions packages/core/src/server/middlewares/publicPath.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { cleanUrl, isDevServer, withTrailingSlash } from '../../utils/index.js';
import { cleanUrl, withTrailingSlash } from '../../utils/index.js';

import type Connect from 'connect';
import { Server as DevServer } from '../index.js';
import type { PreviewServer } from '../preview.js';

export function publicPathMiddleware(
app: DevServer | PreviewServer
app: DevServer | PreviewServer,
middlewareMode: boolean
): Connect.NextHandleFunction {
const isDev = isDevServer(app);

const publicPath = app.publicPath;
const middlewareMode = isDev ? app.serverOptions.middlewareMode : false;

return function handlePublicPathMiddleware(req, res, next) {
// auto redirect to public path
Expand Down
74 changes: 0 additions & 74 deletions packages/core/src/server/middlewares/publicResource.ts

This file was deleted.

Loading

0 comments on commit 7642c9c

Please sign in to comment.