Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add showcase egg&umi with typescript #108

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions assets-with-umi-ts/.autod.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

module.exports = {
write: true,
plugin: 'autod-egg',
prefix: '^',
devprefix: '^',
exclude: [
'test/fixtures',
'coverage',
],
dep: [
'egg',
'egg-scripts',
],
devdep: [
'autod',
'autod-egg',
'egg-bin',
'tslib',
'typescript',
],
keep: [
],
semver: [
],
test: 'scripts',
};
16 changes: 16 additions & 0 deletions assets-with-umi-ts/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# http://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

[Makefile]
indent_style = tab
2 changes: 2 additions & 0 deletions assets-with-umi-ts/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BROWSER=none
ESLINT=1
4 changes: 4 additions & 0 deletions assets-with-umi-ts/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#coverage
coverage/*
#node_modules
node_modules/*
11 changes: 11 additions & 0 deletions assets-with-umi-ts/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "eslint-config-umi",
"parserOptions": {
"ecmaVersion": 2017,
"ecmaFeatures": {
"arrowFunctions": true,
"generators": true,
"jsx": true
}
}
}
18 changes: 18 additions & 0 deletions assets-with-umi-ts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/npm-debug.log*
/yarn-error.log
/yarn.lock
/package-lock.json

# production
/dist

# misc
.DS_Store

# umi
.umi
.umi-production
7 changes: 7 additions & 0 deletions assets-with-umi-ts/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
**/*.md
**/*.svg
**/*.ejs
**/*.html
package.json
.umi
.umi-production
11 changes: 11 additions & 0 deletions assets-with-umi-ts/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"singleQuote": true,
"trailingComma": "all",
"printWidth": 100,
"overrides": [
{
"files": ".prettierrc",
"options": { "parser": "json" }
}
]
}
10 changes: 10 additions & 0 deletions assets-with-umi-ts/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
sudo: false
language: node_js
node_js:
- '8'
install:
- npm i npminstall && npminstall
script:
- npm run ci
after_script:
- npminstall codecov && codecov
13 changes: 13 additions & 0 deletions assets-with-umi-ts/.umirc.local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { IConfig } from 'umi-types';

// ref: https://umijs.org/config/
const config: IConfig = {
proxy: {
'/restapi': {
target: 'http://127.0.0.1:7001/',
changeOrigin: true,
},
},
};

export default config;
14 changes: 14 additions & 0 deletions assets-with-umi-ts/.umirc.prod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { IConfig } from 'umi-types';

// ref: https://umijs.org/config/
// tslint:disable-next-line:no-multi-spaces
const config: IConfig = {
hash: true,
publicPath: '',
outputPath: './app/public',
manifest: {
fileName: '../../config/manifest.json',
},
};

export default config;
31 changes: 31 additions & 0 deletions assets-with-umi-ts/.umirc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { IConfig } from 'umi-types';

// ref: https://umijs.org/config/
const config: IConfig = {
plugins: [
// ref: https://umijs.org/plugin/umi-plugin-react.html
[ 'umi-plugin-react', {
antd: true,
dva: true,
title: 'assets-with-umi-ts',
dll: false,
dynamicImport: {
webpackChunkName: true,
},
routes: {
exclude: [
/models\//,
/services\//,
/model\.(t|j)sx?$/,
/service\.(t|j)sx?$/,
/components\//,
],
},
}],
],
runtimePublicPath: true,
disableCSSModules: false,
cssModulesWithAffix: false,
};

export default config;
34 changes: 34 additions & 0 deletions assets-with-umi-ts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# assets-with-umi-ts

showcase for egg&umi with typescript

## QuickStart

### Development

```bash
$ npm i
$ npm run dev
$ open http://localhost:7001/
```

Don't tsc compile at development mode, if you had run `tsc` then you need to `npm run clean` before `npm run dev`.

### Deploy

```bash
$ npm run build:umi
$ npm run tsc
$ npm start
```

### Npm Scripts

- Use `npm run lint` to check code style
- Use `npm test` to run unit test
- se `npm run clean` to clean compiled js at development mode once

### Requirement

- Node.js 8.x
- Typescript 2.8+
23 changes: 23 additions & 0 deletions assets-with-umi-ts/app/controller/home.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Controller } from 'egg';
import { RequestOptions2 } from 'urllib';

export default class HomeController extends Controller {
async index() {
await this.ctx.render('index.html');
}

async api() {
const ctx = this.ctx;

const url = 'https://h5.ele.me' + ctx.path.replace(/^\/api/, '') + '?' + ctx.querystring;

console.log(url);
const options: RequestOptions2 = {
// @ts-ignore
method: this.ctx.method,
};
const res = await this.ctx.curl(url, options) ;
ctx.body = res.data;
ctx.status = res.status;
}
}
7 changes: 7 additions & 0 deletions assets-with-umi-ts/app/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Application } from 'egg';

export default (app: Application) => {
const { router, controller } = app;
router.all('/restapi/*', controller.home.api);
router.get('*', controller.home.index);
};
11 changes: 11 additions & 0 deletions assets-with-umi-ts/app/service/Test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Service } from 'egg';

/**
* Test Service
*/
export default class Test extends Service {

async sayHi(name: string): Promise<string> {
return `hi, ${name}`;
}
}
25 changes: 25 additions & 0 deletions assets-with-umi-ts/app/view/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="apple-mobile-web-app-capable" content="yes"/>
<meta name="apple-mobile-web-app-status-bar-style" content="black"/>
<meta name="format-detection" content="telephone=no"/>
<meta name="format-detection" content="email=no"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" />
<title></title>
{% if ctx.app.config.env !== 'local' -%}
{{ helper.assets.getStyle('umi.css') | safe }}
{%- endif %}
</head>
<body>

<div id="root"></div>

<script>
window.routerBase = '/';
window.resourceBaseUrl = '{{ helper.assets.resourceBase }}';
</script>
{{ helper.assets.getScript('umi.js') | safe }}
</body>
</html>
14 changes: 14 additions & 0 deletions assets-with-umi-ts/appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
environment:
matrix:
- nodejs_version: '8'

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
55 changes: 55 additions & 0 deletions assets-with-umi-ts/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg';

export default (appInfo: EggAppInfo) => {
const config = {} as PowerPartial<EggAppConfig>;

// override config from framework / plugin
// use for cookie sign key, should change to your own and keep security
config.keys = appInfo.name + '_1559730819165_5307';

// add your egg config in here
config.middleware = [];

config.view = {
mapping: {
'.html': 'nunjucks',
},
};

config.assets = {
publicPath: '/public/',
devServer: {
debug: true,
command: 'umi dev',
port: 8000,
env: {
APP_ROOT: process.cwd(),
BROWSER: 'none',
ESLINT: 'none',
SOCKET_SERVER: 'http://127.0.0.1:8000',
PUBLIC_PATH: 'http://127.0.0.1:8000',
},
},
};

config.security = {
csrf: false,
};

config.logger = {
level: 'INFO',
consoleLevel: 'INFO',
allowDebugAtProd: true,
};

// add your special config in here
const bizConfig = {
sourceUrl: `https://github.com/eggjs/examples/tree/master/${appInfo.name}`,
};

// the return config will combines to EggAppConfig
return {
...config,
...bizConfig,
};
};
11 changes: 11 additions & 0 deletions assets-with-umi-ts/config/config.local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { EggAppConfig, PowerPartial } from 'egg';

export default () => {
const config: PowerPartial<EggAppConfig> = {};
config.logger = {
level: 'INFO',
consoleLevel: 'INFO',
allowDebugAtProd: true,
};
return config;
};
11 changes: 11 additions & 0 deletions assets-with-umi-ts/config/config.prod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { EggAppConfig, PowerPartial } from 'egg';

export default () => {
const config: PowerPartial<EggAppConfig> = {};
config.logger = {
level: 'INFO',
consoleLevel: 'INFO',
allowDebugAtProd: true,
};
return config;
};
14 changes: 14 additions & 0 deletions assets-with-umi-ts/config/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { EggPlugin } from 'egg';

const plugin: EggPlugin = {
nunjucks: {
enable: true,
package: 'egg-view-nunjucks',
},
assets : {
enable: true,
package: 'egg-view-assets',
},
};

export default plugin;
Empty file.
Loading
Oops, something went wrong.