Skip to content

Commit

Permalink
📦 NEW: Support esm and cjs exports
Browse files Browse the repository at this point in the history
closes #5
  • Loading branch information
fengmk2 committed Oct 14, 2022
1 parent 464667e commit 4f16ddd
Show file tree
Hide file tree
Showing 18 changed files with 117 additions and 32 deletions.
14 changes: 14 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "eslint-config-egg/typescript",
"parserOptions": {
// recommend to use another config file like tsconfig.eslint.json and extends tsconfig.json in it.
// because you may be need to lint test/**/*.test.ts but no need to emit to js.
// @see https://github.com/typescript-eslint/typescript-eslint/issues/890
"project": "./tsconfig.json"
},
"ignorePatterns": [
"src/**/*.js",
"src/esm",
"src/cjs"
]
}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ coverage/
dist/
node_modules/
.idea
proto
proto/
.fleet
.DS_Store
logs/
19 changes: 19 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Current Test File",
"autoAttachChildProcesses": true,
"skipFiles": ["<node_internals>/**", "**/node_modules/**"],
"program": "${workspaceRoot}/node_modules/vitest/vitest.mjs",
"args": ["run", "${relativeFile}"],
"smartStep": true,
"console": "integratedTerminal"
}
]
}
35 changes: 22 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,29 @@
"name": "layotto",
"version": "1.0.0",
"description": "Layotto Node.js SDK",
"main": "dist/index.js",
"type": "module",
"exports": {
".": {
"import": {
"types": "./src/esm/index.d.ts",
"default": "./src/esm/index.js"
},
"require": {
"types": "./src/cjs/index.d.ts",
"default": "./src/cjs/index.js"
}
}
},
"types": "./src/esm/index.d.ts",
"main": "./src/cjs/index.js",
"files": [
"dist",
"proto",
"package.json",
"README.md"
"src",
"proto"
],
"scripts": {
"contributor": "git-contributor",
"ci": "npm run lint && npm run build && npm run test:unit",
"test": "jest --runInBand --detectOpenHandles",
"test": "vitest run",
"test:unit": "NODE_ENV=test npm run test 'test/unit/.*\\.test\\.ts'",
"test:demo": "cd demo && npm run test:all",
"lint": "tslint -p tsconfig.json",
Expand Down Expand Up @@ -40,19 +52,16 @@
},
"homepage": "https://github.com/layotto/js-sdk#readme",
"devDependencies": {
"@eggjs/tsconfig": "^1.0.0",
"@types/google-protobuf": "^3.15.5",
"@types/jest": "^27.0.2",
"eslint": "^8.25.0",
"eslint-config-egg": "^12.1.0",
"git-contributor": "^1.1.0",
"grpc-tools": "^1.11.3",
"grpc_tools_node_protoc_ts": "^5.3.2",
"jest": "^27.2.5",
"ts-jest": "^27.0.7",
"ts-node": "^10.3.0",
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0",
"type-fest": "^2.5.2",
"typescript": "^4.4.4"
"typescript": "^4.4.4",
"vitest": "^0.24.1"
},
"dependencies": {
"@grpc/grpc-js": "^1.4.1",
Expand Down
13 changes: 9 additions & 4 deletions scripts/build-grpc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ ARCH=$(uname -m)
PATH_ROOT=$(pwd)
PATH_PROTO_ROOT="${PATH_ROOT}/layotto/spec/proto"
PATH_PROTO_OUTPUT="${PATH_ROOT}/proto"
PATH_PROTO_OUTPUT_RUNTIME_V1="${PATH_PROTO_OUTPUT}/runtime/v1"

PROTO_FILES=(
"runtime/v1/lifecycle.proto"
"runtime/v1/runtime.proto"
"runtime/v1/appcallback.proto"
"runtime/v1/lifecycle.proto"
"runtime/v1/runtime.proto"
"runtime/v1/appcallback.proto"
)

generateGrpc() {
Expand All @@ -48,6 +49,7 @@ generateGrpc() {
--ts_out="grpc_js:$PATH_PROTO_OUTPUT" \
--grpc_out="grpc_js:$PATH_PROTO_OUTPUT" \
"$PATH_PROTO/$PATH_FILE"
cp "$PATH_PROTO/$PATH_FILE" "${PATH_PROTO_OUTPUT_RUNTIME_V1}/"
}

echo ""
Expand All @@ -59,9 +61,12 @@ echo ""
echo "Compiling gRPC files"

for proto_file in ${PROTO_FILES[@]}; do
echo "generate ${proto_file}"
echo "$ generateGrpc $PATH_PROTO_ROOT ${proto_file}"
generateGrpc $PATH_PROTO_ROOT "${proto_file}"
done

echo "Gen commonjs package.json to ${PATH_PROTO_OUTPUT_RUNTIME_V1}/package.json"
echo "{ \"type\": \"commonjs\" }" > "${PATH_PROTO_OUTPUT_RUNTIME_V1}/package.json"

echo ""
echo "DONE"
1 change: 1 addition & 0 deletions test/unit/client/Binding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { describe, beforeAll, it } from 'vitest';
import { strict as assert } from 'assert';
import { Client } from '../../../src';

Expand Down
1 change: 1 addition & 0 deletions test/unit/client/Client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { describe, beforeAll, it } from 'vitest';
import { strict as assert } from 'assert';
import { Client } from '../../../src';

Expand Down
1 change: 1 addition & 0 deletions test/unit/client/Configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { describe, beforeAll, it } from 'vitest';
import { strict as assert } from 'assert';
import { Client, utils } from '../../../src';

Expand Down
1 change: 1 addition & 0 deletions test/unit/client/File.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { describe, beforeAll, it } from 'vitest';
import { strict as assert } from 'assert';
import { tmpdir } from 'os';
import { existsSync, createWriteStream, createReadStream } from 'fs';
Expand Down
1 change: 1 addition & 0 deletions test/unit/client/Invoker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { describe, beforeAll, it } from 'vitest';
import { strict as assert } from 'assert';
import { Client } from '../../../src';
import { RumtimeTypes } from '../../../src';
Expand Down
1 change: 1 addition & 0 deletions test/unit/client/Lock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { describe, beforeAll, it } from 'vitest';
import { strict as assert } from 'assert';
import { Client, RumtimeTypes, utils } from '../../../src';

Expand Down
1 change: 1 addition & 0 deletions test/unit/client/PubSub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { describe, beforeAll, it } from 'vitest';
import { Client } from '../../../src';

describe('client/PubSub.test.ts', () => {
Expand Down
1 change: 1 addition & 0 deletions test/unit/client/Sequencer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { describe, beforeAll, it } from 'vitest';
import { strict as assert } from 'assert';
import { Client, RumtimeTypes } from '../../../src';

Expand Down
1 change: 1 addition & 0 deletions test/unit/client/State.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { describe, beforeAll, it } from 'vitest';
import { strict as assert } from 'assert';
import { Client } from '../../../src';
import { StateOperation, StateOperationType } from '../../../src/types/State';
Expand Down
1 change: 1 addition & 0 deletions test/unit/server/PubSub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { describe, beforeAll, it, afterAll } from 'vitest';
import { strict as assert } from 'assert';
import { execSync } from 'child_process';
import { Server, Client, utils } from '../../../src';
Expand Down
39 changes: 32 additions & 7 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
{
"extends": "@eggjs/tsconfig",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
"module": "CommonJS",
"moduleResolution": "Node"
"useUnknownInCatchVariables": true,
"allowSyntheticDefaultImports": true,
"declaration": true,
"downlevelIteration": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"module": "NodeNext",
"moduleResolution": "Node",
"newLine": "LF",
"checkJs": false,
"allowJs": true,
"strict": true,
"skipLibCheck": true,
"suppressImplicitAnyIndexErrors": true,
"suppressExcessPropertyErrors": true,
"forceConsistentCasingInFileNames": true,
"target": "ES2018",
"sourceMap": true,
"esModuleInterop": true,
"stripInternal": true,
"lib": [
"ESNext"
],
"composite": true,
"types": [
"node"
],
"rootDir": "src"
},
"include": [ "src" ],
"exclude": ["node_modules", "**/test/*"]
"include": [
"src/**/*.ts"
]
}
7 changes: 0 additions & 7 deletions tslint.json

This file was deleted.

10 changes: 10 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from 'vitest/config';

export default defineConfig({
test: {
include: [
'test/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}',
],
testTimeout: 5000,
},
});

0 comments on commit 4f16ddd

Please sign in to comment.