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

feat: support use name define mpa page name #3906

Merged
merged 4 commits into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion packages/build-app-helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@
"fs-extra": "^8.1.0",
"lodash": "^4.17.20"
}
}
}
38 changes: 28 additions & 10 deletions packages/build-app-helpers/src/getMpaEntries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,43 @@ interface IOptions {
export default function (api, options?: IOptions) {
const { target, appJsonPath } = options || {};
if (appJsonPath) {
return getEntriesByJson(target, appJsonPath);
return getEntriesByJson(api, target, appJsonPath);
}
return getEntriesByDir(api);
}

function getEntriesByJson(target, appJsonPath) {
function getEntriesByJson(api, target, appJsonPath) {
const {
context: { rootDir },
} = api;
const routes = getRoutesByAppJson(target, { appJsonPath });
return routes.map((route) => {
const dir = path.dirname(route.source);
const pageName = path.parse(dir).name;
let pageName;
let entryName;
if (route.name) {
entryName = route.name;
pageName = route.name;
} else {
const dir = path.dirname(route.source);
pageName = path.parse(dir).name;
entryName = pageName.toLocaleLowerCase();
}
return {
path: route.path,
entryName: pageName.toLowerCase(),
entryPath: getPageEntryByAppJson(path.resolve(rootDir, 'src', route.source), route.source),
entryName,
pageName,
entryPath: route.source.replace(/\/?pages/, ''),
};
});
}

function getPageEntryByAppJson(absolutePath, source) {
const targetExt = ['ts', 'tsx', 'js', 'jsx'].find(ext => fs.existsSync(`${path.join(absolutePath)}.${ext}`));
if (!targetExt) {
throw new Error(`Cannot find target file ${absolutePath}.`);
}
return `${source}.${targetExt}`;
}

function getEntriesByDir(api: any) {
const {
context: { rootDir },
Expand All @@ -43,18 +61,18 @@ function getEntriesByDir(api: any) {

const entries = pages.map((pageName) => {
const entryName = pageName.toLocaleLowerCase();
const pageEntry = getPageEntry(pagesPath, pageName);
const pageEntry = getPageEntryByDir(pagesPath, pageName);
if (!pageEntry) return null;
return {
entryName,
pageName,
entryPath: `${pageName}/${pageEntry}`,
entryPath: `pages/${pageName}/${pageEntry}`,
};
}).filter(Boolean);
return entries;
}

function getPageEntry(pagesPath: string, pageName: string) {
function getPageEntryByDir(pagesPath: string, pageName: string) {
const pagePath = path.join(pagesPath, pageName);
const pageRootFiles = fs.readdirSync(pagePath);
const appRegexp = /^app\.(t|j)sx?$/;
Expand Down
3 changes: 2 additions & 1 deletion packages/build-app-helpers/src/getRoutesByAppJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import * as fs from 'fs-extra';
interface IRoute {
targets?: string[];
source: string;
path: string;
path?: string;
name?: string;
}
interface IStaticConfig {
routes: IRoute[];
Expand Down
1 change: 0 additions & 1 deletion packages/build-app-helpers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ export { default as formatPath } from './formatPath';
export { default as validation } from './validation';
export { default as unionBy } from './unionBy';
export { default as injectTransformRuntime } from './injectTransformRuntime';

2 changes: 1 addition & 1 deletion packages/build-mpa-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
"loader-utils": "^2.0.0",
"@builder/app-helpers": "^1.0.2"
}
}
}
3 changes: 2 additions & 1 deletion packages/build-mpa-config/src/generateEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ function generateEntry({ framework, type, targetDir, pageEntry, entryName }) {
// eslint-disable-next-line
const entryCode = require(`./template/${framework}`).default({ type, resourcePath: `${formatPath(pageEntry)}`});
const entryFolder = path.join(targetDir, 'mpaEntry');
ensureDirSync(entryFolder);
const entryPath = path.join(entryFolder, `${entryName}.tsx`);
const entryDir = path.dirname(entryPath);
ensureDirSync(entryDir);
writeFileSync(path.join(entryFolder, `${entryName}.tsx`), entryCode);
return entryPath;
}
Expand Down
7 changes: 3 additions & 4 deletions packages/build-mpa-config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import generateEntry from './generateEntry';

interface IEntries {
entryName: string;
pageName: string;
entryPath: string;
}

Expand Down Expand Up @@ -32,7 +31,7 @@ export const generateMPAEntries = (options: IConfigOptions) => {
const parsedEntries = {};
entries.forEach((entry) => {
const { entryName, entryPath } = entry;
const pageEntry = path.join(rootDir, 'src/pages', entryPath);
const pageEntry = path.join(rootDir, 'src', entryPath);
const useOriginEntry = /app\.(t|j)sx?$/.test(entryPath) || type === 'node';
// icejs will config entry by api modifyUserConfig

Expand Down Expand Up @@ -64,11 +63,11 @@ const setMPAConfig = (config, options: IConfigOptions) => {
const matchStrs = [];

Object.keys(parsedEntries).forEach((entryKey) => {
const { entryName, pageName, finalEntry } = parsedEntries[entryKey];
const { entryName, entryPath, finalEntry } = parsedEntries[entryKey];
config.entry(entryName).add(finalEntry);

// get page paths for rule match
const matchStr = `src/pages/${pageName}`;
const matchStr = `src/${entryPath}`;
matchStrs.push(formatPath(matchStr));
});

Expand Down