Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Commit

Permalink
build: switch tsc to preconstruct.tools
Browse files Browse the repository at this point in the history
  • Loading branch information
Lxxyx committed Mar 8, 2021
1 parent 6045268 commit 7d36d5b
Show file tree
Hide file tree
Showing 36 changed files with 1,074 additions and 315 deletions.
14 changes: 14 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
presets: [
'@babel/preset-typescript',
[
'@babel/preset-env',
{
targets: {
node: '12',
},
},
],
],
plugins: [['@babel/plugin-proposal-class-properties', { loose: true }]],
}
16 changes: 2 additions & 14 deletions examples/jest-koa/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,7 @@
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"outDir": "lib",
"tsBuildInfoFile": "./lib/tsconfig.tsbuildinfo"
"outDir": "lib"
},
"exclude": ["lib", "node_modules", "**/__tests__"],
"references": [
{
"path": "../../packages/hooks"
},
{
"path": "../../packages/hooks-core"
},
{
"path": "../../packages/hooks-testing-library"
}
]
"exclude": ["lib", "node_modules", "**/__tests__"]
}
11 changes: 0 additions & 11 deletions examples/vite-react-faas/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,6 @@
"types": ["vite/client", "jest"]
},
"include": ["./src"],
"references": [
{
"path": "../../packages/hooks"
},
{
"path": "../../packages/hooks-core"
},
{
"path": "../../packages/vite-plugin-hooks"
}
],
"ts-node": {
"compilerOptions": {
"module": "commonjs"
Expand Down
11 changes: 0 additions & 11 deletions examples/vite-react-koa/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,6 @@
"types": ["vite/client", "jest"]
},
"include": ["./src"],
"references": [
{
"path": "../../packages/hooks"
},
{
"path": "../../packages/hooks-core"
},
{
"path": "../../packages/vite-plugin-hooks"
}
],
"ts-node": {
"compilerOptions": {
"module": "commonjs"
Expand Down
11 changes: 0 additions & 11 deletions examples/vite-vue-koa/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,6 @@
"skipLibCheck": true
},
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
"references": [
{
"path": "../../packages/hooks"
},
{
"path": "../../packages/hooks-core"
},
{
"path": "../../packages/vite-plugin-hooks"
}
],
"ts-node": {
"compilerOptions": {
"module": "commonjs"
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
'/fixtures/',
'util.ts',
'compiler.ts',
'/.serverless/',
],
testRegex: ['(/__tests__/.*|(\\.|/)(test|spec))\\.[t]sx?$'],
coveragePathIgnorePatterns: [
Expand Down
20 changes: 14 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@
},
"private": true,
"scripts": {
"build": "tsc -b packages",
"watch": "tsc --build --watch packages",
"update:tsconfig": "workspaces-to-typescript-project-references && npm run format",
"build": "preconstruct build",
"watch": "preconstruct watch",
"clean": "lerna clean && lerna run clean --parallel",
"test": "jest --passWithNoTests",
"cov": "npm run test -- --coverage",
"ci": "npm run cov -- --ci",
"format": "pretty-quick"
"format": "pretty-quick",
"postinstall": "preconstruct dev"
},
"dependencies": {
"@monorepo-utils/workspaces-to-typescript-project-references": "^2.4.3",
"@babel/plugin-proposal-class-properties": "^7.13.0",
"@babel/preset-env": "^7.13.9",
"@babel/preset-typescript": "^7.13.0",
"@preconstruct/cli": "^2.0.6",
"codecov": "^3.8.0",
"execa": "^4.0.3",
"husky": "^4.2.3",
Expand All @@ -43,5 +46,10 @@
"workspaces": [
"packages/*",
"examples/*"
]
],
"preconstruct": {
"packages": [
"packages/*"
]
}
}
3 changes: 2 additions & 1 deletion packages/build-plugin-hooks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "@midwayjs/build-plugin-hooks",
"version": "2.0.0",
"description": "Build integration plugin for Rax/Ice to convert references to backend Api in code to frontend SDK calls.",
"main": "lib/index.js",
"main": "dist/midwayjs-build-plugin-hooks.cjs.js",
"module": "dist/midwayjs-build-plugin-hooks.esm.js",
"scripts": {
"build": "tsc",
"clean": "rm -rf lib",
Expand Down
14 changes: 0 additions & 14 deletions packages/build-plugin-hooks/tsconfig.json

This file was deleted.

10 changes: 9 additions & 1 deletion packages/hooks-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"author": "Lxxyx <[email protected]>",
"homepage": "https://github.com/midwayjs/hooks",
"license": "MIT",
"main": "lib/index.js",
"main": "dist/midwayjs-hooks-core.cjs.js",
"module": "dist/midwayjs-hooks-core.esm.js",
"repository": {
"type": "git",
"url": "[email protected]:midwayjs/hooks.git"
Expand All @@ -38,10 +39,17 @@
"koa-static-cache": "^5.1.4",
"lodash": "^4.17.21",
"pkg-dir": "^5.0.0",
"tslib": "^2.1.0",
"upath": "^2.0.1"
},
"devDependencies": {
"@types/es-module-lexer": "^0.3.0",
"@types/koa-static-cache": "^5.1.0"
},
"preconstruct": {
"entrypoints": [
"request/index.ts",
"index.ts"
]
}
}
4 changes: 4 additions & 0 deletions packages/hooks-core/request/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"main": "dist/midwayjs-hooks-core-request.cjs.js",
"module": "dist/midwayjs-hooks-core-request.esm.js"
}
29 changes: 16 additions & 13 deletions packages/hooks-core/src/component/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import {
IMidwayContainer,
MidwayFrameworkType,
} from '@midwayjs/core'
import { __decorate } from 'tslib'
import { Inject, Controller, Get, Post, Provide } from '@midwayjs/decorator'
import { als } from '../runtime'
import { EnhancedFunc } from '../types/common'
import { ServerRouter, getFunctionId } from '../router'
import { getConfig, getProjectRoot } from '../config'
import { InternalConfig } from '../types/config'
import { isProduction } from '@midwayjs/hooks-core'
import { isProduction } from '../util'
import { noop } from 'lodash'
import { join } from 'path'
import staticCache from 'koa-static-cache'
Expand Down Expand Up @@ -139,27 +140,29 @@ class HooksComponent {
const { containerId, httpMethod, httpPath, fn } = config
const Method = httpMethod === 'GET' ? Get : Post

@Provide(containerId)
@Controller('/')
class FunctionContainer {
@Inject()
// Source: https://shorturl.at/pqI06
let FunctionContainer = class FunctionContainer {
ctx: any

@Method(httpPath, { middleware: fn.middleware || [] })
async handler() {
const bindCtx = {
ctx: this.ctx,
}

const bindCtx = { ctx: this.ctx }
let args = this.ctx.request?.body?.args || []
if (typeof args === 'string') {
args = JSON.parse(args)
}

return await als.run(bindCtx, async () => fn(...args))
}
}

__decorate([Inject()], FunctionContainer.prototype, 'ctx', void 0)
__decorate(
[Method(httpPath, { middleware: fn.middleware || [] })],
FunctionContainer.prototype,
'handler',
null
)
FunctionContainer = __decorate(
[Provide(containerId), Controller('/')],
FunctionContainer
)
this.container.bind(containerId, FunctionContainer)
}

Expand Down
7 changes: 2 additions & 5 deletions packages/hooks-core/src/request/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { init, parse } from 'es-module-lexer'
export async function parseAndGenerateSDK(
router: ServerRouter,
sourceFilePath: string,
code: string,
module: 'cjs' | 'esm' = 'esm'
code: string
) {
await init
const [, exports] = parse(code)
Expand All @@ -17,9 +16,7 @@ export async function parseAndGenerateSDK(
const baseUrl = router.getBaseUrl(sourceFilePath)

return `
import { createRequest } from '@midwayjs/hooks-core/lib/${
module === 'esm' ? 'esm/' : ''
}request/sdk';
import { createRequest } from '@midwayjs/hooks-core/request';
${exports
.map((id) => {
Expand Down
36 changes: 33 additions & 3 deletions packages/hooks-core/src/request/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,39 @@
import type { LambdaParam } from '../types/http'
import axios from 'axios'
import { LambdaParam } from '../types/http'

export const defaults = axios.defaults
const defaults = axios.defaults

export async function request(param: LambdaParam) {
async function request(param: LambdaParam) {
const response = await axios(param)
return response.data
}
/**
* @internal private api
*/

function createRequest(baseUrl, name) {
return (...args) => {
return request({
url: getUrl(baseUrl, name),
method: args.length === 0 ? 'GET' : 'POST',
data: {
args,
},
meta: {},
})
}
}

function getUrl(baseUrl, name) {
if (name === 'default') {
return baseUrl
}

if (baseUrl.endsWith('/')) {
return `${baseUrl}${name}`
}

return `${baseUrl}/${name}`
}

export { createRequest, defaults, request }
24 changes: 0 additions & 24 deletions packages/hooks-core/src/request/sdk.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/hooks-core/src/router/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HooksRouter } from './base'
import { join } from 'upath'
import { InternalConfig } from '../types/config'
import { isProduction } from '@midwayjs/hooks-core'
import { isProduction } from '../util'

export class ServerRouter extends HooksRouter {
config: InternalConfig
Expand Down
10 changes: 0 additions & 10 deletions packages/hooks-core/tsconfig.json

This file was deleted.

3 changes: 2 additions & 1 deletion packages/hooks-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "@midwayjs/hooks-loader",
"version": "2.0.0",
"description": "Midway Hooks Webpack Loader",
"main": "lib/index.js",
"main": "dist/midwayjs-hooks-loader.cjs.js",
"module": "dist/midwayjs-hooks-loader.esm.js",
"scripts": {
"build": "tsc",
"clean": "rm -rf lib",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`hooks loader with proxy Compile lambda 1`] = `
import { createRequest } from '@midwayjs/hooks-core/lib/request/sdk';
import { createRequest } from '@midwayjs/hooks-core/request';
export default createRequest('/api', 'default');
`;

exports[`hooks loader with proxy Compile render 1`] = `
import { createRequest } from '@midwayjs/hooks-core/lib/request/sdk';
import { createRequest } from '@midwayjs/hooks-core/request';
export default createRequest('/', 'default');
export const foo = createRequest('/', 'foo');
Expand Down
5 changes: 4 additions & 1 deletion packages/hooks-loader/src/__tests__/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export default (
{
test: /\.ts?$/,
use: {
loader: path.resolve(__dirname, '../../lib/index.js'),
loader: path.resolve(
__dirname,
'../../dist/midwayjs-hooks-loader.cjs.js'
),
},
},
{ test: /\.tsx?$/, loader: 'ts-loader' },
Expand Down
Loading

0 comments on commit 7d36d5b

Please sign in to comment.