Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
djyde committed Aug 5, 2018
0 parents commit c9ea365
Show file tree
Hide file tree
Showing 23 changed files with 10,984 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .autod.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

module.exports = {
write: true,
prefix: '^',
plugin: 'autod-egg',
test: [
'test',
'benchmark',
],
devdep: [
'egg',
'egg-ci',
'egg-bin',
'autod',
'autod-egg',
'eslint',
'eslint-config-egg',
'webstorm-disable-index',
],
exclude: [
'./test/fixtures',
'./docs',
'./coverage',
],
};
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage
client/
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "eslint-config-egg"
}
24 changes: 24 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!--
Thank you for your pull request. Please review below requirements.
Bug fixes and new features should include tests and possibly benchmarks.
Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
感谢您贡献代码。请确认下列 checklist 的完成情况。
Bug 修复和新功能必须包含测试,必要时请附上性能测试。
Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
-->

##### Checklist
<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [ ] `npm test` passes
- [ ] tests and/or benchmarks are included
- [ ] documentation is changed or added
- [ ] commit message follows commit guidelines

##### Affected core subsystem(s)
<!-- Provide affected core subsystem(s). -->


##### Description of change
<!-- Provide a description of the change below this comment. -->
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
logs/
npm-debug.log
node_modules/
coverage/
.idea/
run/
.DS_Store
*.swp
package-lock.json
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
sudo: false
language: node_js
node_js:
- '8'
- '9'
install:
- npm i npminstall && npminstall
script:
- npm run ci
after_script:
- npminstall codecov && codecov
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Alibaba Group Holding Limited and other contributors.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
156 changes: 156 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
# egg-serlina

[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][codecov-image]][codecov-url]
[![David deps][david-image]][david-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]

[npm-image]: https://img.shields.io/npm/v/egg-serlina.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg-serlina
[travis-image]: https://img.shields.io/travis/eggjs/egg-serlina.svg?style=flat-square
[travis-url]: https://travis-ci.org/eggjs/egg-serlina
[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-serlina.svg?style=flat-square
[codecov-url]: https://codecov.io/github/eggjs/egg-serlina?branch=master
[david-image]: https://img.shields.io/david/eggjs/egg-serlina.svg?style=flat-square
[david-url]: https://david-dm.org/eggjs/egg-serlina
[snyk-image]: https://snyk.io/test/npm/egg-serlina/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/egg-serlina
[download-image]: https://img.shields.io/npm/dm/egg-serlina.svg?style=flat-square
[download-url]: https://npmjs.org/package/egg-serlina

egg-serlina is a [Serlina](https://github.com/djyde/serlina) binding for Egg. It brings the best SSR solution to Egg application.

## Install

```bash
$ npm i egg-serlina react react-dom --save
```

## Usage

Enable the plugin:

```js
// {app_root}/config/plugin.js
exports.serlina = {
enable: true,
package: 'egg-serlina',
};
```

Add a `client` folder and create the first page:

```diff
- app_root
- app
+ - client
+ - page
+ - page1.js
```

```js
// {app_root}/client/page/page1.js

export default () => {
return <div>Hello Serlina</div>
}
```

Then visit `http://{your_host}/page1` and you will see the React page.

## Configuration

### options

#### dev

**boolean** dev mode.

default: `appInfo.env === 'local'`

#### baseDir

**string** Serlina baseDir.

default: `path.resolve(appInfo.baseDir, './client')`

#### outputPath

**string** Serlina output files path.

#### publicPath

**string** Webpack's publicPath. Only work in `prod` mode. Usually use it when you upload the Serlina output files to CDN.

default: `/public/`

### map

**object** Using Serlina only in specific path:

```js
exports.serlina = {
map: {
'/p/page1': 'page1' // render SSR page `page1` only when the `ctx.path` is `/p/page1`
}
}
```

see [config/config.default.js](config/config.default.js) for more detail.

## Production deployment

### self serve static files

Before deploy to production, please run `serlina build` first (usually do it on CI):

```json
// ${app_root}/package.json

{
"script": {
"build": "serlina build ./client --publicPath /public/"
}
}
```

Then you need to serve the output path:

```js
// {app_root}/config/config.default.js
exports.static = {
dir: [
path.join(appInfo.baseDir, 'app/public'),
path.join(appInfo.baseDir, 'client/.serlina')
]
};
```

### server static files on CDN

```json
// ${app_root}/package.json

{
"script": {
"build": "serlina build ./client --publicPath ${YOUR_CDN_ENDPOINT}"
}
}
```

```js
// {app_root}/config/config.default.js
exports.serlina = {
publicPath: '${YOUR_CND_ENDPOINT}'
};
```

## Limitation

While Egg will restart a new worker after file changing, Serlina will restart building. Maybe it will be frustrating when the client code getting bigger. PR is welcome if you know how to deal with this problem.

## License

[MIT](LICENSE)
18 changes: 18 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const { Serlina } = require('serlina');

module.exports = app => {
const config = app.config.serlina;
app.config.coreMiddleware.push('serlinaRoute');
app.beforeStart(async () => {
const serlina = new Serlina(Object.assign({}, {
dev: config.dev,
baseDir: config.baseDir,
outputPath: config.outputPath,
publicPath: config.publicPath,
}));
await serlina.prepare();
app.serlina = serlina;
});
};
18 changes: 18 additions & 0 deletions app/middleware/serlina-route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

module.exports = () => {
return async function serlinaRoute(ctx, next) {
const config = ctx.app.config.serlina;
if (config.map) {
if (config.map[ctx.path]) {
const rendered = await ctx.app.serlina.render(config.map[ctx.path], ctx);
ctx.body = rendered.string;
await next();
}
} else {
const rendered = await ctx.app.serlina.render(ctx.path, ctx);
ctx.body = rendered.string;
await next();
}
};
};
15 changes: 15 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
environment:
matrix:
- nodejs_version: '8'
- nodejs_version: '9'

install:
- ps: Install-Product node $env:nodejs_version
- npm i npminstall && node_modules\.bin\npminstall

test_script:
- node --version
- npm --version
- npm run test

build: off
17 changes: 17 additions & 0 deletions config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

/**
* egg-serlina default config
* @member Config#serlina
* @property {String} SOME_KEY - some description
*/

const path = require('path');

exports.serlina = appInfo => {
return {
dev: appInfo.env === 'local',
baseDir: path.resolve(appInfo.baseDir, './client'),
publicPath: '/public/',
};
};
59 changes: 59 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"name": "egg-serlina",
"version": "0.0.1",
"description": "Serlina for Egg",
"eggPlugin": {
"name": "serlina"
},
"keywords": [
"egg",
"eggPlugin",
"egg-plugin"
],
"dependencies": {
"serlina": "0.0.8"
},
"devDependencies": {
"autod": "^3.0.0",
"autod-egg": "^1.0.0",
"egg": "^2.0.0",
"egg-bin": "^4.3.0",
"egg-ci": "^1.8.0",
"egg-mock": "^3.13.0",
"eslint": "^4.11.0",
"eslint-config-egg": "^5.1.0",
"react": "^16.4.2",
"react-dom": "^16.4.2",
"webstorm-disable-index": "^1.2.0"
},
"engines": {
"node": ">=8.0.0"
},
"scripts": {
"test": "npm run lint -- --fix && egg-bin pkgfiles && npm run test-local",
"test-local": "egg-bin test",
"cov": "egg-bin cov",
"lint": "eslint .",
"ci": "egg-bin pkgfiles --check && npm run lint && npm run cov",
"pkgfiles": "egg-bin pkgfiles",
"autod": "autod"
},
"files": [
"app",
"config",
"app.js"
],
"ci": {
"version": "8, 9"
},
"repository": {
"type": "git",
"url": "git+https://github.com/eggjs/egg-serlina.git"
},
"bugs": {
"url": "https://github.com/eggjs/egg/issues"
},
"homepage": "https://github.com/eggjs/egg-serlina#readme",
"author": "Randy",
"license": "MIT"
}
Loading

0 comments on commit c9ea365

Please sign in to comment.