Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚨 supplement lint config #101

Merged
merged 2 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 6 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ module.exports = {
rules: {
...fabric.eslint.rules,
'@typescript-eslint/prefer-interface': 0,
'no-return-assign': 0
'@typescript-eslint/no-explicit-any': 0,
'no-return-assign': 0,
'no-console': 0,
'max-len': ['error', { code: 120, ignoreComments: true }],
// see https://github.com/prettier/prettier/issues/3847
'space-before-function-paren': ['error', { anonymous: 'never', named: 'never', asyncArrow: 'always' }],
},
};
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/test/fixtures
**/*.gif
/dist
1 change: 1 addition & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ const fabric = require('@umijs/fabric');

module.exports = {
...fabric.prettier,
printWidth: 120,
};
24 changes: 20 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
"start:react15": "cd examples/react15 && npm start",
"start:vue": "cd examples/vue && npm start",
"build": "father-build",
"prepush": "npm run lint",
"release": "np --no-cleanup --yolo --no-publish",
"prepublishOnly": "npm run lint && npm run build",
"lint": "eslint --cache --ext .js,.jsx,.ts,.tsx --format=pretty ./src && npm run lint:prettier",
"lint": "npm run lint:js && npm run lint:prettier",
"lint:js": "eslint --cache --ext .js,.jsx,.ts,.tsx --format=pretty ./src",
"lint:fix": "npm run lint:js -- --fix",
"lint:prettier": "check-prettier lint",
"prettier": "prettier -c --write **/*",
"test": "echo \"Error: no test specified\" && exit 1"
Expand All @@ -39,8 +40,7 @@
"@babel/runtime": "^7.5.5",
"import-html-entry": "^1.0.0",
"lodash": "^4.17.11",
"single-spa": "^4.3.4",
"tslib": "^1.9.3"
"single-spa": "^4.3.4"
},
"devDependencies": {
"@types/lodash": "^4.14.129",
Expand All @@ -50,9 +50,25 @@
"concurrently": "^4.1.2",
"father-build": "^1.7.0",
"husky": "^2.3.0",
"lint-staged": "^9.4.2",
"np": "^5.0.3",
"prettier": "^1.18.2",
"rimraf": "^3.0.0",
"typescript": "^3.4.5"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"**/*.{js,ts,json,css,md}": [
"npm run prettier",
"git add"
],
"**/*.{js,ts}": [
"npm run lint:fix",
"git add"
]
}
}
4 changes: 1 addition & 3 deletions src/effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ export function setDefaultMountApp(defaultAppLink: string) {
}

export function runDefaultMountEffects(defaultAppLink: string) {
console.warn(
'runDefaultMountEffects will be removed in next version, please use setDefaultMountApp instead!',
);
console.warn('runDefaultMountEffects will be removed in next version, please use setDefaultMountApp instead!');
setDefaultMountApp(defaultAppLink);
}

Expand Down
3 changes: 2 additions & 1 deletion src/hijackers/historyListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { isFunction, noop } from 'lodash';
export default function hijack() {
// FIXME umi unmount feature request
// @see http://gitlab.alipay-inc.com/bigfish/bigfish/issues/1154
let rawHistoryListen = (..._: any[]) => noop;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let rawHistoryListen = (_: any) => noop;
const historyListeners: Array<typeof noop> = [];
const historyUnListens: Array<typeof noop> = [];

Expand Down
47 changes: 26 additions & 21 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ function toArray<T>(array: T | T[]): T[] {
return Array.isArray(array) ? array : [array];
}

function execHooksChain<T extends object>(
hooks: Array<Lifecycle<T>>,
app: RegistrableApp<T>,
): Promise<any> {
function execHooksChain<T extends object>(hooks: Array<Lifecycle<T>>, app: RegistrableApp<T>): Promise<any> {
if (hooks.length) {
return hooks.reduce((chain, hook) => chain.then(() => hook(app)), Promise.resolve());
}
Expand Down Expand Up @@ -67,17 +64,18 @@ class Deferred<T> {
}
}

/*
* with singular mode, any app will wait to load until other apps are unmouting
* it is useful for the scenario that only one sub app shown at one time
*/
let singularMode: StartOpts['singular'] = false;
let useJsSandbox = false;

export function registerMicroApps<T extends object = {}>(
apps: Array<RegistrableApp<T>>,
lifeCycles: LifeCycles<T> = {},
) {
const {
beforeUnmount = [],
afterUnmount = [],
afterMount = [],
beforeMount = [],
beforeLoad = [],
} = lifeCycles;
const { beforeUnmount = [], afterUnmount = [], afterMount = [], beforeMount = [], beforeLoad = [] } = lifeCycles;
microApps = [...microApps, ...apps];

let prevAppUnmountedDeferred: Deferred<void>;
Expand Down Expand Up @@ -123,7 +121,13 @@ export function registerMicroApps<T extends object = {}>(
return {
bootstrap: [bootstrapApp],
mount: [
async () => (await validateSingularMode(singularMode, app) ? prevAppUnmountedDeferred && prevAppUnmountedDeferred.promise : undefined),
async () => {
if ((await validateSingularMode(singularMode, app)) && prevAppUnmountedDeferred) {
return prevAppUnmountedDeferred.promise;
}

return undefined;
},
async () => execHooksChain(toArray(beforeMount), app),
// 添加 mount hook, 确保每次应用加载前容器 dom 结构已经设置完毕
async () => render({ appContent, loading: true }),
Expand All @@ -133,14 +137,22 @@ export function registerMicroApps<T extends object = {}>(
async () => render({ appContent, loading: false }),
async () => execHooksChain(toArray(afterMount), app),
// initialize the unmount defer after app mounted and resolve the defer after it unmounted
async () => (await validateSingularMode(singularMode, app) ? (prevAppUnmountedDeferred = new Deferred<void>()) : undefined),
async () => {
if (await validateSingularMode(singularMode, app)) {
prevAppUnmountedDeferred = new Deferred<void>();
}
},
],
unmount: [
async () => execHooksChain(toArray(beforeUnmount), app),
unmount,
unmountSandbox,
async () => execHooksChain(toArray(afterUnmount), app),
async () => (await validateSingularMode(singularMode, app) ? prevAppUnmountedDeferred && prevAppUnmountedDeferred.resolve() : undefined),
async () => {
if ((await validateSingularMode(singularMode, app)) && prevAppUnmountedDeferred) {
prevAppUnmountedDeferred.resolve();
}
},
],
};
},
Expand All @@ -153,13 +165,6 @@ export function registerMicroApps<T extends object = {}>(

export * from './effects';

let useJsSandbox = false;
/*
* with singular mode, any app will wait to load until other apps are unmouting
* it is useful for the scenario that only one sub app shown at one time
*/
let singularMode: StartOpts['singular'] = false;

export function start(opts: StartOpts = {}) {
// eslint-disable-next-line no-underscore-dangle
window.__POWERED_BY_QIANKUN__ = true;
Expand Down
6 changes: 2 additions & 4 deletions src/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ export function genSandbox(appName: string) {
}

if (process.env.NODE_ENV === 'development') {
console.warn(
`Try to set window.${p.toString()} while js sandbox destroyed or not active in ${appName}!`,
);
console.warn(`Try to set window.${p.toString()} while js sandbox destroyed or not active in ${appName}!`);
}

return false;
Expand All @@ -102,7 +100,7 @@ export function genSandbox(appName: string) {

const boundValue = value.bind(target);
// some callable function has custom fields, we need to copy the enumerable props to boundValue. such as moment function.
Object.keys(value).forEach(key => boundValue[key] = value[key]);
Object.keys(value).forEach(key => (boundValue[key] = value[key]));
Object.defineProperty(value, boundValueSymbol, { enumerable: false, value: boundValue });
return boundValue;
}
Expand Down
3 changes: 1 addition & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ export function isConstructable(fn: () => void | FunctionConstructor) {

// 有 prototype 并且 prototype 上有定义一系列非 constructor 属性,则可以认为是一个构造函数
return (
(fn.prototype &&
Object.getOwnPropertyNames(fn.prototype).filter(k => k !== 'constructor').length) ||
(fn.prototype && Object.getOwnPropertyNames(fn.prototype).filter(k => k !== 'constructor').length) ||
constructableFunctionRegex.test(fn.toString()) ||
classRegex.test(fn.toString())
);
Expand Down