Skip to content

Commit

Permalink
feat(mock): add "type": "module" as default,for support esm
Browse files Browse the repository at this point in the history
  • Loading branch information
liangskyli committed Dec 6, 2023
1 parent e616e6f commit 593854e
Show file tree
Hide file tree
Showing 30 changed files with 285 additions and 95 deletions.
5 changes: 5 additions & 0 deletions packages/mock/bin/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node
const { commandHttpCli } = require('../lib/index.cjs');
const { version } = require('../package.json');

commandHttpCli(version);
5 changes: 4 additions & 1 deletion packages/mock/bin/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/usr/bin/env node
const { commandHttpCli } = require('../lib/index.cjs');
import { createRequire } from 'node:module';
import { commandHttpCli } from '../lib/index.js';

const require = createRequire(import.meta.url);
const { version } = require('../package.json');

commandHttpCli(version);
2 changes: 1 addition & 1 deletion packages/mock/docs/cli-mock-socket.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

```ts
import { defineConfig } from '@liangskyli/mock';
import path from 'path';
import path from 'node:path';

export default defineConfig({
mockDir: path.join(__dirname, '/'),
Expand Down
2 changes: 1 addition & 1 deletion packages/mock/docs/express-mock-socket.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

```ts
import { defineConfig } from '@liangskyli/mock';
import path from 'path';
import path from 'node:path';

export default defineConfig({
socketConfig: {
Expand Down
2 changes: 1 addition & 1 deletion packages/mock/docs/mock-server-socket.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

```ts
import mockServer from '@liangskyli/mock';
import path from 'path';
import path from 'node:path';

mockServer({
mockDir: path.join(__dirname, '/'),
Expand Down
45 changes: 24 additions & 21 deletions packages/mock/docs/webpack-mock-socket.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

```ts
import { defineConfig } from '@liangskyli/mock';
import path from 'path';
import path from 'node:path';

export default defineConfig({
socketConfig: {
Expand All @@ -26,9 +26,9 @@ export default defineConfig({
- webpack.config.ts 文件

```ts
import type WebpackDevServer from 'webpack-dev-server';
import { getMiddleware, initSocketServer } from '@liangskyli/mock';
import type Webpack from 'webpack';
import {getMiddleware, initSocketServer} from '../src';
import type WebpackDevServer from 'webpack-dev-server';
import mockConfig from './mock.config';

const socketConfig = mockConfig.socketConfig;
Expand All @@ -42,28 +42,31 @@ const webpackConfig: Webpack.Configuration = {
host,
port,
onBeforeSetupMiddleware: (devServer: WebpackDevServer) => {
if (!devServer) {
if (!devServer || !devServer.app) {
throw new Error('webpack-dev-server is not defined');
}

getMiddleware().then(({ middleware, middlewareWatcher }) => {
devServer.app.use(middleware);

devServer.app.get('/', (req, res) => {
res.send('homepage');
});
console.log('look in http://localhost:4000/');

if (socketConfig && socketConfig.enable) {
initSocketServer({
socketConfig,
server: devServer.server,
port,
hostname: host,
middlewareWatcher,
getMiddleware({ mockDir: mockConfig.mockDir }).then(
({ middleware, middlewareWatcher }) => {
devServer.app!.use(middleware);

devServer.app!.get('/', (req, res) => {
res.send('homepage');
});
}
});
console.log(`look in http://localhost:${port}/`);

if (socketConfig && socketConfig.enable) {
initSocketServer({
mockDir: mockConfig.mockDir,
socketConfig,
server: devServer.server!,
port,
hostname: host,
middlewareWatcher,
});
}
},
);
},
},
};
Expand Down
48 changes: 27 additions & 21 deletions packages/mock/docs/webpack-mock.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,37 @@
- webpack.config.ts 文件

```ts
import type WebpackDevServer from 'webpack-dev-server';
import type Webpack from 'webpack';
import { getMiddleware } from '@liangskyli/mock';
import type Webpack from 'webpack';
import type WebpackDevServer from 'webpack-dev-server';

const host = '0.0.0.0';
const port = 8002;

const webpackConfig: Webpack.Configuration = {
entry: './test/app.js',
mode: 'development',
devServer: {
host: '0.0.0.0',
port: 4000,
onBeforeSetupMiddleware: (devServer: WebpackDevServer) => {
if (!devServer) {
throw new Error('webpack-dev-server is not defined');
}

getMiddleware().then((middleware) => {
devServer.app.use(middleware);

devServer.app.get('/', (req, res) => {
res.send('homepage');
});
console.log('look in http://localhost:4000/');
});
entry: './test/app.js',
mode: 'development',
devServer: {
host,
port,
onBeforeSetupMiddleware: (devServer: WebpackDevServer) => {
if (!devServer || !devServer.app) {
throw new Error('webpack-dev-server is not defined');
}

getMiddleware().then(
({ middleware, middlewareWatcher }) => {
devServer.app!.use(middleware);

devServer.app!.get('/', (req, res) => {
res.send('homepage');
});
console.log(`look in http://localhost:${port}/`);

},
);
},
},
};
export default webpackConfig;
```
Expand All @@ -49,4 +55,4 @@ const runServer = async () => {
};

runServer().then();
```
```
3 changes: 3 additions & 0 deletions packages/mock/mock/socket/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
8 changes: 4 additions & 4 deletions packages/mock/mock/socket/sock.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import mockjs from 'mockjs';
import type {
ISocketDefaultController,
ISocketNamespaceController,
} from '../../src';
} from '@liangskyli/mock';
import mockjs from 'mockjs';

const socketDefaultController: ISocketDefaultController = (socket) => {
const data = mockjs.mock({
Expand All @@ -19,7 +19,7 @@ const socketDefaultController: ISocketDefaultController = (socket) => {

// 接收客户端数据
socket.on('toServer', (clientData) => {
console.log('from client:', clientData);
console.log('from client default namespace:', clientData);
});
};

Expand All @@ -38,7 +38,7 @@ const socketNamespaceController: ISocketNamespaceController = () => {

// 接收客户端数据
socket.on('toServer', (clientData) => {
console.log('from client:', clientData);
console.log('from client custom namespace:', clientData);
});
},
};
Expand Down
14 changes: 14 additions & 0 deletions packages/mock/mock/socket/sock3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { ISocketDefaultController } from '@liangskyli/mock';

const socketDefaultController: ISocketDefaultController = (socket) => {
const data = { a: 112 };
// 数据发送客户端
socket.emit('toClient', data);

// 接收客户端数据
socket.on('toServer', (clientData) => {
console.log('from client default namespace:', clientData);
});
};

export default socketDefaultController;
26 changes: 20 additions & 6 deletions packages/mock/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,21 @@
},
"license": "MIT",
"author": "liangsky <[email protected]>",
"main": "./lib/index.cjs.js",
"module": "./lib/index.esm.js",
"type": "module",
"main": "./lib/index.cjs",
"module": "./lib/index.js",
"types": "./lib/index.d.ts",
"typings": "./lib/index.d.ts",
"exports": {
".": {
"types": "./index.d.ts",
"import": "./lib/index.js",
"require": "./lib/index.cjs"
}
},
"bin": {
"mock-server": "./bin/index.js"
"mock-server": "./bin/index.js",
"mock-server-cjs": "./bin/index.cjs"
},
"files": [
"lib",
Expand All @@ -37,9 +46,14 @@
"package.json"
],
"scripts": {
"build": "rollup --bundleConfigAsCjs --config=./rollup.config.js",
"start": "cross-env DEBUG=mock:utils cross-env TS_NODE_PROJECT=test/tsconfig.json node -r ts-node/register test/server.ts",
"test-http-cli": "cross-env DEBUG=mock:utils node bin/index.js -d ./test -e mock/b.ts -c ./test/mock.config.ts",
"build": "rollup --config=./rollup.config.js",
"start-esm": "cross-env DEBUG=mock:* cross-env TS_NODE_PROJECT=test/esm/tsconfig.json node --loader ts-node/esm test/esm/server.ts",
"start-esm-mock": "cross-env DEBUG=mock:* cross-env TS_NODE_PROJECT=test/esm/tsconfig.json node --loader ts-node/esm test/esm/server-mock-esm.ts",
"start": "cross-env DEBUG=mock:* cross-env TS_NODE_PROJECT=test/tsconfig.json node -r ts-node/register test/server.ts",
"test-http-cli": "cross-env DEBUG=mock:* node bin/index.js -d ./test -e mock/b.ts -c ./test/mock.config.ts",
"test-http-cli-esm-config": "cross-env DEBUG=mock:* node bin/index.js -d ./test -e mock/b.ts -c ./test/esm/mock.config.ts",
"test-http-cli-esm-mock": "cross-env DEBUG=mock:* node bin/index.js -e mock/b.ts -c ./test/esm/mock-esm.config.ts",
"test-http-cli-cjs": "cross-env DEBUG=mock:* node bin/index.cjs -d ./test -e mock/b.ts -c ./test/mock.config.ts",
"test:middleware": "cross-env TS_NODE_PROJECT=test/tsconfig.json node -r ts-node/register test/middleware.ts",
"test:webpack-middleware": "cross-env TS_NODE_PROJECT=test/tsconfig.json node -r ts-node/register test/webpack-middleware.ts",
"update:deps": "pnpm update --interactive --latest"
Expand Down
6 changes: 4 additions & 2 deletions packages/mock/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { getConfig } from '../rollup.base.config';
import packageJSON from './package.json';
import { createRequire } from 'node:module';
import { getConfig } from '../rollup.base.config.js';

const require = createRequire(import.meta.url);
const packageJSON = require('./package.json');
export default [getConfig(packageJSON)];
7 changes: 5 additions & 2 deletions packages/mock/src/http/mock/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import { createDebug, winPath } from '@liangskyli/utils';
import assert from 'assert';
import bodyParser from 'body-parser';
import type { NextFunction, Request, RequestHandler } from 'express';
import { existsSync } from 'fs';
import { globSync } from 'glob';
import multer from 'multer';
import { join } from 'path';
import { existsSync } from 'node:fs';
import { createRequire } from 'node:module';
import { join } from 'node:path';
import { pathToRegexp } from 'path-to-regexp';

const require = createRequire(import.meta.url);

// support all openapi method
const VALID_METHODS = [
'get',
Expand Down
2 changes: 1 addition & 1 deletion packages/mock/src/http/server/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { register, winPath } from '@liangskyli/utils';
import type { FSWatcher } from 'chokidar';
import type { NextFunction, Request, Response } from 'express';
import { isAbsolute, join } from 'path';
import { isAbsolute, join } from 'node:path';
import createMiddleware from '../mock/createMiddleware';
import { getMockData } from '../mock/utils';
import { killProcess } from '../tools';
Expand Down
2 changes: 1 addition & 1 deletion packages/mock/src/http/server/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { colors, ip } from '@liangskyli/utils';
import express from 'express';
import type http from 'http';
import type http from 'node:http';
import { killProcess } from '../tools';
import getMiddleware from './middleware';
import type { ISocketConfig } from './socket-server';
Expand Down
7 changes: 5 additions & 2 deletions packages/mock/src/http/server/socket-server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { colors, ip, winPath } from '@liangskyli/utils';
import type { FSWatcher } from 'chokidar';
import type http from 'http';
import { isAbsolute, join } from 'path';
import type http from 'node:http';
import { createRequire } from 'node:module';
import { isAbsolute, join } from 'node:path';
import type { ServerOptions, Socket } from 'socket.io';
import { Server as SocketServer } from 'socket.io';

const require = createRequire(import.meta.url);

export type ISocketConfig = {
enable: boolean;
opts?: Partial<ServerOptions>;
Expand Down
21 changes: 21 additions & 0 deletions packages/mock/test/esm/mock-esm.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { defineConfig } from '@liangskyli/mock';

export default defineConfig({
//mockDir: path.join(curDirName, '../'),
//mockDir: path.join(__dirname, '/gen-mock'),
//mockDir: path.join(__dirname, '../../http-mock-gen/test/all-gen-dirs/gen-mock/'),
port: 8002,
socketConfig: {
enable: true,
opts: {
path: '/socket.io/',
cors: {
origin: ['http://localhost:63342'],
},
},
//mockControllerUrl: path.join(__dirname, '/mock/socket/sock.ts'),
//mockControllerUrl: 'mock/socket/sock.ts',
//mockControllerUrl: 'mock/socket/sock2.js',
mockControllerUrl: 'mock/socket/sock3.ts',
},
});
25 changes: 25 additions & 0 deletions packages/mock/test/esm/mock.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { defineConfig } from '@liangskyli/mock';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

const curDirName = path.dirname(fileURLToPath(import.meta.url));

export default defineConfig({
mockDir: path.join(curDirName, '../'),
//mockDir: path.join(__dirname, '/gen-mock'),
//mockDir: path.join(__dirname, '../../http-mock-gen/test/all-gen-dirs/gen-mock/'),
port: 8002,
socketConfig: {
enable: true,
opts: {
path: '/socket.io/',
cors: {
origin: ['http://localhost:63342'],
},
},
//mockControllerUrl: path.join(__dirname, '/mock/socket/sock.ts'),
//mockControllerUrl: 'mock/socket/sock.ts',
//mockControllerUrl: 'mock/socket/sock2.js',
mockControllerUrl: 'mock/socket/sock3.ts',
},
});
3 changes: 3 additions & 0 deletions packages/mock/test/esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
Loading

0 comments on commit 593854e

Please sign in to comment.