Skip to content
This repository has been archived by the owner on Nov 17, 2022. It is now read-only.

Commit

Permalink
fix: re-compile server bundle after document changed (#663)
Browse files Browse the repository at this point in the history
* fix: re-compile server bundle after document changed

* fix: optimize code
  • Loading branch information
ClarkXia authored Nov 7, 2022
1 parent 504b5af commit ec8553a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
40 changes: 30 additions & 10 deletions packages/ice/src/plugins/web/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as path from 'path';
import consola from 'consola';
import chalk from 'chalk';
import lodash from '@ice/bundles/compiled/lodash/index.js';
import type { Plugin } from '../../types/plugin.js';
import ReCompilePlugin from '../../webpack/ReCompilePlugin.js';
import DataLoaderPlugin from '../../webpack/DataLoaderPlugin.js';
Expand All @@ -10,10 +11,13 @@ import getWebTask from '../../tasks/web/index.js';
import generateHTML from '../../utils/generateHTML.js';
import openBrowser from '../../utils/openBrowser.js';
import getServerCompilerPlugin from '../../utils/getServerCompilerPlugin.js';
import type ServerCompilerPlugin from '../../webpack/ServerCompilerPlugin.js';

const { debounce } = lodash;

const plugin: Plugin = () => ({
name: 'plugin-web',
setup: ({ registerTask, onHook, context, generator, serverCompileTask, dataCache, getAllPlugin }) => {
setup: ({ registerTask, onHook, context, generator, serverCompileTask, dataCache, watch, getAllPlugin }) => {
const { rootDir, commandArgs, command, userConfig } = context;
const { ssg } = userConfig;

Expand Down Expand Up @@ -45,24 +49,26 @@ const plugin: Plugin = () => ({
});

let serverOutfile: string;
let serverCompilerPlugin: ServerCompilerPlugin;
onHook(`before.${command as 'start' | 'build'}.run`, async ({ webpackConfigs, taskConfigs, serverCompiler }) => {
// Compile server entry after the webpack compilation.
const { reCompile: reCompileRouteConfig, ensureRoutesConfig } = getRouteExportConfig(rootDir);
const outputDir = webpackConfigs[0].output.path;
serverOutfile = path.join(outputDir, SERVER_OUTPUT_DIR, `index${userConfig?.server?.format === 'esm' ? '.mjs' : '.cjs'}`);
serverCompilerPlugin = getServerCompilerPlugin(serverCompiler, {
rootDir,
serverEntry: taskConfigs[0].config?.server?.entry,
outputDir: webpackConfigs[0].output.path,
dataCache,
serverCompileTask: command === 'start' ? serverCompileTask : null,
userConfig,
ensureRoutesConfig,
});
webpackConfigs[0].plugins.push(
// Add webpack plugin of data-loader in web task
new DataLoaderPlugin({ serverCompiler, rootDir, dataCache, getAllPlugin }),
// Add ServerCompilerPlugin
getServerCompilerPlugin(serverCompiler, {
rootDir,
serverEntry: taskConfigs[0].config?.server?.entry,
outputDir: webpackConfigs[0].output.path,
dataCache,
serverCompileTask: command === 'start' ? serverCompileTask : null,
userConfig,
ensureRoutesConfig,
}),
serverCompilerPlugin,
);

if (command === 'start') {
Expand All @@ -77,6 +83,20 @@ const plugin: Plugin = () => ({
return files.some((filePath) => routeFiles.some(routeFile => filePath.includes(routeFile)));
}),
);
const debounceCompile = debounce(() => {
console.log('Document updated, try to reload page for latest html content.');
if (serverCompilerPlugin) {
serverCompilerPlugin.compileTask();
}
}, 200);
watch.addEvent([
/src\/document(\/index)?(.js|.jsx|.tsx)/,
(event: string) => {
if (event === 'change') {
debounceCompile();
}
},
]);
}
});

Expand Down
35 changes: 26 additions & 9 deletions packages/ice/src/webpack/ServerCompilerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export default class ServerCompilerPlugin {
private serverCompilerOptions: Parameters<ServerCompiler>;
private serverCompileTask: ExtendsPluginAPI['serverCompileTask'];
private ensureRoutesConfig: () => Promise<void>;
private isCompiling: boolean;
private lastAssets: string;

public constructor(
serverCompiler: ServerCompiler,
Expand All @@ -24,17 +26,32 @@ export default class ServerCompilerPlugin {
this.ensureRoutesConfig = ensureRoutesConfig;
}

public compileTask = async (compilation?: Compilation) => {
const [buildOptions, customConfig = {}] = this.serverCompilerOptions;
let task = null;
if (!this.isCompiling) {
await this.ensureRoutesConfig();
let assets = this.lastAssets;
if (compilation) {
assets = compilation.assets['assets-manifest.json'].source().toString();
// Store last assets for compilation Document.
this.lastAssets = assets;
}
task = this.serverCompiler(buildOptions, {
...customConfig,
assetsManifest: JSON.parse(assets),
});
}
return task;
};

public apply(compiler: Compiler) {
compiler.hooks.watchRun.tap(pluginName, () => {
this.isCompiling = true;
});
compiler.hooks.emit.tapPromise(pluginName, async (compilation: Compilation) => {
const [buildOptions, customConfig = {}] = this.serverCompilerOptions;

await this.ensureRoutesConfig();

const task = this.serverCompiler(buildOptions, {
...customConfig,
assetsManifest: JSON.parse(compilation.assets['assets-manifest.json'].source().toString()),
});

this.isCompiling = false;
const task = await this.compileTask(compilation);
if (this.serverCompileTask) {
this.serverCompileTask.set(task);
} else {
Expand Down

1 comment on commit ec8553a

@vercel
Copy link

@vercel vercel bot commented on ec8553a Nov 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ice-v3 – ./

ice-v3-ice-v3.vercel.app
ice-v3.vercel.app
ice-v3-git-release-next-ice-v3.vercel.app

Please sign in to comment.