Skip to content

Commit

Permalink
export pageGlobSuffix
Browse files Browse the repository at this point in the history
  • Loading branch information
mrm007 committed Oct 13, 2022
1 parent fe850c7 commit c893cf5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
3 changes: 1 addition & 2 deletions packages/core/src/plugins/vite/page-roots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import path from 'path';
import type { Plugin } from 'vite';

import type { EnhancedConfig } from '../../config';

const pageGlobSuffix = '/**/*.page.tsx';
import { pageGlobSuffix } from '../../route-data';

const browserPageModules = '__BROWSER_PAGE_MODULES';
const nodePageModules = '__NODE_PAGE_MODULES';
Expand Down
23 changes: 14 additions & 9 deletions packages/core/src/route-data.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import path from 'path';

import { transformFileAsync as babelTransform } from '@babel/core';
import type { RouteData } from '@crackle/router';
import { build as esbuild } from 'esbuild';
Expand All @@ -7,11 +9,13 @@ import glob from 'fast-glob';
import type { PartialConfig } from './config';
import { getConfig } from './config';

export const pageGlobSuffix = '/**/*.page.tsx';

const routesEntryName = 'ROUTES_ENTRY';
const routeEntryNs = 'ROUTES_ENTRY_NAMESPACE';

const transformWithBabel = async (path: string) => {
const transformedContents = await babelTransform(path, {
const transformWithBabel = async (file: string) => {
const transformedContents = await babelTransform(file, {
plugins: [
[
'@crackle/babel-plugin-remove-exports',
Expand All @@ -25,7 +29,7 @@ const transformWithBabel = async (path: string) => {
});

if (!transformedContents?.code) {
throw new Error(`No result from babel plugin transform of ${path}`);
throw new Error(`No result from babel plugin transform of ${file}`);
}

return {
Expand All @@ -38,7 +42,7 @@ export const getAllRoutes = async (inlineConfig?: PartialConfig) => {
const { root, pageRoots } = getConfig(inlineConfig);

const pageFiles = await glob(
pageRoots.map((pageRoot) => `${pageRoot}/**/*.page.tsx`),
pageRoots.map((pageRoot) => path.join(pageRoot, pageGlobSuffix)),
{ cwd: root },
);

Expand Down Expand Up @@ -66,13 +70,14 @@ export const getAllRoutes = async (inlineConfig?: PartialConfig) => {
namespace: routeEntryNs,
}));

build.onLoad({ filter: new RegExp('.*.page.tsx$') }, ({ path }) =>
transformWithBabel(path),
// TODO: use pageGlobSuffix by compiling to RegExp

build.onLoad({ filter: new RegExp('.*.page.tsx$') }, (args) =>
transformWithBabel(args.path),
);

build.onLoad(
{ filter: new RegExp('.*src/pages/.*.tsx$') },
({ path }) => transformWithBabel(path),
build.onLoad({ filter: new RegExp('.*src/pages/.*.tsx$') }, (args) =>
transformWithBabel(args.path),
);

build.onLoad({ filter, namespace: routeEntryNs }, () => ({
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { clientEntry } from './constants';
import { logger } from './logger';
import { fixViteVanillaExtractDepScanPlugin } from './plugins/esbuild';
import { addPageRoots, stripRouteData } from './plugins/vite';
import { pageGlobSuffix } from './route-data';
import type { CrackleServer } from './types';
import { calculateTime } from './utils/timer';
import { commonViteConfig } from './vite-config';
Expand Down Expand Up @@ -48,7 +49,7 @@ export const start = async (
optimizeDeps: {
entries: [
...config.pageRoots.map((pageRoot) =>
path.join(pageRoot, '/**/*.page.tsx'),
path.join(pageRoot, pageGlobSuffix),
),
config.appShell,
],
Expand Down

0 comments on commit c893cf5

Please sign in to comment.