Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: runtime template windows path #5912

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/flat-bobcats-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@modern-js/runtime': patch
'@modern-js/utils': patch
---

fix: runtime template windows path

fix: 修复 runtime 模板 windows 路径不正确
7 changes: 4 additions & 3 deletions packages/runtime/plugin-runtime/src/cli/alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ export const builderPluginAlias = ({
name: 'runtime:alias',
setup(api) {
api.modifyRsbuildConfig((userConfig, { mergeRsbuildConfig }) => {
// const { entrypoints, internalDirectory } = api.useAppContext();
const entrypointsAlias: Record<string, string> = {};
// main entry alias need to be placed at the end of alias object
const mainEntrypointsAlias: Record<string, string> = {};
entrypoints.forEach(entrypoint => {
entrypointsAlias[
`@${metaName}/runtime/registry/${entrypoint.entryName}`
Expand All @@ -26,7 +27,7 @@ export const builderPluginAlias = ({
ENTRY_POINT_REGISTER_FILE_NAME,
);
if (entrypoint.isMainEntry) {
entrypointsAlias[`@${metaName}/runtime/registry`] = path.join(
mainEntrypointsAlias[`@${metaName}/runtime/registry`] = path.join(
internalDirectory,
entrypoint.entryName,
ENTRY_POINT_REGISTER_FILE_NAME,
Expand All @@ -35,7 +36,7 @@ export const builderPluginAlias = ({
});
return mergeRsbuildConfig(userConfig, {
source: {
alias: entrypointsAlias,
alias: { ...entrypointsAlias, ...mainEntrypointsAlias },
},
});
});
Expand Down
7 changes: 4 additions & 3 deletions packages/runtime/plugin-runtime/src/cli/template.server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { formatImportPath } from '@modern-js/utils';

const SERVER_ENTRY = `
import {
#render,
Expand Down Expand Up @@ -47,9 +49,8 @@ function genHandlerCode({
internalSrcAlias,
}: GenHandlerCodeOptions) {
if (customServerEntry) {
return `export { default as requestHandler } from '${customServerEntry.replace(
srcDirectory,
internalSrcAlias,
return `export { default as requestHandler } from '${formatImportPath(
customServerEntry.replace(srcDirectory, internalSrcAlias),
)}'`;
} else {
const serverEntry = transformServerEntry(SERVER_ENTRY, {
Expand Down
23 changes: 13 additions & 10 deletions packages/runtime/plugin-runtime/src/cli/template.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path';
import { JS_EXTENSIONS, findExists } from '@modern-js/utils';
import { JS_EXTENSIONS, findExists, formatImportPath } from '@modern-js/utils';
import type { RuntimePlugin } from '@modern-js/app-tools';
import {
ENTRY_POINT_RUNTIME_GLOBAL_CONTEXT_FILE_NAME,
Expand All @@ -24,15 +24,16 @@ const genRenderCode = ({
mountId?: string;
}) => {
if (customEntry) {
return `import '${entry.replace(srcDirectory, internalSrcAlias)}'`;
return `import '${formatImportPath(
entry.replace(srcDirectory, internalSrcAlias),
)}'`;
}
return `import { createRoot } from '@${metaName}/runtime/react';
import { render } from '@${metaName}/runtime/browser';
${
customBootstrap
? `import customBootstrap from '${customBootstrap.replace(
srcDirectory,
internalSrcAlias,
? `import customBootstrap from '${formatImportPath(
customBootstrap.replace(srcDirectory, internalSrcAlias),
)}';`
: ''
}
Expand Down Expand Up @@ -159,11 +160,13 @@ export const runtimeGlobalContext = ({

import App from '${
// We need to get the path of App.tsx here, but the entry is `src/entry.tsx`
customEntry
? entry
.replace('entry.tsx', 'App.tsx')
.replace(srcDirectory, internalSrcAlias)
: entry.replace(srcDirectory, internalSrcAlias)
formatImportPath(
customEntry
? entry
.replace('entry.tsx', 'App.tsx')
.replace(srcDirectory, internalSrcAlias)
: entry.replace(srcDirectory, internalSrcAlias),
)
}';

setGlobalContext({
Expand Down
12 changes: 7 additions & 5 deletions packages/runtime/plugin-runtime/src/router/cli/code/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
} from '@modern-js/types';
import {
findExists,
formatImportPath,
fs,
getEntryOptions,
isSSGEntry,
Expand Down Expand Up @@ -506,8 +507,10 @@ export const runtimeGlobalContext = async ({
});
const hasAppConfig = moduleExports.some(e => e.n === APP_CONFIG_NAME);
const hasAppInit = moduleExports.some(e => e.n === APP_INIT_EXPORTED);
const layoutPath = getPathWithoutExt(
replaceWithAlias(srcDirectory, rootLayoutFile, internalSrcAlias),
const layoutPath = formatImportPath(
getPathWithoutExt(
replaceWithAlias(srcDirectory, rootLayoutFile, internalSrcAlias),
),
);
if (hasAppConfig) {
imports.push(`import { config as appConfig } from '${layoutPath}';`);
Expand All @@ -527,9 +530,8 @@ export const runtimeGlobalContext = async ({

if (globalApp) {
imports.push(
`import layoutApp from '${globalApp.replace(
srcDirectory,
internalSrcAlias,
`import layoutApp from '${formatImportPath(
globalApp.replace(srcDirectory, internalSrcAlias),
)}';`,
);
} else {
Expand Down
4 changes: 4 additions & 0 deletions packages/toolkit/utils/src/cli/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ export const removeTailSlash = (s: string): string => s.replace(/\/+$/, '');

export const removeSlash = (s: string): string =>
removeLeadingSlash(removeTailSlash(s));

export function formatImportPath(str: string) {
return str.replace(/\\/g, '/');
}
Loading