Skip to content

Commit

Permalink
fix: import identifier is invalid in route config (#6265)
Browse files Browse the repository at this point in the history
* fix: import identifier is invalid in route config

* chore: changeset
  • Loading branch information
luhc228 authored May 24, 2023
1 parent a4b8514 commit f652be7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-tomatoes-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ice/app': patch
---

fix: import identifier is invalid in route config
1 change: 1 addition & 0 deletions examples/routes-generate/ice.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default defineConfig(() => ({
ignoreFiles: ['about.tsx', 'products.tsx'],
defineRoutes: (route) => {
route('/about-me', 'about.tsx');
route('/about.html', 'about.tsx');

route('/', 'layout.tsx', () => {
route('/product', 'products.tsx');
Expand Down
6 changes: 5 additions & 1 deletion examples/routes-generate/src/pages/about.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import * as React from 'react';
import { Link } from 'ice';
import { Link, definePageConfig } from 'ice';

export default function About() {
return <><h2>About</h2><Link to="/">home</Link></>;
}

export const pageConfig = definePageConfig(() => ({

}));
8 changes: 6 additions & 2 deletions packages/ice/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function getRoutesDefinition(nestRouteManifest: NestedRouteManifest[], la
if (lazy) {
loadStatement = `import(/* webpackChunkName: "p_${componentName}" */ '${formatPath(componentPath)}')`;
} else {
const routeSpecifier = id.replace(/[./-]/g, '_').replace(/[:*]/g, '$');
const routeSpecifier = formatRouteSpecifier(id);
routeImports.push(`import * as ${routeSpecifier} from '${formatPath(componentPath)}';`);
loadStatement = routeSpecifier;
}
Expand Down Expand Up @@ -145,7 +145,7 @@ function generateRouteConfig(
const componentFile = file.replace(new RegExp(`${fileExtname}$`), '');
const componentPath = path.isAbsolute(componentFile) ? componentFile : `@/pages/${componentFile}`;

const loaderName = `${exportKey}_${id}`.replace(/[-/]/g, '_');
const loaderName = formatRouteSpecifier(`${exportKey}_${id}`);
const fullPath = path.join(parentPath, routePath);
imports.push([id, loaderName, fullPath]);
str = `import { ${exportKey} as ${loaderName} } from '${componentPath}';\n`;
Expand All @@ -162,3 +162,7 @@ function generateRouteConfig(
}
return template(importConfig(routes, ''), imports);
}

function formatRouteSpecifier(routeId: string) {
return routeId.replace(/[./-]/g, '_').replace(/[:*]/g, '$');
}

0 comments on commit f652be7

Please sign in to comment.