Skip to content

Commit 5d485f9

Browse files
committed
🐛 更新build脚本. 更新 hot middleware. 等.
1 parent dc51693 commit 5d485f9

File tree

7 files changed

+104
-53
lines changed

7 files changed

+104
-53
lines changed

README.md

Lines changed: 63 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## Install
44

55
```sh
6-
npm install -g @micro-app/cli
6+
yarn add @micro-app/cli
77
```
88

99
or
@@ -17,55 +17,90 @@ npm install -D @micro-app/cli
1717
1. 在项目 `根目录` 初始化创建一个 `micro-app.config.js` 文件
1818

1919
```sh
20-
micro-app init
20+
npx micro-app init
2121
```
2222

2323
2.`micro-app.config.js` 配置文件进行编辑
2424

2525
```js
2626
module.exports = {
27-
name: '@micro-app/demo', // 名称
28-
description: '', // 描述
29-
version: '0.0.1', // 版本
27+
name: '@micro-app/demo',
28+
description: '',
29+
version: '0.0.1',
3030
type: '', // types 类型
3131
webpack: { // webpack 配置
32-
entry: {
33-
34-
},
3532
// output: {
3633
// path: path.resolve(__dirname, 'public'),
3734
// publicPath: '/public/',
3835
// },
39-
resolve: {
40-
alias: {},
41-
// modules: [],
42-
},
43-
plugins: [],
4436
},
45-
alias: { // 前端共享接口
46-
api: '',
37+
38+
entry: {
39+
main: './test/index.js',
4740
},
48-
shared: { // 后端共享接口
49-
config: '',
50-
// middleware: '', // koa-middleware
51-
// router: '', // koa-router
41+
42+
htmls: [
43+
{
44+
template: './test/index.js',
45+
},
46+
],
47+
48+
// dlls: [
49+
// {
50+
// context: __dirname,
51+
// },
52+
// ],
53+
54+
alias: { // 前端
55+
api: '',
56+
config: {
57+
link: '',
58+
description: '配置',
59+
},
60+
service: {
61+
link: '',
62+
description: '接口',
63+
type: 'server',
64+
},
5265
},
5366

67+
strict: true,
68+
5469
micros: [ 'test' ], // 被注册的容器
5570
// micros$$test: { // 单独配置
5671
// disabled: true, // 禁用入口
72+
// link: '', // 本地路径, 进行本地开发使用的软链接.
5773
// },
5874

5975
// 服务配置
6076
server: {
6177
entry: '', // 服务端入口
6278
port: 8088, // 服务端口号
63-
// staticBase: 'public', // 静态文件地址
6479
contentBase: 'public', // 静态文件地址
6580
options: {
6681
// 服务端回调参数
6782
},
6883
},
84+
85+
plugins: [
86+
// [{
87+
// id: 'test',
88+
// description: '这是test',
89+
// link: __dirname + '/test/testPlugin',
90+
// }, {
91+
// a: 1,
92+
// }],
93+
],
94+
95+
// deploy: {
96+
// git: 'git+ssh://[email protected]',
97+
// branch: 'test',
98+
// // branch: {
99+
// // name: 'develop',
100+
// // extends: true,
101+
// // },
102+
// message: '', // 提交 message 中增加内容
103+
// },
69104
};
70105
```
71106

@@ -80,19 +115,19 @@ module.exports = {
80115
4. 开发模式
81116

82117
```sh
83-
micro-app-dev
118+
npx micro-app serve
84119
```
85120

86121
5. Build
87122

88123
```sh
89-
micro-app-build
124+
npx micro-app build
90125
```
91126

92127
6. 运行
93128

94129
```sh
95-
micro-app-build
130+
npx micro-app start
96131
```
97132

98133
## 项目中使用共享接口
@@ -101,22 +136,24 @@ micro-app-build
101136
const api = require('@micro-demo/api');
102137
```
103138

139+
## Plugins 扩展
140+
104141
## 其他
105142

106143
- 展示所有容器
107144

108145
```js
109-
micro-app -l
146+
npx micro-app show micros
110147
```
111148

112149
- 展示所有前端共享接口
113150

114151
```js
115-
micro-app -s alias
152+
npx micro-app show alias
116153
```
117154

118155
- 展示所有后端共享接口
119156

120157
```js
121-
micro-app -s shared
158+
npx micro-app show shared
122159
```

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@micro-app/cli",
3-
"version": "0.1.0-beta.19",
3+
"version": "0.1.0-RC2",
44
"description": "micro app cli",
55
"bin": {
66
"micro-app": "./bin/micro-app.js",
@@ -34,7 +34,7 @@
3434
"webpack": "^4.39.2"
3535
},
3636
"dependencies": {
37-
"@micro-app/core": "^0.1.0-beta.26",
37+
"@micro-app/core": "^0.1.0-RC",
3838
"koa": "^2.8.1",
3939
"koa-static": "^5.0.0",
4040
"opn": "^5.5.0",

plugins/commands/build.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ function runServe(api, type) {
5151

5252
if (type === 'vusion') {
5353
const vusionAdapter = require('../../src/adapter/vusion')(webpackConfig, false, {
54+
modifyDefaultVusionConfig(vusionConfig) {
55+
return api.applyPluginHooks('modifyDefaultVusionConfig', vusionConfig);
56+
},
5457
resolveVusionConfig(vusionConfig) {
5558
return api.applyPluginHooks('modifyVusionConfig', vusionConfig);
5659
},
@@ -66,6 +69,15 @@ function runServe(api, type) {
6669
webpackDevOptions = webpackAdapter.devOptions || {};
6770
}
6871

72+
// [ 'post', 'host', 'contentBase', 'entrys', 'hooks' ]; // serverConfig
73+
const info = {
74+
type,
75+
config: api.config,
76+
serverConfig: api.serverConfig,
77+
onlyNode: false,
78+
webpackConfig,
79+
};
80+
6981
// 更新一次
7082
api.setState('webpackConfig', webpackConfig);
7183

@@ -76,16 +88,8 @@ function runServe(api, type) {
7688
devOptions: webpackDevOptions,
7789
});
7890

79-
// [ 'post', 'host', 'contentBase', 'entrys', 'hooks' ]; // serverConfig
80-
const info = {
81-
type,
82-
config: api.config,
83-
serverConfig: api.serverConfig,
84-
onlyNode: false,
85-
webpackConfig,
86-
compiler,
87-
devOptions,
88-
};
91+
info.compiler = compiler;
92+
info.devOptions = devOptions;
8993

9094
return new Promise((resolve, reject) => {
9195
const spinner = logger.spinner('Building for production...');

src/server/createServer.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ const tryRequire = require('try-require');
99
const HookEvent = require('./HookEvent');
1010
const staticServer = require('./staticServer');
1111

12-
module.exports = function(options = {}, api) {
12+
module.exports = function(opts = {}, api) {
1313
const logger = api.logger;
14-
const isDev = options.isDev || false;
15-
const onlyNode = options.onlyNode || false;
14+
const isDev = opts.isDev || false;
15+
const onlyNode = opts.onlyNode || false;
1616
if (isDev) {
1717
logger.info('Dev Server Start...');
1818
}
1919

20-
const serverConfig = options.serverConfig;
20+
const serverConfig = opts.serverConfig;
2121

2222
const app = new Koa();
2323
// 兼容koa1的中间件
@@ -49,8 +49,8 @@ module.exports = function(options = {}, api) {
4949
applyHooks(_HookEvent, 'after');
5050

5151
if (isDev) {
52-
const compiler = options.compiler || false;
53-
const devOptions = options.devOptions || {};
52+
const compiler = opts.compiler || false;
53+
const devOptions = opts.devOptions || {};
5454
if (!onlyNode && !!compiler) {
5555
const koaWebpackMiddleware = require('./koa-webpack-middleware');
5656
app.use(koaWebpackMiddleware.devMiddleware(compiler, devOptions));
@@ -61,14 +61,24 @@ module.exports = function(options = {}, api) {
6161
const { contentBase, options = {} } = serverConfig;
6262
const koaStatic = staticServer(contentBase, options);
6363
if (koaStatic) {
64+
if (opts.type === 'vusion') {
65+
const config = opts.config;
66+
const prePath = contentBase.replace(config.root, '');
67+
app.use((ctx, next) => {
68+
if (ctx.url) {
69+
ctx.url = ctx.url.replace(prePath, '');
70+
}
71+
return next();
72+
});
73+
}
6474
app.use(koaStatic);
6575
}
6676
}
6777

6878
api.applyPluginHooks('onServerInitDone', { app, config: serverConfig });
6979

70-
const port = options.port || serverConfig.port || 8888;
71-
const host = options.host || serverConfig.host || 'localhost';
80+
const port = opts.port || serverConfig.port || 8888;
81+
const host = opts.host || serverConfig.host || 'localhost';
7282
return new Promise((resolve, reject) => {
7383
app.listen(port, host === 'localhost' ? '0.0.0.0' : host, err => {
7484
if (err) {

src/server/koa-webpack-middleware/devMiddleware.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = function(compiler, opts) {
77
const expressMiddleware = devMiddleware(compiler, opts);
88

99
async function middleware(ctx, next) {
10-
await expressMiddleware(ctx.req, {
10+
return await expressMiddleware(ctx.req, {
1111
end: content => {
1212
ctx.body = content;
1313
},

src/server/koa-webpack-middleware/hotMiddleware.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ module.exports = function(compiler, opts) {
1010
const expressMiddleware = hotMiddleware(compiler, opts);
1111
return async (ctx, next) => {
1212
const stream = new PassThrough();
13-
ctx.body = stream;
14-
await expressMiddleware(ctx.req, {
13+
return await expressMiddleware(ctx.req, {
1514
write: stream.write.bind(stream),
1615
writeHead: (status, headers) => {
16+
ctx.body = stream; // ok 才赋值
1717
ctx.status = status;
1818
ctx.set(headers);
1919
},

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,10 @@
182182
lodash "^4.17.13"
183183
to-fast-properties "^2.0.0"
184184

185-
"@micro-app/core@^0.1.0-beta.26":
186-
version "0.1.0-beta.26"
187-
resolved "https://registry.yarnpkg.com/@micro-app/core/-/core-0.1.0-beta.26.tgz#9c98691a4cd8b4758069ce1b792be3bd8c59dabe"
188-
integrity sha512-5lwI9eYg2QUeN1lMWYlyBClMhBXX1gzyiJlcbJyp7DDYtrkRIiMXu+5ve+BGbny7sEcXveBNcOLTIlC5/P4nwg==
185+
"@micro-app/core@^0.1.0-RC":
186+
version "0.1.0-RC"
187+
resolved "https://registry.yarnpkg.com/@micro-app/core/-/core-0.1.0-RC.tgz#9479c5e0e72648b17335c26d243648f9a9408994"
188+
integrity sha512-p8/bklly6y5ajOrB3Cjp/M16gbD8xiS3Dc7VkBVTOWp2nOMZzu2m6+Ms6a5V1e75mQGPGXk+15zQXg2YD4f/hA==
189189
dependencies:
190190
ajv "^6.10.2"
191191
ajv-keywords "^3.4.1"

0 commit comments

Comments
 (0)