Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

See [https://github.com/ice-lab/icestark/releases](https://github.com/ice-lab/icestark/releases) for what has changed in each version of icestark.

## 2.7.0

- [feat] cache css by default. ([#373](https://github.com/ice-lab/icestark/issues/373))
- [feat] appHistory and <Link /> can both take state. ([#477](https://github.com/ice-lab/icestark/issues/477))
- [chore] narrow scope of `import`'s error. ([#466](https://github.com/ice-lab/icestark/issues/466))
- [chore] add missing props for lifecycles. ([#440](https://github.com/ice-lab/icestark/issues/440))

## 2.6.2

- [fix] avoid to append duplicate assets. ([#331](https://github.com/ice-lab/icestark/issues/331))
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ice/stark",
"version": "2.6.2",
"version": "2.7.0",
"description": "Icestark is a JavaScript library for multiple projects, Ice workbench solution.",
"scripts": {
"install:deps": "rm -rf node_modules && rm -rf ./packages/*/node_modules && yarn install && lerna exec -- npm install",
Expand Down
7 changes: 6 additions & 1 deletion packages/icestark-app/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# Changelog

## 1.5.0

- [feat] `appHistory` and `<Link />` can both take state. ([#478](https://github.com/ice-lab/icestark/pull/428))
- [fix] add missing props for `registerAppEnter` & `registerAppLeave`.

## 1.4.2

- [fix] Bind history to window when using `AppLink`. ([#428](https://github.com/ice-lab/icestark/pull/428))
- [fix] bind history to window when using `AppLink`. ([#428](https://github.com/ice-lab/icestark/pull/428))

## 1.4.1

Expand Down
3 changes: 1 addition & 2 deletions packages/icestark-app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ice/stark-app",
"version": "1.4.2",
"version": "1.5.0",
"description": "icestark-app is a JavaScript library for icestark, used by sub-application.",
"scripts": {
"build": "rm -rf lib && tsc",
Expand Down Expand Up @@ -42,7 +42,6 @@
"@types/jest": "^24.0.12",
"@types/node": "^12.0.0",
"codecov": "^3.4.0",
"eslint": "^5.16.0",
"husky": "^2.2.0",
"jest": "^24.7.1",
"stylelint": "^10.1.0",
Expand Down
27 changes: 23 additions & 4 deletions packages/icestark-app/src/AppLink.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import * as React from 'react';
import formatUrl from './util/formatUrl';

interface To {
/**
* A string representing the path link to
*/
pathname: string;
/**
* A string representing the url search to
*/
search?: string;
/**
* A string representing the url state to
*/
state?: object;
}

export type AppLinkProps = {
to: string;
to: string | To;
hashType?: boolean;
replace?: boolean;
message?: string;
Expand All @@ -11,12 +26,16 @@ export type AppLinkProps = {

const AppLink = (props: AppLinkProps) => {
const { to, hashType, replace, message, children, ...rest } = props;
const linkTo = formatUrl(to, hashType);

const _to = typeof to === 'object' ? `${to.pathname}${to.search ?? ''}` : to;
const _state = typeof to === 'object' ? to.state : {};

const linkTo = formatUrl(_to, hashType);
return (
<a
{...rest}
href={linkTo}
onClick={e => {
onClick={(e) => {
e.preventDefault();
// eslint-disable-next-line no-alert
if (message && window.confirm(message) === false) {
Expand All @@ -28,7 +47,7 @@ const AppLink = (props: AppLinkProps) => {
*/
const changeState = window.history[replace ? 'replaceState' : 'pushState'].bind(window);

changeState({}, null, linkTo);
changeState(_state ?? {}, null, linkTo);
}}
>
{children}
Expand Down
15 changes: 9 additions & 6 deletions packages/icestark-app/src/appHistory.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import formatUrl from './util/formatUrl';
import normalizeArgs from './util/normalizeArgs';

const appHistory = {
push: (url: string, hashType?: boolean) => {
push: (url: string, state?: object | boolean, hashType?: boolean) => {
const [_state, _hashType] = normalizeArgs(state, hashType);
window.history.pushState(
{},
_state ?? {},
null,
formatUrl(url, hashType),
formatUrl(url, _hashType),
);
},
replace: (url: string, hashType?: boolean) => {
replace: (url: string, state?: object | boolean, hashType?: boolean) => {
const [_state, _hashType] = normalizeArgs(state, hashType);
window.history.replaceState(
{},
_state ?? {},
null,
formatUrl(url, hashType),
formatUrl(url, _hashType),
);
},
};
Expand Down
2 changes: 2 additions & 0 deletions packages/icestark-app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export { default as appHistory } from './appHistory';
export { default as isInIcestark } from './isInIcestark';
export { default as AppLink } from './AppLink';
export { default as setLibraryName } from './setLibraryName';

export type { LifecycleProps } from './registerAppEnter';
7 changes: 6 additions & 1 deletion packages/icestark-app/src/registerAppEnter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { setCache } from './cache';

export default (callback?: () => void): void => {
export interface LifecycleProps {
container: HTMLElement | string;
customProps?: object;
}

export default (callback?: (props: LifecycleProps) => void): void => {
if (!callback) return;

if (typeof callback !== 'function') {
Expand Down
3 changes: 2 additions & 1 deletion packages/icestark-app/src/registerAppLeave.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { setCache } from './cache';
import type { LifecycleProps } from './registerAppEnter';

export default (callback?: () => void): void => {
export default (callback?: (props: LifecycleProps) => void): void => {
if (!callback) return;

if (typeof callback !== 'function') {
Expand Down
16 changes: 16 additions & 0 deletions packages/icestark-app/src/util/normalizeArgs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// `hashType' was relocated to the third argument.
const isDev = process.env.NODE_ENV === 'development';

const normalizeArgs = (state?: object | boolean, hashType?: boolean): [object, boolean] => {
if (typeof state === 'boolean') {
isDev && console.warn('[icestark]: hashType was relocated to the third argument.');
return [{}, hashType ?? state];
}
if (typeof state === 'object') {
return [state, hashType];
}

return [{}, hashType];
};

export default normalizeArgs;
17 changes: 16 additions & 1 deletion packages/icestark-app/tests/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '../src/index';
import { setCache, getCache } from '../src/cache';
import formatUrl from '../src/util/formatUrl';
import normalizeArgs from '../src/util/normalizeArgs';

const namespace = 'ICESTARK';

Expand Down Expand Up @@ -146,4 +147,18 @@ describe('formatUrl', () => {

expect(formatUrl('/seller', true)).toBe('#/seller');
})
})
});

describe('normalizeArgs', () => {
test('normalizeArgs', () => {
expect(normalizeArgs(true)).toEqual([{}, true]);
expect(normalizeArgs(false, true)).toEqual([{}, true]);

expect(normalizeArgs({ framework: 'icestark' })).toEqual([{ framework: 'icestark' }, undefined]);
expect(normalizeArgs({ framework: 'icestark' }, true)).toEqual([{ framework: 'icestark' }, true]);
expect(normalizeArgs({ framework: 'icestark' }, false)).toEqual([{ framework: 'icestark' }, false]);

expect(normalizeArgs()).toEqual([{}, undefined]);
expect(normalizeArgs(null)).toEqual([null, undefined]);
})
});
1 change: 0 additions & 1 deletion packages/icestark-data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"@types/jest": "^24.0.12",
"@types/node": "^12.0.0",
"codecov": "^3.4.0",
"eslint": "^5.16.0",
"husky": "^2.2.0",
"jest": "^24.7.1",
"stylelint": "^10.1.0",
Expand Down
4 changes: 2 additions & 2 deletions src/AppRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export default class AppRouter extends React.Component<AppRouterProps, AppRouter
ErrorComponent: ({ err }: { err: string | Error}) => <div>{ typeof err === 'string' ? err : err?.message }</div>,
LoadingComponent: <div>Loading...</div>,
NotFoundComponent: <div>NotFound</div>,
shouldAssetsRemove: () => true,
onAppEnter: () => {},
onAppLeave: () => {},
onLoadingApp: () => {},
Expand Down Expand Up @@ -93,7 +92,6 @@ export default class AppRouter extends React.Component<AppRouterProps, AppRouter
*/
const { shouldAssetsRemove, onAppEnter, onAppLeave, fetch, basename } = this.props;
start({
shouldAssetsRemove,
onAppLeave,
onAppEnter,
onLoadingApp: this.loadingApp,
Expand All @@ -102,7 +100,9 @@ export default class AppRouter extends React.Component<AppRouterProps, AppRouter
reroute: this.handleRouteChange,
fetch,
basename,
...(shouldAssetsRemove ? { shouldAssetsRemove } : {}),
});

this.setState({ started: true });
}

Expand Down
Loading