Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nitedani committed Jan 1, 2024
1 parent 6c3d143 commit 6ae0176
Show file tree
Hide file tree
Showing 15 changed files with 45 additions and 124 deletions.
4 changes: 3 additions & 1 deletion packages/angular-renderer-core/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { renderPage };

import 'zone.js';
import {
APP_BOOTSTRAP_LISTENER,
Expand Down Expand Up @@ -28,7 +30,7 @@ if (import.meta.env.PROD) {
enableProdMode();
}
let hydrated = false;
export const renderPage = async <T, U>({
const renderPage = async <T, U>({
page,
layout,
pageContext,
Expand Down
2 changes: 2 additions & 0 deletions packages/angular-renderer-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
//TODO: move this to vike-angular

export { wait, wait$ } from './shared/wait';
8 changes: 5 additions & 3 deletions packages/angular-renderer-core/src/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { renderToString };

import '@angular/compiler';
import '@angular/platform-server/init';
import 'zone.js/node';
Expand Down Expand Up @@ -40,7 +42,7 @@ if (import.meta.env.PROD) {
enableProdMode();
}

export const SSR_PAGE_PROPS = new InjectionToken<{
const SSR_PAGE_PROPS = new InjectionToken<{
pageProps: Record<string, unknown>;
page: Type<{}> | null;
layout: Type<{}> | null;
Expand All @@ -54,7 +56,7 @@ export const SSR_PAGE_PROPS = new InjectionToken<{
},
});

export const SSR_PAGE_PROPS_HOOK_PROVIDER: Provider = {
const SSR_PAGE_PROPS_HOOK_PROVIDER: Provider = {
provide: APP_BOOTSTRAP_LISTENER,
useFactory: (
appRef: ApplicationRef,
Expand Down Expand Up @@ -107,7 +109,7 @@ export interface RenderToStringOptions<T = any, U = any>
url?: string;
}

export const renderToString = async <T, U>({
const renderToString = async <T, U>({
page,
layout,
pageContext,
Expand Down
7 changes: 5 additions & 2 deletions packages/angular-renderer-core/src/shared/mountPage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
export { mountPage };
export type { LayoutComponent };

import { ComponentRef, Type, ViewContainerRef } from '@angular/core';

export interface LayoutComponent<U> {
interface LayoutComponent<U> {
page: ViewContainerRef;
}

export const mountPage = <T, U>({
const mountPage = <T, U>({
compRef,
page,
layout,
Expand Down
8 changes: 6 additions & 2 deletions packages/angular-renderer-core/src/shared/wait.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
//TODO: move this to vike-angular

export { wait, wait$ };

import { defer, from, ObservableInput, tap } from 'rxjs';

/** Delays the server-side render until the input resolves. */
export const wait = <T, O extends Promise<T>>(input: O) => {
const wait = <T, O extends Promise<T>>(input: O) => {
if (import.meta.env.SSR) {
const i = setTimeout(() => {}, 10000);
input.finally(() => clearTimeout(i));
Expand All @@ -11,7 +15,7 @@ export const wait = <T, O extends Promise<T>>(input: O) => {
};

/** Delays the server-side render until the input resolves. */
export const wait$ = <T, O extends ObservableInput<T>>(input: O) => {
const wait$ = <T, O extends ObservableInput<T>>(input: O) => {
const obs = from(input);
if (import.meta.env.SSR) {
const i = setTimeout(() => {}, 10000);
Expand Down
5 changes: 3 additions & 2 deletions packages/vite-plugin-angular/src/plugin/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
export { angular };

import { Plugin } from 'vite';
import { DirImporterPlugin } from './plugins/dirImporterPlugin.js';
import { ConfigPlugin } from './plugins/configPlugin.js';
import { DevelopmentPlugin } from './plugins/devPlugin.js';
import { BuildPlugin } from './plugins/buildPlugin.js';

export function angular(): Plugin[] {
function angular(): Plugin[] {
const plugins = [
...ConfigPlugin,
DirImporterPlugin,
DevelopmentPlugin,
...BuildPlugin(),
];

return plugins;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
export { BuildOptimizerPlugin };

import angularApplicationPreset from '@angular-devkit/build-angular/src/tools/babel/presets/application.js';
import { loadEsmModule } from '@angular-devkit/build-angular/src/utils/load-esm.js';
import { transformAsync } from '@babel/core';
import { Plugin } from 'vite';
import { requiresLinking } from './utils.js';

export const BuildOptimizerPlugin: Plugin = {
const BuildOptimizerPlugin: Plugin = {
name: 'vite-plugin-angular-optimizer',
apply(config, env) {
return env.command === 'build';
Expand Down
4 changes: 3 additions & 1 deletion packages/vite-plugin-angular/src/plugin/plugins/devPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export { DevelopmentPlugin };

import type { Plugin } from 'vite';
import { swcTransform } from '../swc/transform.js';

export const DevelopmentPlugin: Plugin = {
const DevelopmentPlugin: Plugin = {
name: 'vite-plugin-angular-dev',
enforce: 'pre',
apply(_, env) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
export { DirImporterPlugin };

import { stat } from 'fs/promises';
import { relative, resolve } from 'path';
import { cwd } from 'process';
import { normalizePath, Plugin } from 'vite';

// Workaround for Node.js [ERR_UNSUPPORTED_DIR_IMPORT]
export const DirImporterPlugin: Plugin = {
const DirImporterPlugin: Plugin = {
name: 'vite-plugin-angular/dir-importer',
enforce: 'pre',
async resolveId(source, importer, options) {
Expand Down Expand Up @@ -38,11 +40,4 @@ export const DirImporterPlugin: Plugin = {
}
} catch {}
},
config() {
return {
ssr: {
noExternal: /apollo-angular/,
},
};
},
};
4 changes: 2 additions & 2 deletions packages/vite-plugin-angular/src/plugin/plugins/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { requiresLinking };

import * as wbl from '@angular-devkit/build-angular/src/tools/babel/webpack-loader.js';
import * as app from '@angular-devkit/build-angular/src/tools/babel/presets/application.js';

Expand All @@ -10,5 +12,3 @@ if (typeof (wbl as any)['requiresLinking'] !== 'undefined') {
} else if (typeof (app as any)['requiresLinking'] !== 'undefined') {
requiresLinking = (app as any)['requiresLinking'] as Function;
}

export { requiresLinking };
38 changes: 0 additions & 38 deletions packages/vite-plugin-angular/src/plugin/swc/visitors/compiler.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { AngularComponents, AngularComponentOptions };

import {
ArrayExpression,
CallExpression,
Expand Down Expand Up @@ -28,10 +30,10 @@ const isComponentDecorator = (decorator: Decorator) =>
decorator.expression.type === 'CallExpression' &&
(decorator.expression?.callee as Identifier).value === 'Component';

export interface AngularComponentOptions {
interface AngularComponentOptions {
sourceUrl: string;
}
export class AngularComponents extends Visitor {
class AngularComponents extends Visitor {
importFiles: {
url: string;
identifier: string;
Expand Down Expand Up @@ -100,7 +102,7 @@ export class AngularComponents extends Visitor {

if (extname(actualImportPath) !== '.html') {
throw new Error(
`HTML type ${extname(actualImportPath)} is not supported.`
`HTML type ${extname(actualImportPath)} is not supported.`,
);
}
const identifier = randomIdentifier();
Expand All @@ -110,7 +112,7 @@ export class AngularComponents extends Visitor {
});
return createKeyValueProperty(
createIdentifer('template'),
createIdentifer(identifier)
createIdentifer(identifier),
);
}

Expand All @@ -131,7 +133,7 @@ export class AngularComponents extends Visitor {
...prop,
key: createIdentifer('styles'),
value: createArrayExpression(
styles.map(c => createExpressionStatement(createIdentifer(c)))
styles.map(c => createExpressionStatement(createIdentifer(c))),
),
};
}
Expand Down
2 changes: 0 additions & 2 deletions packages/vite-plugin-angular/src/plugin/swc/visitors/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
export * from './compiler.js';
export * from './components.js';
export * from './injector.js';
export * from './swap-dynamic-import.js';
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { AngularInjector };

import {
Argument,
CallExpression,
Expand Down Expand Up @@ -43,7 +45,7 @@ function createCallExpression(
return object;
}

export class AngularInjector extends Visitor {
class AngularInjector extends Visitor {
private hasInjectorImport = false;
private hasInjectedConstructor = false;
private isAngularClass = false;
Expand Down

This file was deleted.

0 comments on commit 6ae0176

Please sign in to comment.