Skip to content

Commit 9f30a1f

Browse files
committed
perf(module-loader): 移除ssr加载功能
affects: @vue-async/module-loader BREAKING CHANGE: 不可以server端使用
1 parent c53b113 commit 9f30a1f

File tree

9 files changed

+112
-92
lines changed

9 files changed

+112
-92
lines changed

.eslintrc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ module.exports = {
9393
'@typescript-eslint/no-this-alias': ['error', { allowedNames: ['vm'] }],
9494
'@typescript-eslint/no-use-before-define': ['error', { functions: false }],
9595
'@typescript-eslint/no-empty-function': 'off',
96+
'@typescript-eslint/no-empty-interface': [
97+
'error',
98+
{
99+
allowSingleExtends: false,
100+
},
101+
],
96102
'@typescript-eslint/no-unused-vars': [
97103
'error',
98104
{

packages/module-loader/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"dev": "concurrently --raw \"cd ../../dev-modules && yarn serve\" \"rollup -c ./rollup.config.dev.js -w\" \"node ./dev/server.js\"",
2727
"build": "rollup -c --environment BUILD:production",
2828
"build:es": "rollup -c --environment TARGET:es,BUILD:production",
29-
"build:dev-modules": "cd dev-modules && node scripts/buildModule.js src/dymanicRouter",
3029
"release": "bash scripts/release.sh",
3130
"test": "cross-env NODE_ENV=test jest --passWithNoTests --config jest.config.json",
3231
"lint": "eslint . --cache --report-unused-disable-directives --ignore-path=../../.eslintignore",
@@ -42,11 +41,13 @@
4241
"url": "https://github.com/aceHubert/vus-async/issues"
4342
},
4443
"dependencies": {
45-
"@vue-async/utils": "^1.1.0"
44+
"@vue-async/utils": "^1.1.0",
45+
"warning": "^4.0.3"
4646
},
4747
"devDependencies": {
4848
"@nuxt/types": "^2.14.6",
4949
"@nuxt/typescript-build": "^2.0.3",
50+
"@types/warning": "^3.0.0",
5051
"nuxt": "^2.14.6"
5152
},
5253
"peerDependencies": {

packages/module-loader/src/ability/componentLoader.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
* componentLoader
33
*/
44
import Vue from 'vue';
5-
import { warn as globalWarn, isPlainObject, isFunction, hasOwn } from '@vue-async/utils';
6-
7-
import * as ssr from '../utils/ssr';
5+
import warning from 'warning';
6+
import { isPlainObject, isFunction, hasOwn } from '@vue-async/utils';
87
import * as spa from '../utils/spa';
8+
// import * as ssr from '../utils/ssr';
99

1010
// Types
1111
import { VueConstructor, Component as VueComponent } from 'vue';
@@ -34,8 +34,8 @@ function getComponentFromExport(scriptExports: any, componentName: string, globa
3434
return (scriptExports && scriptExports.default) || scriptExports;
3535
}
3636

37-
globalWarn(
38-
true,
37+
warning(
38+
false,
3939
`[moduleLoader] component not found from ${componentName} entry exports, fallback to get from window['${componentName}']`,
4040
);
4141

@@ -52,31 +52,31 @@ function getComponentFromExport(scriptExports: any, componentName: string, globa
5252
// eslint-disable-next-line @typescript-eslint/no-unused-vars
5353
export default (Vue: VueConstructor) => {
5454
return function loader(componentName: string, src: string, styles?: string | string[]): Promise<VueComponent> {
55-
if (Vue.prototype.$isServer) {
56-
const global = ssr.createSandbox();
55+
// if (Vue.prototype.$isServer) {
56+
// const global = ssr.createSandbox();
5757

58-
return ssr.execScript(src, global).then((scriptExports) => {
59-
return getComponentFromExport(scriptExports, componentName, global);
60-
});
61-
} else {
62-
const global: WindowProxy = window;
58+
// return ssr.execScript(src, global).then((scriptExports) => {
59+
// return getComponentFromExport(scriptExports, componentName, global);
60+
// });
61+
// } else {
62+
const global: WindowProxy = window;
6363

64-
if (!global.Vue) {
65-
global.Vue = Vue;
66-
}
64+
if (!global.Vue) {
65+
global.Vue = Vue;
66+
}
6767

68-
// load styles
69-
if (styles) {
70-
if (typeof styles === 'string') {
71-
styles = [styles];
72-
}
73-
spa.execStyles(styles, componentName);
68+
// load styles
69+
if (styles) {
70+
if (typeof styles === 'string') {
71+
styles = [styles];
7472
}
75-
76-
// exec script
77-
return spa.execScript(src, global).then((scriptExports) => {
78-
return getComponentFromExport(scriptExports, componentName, global);
79-
});
73+
spa.execStyles(styles, componentName);
8074
}
75+
76+
// exec script
77+
return spa.execScript(src, global).then((scriptExports) => {
78+
return getComponentFromExport(scriptExports, componentName, global);
79+
});
80+
// }
8181
};
8282
};

packages/module-loader/src/ability/eventBus.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/**
22
* eventBus
33
*/
4-
5-
import { warn, error } from '@vue-async/utils';
4+
import warning from 'warning';
65

76
// Types
87
import { VueConstructor } from 'vue';
@@ -22,7 +21,7 @@ export default function (Vue: VueConstructor): ModuleContext['$eventBus'] {
2221
*/
2322
emit(eventName, payload) {
2423
if (!events[eventName]) {
25-
return warn(
24+
return warning(
2625
process.env.NODE_ENV === 'production',
2726
`[moduleLoader] event "${eventName}" is triggered, but no listeners,nothing will be happening.`,
2827
);
@@ -41,10 +40,7 @@ export default function (Vue: VueConstructor): ModuleContext['$eventBus'] {
4140
if (!events[eventName]) events[eventName] = new Set();
4241
events[eventName].add(handler);
4342
} else {
44-
error(
45-
process.env.NODE_ENV === 'production',
46-
`[moduleLoader] handler must be a function, but recived ${typeof handler}`,
47-
);
43+
warning(false, `[moduleLoader] handler must be a function, but recived ${typeof handler}`);
4844
}
4945
},
5046
/**
@@ -54,9 +50,9 @@ export default function (Vue: VueConstructor): ModuleContext['$eventBus'] {
5450
*/
5551
off(eventName, handler) {
5652
if (!handler) {
57-
return warn(false, '[moduleLoader] handler must be a valid event function');
53+
return warning(false, '[moduleLoader] handler must be a valid event function');
5854
} else if (!events.hasOwnProperty(eventName)) {
59-
return error(
55+
return warning(
6056
process.env.NODE_ENV === 'production',
6157
`[moduleLoader] no event name of "${eventName}" is on listening`,
6258
);

packages/module-loader/src/ability/moduleLoader.ts

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/**
22
* moduleLoader
33
*/
4-
import { error as globalError, warn as globalWarn, isPlainObject, isFunction } from '@vue-async/utils';
4+
import warning from 'warning';
5+
import { isPlainObject, isFunction } from '@vue-async/utils';
56
import * as spa from '../utils/spa';
6-
import * as ssr from '../utils/ssr';
7+
// import * as ssr from '../utils/ssr';
78

89
// Types
910
import { VueConstructor } from 'vue';
@@ -88,7 +89,7 @@ function getLifecyclesFromExports(scriptExports: any, moduleName: string, global
8889
return isFunction(_export) ? { bootstrap: _export } : scriptExports;
8990
}
9091

91-
globalWarn(
92+
warning(
9293
process.env.NODE_ENV === 'production',
9394
`[moduleLoader] lifecycle not found from ${moduleName} entry exports, fallback to get from window['${moduleName}']`,
9495
);
@@ -114,7 +115,7 @@ export default (Vue: VueConstructor, status: MutableRefObject<boolean>) => {
114115
sync,
115116
success,
116117
error = (msg: string) => {
117-
globalError(true, msg);
118+
warning(false, msg);
118119
},
119120
} = options;
120121

@@ -153,46 +154,46 @@ export default (Vue: VueConstructor, status: MutableRefObject<boolean>) => {
153154
else {
154155
const { moduleName, entry, styles = [], args } = module;
155156
// server render
156-
if (Vue.prototype.$isServer) {
157-
const global = ssr.createSandbox();
158-
159-
return ssr
160-
.execScript(entry, global)
161-
.then((scriptExports: any) => {
162-
const { bootstrap } = getLifecyclesFromExports(scriptExports, moduleName, global);
163-
bootstrap.call(_self, Vue, args);
164-
})
165-
.catch((err: Error) => {
166-
// 异常不阻止当前执行,error 中处理
167-
try {
168-
error(err.message, module);
169-
} catch {}
170-
});
171-
} else {
172-
// todo: load in sandbox
173-
const global: WindowProxy = window;
174-
175-
if (!global.Vue) {
176-
global.Vue = Vue;
177-
}
178-
179-
// load styles
180-
spa.execStyles(styles, moduleName);
181-
182-
// exec script
183-
return spa
184-
.execScript(entry, global)
185-
.then((scriptExports: any) => {
186-
const { bootstrap } = getLifecyclesFromExports(scriptExports, moduleName, global);
187-
bootstrap.call(_self, Vue, args);
188-
})
189-
.catch((err: Error) => {
190-
// 异常不阻止当前执行,error 中处理
191-
try {
192-
error(err.message, module);
193-
} catch {}
194-
});
157+
// if (Vue.prototype.$isServer) {
158+
// const global = ssr.createSandbox();
159+
160+
// return ssr
161+
// .execScript(entry, global)
162+
// .then((scriptExports: any) => {
163+
// const { bootstrap } = getLifecyclesFromExports(scriptExports, moduleName, global);
164+
// bootstrap.call(_self, Vue, args);
165+
// })
166+
// .catch((err: Error) => {
167+
// // 异常不阻止当前执行,error 中处理
168+
// try {
169+
// error(err.message, module);
170+
// } catch {}
171+
// });
172+
// } else {
173+
// todo: load in sandbox
174+
const global: WindowProxy = window;
175+
176+
if (!global.Vue) {
177+
global.Vue = Vue;
195178
}
179+
180+
// load styles
181+
spa.execStyles(styles, moduleName);
182+
183+
// exec script
184+
return spa
185+
.execScript(entry, global)
186+
.then((scriptExports: any) => {
187+
const { bootstrap } = getLifecyclesFromExports(scriptExports, moduleName, global);
188+
bootstrap.call(_self, Vue, args);
189+
})
190+
.catch((err: Error) => {
191+
// 异常不阻止当前执行,error 中处理
192+
try {
193+
error(err.message, module);
194+
} catch {}
195+
});
196+
// }
196197
}
197198
}
198199

packages/module-loader/src/framework.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import warning from 'warning';
12
import { RouteConfig } from 'vue-router';
2-
import { error, warn, hasOwn } from '@vue-async/utils';
3+
import { hasOwn } from '@vue-async/utils';
34
import install from './install';
45

56
// Types
@@ -19,7 +20,7 @@ export default class ModuleLoader<Options = Record<string, any>> {
1920
if (!hasOwn(this._framework, key)) {
2021
this._framework[key] = value;
2122
} else {
22-
warn(process.env.NODE_ENV === 'production', `参数 ${key} 已在在`);
23+
warning(process.env.NODE_ENV === 'production', `${key} has been defined`);
2324
}
2425
});
2526
}
@@ -39,7 +40,7 @@ export default class ModuleLoader<Options = Record<string, any>> {
3940
if (root.$router) {
4041
root.$router.addRoutes(routes);
4142
} else {
42-
error(process.env.NODE_ENV === 'production', 'vm.$router未定义');
43+
warning(process.env.NODE_ENV === 'production', 'vm.$router is not exists!');
4344
}
4445
};
4546
}

packages/module-loader/src/utils/spa.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @author Hubert
33
* fork from https://github.com/kuitos/import-html-entry/blob/master/src/utils.js
44
*/
5-
import { error } from '@vue-async/utils';
5+
import warning from 'warning';
66

77
const isIE11 = typeof navigator !== 'undefined' && navigator.userAgent.indexOf('Trident') !== -1;
88

@@ -78,7 +78,7 @@ export function execScript(entry: string, proxy: WindowProxy = window) {
7878
resolve(exports);
7979
};
8080
script.onerror = (err) => {
81-
error(process.env.NODE_ENV === 'production', `[moduleLoader] script had a problem to create, entry:${entry}`);
81+
warning(process.env.NODE_ENV === 'production', `[moduleLoader] script had a problem to create, entry:${entry}`);
8282
reject(new Error(`script load error, error: ${err}`));
8383
};
8484
proxy.document.body.appendChild(script);

packages/module-loader/src/utils/ssr.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { error } from '@vue-async/utils';
1+
import warning from 'warning';
22

33
// Types
44
import { Context as vmContext } from 'vm';
@@ -49,7 +49,10 @@ export function execScript(entry: string, _proxy: vmContext = { exports: {} }) {
4949
const { statusCode } = res;
5050

5151
if (statusCode !== 200) {
52-
error(process.env.NODE_ENV === 'production', `[moduleLoader] script had a problem to create, entry:${entry}`);
52+
warning(
53+
process.env.NODE_ENV === 'production',
54+
`[moduleLoader] script had a problem to create, entry:${entry}`,
55+
);
5356
reject(new Error(`script load error, statusCode: ${statusCode}`));
5457
}
5558
res.setEncoding('utf8');
@@ -86,7 +89,7 @@ export function execScript(entry: string, _proxy: vmContext = { exports: {} }) {
8689

8790
resolve(exports);
8891
} catch (err) {
89-
error(
92+
warning(
9093
process.env.NODE_ENV === 'production',
9194
`[moduleLoader] script had a problem to create, entry:${entry}`,
9295
);
@@ -96,7 +99,7 @@ export function execScript(entry: string, _proxy: vmContext = { exports: {} }) {
9699
});
97100

98101
request.on('error', (err) => {
99-
error(process.env.NODE_ENV === 'production', `[moduleLoader] http.request error, entry:${entry}`);
102+
warning(process.env.NODE_ENV === 'production', `[moduleLoader] http.request error, entry:${entry}`);
100103
reject(new Error(`script load error, error: ${err.message}`));
101104
});
102105
});

yarn.lock

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2838,6 +2838,11 @@
28382838
dependencies:
28392839
source-map "^0.6.1"
28402840

2841+
"@types/warning@^3.0.0":
2842+
version "3.0.0"
2843+
resolved "https://registry.yarnpkg.com/@types/warning/-/warning-3.0.0.tgz#0d2501268ad8f9962b740d387c4654f5f8e23e52"
2844+
integrity sha1-DSUBJorY+ZYrdA04fEZU9fjiPlI=
2845+
28412846
"@types/webpack-bundle-analyzer@^3.9.0":
28422847
version "3.9.0"
28432848
resolved "https://registry.yarnpkg.com/@types/webpack-bundle-analyzer/-/webpack-bundle-analyzer-3.9.0.tgz#bf2f3fd7f1fe6a71dff8968afeb12785d1ce737b"
@@ -4280,9 +4285,9 @@ caniuse-api@^3.0.0:
42804285
lodash.uniq "^4.5.0"
42814286

42824287
caniuse-lite@^1.0.0, caniuse-lite@^1.0.30000981, caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001164, caniuse-lite@^1.0.30001165:
4283-
version "1.0.30001166"
4284-
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001166.tgz#ca73e8747acfd16a4fd6c4b784f1b995f9698cf8"
4285-
integrity sha512-nCL4LzYK7F4mL0TjEMeYavafOGnBa98vTudH5c8lW9izUjnB99InG6pmC1ElAI1p0GlyZajv4ltUdFXvOHIl1A==
4288+
version "1.0.30001235"
4289+
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001235.tgz"
4290+
integrity sha512-zWEwIVqnzPkSAXOUlQnPW2oKoYb2aLQ4Q5ejdjBcnH63rfypaW34CxaeBn1VMya2XaEU3P/R2qHpWyj+l0BT1A==
42864291

42874292
capture-exit@^2.0.0:
42884293
version "2.0.0"
@@ -13832,6 +13837,13 @@ walker@^1.0.7, walker@~1.0.5:
1383213837
dependencies:
1383313838
makeerror "1.0.x"
1383413839

13840+
warning@^4.0.3:
13841+
version "4.0.3"
13842+
resolved "https://registry.yarnpkg.com/warning/-/warning-4.0.3.tgz#16e9e077eb8a86d6af7d64aa1e05fd85b4678ca3"
13843+
integrity sha512-rpJyN222KWIvHJ/F53XSZv0Zl/accqHR8et1kpaMTD/fLCRxtV8iX8czMzY7sVZupTI3zcUTg8eycS2kNF9l6w==
13844+
dependencies:
13845+
loose-envify "^1.0.0"
13846+
1383513847
watchpack-chokidar2@^2.0.1:
1383613848
version "2.0.1"
1383713849
resolved "https://registry.yarnpkg.com/watchpack-chokidar2/-/watchpack-chokidar2-2.0.1.tgz#38500072ee6ece66f3769936950ea1771be1c957"

0 commit comments

Comments
 (0)