Skip to content

Commit

Permalink
feat: support cli code hmr
Browse files Browse the repository at this point in the history
  • Loading branch information
sanyuan0704 committed Sep 20, 2022
1 parent a96a3aa commit f071dd2
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion bin/island.js
@@ -1,2 +1,2 @@
#!/usr/bin/env node
import('../dist/cli.js');
import('../dist/node/cli.js');
2 changes: 1 addition & 1 deletion docs/.island/config.ts
@@ -1,4 +1,4 @@
import { defineConfig } from '../../dist';
import { defineConfig } from '../../dist/node';

export default defineConfig({
lang: 'en-US',
Expand Down
2 changes: 1 addition & 1 deletion src/node/cli.ts
@@ -1,7 +1,6 @@
import { resolve } from 'path';
import { cac } from 'cac';
import { build } from './build';
import { createDevServer } from './dev';
import { serve } from './serve';

const cli = cac('island').version('0.0.0').help();
Expand All @@ -12,6 +11,7 @@ cli
.action(async (root: string) => {
try {
const createServer = async () => {
const { createDevServer } = await import(`./dev.js?t=${Date.now()}`);
const server = await createDevServer(root, async () => {
await server.close();
await createServer();
Expand Down
13 changes: 9 additions & 4 deletions src/node/constants/index.ts
Expand Up @@ -4,7 +4,10 @@ export const isProduction = () => process.env.NODE_ENV === 'production';

export const TS_REGEX = /(c|m)?tsx?$/;

export const PACKAGE_ROOT_PATH = join(fileURLToPath(import.meta.url), '../..');
export const PACKAGE_ROOT_PATH = join(
fileURLToPath(import.meta.url),
'../../..'
);

export const CLIENT_RUNTIME_PATH = join(PACKAGE_ROOT_PATH, 'src/runtime');

Expand Down Expand Up @@ -39,10 +42,12 @@ export const DEFAULT_EXTERNALS: string[] = [
'react/jsx-runtime'
];

export const CLI_BUNDLE_OUTDIR = join(PACKAGE_ROOT_PATH, 'dist');
export const CLI_BUNDLE_OUTDIR = join(PACKAGE_ROOT_PATH, 'dist', 'node');

export const ISLAND_JSX_RUNTIME_PATH = CLI_BUNDLE_OUTDIR;
export const RUNTIME_BUNDLE_OUTDIR = join(PACKAGE_ROOT_PATH, 'dist', 'runtime');

export const ISLAND_CLI_PATH = join(PACKAGE_ROOT_PATH, DIST_PATH, 'cli.js');
export const ISLAND_JSX_RUNTIME_PATH = RUNTIME_BUNDLE_OUTDIR;

export const ISLAND_CLI_PATH = join(CLI_BUNDLE_OUTDIR, 'cli.js');

export const VENDOR_PATH = join(PACKAGE_ROOT_PATH, 'vendors');
5 changes: 2 additions & 3 deletions src/node/plugin-island/config.ts
Expand Up @@ -6,7 +6,6 @@ import {
ISLAND_CLI_PATH,
ISLAND_JSX_RUNTIME_PATH,
isProduction,
PACKAGE_ROOT_PATH,
ROUTE_PATH
} from '../constants';
import { Plugin } from 'vite';
Expand Down Expand Up @@ -82,9 +81,9 @@ export function pluginConfig(
// Restart when config file changes
async handleHotUpdate(ctx) {
const customWatchedFiles = [
...(config.configDeps || []),
ISLAND_CLI_PATH,
config.configPath,
ISLAND_CLI_PATH
...(config.configDeps || [])
];
if (customWatchedFiles.includes(ctx.file)) {
console.log(
Expand Down
1 change: 0 additions & 1 deletion src/node/plugin-mdx/rehypePlugins/preWrapper.ts
Expand Up @@ -16,7 +16,6 @@ export const rehypePluginPreWrapper: Plugin<[], import('hast').Root> = () => {
const codeNode = node.children[0];
const codeClassName = codeNode.properties?.className?.toString() || '';
const lang = codeClassName.split('-')[1];

if (!codeClassName || !lang) {
return;
}
Expand Down
2 changes: 0 additions & 2 deletions src/node/plugin-mdx/remarkPlugins/toc.ts
Expand Up @@ -30,13 +30,11 @@ export const remarkPluginToc: Plugin = () => {
toc.push({ id, text: originText, depth });
}
})(tree);

const insertedTocCode = `export const toc = ${JSON.stringify(
toc,
null,
2
)}`;

// Add toc ast to current ast tree
tree.children.push({
type: 'mdxjsEsm',
Expand Down
4 changes: 2 additions & 2 deletions src/node/plugin-routes/RouteService.ts
@@ -1,6 +1,6 @@
import fastGlob from 'fast-glob';
import fs from 'fs-extra';
import { CLI_BUNDLE_OUTDIR } from '../constants';
import { RUNTIME_BUNDLE_OUTDIR } from '../constants';
import path from 'path';

export interface RouteMeta {
Expand All @@ -19,7 +19,7 @@ export const normalizeRoutePath = (routePath: string) => {
};

const lazyWithPreloadRuntimeCode = fs.readFileSync(
path.join(CLI_BUNDLE_OUTDIR, 'lazyWithPreload.js'),
path.join(RUNTIME_BUNDLE_OUTDIR, 'lazyWithPreload.js'),
'utf-8'
);

Expand Down
1 change: 0 additions & 1 deletion src/runtime/app.tsx
Expand Up @@ -16,7 +16,6 @@ export async function waitForApp(path: string): Promise<PageData> {
const mod = await (matched[0].route as Route).preload();
const pagePath = cleanUrl((matched[0].route as Route).filePath);
const relativePagePath = getRelativePagePath(path, pagePath, siteData.base);
console.log('relativePagePath', relativePagePath);
return {
siteData,
pagePath,
Expand Down
13 changes: 9 additions & 4 deletions tsup.config.ts
Expand Up @@ -3,8 +3,8 @@ import { defineConfig } from 'tsup';
export default defineConfig([
{
entry: {
'jsx-runtime': 'src/runtime/island-jsx-runtime.js',
cli: 'src/node/cli.ts',
dev: 'src/node/dev.ts',
index: 'src/node/index.ts'
},
minifyIdentifiers: false,
Expand All @@ -13,17 +13,22 @@ export default defineConfig([
format: 'esm',
dts: true,
sourcemap: true,
splitting: false,
splitting: true,
keepNames: true,
minify: process.env.NODE_ENV === 'production',
skipNodeModulesBundle: true
skipNodeModulesBundle: true,
outDir: 'dist/node',
clean: true
},
{
entry: {
'jsx-runtime': 'src/runtime/island-jsx-runtime.js',
lazyWithPreload: 'src/runtime/lazyWithPreload.tsx'
},
format: 'esm',
dts: false,
minify: false
minify: false,
outDir: 'dist/runtime',
clean: true
}
]);

0 comments on commit f071dd2

Please sign in to comment.