fix: merge failure

This commit is contained in:
ZeroLing 2023-01-12 16:19:27 +08:00
parent 9f2ee217c2
commit d66bac736a
3 changed files with 24 additions and 141 deletions

View File

@ -18,6 +18,8 @@ import warnOnHashRouterEnabled from '../utils/warnOnHashRouterEnabled.js';
import generateEntry from '../utils/generateEntry.js';
import { logger } from '../utils/logger.js';
import { getExpandedEnvs } from '../utils/runtimeEnv.js';
import getRouterManifest from '../utils/getRouterManifest';
import getRoutePaths from '../utils/getRoutePaths';
const build = async (
context: Context<Config, ExtendsPluginAPI>,
@ -135,6 +137,7 @@ const build = async (
}
const serverOutfile = path.join(outputDir, SERVER_OUTPUT_DIR, `index${userConfig?.server?.format === 'esm' ? '.mjs' : '.cjs'}`);
serverEntryRef.current = serverOutfile;
const routeType = appConfig?.router?.type;
const {
outputPaths = [],
} = await generateEntry({
@ -150,6 +153,24 @@ const build = async (
// This depends on orders.
output.paths = [...outputPaths];
if (routeType === 'memory' && userConfig?.routes?.injectInitialEntry) {
// Read the latest routes info.
const routes = getRouterManifest(rootDir);
const routePaths = getRoutePaths(routes);
routePaths.forEach((routePath) => {
// Inject `initialPath` when router type is memory.
const routeAssetPath = path.join(outputDir, 'js',
`p_${routePath === '/' ? 'index' : routePath.replace(/^\//, '').replace(/\//g, '-')}.js`);
if (fse.existsSync(routeAssetPath)) {
fse.writeFileSync(routeAssetPath,
`window.__ICE_APP_CONTEXT__=Object.assign(window.__ICE_APP_CONTEXT__||{}, {routePath: '${routePath}'});${
fse.readFileSync(routeAssetPath, 'utf-8')}`);
} else {
logger.warn(`Can not find ${routeAssetPath} when inject initial path.`);
}
});
}
await applyHook('after.build.compile', {
stats,
isSuccessful,

View File

@ -1,153 +1,17 @@
import * as path from 'path';
import fse from 'fs-extra';
import chalk from 'chalk';
import type { RenderMode } from '@ice/runtime';
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';
import { getRouteExportConfig } from '../../service/config.js';
import { WEB, SERVER_OUTPUT_DIR, IMPORT_META_TARGET, IMPORT_META_RENDERER } from '../../constant.js';
import generateEntry from '../../utils/generateEntry.js';
import { WEB } from '../../constant.js';
import openBrowser from '../../utils/openBrowser.js';
import getServerCompilerPlugin from '../../utils/getServerCompilerPlugin.js';
import type ServerCompilerPlugin from '../../webpack/ServerCompilerPlugin.js';
import { logger } from '../../utils/logger.js';
import getRoutePaths from '../../utils/getRoutePaths.js';
import getRouterManifest from '../../utils/getRouterManifest.js';
import getWebTask from './task.js';
const { debounce } = lodash;
const plugin: Plugin = () => ({
name: 'plugin-web',
setup: ({ registerTask, onHook, context, generator, serverCompileTask, dataCache, watch, getAllPlugin }) => {
setup: ({ registerTask, onHook, context, dataCache }) => {
const { rootDir, commandArgs, command, userConfig } = context;
const { ssg } = userConfig;
registerTask(WEB, getWebTask({ rootDir, command, dataCache, userConfig }));
generator.addExport({
specifier: ['Link', 'Outlet', 'useParams', 'useSearchParams', 'useLocation', 'useNavigate'],
source: '@ice/runtime/router',
});
generator.addExport({
specifier: [
'defineAppConfig',
'useAppData',
'useData',
'useConfig',
'Meta',
'Title',
'Links',
'Scripts',
'Data',
'Main',
'history',
'KeepAliveOutlet',
'useMounted',
'ClientOnly',
'defineDataLoader',
'defineServerDataLoader',
'defineStaticDataLoader',
],
source: '@ice/runtime',
});
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,
runtimeDefineVars: {
[IMPORT_META_TARGET]: JSON.stringify('web'),
[IMPORT_META_RENDERER]: JSON.stringify('server'),
},
incremental: command === 'start',
});
webpackConfigs[0].plugins.push(
// Add webpack plugin of data-loader in web task
new DataLoaderPlugin({ serverCompiler, rootDir, dataCache, getAllPlugin }),
// Add ServerCompilerPlugin
serverCompilerPlugin,
);
if (command === 'start') {
webpackConfigs[0].plugins.push(
new ReCompilePlugin(reCompileRouteConfig, (files) => {
// Only when routes file changed.
const routeManifest = JSON.parse(dataCache.get('routes'))?.routeManifest || {};
const routeFiles = Object.keys(routeManifest).map((key) => {
const { file } = routeManifest[key];
return `src/pages/${file}`;
});
return files.some((filePath) => routeFiles.some(routeFile => filePath.includes(routeFile)));
}),
);
const debounceCompile = debounce(() => {
serverCompilerPlugin?.buildResult?.rebuild();
console.log('Document updated, try to reload page for latest html content.');
}, 200);
watch.addEvent([
/src\/document(\/index)?(.js|.jsx|.tsx)/,
(event: string) => {
if (event === 'change') {
debounceCompile();
}
},
]);
}
});
onHook('after.build.compile', async ({ webpackConfigs, serverEntryRef, appConfig }) => {
const outputDir = webpackConfigs[0].output.path;
let renderMode: RenderMode;
if (ssg) {
renderMode = 'SSG';
}
serverEntryRef.current = serverOutfile;
const routeType = appConfig?.router?.type;
await generateEntry({
rootDir,
outputDir,
entry: serverOutfile,
// only ssg need to generate the whole page html when build time.
documentOnly: !ssg,
renderMode,
routeType: appConfig?.router?.type,
distType: 'html',
});
if (routeType === 'memory' && userConfig?.routes?.injectInitialEntry) {
// Read the latest routes info.
const routes = getRouterManifest(rootDir);
const routePaths = getRoutePaths(routes);
routePaths.forEach((routePath) => {
// Inject `initialPath` when router type is memory.
const routeAssetPath = path.join(outputDir, 'js',
`p_${routePath === '/' ? 'index' : routePath.replace(/^\//, '').replace(/\//g, '-')}.js`);
if (fse.existsSync(routeAssetPath)) {
fse.writeFileSync(routeAssetPath,
`window.__ICE_APP_CONTEXT__=Object.assign(window.__ICE_APP_CONTEXT__||{}, {routePath: '${routePath}'});${
fse.readFileSync(routeAssetPath, 'utf-8')}`);
} else {
logger.warn(`Can not find ${routeAssetPath} when inject initial path.`);
}
});
}
});
onHook('after.start.compile', async ({ isSuccessful, isFirstCompile, urls, devUrlInfo }) => {
const { port, open } = commandArgs;
const { devPath } = devUrlInfo;

View File

@ -93,10 +93,8 @@ async function webpackCompiler(options: {
}),
);
const debounceCompile = debounce(() => {
serverCompilerPlugin?.buildResult?.rebuild();
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)/,