Skip to content

Commit

Permalink
feat: Further improve typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Ackermann committed Aug 25, 2024
1 parent a70867f commit e69e83f
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 28 deletions.
155 changes: 155 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
}
},
"scripts": {
"build": "rimraf dist && rollup -c",
"build": "rimraf dist && rollup -c && tsc-alias",
"build:watch": "rollup -cw",
"test": "cross-env BABEL_ENV=test vitest run --coverage",
"format": "prettier --write .",
Expand Down Expand Up @@ -68,6 +68,7 @@
"rimraf": "^6.0.1",
"rollup": "^4.20.0",
"single-spa": "^6.0.1",
"tsc-alias": "^1.8.10",
"typescript": "^5.5.4",
"typescript-eslint": "^8.0.1",
"vitest": "^2.0.5",
Expand Down
21 changes: 11 additions & 10 deletions src/single-spa-vue.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Component, ComponentOptions, h as H } from "vue-demi";
import type {
import {
SingleSpaVueOpts,
SingleSpaOptsVue2,
SingleSpaOptsVue3,
Expand All @@ -10,11 +10,12 @@ import type {
InstanceVue2,
InstanceVue3,
VueLifecycles,
Props,
} from "@/types";
import type { AppProps, ParcelProps } from "single-spa";
import { AppProps } from "single-spa";

export default function singleSpaVue<ExtraProps>(
opts: SingleSpaVueOpts,
opts: SingleSpaVueOpts<ExtraProps>,
): VueLifecycles<ExtraProps> {
const isVue2 = (opts: SingleSpaVueOpts): opts is SingleSpaOptsVue2 => {
return (opts as SingleSpaOptsVue2).Vue !== undefined;
Expand Down Expand Up @@ -53,8 +54,8 @@ export default function singleSpaVue<ExtraProps>(
opts.createApp || (opts.Vue && opts.Vue.createApp);

const resolveAppOptions = async (
opts: SingleSpaVueOpts,
props: ExtraProps & AppProps,
opts: SingleSpaVueOpts<ExtraProps & Props>,
props: ExtraProps & Props,
): Promise<AppOptionsObject> => {
if (typeof opts.appOptions === "function") {
return (opts.appOptions as AppOptionsFunction)(opts, props);
Expand All @@ -64,9 +65,9 @@ export default function singleSpaVue<ExtraProps>(
};

const mount = async (
opts: SingleSpaVueOpts,
opts: SingleSpaVueOpts<ExtraProps & Props>,
mountedInstances: Record<string, Instance>,
props: ExtraProps & AppProps & ParcelProps,
props: ExtraProps & Props,
) => {
await Promise.resolve();
const instance: Instance = {};
Expand Down Expand Up @@ -172,7 +173,7 @@ export default function singleSpaVue<ExtraProps>(
}
};

const bootstrap = async (opts: SingleSpaVueOpts) => {
const bootstrap = async (opts: SingleSpaVueOpts<ExtraProps & Props>) => {
if (opts.loadRootComponent) {
const root = await opts.loadRootComponent();
return (opts.rootComponent = root);
Expand All @@ -182,7 +183,7 @@ export default function singleSpaVue<ExtraProps>(
};

const update = async (
opts: SingleSpaVueOpts,
opts: SingleSpaVueOpts<ExtraProps & Props>,
mountedInstances: Record<string, Instance>,
props: ExtraProps & AppProps,
) => {
Expand All @@ -208,7 +209,7 @@ export default function singleSpaVue<ExtraProps>(
};

const unmount = async (
opts: SingleSpaVueOpts,
opts: SingleSpaVueOpts<ExtraProps & Props>,
mountedInstances: Record<string, Instance>,
props: ExtraProps & AppProps,
) => {
Expand Down
36 changes: 20 additions & 16 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
CreateAppFunction,
Vue2,
} from "vue-demi";
import { AppProps, LifeCycleFn, ParcelProps } from "single-spa";
import { AppProps, CustomProps, LifeCycleFn, ParcelProps } from "single-spa";

export type Vue = typeof Vue2;

Expand All @@ -29,21 +29,25 @@ export interface BaseSingleSpaVueOptions {
rootComponent?: Component;
}

export type SingleSpaOptsVue2 = BaseSingleSpaVueOptions & {
vueVersion: 2;
appOptions: AppOptions;
Vue: Vue;
handleInstance?(app: Vue, props: Props): Promise<void> | void;
};

export type SingleSpaOptsVue3 = BaseSingleSpaVueOptions & {
vueVersion: 3;
appOptions: AppOptions;
createApp: CreateAppFunction<Element>;
handleInstance?(app: App, props: Props): Promise<void> | void;
};

export type SingleSpaVueOpts = SingleSpaOptsVue2 | SingleSpaOptsVue3;
export type SingleSpaOptsVue2<ExtraProps = CustomProps> =
BaseSingleSpaVueOptions & {
vueVersion: 2;
appOptions: AppOptions;
Vue: Vue;
handleInstance?(app: Vue, props: Props & ExtraProps): Promise<void> | void;
};

export type SingleSpaOptsVue3<ExtraProps = CustomProps> =
BaseSingleSpaVueOptions & {
vueVersion: 3;
appOptions: AppOptions;
createApp: CreateAppFunction<Element>;
handleInstance?(app: App, props: Props & ExtraProps): Promise<void> | void;
};

export type SingleSpaVueOpts<ExtraProps = CustomProps> =
| SingleSpaOptsVue2<ExtraProps>
| SingleSpaOptsVue3<ExtraProps>;

export interface BaseInstance {
domEl?: HTMLElement;
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"noUnusedParameters": true,
"noImplicitReturns": true,
"skipLibCheck": true,
"declarationDir": "dist",
"outDir": "dist",
"types": ["vite/client", "vitest/globals"],
"paths": {
"@/*": ["./src/*"]
Expand Down

0 comments on commit e69e83f

Please sign in to comment.