Skip to content

Commit

Permalink
feat: migrate analyze plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
caohuilin committed Dec 3, 2024
1 parent 9cd6465 commit 28c68e0
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 237 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import path from 'path';
import type { CliHooksRunner } from '@modern-js/core';
import type { Entrypoint } from '@modern-js/types';
import {
fs,
Expand All @@ -8,7 +7,8 @@ import {
ensureAbsolutePath,
findExists,
} from '@modern-js/utils';
import type { AppNormalizedConfig, AppTools, IAppContext } from '../../types';
import type { AppNormalizedConfig } from '../../types';
import type { AppToolsContext, AppToolsHooks } from '../../types/new';
import { getFileSystemEntry } from './getFileSystemEntry';
import { isSubDirOrEqual } from './utils';

Expand Down Expand Up @@ -51,8 +51,8 @@ const ifAlreadyExists = (
});

export const getBundleEntry = async (
hookRunners: CliHooksRunner<AppTools<'shared'>>,
appContext: IAppContext,
hooks: AppToolsHooks<'shared'>,
appContext: AppToolsContext<'shared'>,
config: AppNormalizedConfig<'shared'>,
) => {
const { appDirectory, packageName } = appContext;
Expand All @@ -61,7 +61,7 @@ export const getBundleEntry = async (

const defaults = disableDefaultEntries
? []
: await getFileSystemEntry(hookRunners, appContext, config);
: await getFileSystemEntry(hooks, appContext, config);

// merge entrypoints from user config with directory convention.
if (entries) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import fs from 'fs';
import path from 'path';
import type { CliHooksRunner } from '@modern-js/core';
import type { Entrypoint } from '@modern-js/types';
import {
JS_EXTENSIONS,
ensureAbsolutePath,
findExists,
} from '@modern-js/utils';
import type { AppNormalizedConfig, AppTools, IAppContext } from '../../types';
import type { AppNormalizedConfig } from '../../types';
import type { AppToolsContext, AppToolsHooks } from '../../types/new';
import { ENTRY_FILE_NAME, INDEX_FILE_NAME } from './constants';
import { isDefaultExportFunction } from './isDefaultExportFunction';

Expand All @@ -33,11 +33,11 @@ const hasServerEntry = (dir: string) =>
);

const isBundleEntry = async (
hookRunners: CliHooksRunner<AppTools<'shared'>>,
hooks: AppToolsHooks<'shared'>,
dir: string,
enableCustomEntry?: boolean,
) => {
const { entry } = await hookRunners.checkEntryPoint({
const { entry } = await hooks.checkEntryPoint.call({
path: dir,
entry: false,
});
Expand All @@ -52,7 +52,7 @@ const isBundleEntry = async (
};

const scanDir = async (
hookRunners: CliHooksRunner<AppTools<'shared'>>,
hooks: AppToolsHooks<'shared'>,
dirs: string[],
enableCustomEntry?: boolean,
): Promise<Entrypoint[]> => {
Expand All @@ -79,7 +79,7 @@ const scanDir = async (
}

const entryFile = (
await hookRunners.checkEntryPoint({
await hooks.checkEntryPoint.call({
path: dir,
entry: false,
})
Expand Down Expand Up @@ -120,8 +120,8 @@ const scanDir = async (
};

export const getFileSystemEntry = async (
hookRunners: CliHooksRunner<AppTools<'shared'>>,
appContext: IAppContext,
hooks: AppToolsHooks<'shared'>,
appContext: AppToolsContext<'shared'>,
config: AppNormalizedConfig<'shared'>,
): Promise<Entrypoint[]> => {
const { appDirectory } = appContext;
Expand All @@ -140,23 +140,23 @@ export const getFileSystemEntry = async (

if (fs.existsSync(src)) {
if (fs.statSync(src).isDirectory()) {
if (await isBundleEntry(hookRunners, src, enableCustomEntry)) {
return scanDir(hookRunners, [src], enableCustomEntry);
if (await isBundleEntry(hooks, src, enableCustomEntry)) {
return scanDir(hooks, [src], enableCustomEntry);
}
const dirs: string[] = [];
await Promise.all(
fs.readdirSync(src).map(async filename => {
const file = path.join(src, filename);
if (
fs.statSync(file).isDirectory() &&
(await isBundleEntry(hookRunners, file, enableCustomEntry)) &&
(await isBundleEntry(hooks, file, enableCustomEntry)) &&
!disabledDirs.includes(file)
) {
dirs.push(file);
}
}),
);
return scanDir(hookRunners, dirs, enableCustomEntry);
return scanDir(hooks, dirs, enableCustomEntry);
} else {
throw Error(`source.entriesDir accept a directory.`);
}
Expand Down
52 changes: 19 additions & 33 deletions packages/solutions/app-tools/src/plugins/analyze/getHtmlTemplate.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import path from 'path';
import type { Entrypoint, HtmlPartials, HtmlTemplates } from '@modern-js/types';
import { fs, findExists } from '@modern-js/utils';
import type {
AppNormalizedConfig,
AppTools,
IAppContext,
PluginAPI,
} from '../../types';
import type { AppNormalizedConfig } from '../../types';
import type { AppToolsContext, AppToolsHooks } from '../../types/new';
import { HTML_PARTIALS_EXTENSIONS, HTML_PARTIALS_FOLDER } from './constants';
import * as templates from './templates';

Expand Down Expand Up @@ -80,12 +76,12 @@ export const getModifyHtmlPartials = (
// generate html template for
export const getHtmlTemplate = async (
entrypoints: Entrypoint[],
api: PluginAPI<AppTools<'shared'>>,
hooks: AppToolsHooks<'shared'>,
{
appContext,
config,
}: {
appContext: IAppContext;
appContext: AppToolsContext<'shared'>;
config: AppNormalizedConfig<'shared'>;
},
) => {
Expand Down Expand Up @@ -115,27 +111,19 @@ export const getHtmlTemplate = async (
if (customIndexTemplate) {
htmlTemplates[entryName] = customIndexTemplate.file;
} else {
const hookRunners = api.useHookRunners();
const { partials } = await hookRunners.htmlPartials({
const getPartialInitValue = (position: PartialPosition) => {
const partial = findPartials(htmlDir, name, position);
return partial ? [partial.content] : [];
};
const partials: Record<keyof HtmlPartials, string[]> = {
top: getPartialInitValue(PartialPosition.TOP),
head: getPartialInitValue(PartialPosition.HEAD),
body: getPartialInitValue(PartialPosition.BODY),
};

await hooks.modifyHtmlPartials.call({
entrypoint,
partials: [
PartialPosition.TOP,
PartialPosition.HEAD,
PartialPosition.BODY,
].reduce<HtmlPartials>(
(previous, position) => {
const found = findPartials(htmlDir, name, position);
previous[position as keyof HtmlPartials] = found
? [found.content]
: [];
return previous;
},
{
top: [],
head: [],
body: [],
},
),
partials: getModifyHtmlPartials(partials),
});

const templatePath = path.resolve(
Expand All @@ -159,10 +147,8 @@ export const getHtmlTemplate = async (
}
}
}
// sync partialsByEntrypoint to context
api.setAppContext({
...api.useAppContext(),
return {
partialsByEntrypoint,
});
return htmlTemplates;
htmlTemplates,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
urlJoin,
} from '@modern-js/utils';
import type { AppNormalizedConfig } from '../../types';
import type { AppToolsContext } from '../../types/new';
import { isMainEntry } from '../../utils/routes';
import { walkDirectory } from './utils';

Expand Down Expand Up @@ -116,7 +117,7 @@ const applyRouteOptions = (
*/
const collectHtmlRoutes = (
entrypoints: Entrypoint[],
appContext: IAppContext,
appContext: AppToolsContext<'shared'>,
config: AppNormalizedConfig<'shared'>,
): ServerRoute[] => {
const {
Expand Down Expand Up @@ -200,7 +201,7 @@ const collectHtmlRoutes = (
* @returns Static public file routes.
*/
const collectStaticRoutes = (
appContext: IAppContext,
appContext: AppToolsContext<'shared'>,
config: AppNormalizedConfig<'shared'>,
): ServerRoute[] => {
const { appDirectory } = appContext;
Expand Down Expand Up @@ -241,7 +242,7 @@ export const getServerRoutes = (
appContext,
config,
}: {
appContext: IAppContext;
appContext: AppToolsContext<'shared'>;
config: AppNormalizedConfig<'shared'>;
},
): ServerRoute[] => [
Expand Down
Loading

0 comments on commit 28c68e0

Please sign in to comment.