Skip to content

Commit

Permalink
Merge pull request #3446 from alibaba/release-next
Browse files Browse the repository at this point in the history
  • Loading branch information
chenbin92 authored Aug 18, 2020
2 parents 78643fd + 740cd4e commit 3bbcf38
Show file tree
Hide file tree
Showing 17 changed files with 74 additions and 81 deletions.
3 changes: 1 addition & 2 deletions examples/basic-spa/build.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@
"eslint": {
"disable": false,
"quiet": true
},
"tsChecker": true
}
}
8 changes: 6 additions & 2 deletions examples/basic-spa/src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import React from 'react';
import { createApp, APP_MODE, IAppConfig } from 'ice';
import { createApp, APP_MODE, IAppConfig, request } from 'ice';

const appConfig: IAppConfig = {
app: {
rootId: 'ice-container',
errorBoundary: true,
parseSearchParams: true,
getInitialData: async() => {
const result = await request('/repo');
console.log('request result:', result);
},
onShow() {
console.log('app show...');
},
Expand All @@ -23,7 +27,7 @@ const appConfig: IAppConfig = {
},
request: {
timeout: 5000,
baseURL: '/',
baseURL: '/api',
interceptors: {
request: {
onConfig: (config) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/create-app-shared/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-app-shared",
"version": "0.1.8",
"version": "0.1.9",
"description": "",
"author": "ice-admin@alibaba-inc.com",
"homepage": "https://github.com/alibaba/ice#readme",
Expand Down
6 changes: 2 additions & 4 deletions packages/create-app-shared/src/createBaseApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ const DEFAULE_APP_CONFIG = {
}
};

export default ({ loadRuntimeModules, loadStaticModules, createElement }) => {
export default ({ loadRuntimeModules, createElement }) => {
const createBaseApp = (appConfig, buildConfig, context: any = {}) => {

// Merge default appConfig to user appConfig
appConfig = deepmerge(DEFAULE_APP_CONFIG, appConfig);

// load module to run before createApp ready
loadStaticModules(appConfig);

// Set history
let history = {};
if (!isMiniAppPlatform) {
Expand Down
4 changes: 2 additions & 2 deletions packages/create-app-shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import useSearchParams from './useSearchParams';
import withSearchParams from './withSearchParams';
import collectAppLifeCycle from './collectAppLifeCycle';

function createShareAPI({ withRouter, createElement, useEffect }, loadRuntimeModules, loadStaticModules) {
function createShareAPI({ withRouter, createElement, useEffect }, loadRuntimeModules) {
const { usePageShow, usePageHide } = createUsePageLifeCycle({ useEffect });
return {
createBaseApp: createBaseApp({ loadRuntimeModules, loadStaticModules, createElement }),
createBaseApp: createBaseApp({ loadRuntimeModules, createElement }),

// history api
withRouter: enhanceWithRouter({ withRouter, createElement }),
Expand Down
8 changes: 4 additions & 4 deletions packages/icejs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ice.js",
"version": "1.7.2",
"version": "1.7.3",
"description": "command line interface and builtin plugin for icejs",
"author": "ice-admin@alibaba-inc.com",
"homepage": "",
Expand All @@ -26,15 +26,15 @@
"dependencies": {
"@alib/build-scripts": "^0.1.13",
"build-plugin-ice-config": "1.7.0",
"build-plugin-app-core": "0.1.10",
"build-plugin-app-core": "0.1.11",
"build-plugin-ice-helpers": "1.7.0",
"build-plugin-ice-logger": "1.7.0",
"build-plugin-ice-mpa": "1.7.0",
"build-plugin-ice-request": "1.7.1",
"build-plugin-ice-router": "1.7.1",
"build-plugin-ice-ssr": "1.7.0",
"build-plugin-ice-ssr": "1.7.1",
"build-plugin-ice-store": "1.7.1",
"build-plugin-react-app": "1.7.1",
"build-plugin-react-app": "1.7.2",
"build-plugin-miniapp": "0.1.2",
"create-cli-utils": "^0.1.0",
"chokidar": "^3.3.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-app-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "build-plugin-app-core",
"version": "0.1.10",
"version": "0.1.11",
"description": "the core plugin for icejs and raxjs.",
"author": "ice-admin@alibaba-inc.com",
"homepage": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const {
createElement,
useEffect,
withRouter: defaultWithRouter
}, loadRuntimeModules, loadStaticModules);
}, loadRuntimeModules);

export function runApp(appConfig) {
let renderer;
Expand All @@ -65,6 +65,7 @@ export function runApp(appConfig) {
getHistory,
emitLifeCycles,
pathRedirect,
loadStaticModules,
ErrorBoundary
}, {
createElement,
Expand Down
6 changes: 4 additions & 2 deletions packages/plugin-app-core/src/utils/getSourceDir.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as path from 'path';
import formatPath from './formatPath';

// entry: src/app -> srcDir: src
// entry: client/app -> srcDir: client
Expand All @@ -14,7 +14,9 @@ function getSourceDir (entry): string {
}
}

return entryStr.split(path.sep)[0];
entryStr = formatPath(entryStr);

return entryStr.split('/')[0];
}

export default getSourceDir;
2 changes: 1 addition & 1 deletion packages/plugin-react-app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "build-plugin-react-app",
"version": "1.7.1",
"version": "1.7.2",
"description": "The basic webpack configuration for ice project",
"author": "ice-admin@alibaba-inc.com",
"main": "src/index.js",
Expand Down
6 changes: 2 additions & 4 deletions packages/plugin-react-app/src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ const collect = require('./utils/collect');
// eslint-disable-next-line
const chalk = require('chalk');

module.exports = (api) => {
module.exports = (api, { isMiniapp }) => {
const { context, log } = api;
const { command, rootDir, webpack, commandArgs, pkg, userConfig: { targets } } = context;
const isMiniapp = Array.isArray(targets)
&& (targets.includes('miniapp') || targets.includes('wechat-miniprogram'));
const { command, rootDir, webpack, commandArgs, pkg } = context;
const appMode = commandArgs.mode || command;
collect({ command, log, rootDir, pkg });
const babelConfig = getBabelConfig();
Expand Down
5 changes: 2 additions & 3 deletions packages/plugin-react-app/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ module.exports = (api) => {
const { onGetJestConfig, onGetWebpackConfig, context, registerTask, onHook } = api;
const { command, rootDir, commandArgs, userConfig } = context;
const { targets = [WEB] } = userConfig;
const isMiniapp = Array.isArray(targets)
&& (targets.includes(MINIAPP) || targets.includes(WECHAT_MINIPROGRAM));
const isMiniapp = targets.includes(MINIAPP) || targets.includes(WECHAT_MINIPROGRAM);

// register cli option
registerCliOption(api);
Expand All @@ -32,7 +31,7 @@ module.exports = (api) => {
chainConfig.resolve.modules.add(path.join(rootDir, 'node_modules'));
});

targets.forEach(target => registerTask(target, getBase(api)));
targets.forEach(target => registerTask(target, getBase(api, { isMiniapp })));

if (command === 'test') {
onGetJestConfig((jestConfig) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-react-app/src/utils/getMiniappOutputPath.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const fs = require('fs-extra');
const path = require('path');
const { MINIAPP } = require('../constants');
const defaultConfig = require('../config/default.config');

module.exports = (context, options = {}) => {
const { rootDir, userConfig } = context;
const { outputDir } = userConfig;
const { outputDir = defaultConfig.outputDir } = userConfig;
const output = path.resolve(rootDir, outputDir);
fs.ensureDirSync(output);
return path.resolve(output, options.target || MINIAPP);
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-ssr/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "build-plugin-ice-ssr",
"version": "1.7.0",
"version": "1.7.1",
"description": "ssr plugin",
"author": "ice-admin@alibaba-inc.com",
"homepage": "",
Expand Down
13 changes: 6 additions & 7 deletions packages/plugin-ssr/src/server.ts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,14 @@ const serverRender = async ({ context, pathname, initialData, htmlTemplate }) =>
// generate bundle content and register global variables in html
console.log('[SSR]', 'generating html content');
const bundleContent = reactAppRendererWithSSR({
staticContext: context,
pathname,
initialData,
pageInitialProps
}, {
appConfig,
createBaseApp,
emitLifeCycles,
context: {
staticContext: context,
pathname,
initialData,
pageInitialProps
}
emitLifeCycles
});
$('#ice-container').append(bundleContent);
$('head').append(`<script>
Expand Down
2 changes: 1 addition & 1 deletion packages/react-app-renderer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-app-renderer",
"version": "0.1.5",
"version": "0.1.6",
"description": "",
"author": "ice-admin@alibaba-inc.com",
"homepage": "https://github.com/alibaba/ice#readme",
Expand Down
80 changes: 36 additions & 44 deletions packages/react-app-renderer/src/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,76 @@ import * as React from 'react';
import * as ReactDOM from 'react-dom';
import * as ReactDOMServer from 'react-dom/server';

export interface IContext {
initialData?: any;
pageInitialProps?: any;
pathname?: string;
}

let _createBaseApp;
let _emitLifeCycles;
let ErrorBoundaryComp;

function reactAppRendererWithSSR({appConfig, createBaseApp, emitLifeCycles, context}) {
_createBaseApp = createBaseApp;
_emitLifeCycles = emitLifeCycles;
function reactAppRendererWithSSR(context, options) {
const { appConfig } = options || {};
appConfig.router.type = 'static';
return renderApp(appConfig, context);
return renderApp(context, options);
}

function reactAppRenderer({ appConfig, createBaseApp, setAppConfig, emitLifeCycles, ErrorBoundary }) {
_createBaseApp = createBaseApp;
_emitLifeCycles = emitLifeCycles;
ErrorBoundaryComp = ErrorBoundary;
async function reactAppRenderer(options) {
const { appConfig, setAppConfig, loadStaticModules } = options || {};

// set appConfig to application life cycle
setAppConfig(appConfig);

if (process.env.__IS_SERVER__) {
return;
}
loadStaticModules(appConfig);

if (process.env.__IS_SERVER__) return;

let initialData = {};
let pageInitialProps = {};

// ssr enabled and the server has returned data
// @ts-ignore
if (window.__ICE_APP_DATA__) {
// @ts-ignore
initialData = window.__ICE_APP_DATA__;
// @ts-ignore
pageInitialProps = window.__ICE_PAGE_PROPS__;
renderApp(appConfig, { initialData, pageInitialProps });
if ((window as any).__ICE_APP_DATA__) {
initialData = (window as any).__ICE_APP_DATA__;
pageInitialProps = (window as any).__ICE_PAGE_PROPS__;
} else {
// ssr not enabled, or SSR is enabled but the server does not return data
// eslint-disable-next-line
if (appConfig.app && appConfig.app.getInitialData) {
(async() => {
// @ts-ignore
initialData = await appConfig.app.getInitialData();
renderApp(appConfig, { initialData, pageInitialProps });
})();
} else {
renderApp(appConfig, {});
initialData = await appConfig.app.getInitialData();
}
}

const context = { initialData, pageInitialProps };
renderApp(context, options);
}

function renderApp(appConfig: any, context = {}) {
const { runtime, appConfig: modifiedAppConfig } = _createBaseApp(appConfig, {}, context);
function renderApp(context, options) {
const { appConfig, staticConfig, createBaseApp, ErrorBoundary, emitLifeCycles } = options;
const { runtime, appConfig: modifiedAppConfig } = createBaseApp(appConfig, {}, context);
const { modifyDOMRender } = runtime;
const { rootId, mountNode, ErrorBoundaryFallback, onErrorBoundaryHander, errorBoundary } = modifiedAppConfig.app;

// for miniapp routes
let routes = [];
if (staticConfig && staticConfig.routes) {
routes = staticConfig.routes.map(route => {
const pageComponent = route.component()();
return {
...route,
component: pageComponent
};
});
};

const AppProvider = runtime.composeAppProvider();
const AppRouter = runtime.getAppRouter();
const AppRouter = runtime.getAppRouter(routes);

function App() {
const appRouter = <AppRouter />;
const rootApp = AppProvider ? <AppProvider>{appRouter}</AppProvider> : appRouter;
if (errorBoundary) {
return (
<ErrorBoundaryComp Fallback={ErrorBoundaryFallback} onError={onErrorBoundaryHander}>
<ErrorBoundary Fallback={ErrorBoundaryFallback} onError={onErrorBoundaryHander}>
{rootApp}
</ErrorBoundaryComp>
</ErrorBoundary>
);
}
return rootApp;
}

// Emit app launch cycle
_emitLifeCycles();
emitLifeCycles();

if (process.env.__IS_SERVER__) {
return ReactDOMServer.renderToString(<App />);
Expand All @@ -89,8 +82,7 @@ function renderApp(appConfig: any, context = {}) {
if (modifyDOMRender) {
return modifyDOMRender({ App, appMountNode });
} else {
// @ts-ignore
return ReactDOM[window.__ICE_SSR_ENABLED__ ? 'hydrate' : 'render'](<App />, appMountNode);
return ReactDOM[(window as any).__ICE_SSR_ENABLED__ ? 'hydrate' : 'render'](<App />, appMountNode);
}
}
}
Expand Down

0 comments on commit 3bbcf38

Please sign in to comment.