Skip to content

Commit

Permalink
feat: server.js for server-only hooks (#16)
Browse files Browse the repository at this point in the history
* feat: server.js for server-only hooks

* test: test html content
  • Loading branch information
liximomo authored Jul 1, 2020
1 parent 9a44420 commit d2528f0
Show file tree
Hide file tree
Showing 17 changed files with 120 additions and 267 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"test": "yarn test:fixtures && yarn test:unit",
"test:unit": "jest packages",
"test:fixtures": "jest test/fixtures",
"test:e2e": "jest test/e2e"
"test:e2e": "jest test/e2e --runInBand"
},
"husky": {
"hooks": {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/app/components/PluginFile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useSelector } from '../models/store';
function Plugin() {
const plugins = useSelector(state => state.runtimePlugins);

// TODO: move userPlugin create to core
let content = 'import initPlugins from "@shuvi/app/core/userPlugin"\n';
let pluginsHash = '';

Expand Down
2 changes: 2 additions & 0 deletions packages/runtime-core/src/lib/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export class Application<Context extends {}> extends Hookable
await this._init();
await this._createApplicationContext();
await this._render();

return this._context;
}

async rerender() {
Expand Down
14 changes: 6 additions & 8 deletions packages/shuvi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,22 @@
"commander": "5.1.0",
"connect": "3.7.0",
"cross-spawn": "7.0.1",
"detect-port": "^1.3.0",
"dotenv": "^8.2.0",
"dotenv-expand": "^5.1.0",
"detect-port": "1.3.0",
"dotenv": "8.2.0",
"dotenv-expand": "5.1.0",
"ejs": "3.0.1",
"fs-extra": "8.1.0",
"http-proxy-middleware": "1.0.3",
"send": "0.17.1",
"tapable": "1.1.3",
"webpack": "4.41.2",
"webpack-dev-middleware": "3.7.2",
"webpack-hot-middleware": "2.25.0"
},
"devDependencies": {
"@types/connect": "^3.4.33",
"@types/cross-spawn": "6.0.1",
"@types/detect-port": "^1.3.0",
"@types/dotenv": "^8.2.0",
"@types/ejs": "3.0.4",
"@types/tapable": "^1.0.5"
"@types/detect-port": "1.3.0",
"@types/dotenv": "8.2.0",
"@types/ejs": "3.0.4"
}
}
17 changes: 17 additions & 0 deletions packages/shuvi/src/api/setupApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ export async function setupApp(api: Api) {
}),
'core'
);
api.addAppFile(
File.moduleProxy('server.js', {
source: [
...withExts(api.resolveUserFile('server'), moduleFileExtensions),
require.resolve('@shuvi/utils/lib/noop')
]
}),
'core'
);
api.addAppFile(
File.moduleProxy('document.js', {
source: [
Expand Down Expand Up @@ -139,6 +148,10 @@ export async function setupApp(api: Api) {
File.module('server.js', {
exports: api.config.ssr
? {
[api.resolveAppFile('core', 'server')]: {
imported: '*',
local: 'server'
},
[api.resolveAppFile('core', 'document')]: {
imported: '*',
local: 'document'
Expand All @@ -153,6 +166,10 @@ export async function setupApp(api: Api) {
}
}
: {
[api.resolveAppFile('core', 'server')]: {
imported: '*',
local: 'server'
},
[api.resolveAppFile('core', 'document')]: {
imported: '*',
local: 'document'
Expand Down
1 change: 1 addition & 0 deletions packages/shuvi/src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Bundler, IApi, Runtime, IApiConfig } from '@shuvi/types';

export type IBuiltResource = {
server: {
server: Runtime.IServerModule;
application: Runtime.IApplicationModule;
document: Partial<Runtime.IDocumentModule>;
renderer: Runtime.IServerRenderer;
Expand Down
86 changes: 0 additions & 86 deletions packages/shuvi/src/lib/hooks/__tests__/hooks.test.ts

This file was deleted.

138 changes: 0 additions & 138 deletions packages/shuvi/src/lib/hooks/hooks.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/shuvi/src/lib/hooks/index.ts

This file was deleted.

11 changes: 6 additions & 5 deletions packages/shuvi/src/lib/renderToHTML.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ export async function renderToHTML({
req: Runtime.IRequest;
api: Api;
onRedirect?(redirect: Runtime.IRenderResultRedirect): void;
}): Promise<null | string> {
}): Promise<{ html: string | null; appContext: any }> {
let html: null | string = null;
const renderer = new Renderer({ api });
const { application } = api.resources.server;
const app = application.create(
{
req,
req
},
{
async render({ appContext, AppComponent, routes }) {
Expand All @@ -25,7 +25,7 @@ export async function renderToHTML({
url: req.url || '/',
AppComponent,
routes,
appContext,
appContext
});

if (isRedirect(result)) {
Expand All @@ -37,13 +37,14 @@ export async function renderToHTML({
}
);

let appContext: any;
try {
await app.run();
appContext = await app.run();
} catch (error) {
console.log('shuvi app run error', error);
} finally {
await app.dispose();
}

return html;
return { appContext, html };
}
Loading

0 comments on commit d2528f0

Please sign in to comment.