Skip to content
This repository has been archived by the owner on Jul 2, 2020. It is now read-only.

Commit

Permalink
fix: package root dir (#122)
Browse files Browse the repository at this point in the history
* fix: process

* fix: package rootDir
  • Loading branch information
echosoar committed Apr 9, 2020
1 parent 1f54bc2 commit 2c3f9ef
Show file tree
Hide file tree
Showing 17 changed files with 342 additions and 10 deletions.
17 changes: 7 additions & 10 deletions packages/faas-cli-plugin-package/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,16 +334,13 @@ export class PackagePlugin extends BasePlugin {
return;
}
this.core.cli.log(' - Using tradition build mode');
if (this.codeAnalyzeResult.integrationProject) {
await compileInProject(this.servicePath, join(this.midwayBuildPath, 'dist'), this.mwccHintConfig, {
compilerOptions: { sourceRoot: '../src' },
include: [this.codeAnalyzeResult.tsCodeRoot]
});
} else {
await compileInProject(this.servicePath, join(this.midwayBuildPath, 'dist'), this.mwccHintConfig, {
compilerOptions: { sourceRoot: '../src' }
});
}
await compileInProject(this.servicePath, join(this.midwayBuildPath, 'dist'), this.mwccHintConfig, {
compilerOptions: {
sourceRoot: '../src',
rootDir: this.codeAnalyzeResult.tsCodeRoot
},
include: [this.codeAnalyzeResult.tsCodeRoot]
});
const tmpOutDir = resolve(this.defaultTmpFaaSOut, 'src');
if (existsSync(tmpOutDir)) {
await compileInProject(this.servicePath, join(this.midwayBuildPath, 'dist'), this.mwccHintConfig, {
Expand Down
10 changes: 10 additions & 0 deletions packages/faas-cli-plugin-package/test/fixtures/ts-dir/build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"entry": "src/index.tsx",
"plugins": [
"build-plugin-ice-app",
["build-plugin-fusion", {
"themePackage": "@icedesign/theme"
}],
"./build.plugin.js"
]
}
11 changes: 11 additions & 0 deletions packages/faas-cli-plugin-package/test/fixtures/ts-dir/f.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
service: fc-test

provider:
name: fc
runtime: nodejs10

functions:
test1:
handler: index.handler
events:
- http: true
18 changes: 18 additions & 0 deletions packages/faas-cli-plugin-package/test/fixtures/ts-dir/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@icedesign/ts-scaffold9ade4c90-5de3-11e9-aef1-79dc0073b61f",
"version": "1.0.3",
"description": "该模板基于 TypeScript 适用于从 0 到 1 开始搭建项目,内置基础的页面,路由和菜单展示",
"dependencies": {
"@midwayjs/faas": "*",
"react-router-dom": "*",
"tslib": "*"
},
"scripts": {
"start": "build-scripts start",
"build": "build-scripts build",
"lint": "eslint . --ext '.js,.jsx' --fix"
},
"engines": {
"node": ">=8.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
provide,
func,
FunctionHandler,
inject,
FaaSContext,
} from '@midwayjs/faas';

@provide()
@func('index.handler')
export class IndexHandler implements FunctionHandler {
@inject()
ctx: FaaSContext;
/**
* 发布为 hsf 时
* 这个参数是 ginkgo 固定的,入参出参都为字符串
* @param event
*/
async handler(event: string) {
return 'hello';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';

export interface Props {
name: string;
}

const Greeting = ({ name }: Props) => {
return (
<div style={{ textAlign: 'center', fontSize: '40px', fontWeight: 'bold' }}>
Hello, {name}
</div>
);
};

export default Greeting;
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import React from 'react';
import { Button } from '@alifd/next';

const Guide = () => {
return (
<div style={{ width: '400px', margin: '40px auto' }}>
<h2 style={{ textAlign: 'center' }}>使用指南</h2>
<ul>
<li style={styles.item}>
1. 该模板适用于从 0 到 1 开始搭建项目,内置引导页面,路由和菜单展示。
</li>
<li style={styles.item}>2. 菜单配置: menuConfig.js。</li>
<li style={styles.item}>3. 路由配置: routerConfig.js。</li>
<li style={styles.item}>
4. 通过 GUI 工具{' '}
<a
href="https://alibaba.github.io/ice/iceworks"
target="_blank"
rel="noopener noreferrer"
>
Iceworks
</a>{' '}
创建页面,会同步的更新菜单和路由配置。
</li>
<li style={styles.item}>
5. 基于{' '}
<a
href="https://alibaba.github.io/ice/block"
target="_blank"
rel="noopener noreferrer"
>
物料
</a>{' '}
生成的页面将会添加在 pages 目录。
</li>
<li style={styles.item}>
6. 让前端工程变的轻松便捷,
<a
href="https://alibaba.github.io/ice/docs/iceworks"
target="_blank"
rel="noopener noreferrer"
>
下载 iceworks
</a>{' '}
</li>
</ul>
<div style={{ textAlign: 'center', marginTop: '40px' }}>
<a
href="https://alibaba.github.io/ice/docs/iceworks"
target="_blank"
rel="noopener noreferrer"
>
<Button type="secondary" style={{ marginRight: '20px' }}>
快速开始{' '}
</Button>
</a>
<a
href="https://www.tslang.cn/docs/home.html"
target="_blank"
rel="noopener noreferrer"
>
<Button type="primary">学习 TypeScript</Button>
</a>
</div>
</div>
);
};

const styles = {
item: {
height: '34px',
lineHeight: '34px',
},
};

export default Guide;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import ReactDOM from 'react-dom';

// 载入默认全局样式 normalize
import '@alifd/next/reset.scss';

import router from './router';

const ICE_CONTAINER = document.getElementById('ice-container');

if (!ICE_CONTAINER) {
throw new Error('当前页面不存在 <div id="ice-container"></div> 节点.');
}

ReactDOM.render(router(), ICE_CONTAINER);
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React, { Component } from 'react';
import { Redirect, Switch, Route } from 'react-router-dom';
import routerConfig from '../../routerConfig';
import Guide from '../../components/Guide';

class MainRoutes extends Component {
/**
* 渲染路由组件
*/
renderNormalRoute = (item, index) => {
return item.component ? (
<Route
key={index}
path={item.path}
component={item.component}
exact={item.exact}
/>
) : null;
};

render() {
return (
<Switch>
{/* 渲染路由表 */}
{routerConfig.map(this.renderNormalRoute)}

{/* 首页默认重定向到 /dashboard */}
<Redirect exact from="/" to="/dashboard" />

{/* 未匹配到的路由重定向到 <Guide> 组件,实际情况应该重定向到 404 */}
<Route component={Guide} />
</Switch>
);
}
}

export default MainRoutes;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React, { Component } from 'react';
import MainRoutes from './MainRoutes';

export default class BasicLayout extends Component {
render() {
return (
<div style={{ paddingTop: '100px' }}>
<MainRoutes />
</div>
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// 菜单配置

const asideMenuConfig = [];

export { asideMenuConfig };
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React, { Component } from 'react';
import Guide from '../../components/Guide';
import Greeting from '../../components/Greeting';

export default class Dashboard extends Component {
render() {
return (
<div>
<Greeting name="TypeScript" />
<Guide />
</div>
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* 定义应用路由
*/
import { HashRouter, Switch, Route } from 'react-router-dom';
import React from 'react';
import BasicLayout from './layouts/BasicLayout';

const router = () => {
return (
<HashRouter>
<Switch>
<Route path="/" component={BasicLayout} />
</Switch>
</HashRouter>
);
};

export default router;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 以下文件格式为描述路由的协议格式
// 你可以调整 routerConfig 里的内容
// 变量名 routerConfig 为 iceworks 检测关键字,请不要修改名称
import Dashboard from './pages/Dashboard';

const routerConfig = [
{
path: '/dashboard',
component: Dashboard,
},
];

export default routerConfig;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function foo () {
return 'bar'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"compileOnSave": false,
"buildOnSave": false,
"compilerOptions": {
"baseUrl": ".",
"outDir": "build",
"module": "esnext",
"target": "es6",
"jsx": "react",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"lib": ["es6", "dom"],
"sourceMap": true,
"allowJs": true,
"rootDir": "src",
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noImplicitAny": false,
"importHelpers": true,
"strictNullChecks": true,
"suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": true,
"skipLibCheck": true
},
"include": ["src/*"],
"exclude": ["node_modules", "build", "public"]
}
38 changes: 38 additions & 0 deletions packages/faas-cli-plugin-package/test/package-ice.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { CommandHookCore, loadSpec } from '@midwayjs/fcli-command-core';
import { PackagePlugin } from '../src/index';
import { resolve, join } from 'path';
import { existsSync, remove } from 'fs-extra';
import * as assert from 'assert';

describe('/test/package-ice.test.ts', () => {
describe('package base midway faas project', () => {
const baseDir = resolve(__dirname, './fixtures/ts-dir');

afterEach(async () => {
await remove(join(baseDir, 'serverless.zip'));
await remove(join(baseDir, 'package-lock.json'));
// await remove(join(baseDir, '.serverless'));
await remove(join(baseDir, 'node_modules'));
});
it('base package', async () => {
const core = new CommandHookCore({
config: {
servicePath: baseDir,
},
commands: ['package'],
service: loadSpec(baseDir),
provider: 'aliyun',
options: {
sourceDir: 'src/apis'
},
log: console,
});
core.addPlugin(PackagePlugin);
await core.ready();
await core.invoke(['package']);
const buildPath = join(baseDir, '.serverless');
assert(existsSync(join(buildPath, 'dist/index.js')));
assert(!existsSync(join(buildPath, 'dist/apis')));
});
});
});

0 comments on commit 2c3f9ef

Please sign in to comment.