@shuvi/platform-web 0.0.1-pre.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. package/lib/index.d.ts +3 -0
  2. package/lib/index.js +44 -0
  3. package/lib/paths.d.ts +4 -0
  4. package/lib/paths.js +7 -0
  5. package/package.json +40 -0
  6. package/shuvi-app/application/client/create-application-factory.d.ts +3 -0
  7. package/shuvi-app/application/client/create-application-factory.js +70 -0
  8. package/shuvi-app/application/client/create-application-history-browser.d.ts +1 -0
  9. package/shuvi-app/application/client/create-application-history-browser.js +3 -0
  10. package/shuvi-app/application/client/create-application-history-hash.d.ts +1 -0
  11. package/shuvi-app/application/client/create-application-history-hash.js +3 -0
  12. package/shuvi-app/application/client/create-application-history-memory.d.ts +1 -0
  13. package/shuvi-app/application/client/create-application-history-memory.js +3 -0
  14. package/shuvi-app/application/server/create-application-spa.d.ts +2 -0
  15. package/shuvi-app/application/server/create-application-spa.js +8 -0
  16. package/shuvi-app/application/server/create-application.d.ts +2 -0
  17. package/shuvi-app/application/server/create-application.js +25 -0
  18. package/shuvi-app/dev/webpackHotDevClient.d.ts +5 -0
  19. package/shuvi-app/dev/webpackHotDevClient.js +33 -0
  20. package/shuvi-app/entry/client/index.d.ts +1 -0
  21. package/shuvi-app/entry/client/index.js +11 -0
  22. package/shuvi-app/entry/client/run.dev.d.ts +2 -0
  23. package/shuvi-app/entry/client/run.dev.js +30 -0
  24. package/shuvi-app/entry/client/run.prod.d.ts +2 -0
  25. package/shuvi-app/entry/client/run.prod.js +3 -0
  26. package/shuvi-app/entry/client/setup-app.d.ts +3 -0
  27. package/shuvi-app/entry/client/setup-app.js +38 -0
  28. package/shuvi-app/entry/client/setup-env.d.ts +5 -0
  29. package/shuvi-app/entry/client/setup-env.js +27 -0
  30. package/shuvi-app/entry/server/index.d.ts +6 -0
  31. package/shuvi-app/entry/server/index.js +7 -0
package/lib/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { Runtime } from '@shuvi/service';
2
+ declare const platform: Runtime.IRuntime<any>;
3
+ export default platform;
package/lib/index.js ADDED
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const paths_1 = require("./paths");
13
+ const platform = {
14
+ install(api) {
15
+ return __awaiter(this, void 0, void 0, function* () {
16
+ // set application and entry
17
+ const { ssr, router: { history } } = api.config;
18
+ let ApplicationModule;
19
+ if (history === 'browser') {
20
+ ApplicationModule = 'create-application-history-browser';
21
+ }
22
+ else if (history === 'hash') {
23
+ ApplicationModule = 'create-application-history-hash';
24
+ }
25
+ else {
26
+ ApplicationModule = 'create-application-history-memory';
27
+ }
28
+ api.setServerModule({
29
+ application: paths_1.resolveAppFile('application', 'server', ssr ? 'create-application' : 'create-application-spa'),
30
+ entry: paths_1.resolveAppFile('entry', 'server')
31
+ });
32
+ api.setClientModule({
33
+ application: paths_1.resolveAppFile('application', 'client', ApplicationModule),
34
+ entry: paths_1.resolveAppFile('entry', 'client')
35
+ });
36
+ // install framework
37
+ const { framework = 'react' } = api.config.platform || {};
38
+ const frameworkInstance = require(`@shuvi/platform-web-${framework}`)
39
+ .default;
40
+ frameworkInstance.install(api);
41
+ });
42
+ }
43
+ };
44
+ exports.default = platform;
package/lib/paths.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export declare const PACKAGE_DIR: string;
2
+ export declare const resolveAppFile: (...paths: string[]) => string;
3
+ export declare const resolveDep: (module: string) => string;
4
+ export declare const resolveLib: (module: string) => string;
package/lib/paths.js ADDED
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const path_1 = require("path");
4
+ exports.PACKAGE_DIR = path_1.dirname(require.resolve('@shuvi/platform-web/package.json'));
5
+ exports.resolveAppFile = (...paths) => `${path_1.resolve(exports.PACKAGE_DIR, 'shuvi-app', ...paths)}`;
6
+ exports.resolveDep = (module) => require.resolve(module);
7
+ exports.resolveLib = (module) => path_1.dirname(exports.resolveDep(path_1.join(module, 'package.json')));
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@shuvi/platform-web",
3
+ "version": "0.0.1-pre.1",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "git+https://github.com/shuvijs/shuvi.git",
7
+ "directory": "packages/platform-web"
8
+ },
9
+ "author": "liximomo",
10
+ "license": "MIT",
11
+ "sideEffects": [
12
+ "**/shuvi-app/entry/client/**"
13
+ ],
14
+ "main": "./lib/index.js",
15
+ "module": "./es/index.js",
16
+ "files": [
17
+ "lib",
18
+ "shuvi-app"
19
+ ],
20
+ "scripts": {
21
+ "dev": "run-p watch:*",
22
+ "watch:app": "tsc -p tsconfig.build.app.json -w",
23
+ "watch:node": "tsc -p tsconfig.build.node.json -w",
24
+ "prebuild": "rimraf lib shuvi-app",
25
+ "build": "run-p build:*",
26
+ "build:app": "tsc -p tsconfig.build.app.json",
27
+ "build:node": "tsc -p tsconfig.build.node.json"
28
+ },
29
+ "engines": {
30
+ "node": ">= 12.0.0"
31
+ },
32
+ "dependencies": {
33
+ "@shuvi/app": "0.0.1-pre.1",
34
+ "@shuvi/platform-core": "0.0.1-pre.1",
35
+ "@shuvi/platform-web-react": "0.0.1-pre.1",
36
+ "@shuvi/service": "0.0.1-pre.1",
37
+ "@shuvi/shared": "0.0.1-pre.1"
38
+ },
39
+ "gitHead": "0bc0bc76e72ca5a339341e8b71d6bb6bb41409e8"
40
+ }
@@ -0,0 +1,3 @@
1
+ import { History } from '@shuvi/router/lib/types';
2
+ import { Runtime } from '@shuvi/service';
3
+ export declare const createFactory: (historyCreater: () => History) => Runtime.ApplicationCreater;
@@ -0,0 +1,70 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import AppComponent from '@shuvi/app/core/app';
11
+ import routes from '@shuvi/app/core/routes';
12
+ import { getRoutes } from '@shuvi/app/core/platform';
13
+ import { Application } from '@shuvi/platform-core';
14
+ import runPlugins from '@shuvi/platform-core/lib/runPlugins';
15
+ import { createRouter } from '@shuvi/router';
16
+ let app;
17
+ let history;
18
+ let appContext;
19
+ export const createFactory = (historyCreater) => {
20
+ const create = function (context, options) {
21
+ appContext = context;
22
+ // app is a singleton in client side
23
+ if (app) {
24
+ return app;
25
+ }
26
+ history = historyCreater();
27
+ const router = createRouter({
28
+ history,
29
+ routes: getRoutes(routes, context)
30
+ });
31
+ app = new Application({
32
+ AppComponent,
33
+ router,
34
+ context,
35
+ render: options.render
36
+ });
37
+ runPlugins(app);
38
+ return app;
39
+ };
40
+ return create;
41
+ };
42
+ if (module.hot) {
43
+ module.hot.accept([
44
+ '@shuvi/app/entry.client',
45
+ '@shuvi/app/core/app',
46
+ '@shuvi/app/core/routes',
47
+ '@shuvi/app/user/plugin'
48
+ ], () => __awaiter(void 0, void 0, void 0, function* () {
49
+ const rerender = () => {
50
+ const AppComponent = require('@shuvi/app/core/app').default;
51
+ const routes = require('@shuvi/app/core/routes').default;
52
+ const router = createRouter({
53
+ history,
54
+ routes: getRoutes(routes, appContext)
55
+ });
56
+ app.rerender({ router, AppComponent });
57
+ };
58
+ // to solve routing problem, we need to rerender routes
59
+ // wait navigation complete only rerender to ensure getInitialProps is called
60
+ if (__SHUVI.router._pending) {
61
+ const removelistener = __SHUVI.router.afterEach(() => {
62
+ rerender();
63
+ removelistener();
64
+ });
65
+ }
66
+ else {
67
+ rerender();
68
+ }
69
+ }));
70
+ }
@@ -0,0 +1 @@
1
+ export declare const create: import("@shuvi/service/lib/types/runtime").ApplicationCreater;
@@ -0,0 +1,3 @@
1
+ import { createFactory } from './create-application-factory';
2
+ import { createBrowserHistory } from '@shuvi/router';
3
+ export const create = createFactory(createBrowserHistory);
@@ -0,0 +1 @@
1
+ export declare const create: import("@shuvi/service/lib/types/runtime").ApplicationCreater;
@@ -0,0 +1,3 @@
1
+ import { createFactory } from './create-application-factory';
2
+ import { createHashHistory } from '@shuvi/router';
3
+ export const create = createFactory(createHashHistory);
@@ -0,0 +1 @@
1
+ export declare const create: import("@shuvi/service/lib/types/runtime").ApplicationCreater;
@@ -0,0 +1,3 @@
1
+ import { createFactory } from './create-application-factory';
2
+ import { createMemoryHistory } from '@shuvi/router';
3
+ export const create = createFactory(createMemoryHistory);
@@ -0,0 +1,2 @@
1
+ import { Runtime } from '@shuvi/service';
2
+ export declare const create: Runtime.ApplicationCreater;
@@ -0,0 +1,8 @@
1
+ import { Application } from '@shuvi/platform-core';
2
+ export const create = function (context, options) {
3
+ return new Application({
4
+ AppComponent: null,
5
+ context,
6
+ render: options.render
7
+ });
8
+ };
@@ -0,0 +1,2 @@
1
+ import { Runtime } from '@shuvi/service';
2
+ export declare const create: Runtime.ApplicationCreater;
@@ -0,0 +1,25 @@
1
+ import AppComponent from '@shuvi/app/core/app';
2
+ import routes from '@shuvi/app/core/routes';
3
+ import { getRoutes } from '@shuvi/app/core/platform';
4
+ import { Application } from '@shuvi/platform-core';
5
+ import runPlugins from '@shuvi/platform-core/lib/runPlugins';
6
+ import { createRouter, createMemoryHistory } from '@shuvi/router';
7
+ export const create = function (context, options) {
8
+ const { req } = context;
9
+ const history = createMemoryHistory({
10
+ initialEntries: [(req && req.url) || '/'],
11
+ initialIndex: 0
12
+ });
13
+ const router = createRouter({
14
+ history,
15
+ routes: getRoutes(routes, context)
16
+ });
17
+ const app = new Application({
18
+ AppComponent,
19
+ router,
20
+ context,
21
+ render: options.render
22
+ });
23
+ runPlugins(app);
24
+ return app;
25
+ };
@@ -0,0 +1,5 @@
1
+ declare const _default: (options?: {}) => {
2
+ subscribeToHmrEvent(handler: any): void;
3
+ reportRuntimeError(err: any): void;
4
+ };
5
+ export default _default;
@@ -0,0 +1,33 @@
1
+ import connect from '@shuvi/toolpack/lib/utils/hotDevClient';
2
+ import { DEV_HOT_MIDDLEWARE_PATH, DEV_HOT_LAUNCH_EDITOR_ENDPOINT } from '@shuvi/shared/lib/constants';
3
+ let devClient;
4
+ export default (options = {}) => {
5
+ if (devClient) {
6
+ return devClient;
7
+ }
8
+ devClient = connect(Object.assign(Object.assign({}, options), { launchEditorEndpoint: DEV_HOT_LAUNCH_EDITOR_ENDPOINT, path: DEV_HOT_MIDDLEWARE_PATH }));
9
+ devClient.subscribeToHmrEvent((event) => {
10
+ // if (obj.action === "reloadPage") {
11
+ // return window.location.reload();
12
+ // }
13
+ // if (obj.action === "removedPage") {
14
+ // const [page] = obj.data;
15
+ // if (page === window.next.router.pathname) {
16
+ // return window.location.reload();
17
+ // }
18
+ // return;
19
+ // }
20
+ // if (obj.action === "addedPage") {
21
+ // const [page] = obj.data;
22
+ // if (
23
+ // page === window.next.router.pathname &&
24
+ // typeof window.next.router.components[page] === "undefined"
25
+ // ) {
26
+ // return window.location.reload();
27
+ // }
28
+ // return;
29
+ // }
30
+ // throw new Error("Unexpected action " + obj.action);
31
+ });
32
+ return devClient;
33
+ };
@@ -0,0 +1 @@
1
+ import './setup-env';
@@ -0,0 +1,11 @@
1
+ // This is the shuvi client-side entry
2
+ // IMPORTANT: `setup-env` must be runned before any other codes
3
+ import './setup-env';
4
+ import { rerender } from './setup-app';
5
+ window.__SHUVI = Object.assign(Object.assign({}, (window.__SHUVI || {})), { rerender });
6
+ if (process.env.NODE_ENV === 'development') {
7
+ require('./run.dev');
8
+ }
9
+ else {
10
+ require('./run.prod');
11
+ }
@@ -0,0 +1,2 @@
1
+ /// <reference lib="dom" />
2
+ export {};
@@ -0,0 +1,30 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ /// <reference lib="dom" />
11
+ import { DEV_STYLE_HIDE_FOUC, DEV_STYLE_PREPARE } from '@shuvi/shared/lib/constants';
12
+ import initWebpackHMR from '../../dev/webpackHotDevClient';
13
+ import { app } from './setup-app';
14
+ function init() {
15
+ return __awaiter(this, void 0, void 0, function* () {
16
+ initWebpackHMR();
17
+ // reduce FOUC caused by style-loader
18
+ const styleReady = new Promise(resolve => {
19
+ (window.requestAnimationFrame || setTimeout)(() => __awaiter(this, void 0, void 0, function* () {
20
+ yield window[DEV_STYLE_PREPARE];
21
+ document
22
+ .querySelectorAll(`[${DEV_STYLE_HIDE_FOUC}]`)
23
+ .forEach(el => { var _a; return (_a = el.parentElement) === null || _a === void 0 ? void 0 : _a.removeChild(el); });
24
+ resolve();
25
+ }));
26
+ });
27
+ yield styleReady;
28
+ });
29
+ }
30
+ init().then(() => app.run());
@@ -0,0 +1,2 @@
1
+ /// <reference lib="dom" />
2
+ export {};
@@ -0,0 +1,3 @@
1
+ /// <reference lib="dom" />
2
+ import { app } from './setup-app';
3
+ app.run();
@@ -0,0 +1,3 @@
1
+ declare const app: import("@shuvi/runtime-core").IApplication;
2
+ declare const rerender: () => void;
3
+ export { app, rerender };
@@ -0,0 +1,38 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { CLIENT_CONTAINER_ID } from '@shuvi/shared/lib/constants';
11
+ // renderer must be imported before application
12
+ // we need to init init renderer before import AppComponent
13
+ import { view } from '@shuvi/app/core/platform';
14
+ import { create } from '@shuvi/app/core/client/application';
15
+ import { getAppData } from '@shuvi/platform-core/lib/helper';
16
+ const appData = getAppData();
17
+ const { routeProps = {} } = appData;
18
+ const app = create({
19
+ pageData: appData.pageData || {},
20
+ routeProps
21
+ }, {
22
+ render({ appContext, AppComponent, router }) {
23
+ return __awaiter(this, void 0, void 0, function* () {
24
+ const appContainer = document.getElementById(CLIENT_CONTAINER_ID);
25
+ view.renderApp({
26
+ AppComponent: AppComponent,
27
+ router: router,
28
+ appData,
29
+ appContainer,
30
+ appContext
31
+ });
32
+ });
33
+ }
34
+ });
35
+ const rerender = () => {
36
+ app.rerender();
37
+ };
38
+ export { app, rerender };
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Following codes should be run beforen any other codes.
3
+ * Do not try to import any module from `@shuvi/app`.
4
+ */
5
+ export {};
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Following codes should be run beforen any other codes.
3
+ * Do not try to import any module from `@shuvi/app`.
4
+ */
5
+ import { IDENTITY_RUNTIME_PUBLICPATH, IDENTITY_SSR_RUNTIME_PUBLICPATH } from '@shuvi/shared/lib/constants';
6
+ import setRuntimeConfig from '@shuvi/app/core/setRuntimeConfig';
7
+ import runtimeConfig from '@shuvi/app/core/runtimeConfig';
8
+ import { getAppData } from '@shuvi/platform-core/lib/helper';
9
+ const win = window;
10
+ // server runtime public path
11
+ if (win[IDENTITY_SSR_RUNTIME_PUBLICPATH]) {
12
+ __webpack_public_path__ = win[IDENTITY_SSR_RUNTIME_PUBLICPATH];
13
+ }
14
+ // client runtime public path
15
+ if (win[IDENTITY_RUNTIME_PUBLICPATH]) {
16
+ __webpack_public_path__ = win[IDENTITY_RUNTIME_PUBLICPATH];
17
+ }
18
+ // === set runtime config ===
19
+ const appData = getAppData();
20
+ // build-time config for none-ssr
21
+ if (runtimeConfig) {
22
+ setRuntimeConfig(runtimeConfig);
23
+ }
24
+ // runtime config from server
25
+ if (appData.runtimeConfig) {
26
+ setRuntimeConfig(appData.runtimeConfig);
27
+ }
@@ -0,0 +1,6 @@
1
+ import * as server from '@shuvi/app/user/server';
2
+ import * as document from '@shuvi/app/user/document';
3
+ import * as application from '@shuvi/app/core/server/application';
4
+ import { default as apiRoutes } from '@shuvi/app/core/apiRoutes';
5
+ export { server, document, application, apiRoutes };
6
+ export { view } from '@shuvi/app/core/platform';
@@ -0,0 +1,7 @@
1
+ // This is the shuvi server-side main module exports collection
2
+ import * as server from '@shuvi/app/user/server';
3
+ import * as document from '@shuvi/app/user/document';
4
+ import * as application from '@shuvi/app/core/server/application';
5
+ import { default as apiRoutes } from '@shuvi/app/core/apiRoutes';
6
+ export { server, document, application, apiRoutes };
7
+ export { view } from '@shuvi/app/core/platform';