-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
97 changed files
with
1,484 additions
and
1,129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
'@modern-js/runtime': patch | ||
'@modern-js/app-tools': patch | ||
'@modern-js/utils': patch | ||
--- | ||
|
||
feat: refactor app-tools command and analyze check entry point | ||
|
||
feat: 重构 app-tools 命令和 analyze 插件识别入口逻辑 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const APP_FILE_NAME = 'App'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import path from 'path'; | ||
import { JS_EXTENSIONS, findExists } from '@modern-js/utils'; | ||
import { APP_FILE_NAME } from './constants'; | ||
|
||
const hasApp = (dir: string) => | ||
findExists( | ||
JS_EXTENSIONS.map(ext => path.resolve(dir, `${APP_FILE_NAME}${ext}`)), | ||
); | ||
|
||
export const isRuntimeEntry = (dir: string) => hasApp(dir); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...nalyze/getClientRoutes/getRoutesLegacy.ts → ...i/code/getClientRoutes/getRoutesLegacy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
8 changes: 6 additions & 2 deletions
8
...ools/src/analyze/getClientRoutes/utils.ts → .../router/cli/code/getClientRoutes/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 187 additions & 0 deletions
187
packages/runtime/plugin-runtime/src/router/cli/code/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
import path from 'path'; | ||
import { | ||
fs, | ||
getEntryOptions, | ||
isRouterV5, | ||
isSSGEntry, | ||
isUseSSRBundle, | ||
logger, | ||
} from '@modern-js/utils'; | ||
import { IAppContext, PluginAPI } from '@modern-js/core'; | ||
import type { | ||
Entrypoint, | ||
Route, | ||
RouteLegacy, | ||
PageRoute, | ||
SSRMode, | ||
NestedRouteForCli, | ||
} from '@modern-js/types'; | ||
import { | ||
AppNormalizedConfig, | ||
AppTools, | ||
ImportStatement, | ||
} from '@modern-js/app-tools'; | ||
import { FILE_SYSTEM_ROUTES_FILE_NAME } from '../constants'; | ||
import * as templates from './templates'; | ||
import { getClientRoutes, getClientRoutesLegacy } from './getClientRoutes'; | ||
import { getServerLoadersFile, getServerCombinedModueFile } from './utils'; | ||
import { walk } from './nestedRoutes'; | ||
|
||
export const generateCode = async ( | ||
appContext: IAppContext, | ||
config: AppNormalizedConfig<'shared'>, | ||
entrypoints: Entrypoint[], | ||
api: PluginAPI<AppTools<'shared'>>, | ||
) => { | ||
const { | ||
internalDirectory, | ||
srcDirectory, | ||
internalDirAlias, | ||
internalSrcAlias, | ||
packageName, | ||
} = appContext; | ||
|
||
const hookRunners = api.useHookRunners(); | ||
|
||
const isV5 = isRouterV5(config); | ||
const getRoutes = isV5 ? getClientRoutesLegacy : getClientRoutes; | ||
const importsStatemets = new Map<string, ImportStatement[]>(); | ||
const oldVersion = | ||
typeof (config?.runtime.router as { oldVersion: boolean }) === 'object' | ||
? Boolean((config?.runtime.router as { oldVersion: boolean }).oldVersion) | ||
: false; | ||
|
||
await Promise.all(entrypoints.map(generateEntryCode)); | ||
|
||
return { | ||
importsStatemets, | ||
}; | ||
|
||
async function generateEntryCode(entrypoint: Entrypoint) { | ||
const { entryName, isMainEntry, isAutoMount, fileSystemRoutes } = | ||
entrypoint; | ||
if (isAutoMount) { | ||
// generate routes file for file system routes entrypoint. | ||
if (fileSystemRoutes) { | ||
let initialRoutes: (NestedRouteForCli | PageRoute)[] | RouteLegacy[] = | ||
[]; | ||
let nestedRoutes: NestedRouteForCli | NestedRouteForCli[] | null = null; | ||
if (entrypoint.entry) { | ||
initialRoutes = getRoutes({ | ||
entrypoint, | ||
srcDirectory, | ||
srcAlias: internalSrcAlias, | ||
internalDirectory, | ||
internalDirAlias, | ||
}); | ||
} | ||
if (!isV5 && entrypoint.nestedRoutesEntry) { | ||
nestedRoutes = await walk( | ||
entrypoint.nestedRoutesEntry, | ||
entrypoint.nestedRoutesEntry, | ||
{ | ||
name: internalSrcAlias, | ||
basename: srcDirectory, | ||
}, | ||
entrypoint.entryName, | ||
entrypoint.isMainEntry, | ||
oldVersion, | ||
); | ||
if (nestedRoutes) { | ||
// eslint-disable-next-line max-depth | ||
if (!Array.isArray(nestedRoutes)) { | ||
nestedRoutes = [nestedRoutes]; | ||
} | ||
// eslint-disable-next-line max-depth | ||
for (const route of nestedRoutes) { | ||
(initialRoutes as Route[]).unshift(route); | ||
} | ||
} | ||
} | ||
|
||
const { routes } = await hookRunners.modifyFileSystemRoutes({ | ||
entrypoint, | ||
routes: initialRoutes, | ||
}); | ||
|
||
const config = api.useResolvedConfigContext(); | ||
const ssr = getEntryOptions( | ||
entryName, | ||
isMainEntry, | ||
config.server.ssr, | ||
config.server.ssrByEntries, | ||
packageName, | ||
); | ||
const useSSG = isSSGEntry(config, entryName, entrypoints); | ||
|
||
let mode: SSRMode | undefined; | ||
if (ssr) { | ||
mode = typeof ssr === 'object' ? ssr.mode || 'string' : 'string'; | ||
} | ||
if (mode === 'stream') { | ||
const hasPageRoute = routes.some( | ||
route => 'type' in route && route.type === 'page', | ||
); | ||
if (hasPageRoute) { | ||
logger.error( | ||
'Streaming ssr is not supported when pages dir exists', | ||
); | ||
// eslint-disable-next-line no-process-exit | ||
process.exit(1); | ||
} | ||
} | ||
|
||
const { code } = await hookRunners.beforeGenerateRoutes({ | ||
entrypoint, | ||
code: await templates.fileSystemRoutes({ | ||
routes, | ||
ssrMode: useSSG ? 'string' : mode, | ||
nestedRoutesEntry: entrypoint.nestedRoutesEntry, | ||
entryName: entrypoint.entryName, | ||
internalDirectory, | ||
splitRouteChunks: config?.output?.splitRouteChunks, | ||
}), | ||
}); | ||
|
||
// extract nested router loaders | ||
if (entrypoint.nestedRoutesEntry && isUseSSRBundle(config)) { | ||
const routesServerFile = getServerLoadersFile( | ||
internalDirectory, | ||
entryName, | ||
); | ||
|
||
const code = templates.routesForServer({ | ||
routes: routes as (NestedRouteForCli | PageRoute)[], | ||
}); | ||
|
||
await fs.ensureFile(routesServerFile); | ||
await fs.writeFile(routesServerFile, code); | ||
} | ||
|
||
const serverLoaderCombined = templates.ssrLoaderCombinedModule( | ||
entrypoints, | ||
entrypoint, | ||
config, | ||
appContext, | ||
); | ||
if (serverLoaderCombined) { | ||
const serverLoaderFile = getServerCombinedModueFile( | ||
internalDirectory, | ||
entryName, | ||
); | ||
|
||
await fs.outputFile(serverLoaderFile, serverLoaderCombined); | ||
} | ||
|
||
fs.outputFileSync( | ||
path.resolve( | ||
internalDirectory, | ||
`./${entryName}/${FILE_SYSTEM_ROUTES_FILE_NAME}`, | ||
), | ||
code, | ||
'utf8', | ||
); | ||
} | ||
} | ||
} | ||
}; |
File renamed without changes.
7 changes: 3 additions & 4 deletions
7
...ons/app-tools/src/analyze/nestedRoutes.ts → ...ntime/src/router/cli/code/nestedRoutes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.