Skip to content

Commit

Permalink
fix: resource generate compile pending error
Browse files Browse the repository at this point in the history
  • Loading branch information
ErKeLost committed Sep 19, 2024
1 parent 36fb5c6 commit ad3d3df
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 33 deletions.
6 changes: 3 additions & 3 deletions examples/refactor-react/farm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ export default defineConfig({
progress: false,
output: {
// publicPath: "/aaa/",
},
}
},
server: {},
});


function myCustomPlugin() {
return {
name: 'vite-plugin-custom',
name: 'custom',
updateModules: {
executor(data) {
executor(data: any) {
console.log(data, "更新的模块");
},
},
Expand Down
1 change: 1 addition & 0 deletions examples/refactor-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"clean": "farm clean"
},
"dependencies": {
"@ant-design/happy-work-theme": "^1.0.0",
"antd": "^5.4.2",
"react": "18",
"react-dom": "18"
Expand Down
13 changes: 9 additions & 4 deletions examples/refactor-react/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@ import { useState } from "react";
import "./main.css";
import reactLogo from "/react.svg";
import FarmLogo from "./assets/logo.png";
import { a } from './a.js'
// import { a } from './a.js'
import { Button } from 'antd'
import { HappyProvider } from '@ant-design/happy-work-theme';
export function Main() {
const [count, setCount] = useState(0);
console.log(a);
// console.log(a);

return (
<>
<div>
<a href="https://farmfe.org/" target="_blank">
<img src={FarmLogo} className="logo" alt="Farm logo" />
</a>
</a>2
<a href="https://react.dev" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
<HappyProvider>
<Button type="primary">Primary Button</Button>
</HappyProvider>
<h1>Farm + react</h1>
<div className="card">
<button onClick={() => setCount((count) => count + 1)}>
Expand Down
23 changes: 15 additions & 8 deletions examples/vue3/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
<script setup lang="ts">
import HelloWorld from "./components/HelloWorld.vue";
// console.log(import.meta.env);
import { bbb} from './indexa.ts'
bbb()
fetch("https://wallhaven.fun/api/wallhaven/w/yx6e9l")
.then((res) => res.json()
// .then((data) => console.log(data))
// .catch((err) => console.error(err));
import { bbb } from "./indexa.ts";
bbb();
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" })
);
};
</script>

<template>
<div>
<a href="https://farmfe.org/" target="_blank">
<img src="./assets/logo.png" class="logo" alt="Farm logo" />
</a>ww
<img src="./assets/logo.png" class="logo" alt="Farm logo" /> </a
>ww
<a href="https://vuejs.org/" target="_blank">
<img src="./assets/vue.svg" class="logo vue" alt="Vue logo" />
</a>
Expand Down
42 changes: 40 additions & 2 deletions packages/core/src/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export class Compiler {
private _onUpdateFinishQueue: (() => void | Promise<void>)[] = [];

public compiling = false;
private _compileFinishPromise: Promise<void> | null = null;
private _resolveCompileFinish: (() => void) | null = null;
private _isInitialCompile = true;

constructor(
public config: Config,
Expand All @@ -54,22 +57,30 @@ export class Compiler {

async compile() {
this.checkCompiling();

this._createCompileFinishPromise();
this.compiling = true;
if (process.env.FARM_PROFILE) {
this._bindingCompiler.compileSync();
} else {
await this._bindingCompiler.compile();
}
this.compiling = false;
this._resolveCompileFinishPromise();
if (this._isInitialCompile) {
this._isInitialCompile = false;
}
}

compileSync() {
this.checkCompiling();

this._createCompileFinishPromise();
this.compiling = true;
this._bindingCompiler.compileSync();
this.compiling = false;
this._resolveCompileFinishPromise();
if (this._isInitialCompile) {
this._isInitialCompile = false;
}
}

async update(
Expand Down Expand Up @@ -215,6 +226,33 @@ export class Compiler {
return this._bindingCompiler.stats();
}

// wait for the compiler to finish compiling
async waitForInitialCompileFinish() {
if (this._isInitialCompile) {
await this.waitForCompileFinish();
}
}

async waitForCompileFinish() {
if (this.compiling && this._compileFinishPromise) {
await this._compileFinishPromise;
}
}

private _createCompileFinishPromise() {
this._compileFinishPromise = new Promise<void>((resolve) => {
this._resolveCompileFinish = resolve;
});
}

private _resolveCompileFinishPromise() {
if (this._resolveCompileFinish) {
this._resolveCompileFinish();
this._compileFinishPromise = null;
this._resolveCompileFinish = null;
}
}

private checkCompiling() {
if (this.compiling) {
this.logger.error('Already compiling', {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/server/hmr-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export class HmrEngine {
});

const start = performance.now();
console.log(queue);

const result = await this.app.compiler.update(queue);

this.app.logger.info(
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ export class Server extends httpServer {
this.middlewares.use(adaptorViteMiddleware(this));
}

// TODO server post hooks not work bug!
this.postConfigureServerHooks.forEach((fn) => fn && fn());

// TODO todo add appType 这块要判断 单页面还是 多页面 多 html 处理不一样
Expand Down Expand Up @@ -616,6 +617,7 @@ export class Server extends httpServer {
);
const start = performance.now();
await this.#compile();

const duration = performance.now() - start;
bootstrap(
duration,
Expand Down
12 changes: 8 additions & 4 deletions packages/core/src/server/middlewares/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@ export function resourceMiddleware(app: any) {
// if (url?.endsWith('.html') && req.headers['sec-fetch-dest'] !== 'script') {
// }

if (compiler.compiling) {
await new Promise((resolve) => {
compiler.onUpdateFinish(() => resolve(undefined));
});
if (compiler._isInitialCompile) {
await compiler.waitForInitialCompileFinish();
} else {
if (compiler.compiling) {
await new Promise((resolve) => {
compiler.onUpdateFinish(() => resolve(undefined));
});
}
}

const resourceResult: any = findResource(req, res, compiler, publicPath);
Expand Down
5 changes: 3 additions & 2 deletions packages/runtime-plugin-hmr/src/overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ export class ErrorOverlay extends HTMLElement {

const splitMessage = splitErrorMessage(msg);

console.error(splitMessage.errorBrowser);
console.error(splitMessage.errorInfo);

if (splitMessage.codeBlocks && splitMessage.codeBlocks.length > 0) {
splitMessage.codeBlocks.forEach((codeBlock, blockIndex) => {
Expand Down Expand Up @@ -635,8 +635,9 @@ export class ErrorOverlay extends HTMLElement {
});
} else if (splitMessage.errorInfo) {
const terminalBlock = document.createElement('div');

const highlightedCode = this.highlightCode(
extractErrorMessage(splitMessage.errorInfo)
extractErrorMessage(splitMessage.frame)
);

terminalBlock.className = 'terminal-block';
Expand Down
6 changes: 3 additions & 3 deletions packages/runtime-plugin-hmr/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ export function cleanStack(stack: string) {
export function splitErrorMessage(errorMsg: any) {
// @ts-ignore
const potentialCauses = errorMsg.cause;
const { codeBlocks, idCodeLines } = extractSwcCodeBlocks(
stripAnsi(errorMsg?.errorFrame ?? '')
);
const stripErrorFrame = stripAnsi(errorMsg.errorFrame ?? '');
const { codeBlocks, idCodeLines } = extractSwcCodeBlocks(stripErrorFrame);
return {
errorInfo: stripAnsi(errorMsg.message),
codeBlocks,
potentialCauses,
idCodeLines,
frame: stripErrorFrame,
errorBrowser: `${stripAnsi(errorMsg.message)}\n\n${potentialCauses}\n\n`
};
}
Expand Down
37 changes: 30 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ad3d3df

Please sign in to comment.