Skip to content

Commit

Permalink
feat: remove routesParsing node
Browse files Browse the repository at this point in the history
BREAKING CHANGE: remove `routesParsing` node
  • Loading branch information
MatthewPattell committed Jan 9, 2025
1 parent db256c2 commit d78a886
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 456 deletions.
122 changes: 24 additions & 98 deletions __mocks__/route-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const routes: TRouteObject[] = [
export default routes;
`;

const routesCode1After = `import n from '@lomray/vite-ssr-boost/helpers/import-route';
const routesCode1After = `import n from "@lomray/vite-ssr-boost/helpers/import-route";
import type { TRouteObject } from '@lomray/vite-ssr-boost/interfaces/route-object';
import AppLayout from '@components/layouts/app';
import NotFound from '@pages/not-found';
Expand All @@ -80,27 +80,27 @@ const routes: TRouteObject[] = [
children: [
{
index: true,
lazy: n(() => import('@pages/home'),'@pages/home')
lazy: n(() => import('@pages/home')), pathId: "@pages/home"
},
{
path: RouteManager.path('details'),
children: DetailsRoutes
},
{
path: RouteManager.path('errorBoundary'),
lazy: n(() => import('@pages/error-boundary'),'@pages/error-boundary')
lazy: n(() => import('@pages/error-boundary')), pathId: "@pages/error-boundary"
},
{
path: RouteManager.path('nestedSuspense'),
lazy: n(() => import('@pages/nested-suspense'),'@pages/nested-suspense')
lazy: n(() => import('@pages/nested-suspense')), pathId: "@pages/nested-suspense"
},
{
path: RouteManager.path('redirect'),
lazy: n(() => import('@pages/redirect'),'@pages/redirect')
lazy: n(() => import('@pages/redirect')), pathId: "@pages/redirect"
},
{
path: RouteManager.path('redirect'),
lazy: n(() => import('@pages/redirect'),'@pages/redirect')
lazy: n(() => import('@pages/redirect')), pathId: "@pages/redirect"
},
{
path: RouteManager.path('notLazy'),
Expand Down Expand Up @@ -131,18 +131,16 @@ import RouteManager from '@services/route-manager';
* Application routes
*/
const routes: TRouteObject[] = [
{
path: RouteManager.path('notLazy'),
element: <NotLazyPage />,
},
{ path: RouteManager.path('notLazy'), element: <NotLazyPage /> },
{ element: <NotLazyPage /> },
{ element: <NotLazyPage />, path: RouteManager.path('notLazy') },
{ Component: NotLazyPage },
];
{
path: RouteManager.path('notLazy'),
element: <NotLazyPage />
},
{ path: RouteManager.path('notLazy'), element: <NotLazyPage /> },
{ element: <NotLazyPage /> },
{ element: <NotLazyPage />, path: RouteManager.path('notLazy') },
{ Component: NotLazyPage }];
export default routes;
`;
export default routes;`;

const routesCode2After = `
import type { TRouteObject } from '@lomray/vite-ssr-boost/interfaces/route-object';
Expand All @@ -162,7 +160,6 @@ const routes: TRouteObject[] = [
{ element: <NotLazyPage />, pathId: "@pages/not-lazy", path: RouteManager.path('notLazy') },
{ Component: NotLazyPage, pathId: "@pages/not-lazy" }];
export default routes;`;

const routesCode3Before = `
Expand Down Expand Up @@ -216,7 +213,7 @@ const routes: TRouteObject[] = [
export default routes;
`;

const routesCodeLazyAfter = `import n from '@lomray/vite-ssr-boost/helpers/import-route';
const routesCodeLazyAfter = `import n from "@lomray/vite-ssr-boost/helpers/import-route";
import RouteManager from '@services/route-manager';
/**
Expand All @@ -231,21 +228,20 @@ const routes: TRouteObject[] = [
export default routes;`;

const routesCodeLazyAfterClean = `import n from '@lomray/vite-ssr-boost/helpers/import-route';
const routesCodeLazyAfterClean = `import n from "@lomray/vite-ssr-boost/helpers/import-route";
import RouteManager from '@services/route-manager';
/**
* Application routes
*/
const routes: TRouteObject[] = [
{
path: RouteManager.path('errorBoundary'),
lazy: n(() => import('@pages/error-boundary')),
},
];
{
path: RouteManager.path('errorBoundary'),
lazy: n(() => import('@pages/error-boundary'))
}];
export default routes;
`;
export default routes;`;

const routesCode4Before = `
import type { TRouteObject } from '@lomray/vite-ssr-boost/interfaces/route-object';
Expand Down Expand Up @@ -278,7 +274,7 @@ const routes: TRouteObject[] = [
export default routes;
`;

const routesCode4After = `import n from '@lomray/vite-ssr-boost/helpers/import-route';
const routesCode4After = `import n from "@lomray/vite-ssr-boost/helpers/import-route";
import type { TRouteObject } from '@lomray/vite-ssr-boost/interfaces/route-object';
import { lazy } from 'react';
import AppLayout from '@components/layouts/app';
Expand Down Expand Up @@ -308,74 +304,6 @@ const routes: TRouteObject[] = [
export default routes;`;

const compiledRoutesCode1Before = `{
ErrorBoundary: NotFound,
Component: AppLayout,
children: [
{
index: true,
lazy: e$2(() => __vitePreload(() => import("./index-ojFAmQK4.js"), true ? __vite__mapDeps([7,8]) : void 0), "@pages/home")
},
{
path: manager.path("details"),
children: detailsRoutes
},
{
path: manager.path("errorBoundary"),
lazy: e$2(() => __vitePreload(() => import("./index-DAh3GXwI.js"), true ? __vite__mapDeps([9,1,5]) : void 0), "@pages/error-boundary")
},
{
path: manager.path("nestedSuspense"),
lazy: e$2(() => __vitePreload(() => import("./index-BlcC__XX.js"), true ? __vite__mapDeps([10,1,2]) : void 0), "@pages/nested-suspense")
},
{
path: manager.path("redirect"),
lazy:Jr(()=>cr(()=>import("./index-B19FHlDU.js"),__vite__mapDeps([4,1,5,2,6])),"@pages/details/user")
},
{
path: manager.path("notLazy"),
Component: NotLazy
}
]
}
// minified
lazy:Jr(()=>cr(()=>import("./index-B19FHlDU.js"),__vite__mapDeps([4,1,5,2,6])),"@pages/details/user")itePreload(() => import("./index-BlcC_
`;

const compiledRoutesCode1After = `{
ErrorBoundary: NotFound,
Component: AppLayout,
children: [
{
index: true,
lazy: e$2(() => __vitePreload(() => import("./index-ojFAmQK4.js"), true ? __vite__mapDeps([7,8]) : void 0))
},
{
path: manager.path("details"),
children: detailsRoutes
},
{
path: manager.path("errorBoundary"),
lazy: e$2(() => __vitePreload(() => import("./index-DAh3GXwI.js"), true ? __vite__mapDeps([9,1,5]) : void 0))
},
{
path: manager.path("nestedSuspense"),
lazy: e$2(() => __vitePreload(() => import("./index-BlcC__XX.js"), true ? __vite__mapDeps([10,1,2]) : void 0))
},
{
path: manager.path("redirect"),
lazy:Jr(()=>cr(()=>import("./index-B19FHlDU.js"),__vite__mapDeps([4,1,5,2,6])))
},
{
path: manager.path("notLazy"),
Component: NotLazy
}
]
}
// minified
lazy:Jr(()=>cr(()=>import("./index-B19FHlDU.js"),__vite__mapDeps([4,1,5,2,6])))itePreload(() => import("./index-BlcC_
`;

const routesDetailsCode = `
import type { TRouteObject } from '@lomray/vite-ssr-boost/interfaces/route-object';
import RouteManager from '@services/route-manager';
Expand Down Expand Up @@ -440,8 +368,6 @@ export {
routesCodeLazyBefore,
routesCodeLazyAfter,
routesCodeLazyAfterClean,
compiledRoutesCode1Before,
compiledRoutesCode1After,
routesDetailsCode,
routesCode5Before,
routesCode5After,
Expand Down
6 changes: 2 additions & 4 deletions __tests__/helpers/import-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ describe('importRoute', () => {
isDefaultExport ? { default: { Component, ...props } } : { Component, ...props },
)) as unknown as IDynamicRoute;

it('should import dynamic route and return an IAsyncRoute object with Component and pathId', async () => {
const id = 'routeId';
const result = await importRoute(getDynamicRoute(), id)();
it('should import dynamic route and return an IAsyncRoute object with Component', async () => {
const result = await importRoute(getDynamicRoute())();

expect(result.Component).to.be.a('function');
expect(result.Component).to.equal(Component);
expect(result.pathId).to.equal(id);
});

it('should handle dynamic route with additional properties', async () => {
Expand Down
63 changes: 0 additions & 63 deletions __tests__/helpers/sst-meta.ts

This file was deleted.

48 changes: 5 additions & 43 deletions __tests__/plugins/normalize-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ describe('normalizeRoute', () => {
normalizeRoute(...params).transform as TSimpleTransform;

it('should return routes with injected pathId: lazy, Component', () => {
const result = getTransform({ isSSR: true, isNodeParsing: true })(
routesCode1Before,
allowedFileId,
);
const result = getTransform({ isSSR: true })(routesCode1Before, allowedFileId);

expect(result?.code).to.equal(routesCode1After);
});
Expand All @@ -60,19 +57,19 @@ describe('normalizeRoute', () => {
});

it('should return routes with injected pathId: element,Component', () => {
const result = getTransform()(routesCode2Before, allowedFileId);
const result = getTransform({ isSSR: true })(routesCode2Before, allowedFileId);

expect(result?.code).to.equal(routesCode2After);
});

it('should return routes with injected pathId formatting: element,Component', () => {
const result = getTransform()(routesCode3Before, allowedFileId);
const result = getTransform({ isSSR: true })(routesCode3Before, allowedFileId);

expect(result?.code).to.equal(routesCode3After);
});

it('should return original routes', () => {
const result = getTransform()(routesCode4Before, allowedFileId);
const result = getTransform({ isSSR: true })(routesCode4Before, allowedFileId);

expect(result?.code).to.equal(routesCode4After);
});
Expand Down Expand Up @@ -109,38 +106,6 @@ const routes = [
expect(result?.code).to.equal(code);
});

it('should set config & get transformed route & write metadata', () => {
const writeFileSyncStub = sandbox.stub(fs, 'writeFileSync');
const plugin = normalizeRoute({ isSSR: true, isNodeParsing: true });
const bundle = {
'/assets/index-JDj23ja.js': {
type: 'chunk',
modules: { [allowedFileId]: 'test' },
},
'/assets/index-Jx9999.js': {
type: 'chunk',
modules: { '/': 'test' },
},
};

// @ts-expect-error ignore error, we know config type
plugin.transform(routesCode1Before, allowedFileId);
// @ts-expect-error ignore error, we know config type
plugin.generateBundle?.({}, bundle);
// @ts-expect-error ignore error, we know config type
plugin.config?.({ root: '/src', build: { outDir: '/build/client' } }, { isSsrBuild: false });
// @ts-expect-error ignore error, we know config type
plugin.writeBundle?.();

const [, data] = writeFileSyncStub.firstCall.args;

expect(JSON.parse(data as string)).to.deep.equal({
routeFiles: {
[allowedFileId]: Object.keys(bundle)[0],
},
});
});

it('should ignore set config & write metadata', () => {
const writeFileSyncStub = sandbox.stub(fs, 'writeFileSync');
const plugin = normalizeRoute({ isSSR: true });
Expand All @@ -154,10 +119,7 @@ const routes = [
});

it('should return routes with injected pathId: Components has JSX props', () => {
const result = getTransform({ isSSR: true, isNodeParsing: true })(
routesCode5Before,
allowedFileId,
);
const result = getTransform({ isSSR: true })(routesCode5Before, allowedFileId);

expect(result?.code).to.equal(routesCode5After);
});
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"lint:format": "eslint --fix \"src/**/*.{ts,tsx,*.ts,*tsx}\"",
"ts:check": "tsc --project ./tsconfig.json --skipLibCheck --noemit",
"test": "vitest run",
"test:coverage": "vitest run --coverage",
"prepare": "husky"
},
"dependencies": {
Expand Down
Loading

0 comments on commit d78a886

Please sign in to comment.