-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
163 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import fs from 'fs'; | ||
import type { Config as TnfConfig } from '../config'; | ||
import { writeClientEntry } from './write_client_entry'; | ||
import { writeGlobalStyle } from './write_global_style'; | ||
import { writeRouteTree } from './write_route_tree'; | ||
import { writeTailwindcss } from './write_tailwindcss'; | ||
|
||
interface BaseOptions { | ||
cwd: string; | ||
tmpPath: string; | ||
config?: TnfConfig; | ||
mode: 'development' | 'production'; | ||
} | ||
|
||
export interface SyncOptions extends BaseOptions { | ||
runAgain?: boolean; | ||
} | ||
|
||
export async function sync(opts: SyncOptions) { | ||
const { tmpPath } = opts; | ||
|
||
fs.rmSync(tmpPath, { recursive: true, force: true }); | ||
fs.mkdirSync(tmpPath, { recursive: true }); | ||
|
||
await writeRouteTree(opts); | ||
const globalStyleImportPath = writeGlobalStyle(opts); | ||
const tailwindcssPath = await writeTailwindcss(opts); | ||
writeClientEntry({ | ||
opts, | ||
globalStyleImportPath, | ||
tailwindcssPath, | ||
}); | ||
|
||
console.log('Synced'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import fs from 'fs'; | ||
import path from 'pathe'; | ||
import type { SyncOptions } from './sync'; | ||
|
||
export function writeClientEntry({ | ||
opts, | ||
globalStyleImportPath, | ||
tailwindcssPath, | ||
}: { | ||
opts: SyncOptions; | ||
globalStyleImportPath: string; | ||
tailwindcssPath: string | undefined; | ||
}) { | ||
const { tmpPath, config } = opts; | ||
|
||
fs.writeFileSync( | ||
path.join(tmpPath, 'client.tsx'), | ||
` | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom/client'; | ||
import { | ||
RouterProvider, | ||
createRouter, | ||
} from '@umijs/tnf/router'; | ||
import { routeTree } from './routeTree.gen'; | ||
${globalStyleImportPath} | ||
${tailwindcssPath ? `import '${tailwindcssPath}'` : ''} | ||
const router = createRouter({ | ||
routeTree, | ||
defaultPreload: ${config?.router?.defaultPreload ? `'${config.router.defaultPreload}'` : 'false'}, | ||
defaultPreloadDelay: ${config?.router?.defaultPreloadDelay || 50}, | ||
}); | ||
declare module '@tanstack/react-router' { | ||
interface Register { | ||
router: typeof router | ||
} | ||
} | ||
const TanStackRouterDevtools = | ||
process.env.NODE_ENV === 'production' | ||
? () => null | ||
: React.lazy(() => | ||
import('@tanstack/router-devtools').then((res) => ({ | ||
default: res.TanStackRouterDevtools, | ||
})), | ||
) | ||
ReactDOM.createRoot(document.getElementById('root')!).render( | ||
<> | ||
<RouterProvider router={router} /> | ||
${ | ||
config?.router?.devtool !== false | ||
? `<TanStackRouterDevtools router={router} initialIsOpen=${config?.router?.devtool?.options?.initialIsOpen || '{false}'} position=${config?.router?.devtool?.options?.position || '"bottom-left"'} />` | ||
: '' | ||
} | ||
</> | ||
); | ||
`, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import fs from 'fs'; | ||
import path from 'pathe'; | ||
import type { SyncOptions } from './sync'; | ||
|
||
const supportedExtensions = ['.css', '.less']; | ||
|
||
export function getStyleImportPath(basePath: string) { | ||
const ext = supportedExtensions.find((ext) => | ||
fs.existsSync(path.join(basePath + ext)), | ||
); | ||
return ext ? `import '${basePath}${ext}';` : ''; | ||
} | ||
|
||
export function writeGlobalStyle(opts: SyncOptions) { | ||
const { cwd } = opts; | ||
const globalStylePath = path.join(cwd, 'src/global'); | ||
return getStyleImportPath(globalStylePath); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { type Config, generator } from '@tanstack/router-generator'; | ||
import path from 'pathe'; | ||
import type { SyncOptions } from './sync'; | ||
|
||
export async function writeRouteTree(opts: SyncOptions) { | ||
const { cwd, tmpPath } = opts; | ||
await generator({ | ||
routeFileIgnorePrefix: '-', | ||
routesDirectory: path.join(cwd, 'src/pages'), | ||
generatedRouteTree: path.join(tmpPath, 'routeTree.gen.ts'), | ||
quoteStyle: 'single', | ||
semicolons: false, | ||
disableTypes: false, | ||
addExtensions: false, | ||
disableLogging: false, | ||
disableManifestGeneration: false, | ||
apiBase: '/api', | ||
routeTreeFileHeader: [ | ||
'/* prettier-ignore-start */', | ||
'/* eslint-disable */', | ||
'// @ts-nocheck', | ||
'// noinspection JSUnusedGlobalSymbols', | ||
], | ||
routeTreeFileFooter: ['/* prettier-ignore-end */'], | ||
indexToken: 'index', | ||
routeToken: 'route', | ||
experimental: { | ||
enableCodeSplitting: true, | ||
}, | ||
} as Config); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { generateTailwindcss } from '../fishkit/tailwindcss'; | ||
import type { SyncOptions } from './sync'; | ||
|
||
export async function writeTailwindcss(opts: SyncOptions) { | ||
const { cwd, tmpPath, config, mode, runAgain } = opts; | ||
|
||
let tailwindcssPath: string | undefined; | ||
if (config?.tailwindcss && !runAgain) { | ||
tailwindcssPath = await generateTailwindcss({ | ||
cwd, | ||
tmpPath, | ||
mode, | ||
}); | ||
} | ||
return tailwindcssPath; | ||
} |