From 7eaf1ea34833b5d7e8c847beef936e9a06845159 Mon Sep 17 00:00:00 2001 From: Yuta Shimakawa Date: Sun, 4 Oct 2020 14:49:13 +0900 Subject: [PATCH 1/6] chore: invalid syntax in the example code * remove duplicated import statement * fix invalid assignment statement --- README.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/README.md b/README.md index b0d35edb..840f7750 100644 --- a/README.md +++ b/README.md @@ -280,10 +280,7 @@ extensionCodec.register({ }, }); -// and later -import { encode, decode } from "@msgpack/msgpack"; - -const encoded = = encode([new Set(), new Map()], { extensionCodec }); +const encoded = encode([new Set(), new Map()], { extensionCodec }); const decoded = decode(encoded, { extensionCodec }); ``` From 0709254d9bb26d227758fdb0a6e34c1600be712d Mon Sep 17 00:00:00 2001 From: Yuta Shimakawa Date: Sun, 4 Oct 2020 17:31:09 +0900 Subject: [PATCH 2/6] chore: add annotation typescript usage in README * recommend enabling `strict` mode for avoiding type checking failure --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 840f7750..a44e45c6 100644 --- a/README.md +++ b/README.md @@ -489,6 +489,8 @@ This module requires definitions of `AsyncIterator` and whatwg streams, which is For the TypeScript version, the latest TypeScript is tested in development, but older versions of TypeScript might be able to compile this module. +We also recommend enabling `strict` mode (or `strictNullCheck` mode at least). Specifically when you use `ExtensionCodec` with the ContextType that you defines, the type checking for `EncodeOptions` argument of `encode` function and `DecodeOptions` arugument of `decode` function will fail because ts compiler cannot infer the `EncodeOptions`/`DecodeOptions` generic type appropriately unless `strictNullChecks` property is set to be `true` in `tsconfig.json`. + ## Benchmark Run-time performance is not the only reason to use MessagePack, but it's important to choose MessagePack libraries, so a benchmark suite is provided to monitor the performance of this library. From 35a949db25bed716694d26ee70a5884d27b0fecb Mon Sep 17 00:00:00 2001 From: FUJI Goro Date: Sun, 4 Oct 2020 22:28:11 +0900 Subject: [PATCH 3/6] provide ES modules for tree-shaking packagers --- .gitignore | 2 +- package.json | 8 ++++--- src/index.ts | 46 ++++++++++++++++++++++++++------------ tools/esmify.ts | 13 +++++++++++ tsconfig.dist.es5+esm.json | 12 ++++++++++ webpack.config.ts | 8 +++---- 6 files changed, 67 insertions(+), 22 deletions(-) create mode 100644 tools/esmify.ts create mode 100644 tsconfig.dist.es5+esm.json diff --git a/.gitignore b/.gitignore index 92fd1349..c67b64cb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ node_modules/ dist/ -dist.es5/ +dist.*/ build/ .nyc_output/ coverage/ diff --git a/package.json b/package.json index 68bf6641..093e5665 100644 --- a/package.json +++ b/package.json @@ -5,12 +5,13 @@ "author": "The MessagePack community", "license": "ISC", "main": "./dist/index.js", - "browser": "./dist.es5/msgpack.min.js", + "browser": "./dist.es5+umd/msgpack.min.js", + "module": "./dist.es5+esm/index.js", "types": "./dist/index.d.ts", "sideEffects": false, "scripts": { "build": "npm publish --dry-run", - "prepare": "npm run clean && tsc -p tsconfig.dist.json && webpack", + "prepare": "npm run clean && webpack && tsc --build tsconfig.dist.json tsconfig.dist.es5+esm.json && ts-node tools/esmify.ts dist.es5+esm/*.js dist.es5+esm/*/*.js", "prepublishOnly": "run-p 'test:dist:*' && npm run test:browser", "clean": "rimraf build dist dist.*", "test": "mocha 'test/**/*.test.ts'", @@ -88,6 +89,7 @@ "files": [ "src/**/*.*", "dist/**/*.*", - "dist.es5/**/*.*" + "dist.es5/**/*.*", + "dist.es5+esm/**/*.*" ] } diff --git a/src/index.ts b/src/index.ts index 6df03dda..2d3a5b9b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,24 +1,34 @@ // Main Functions: -export { encode, EncodeOptions } from "./encode"; -export { decode, DecodeOptions } from "./decode"; -export { decodeAsync, decodeArrayStream, decodeStream } from "./decodeAsync"; +import { encode } from "./encode"; +export { encode }; +import type { EncodeOptions } from "./encode"; +export type { EncodeOptions }; -/** - * @experimental `Decoder` is exported for experimental use. - */ -export { Decoder } from "./Decoder"; +import { decode } from "./decode"; +export { decode }; +import type { DecodeOptions } from "./decode"; +export { DecodeOptions }; -/** - * @experimental `Encoder` is exported for experimental use. - */ -export { Encoder } from "./Encoder"; +import { decodeAsync, decodeArrayStream, decodeStream } from "./decodeAsync"; +export { decodeAsync, decodeArrayStream, decodeStream }; + +import { Decoder } from "./Decoder"; +export { Decoder }; + +import { Encoder } from "./Encoder"; +export { Encoder }; // Utilitiies for Extension Types: -export { ExtensionCodec, ExtensionCodecType, ExtensionDecoderType, ExtensionEncoderType } from "./ExtensionCodec"; -export { ExtData } from "./ExtData"; -export { +import { ExtensionCodec } from "./ExtensionCodec"; +export { ExtensionCodec }; +import type { ExtensionCodecType, ExtensionDecoderType, ExtensionEncoderType } from "./ExtensionCodec"; +export type { ExtensionCodecType, ExtensionDecoderType, ExtensionEncoderType }; +import { ExtData } from "./ExtData"; +export { ExtData }; + +import { EXT_TIMESTAMP, encodeDateToTimeSpec, encodeTimeSpecToTimestamp, @@ -26,3 +36,11 @@ export { encodeTimestampExtension, decodeTimestampExtension, } from "./timestamp"; +export { + EXT_TIMESTAMP, + encodeDateToTimeSpec, + encodeTimeSpecToTimestamp, + decodeTimestampToTimeSpec, + encodeTimestampExtension, + decodeTimestampExtension, +}; diff --git a/tools/esmify.ts b/tools/esmify.ts new file mode 100644 index 00000000..b3a22a4c --- /dev/null +++ b/tools/esmify.ts @@ -0,0 +1,13 @@ +#!ts-node +/* eslint-disable no-console */ + +import fs from "fs"; + +const files = process.argv.slice(2); + +for (const file of files) { + console.info(`Processing ${file}`); + const content = fs.readFileSync(file).toString("utf8"); + const newContent = content.replace(/\bfrom "([^"]+)";/g, 'from "$1.js";'); + fs.writeFileSync(file, newContent); +} diff --git a/tsconfig.dist.es5+esm.json b/tsconfig.dist.es5+esm.json new file mode 100644 index 00000000..0a359972 --- /dev/null +++ b/tsconfig.dist.es5+esm.json @@ -0,0 +1,12 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "target": "es5", + "module": "es2015", + "outDir": "./dist.es5+esm", + "declaration": false, + "noEmitOnError": true, + "incremental": false + }, + "include": ["src/**/*.ts"] +} diff --git a/webpack.config.ts b/webpack.config.ts index dba3aec7..6b224628 100644 --- a/webpack.config.ts +++ b/webpack.config.ts @@ -1,9 +1,9 @@ import path from "path"; -// @ts-ignore +// @ts-expect-error import webpack from "webpack"; -// @ts-ignore +// @ts-expect-error import { CheckEsVersionPlugin } from "@bitjourney/check-es-version-webpack-plugin"; -// @ts-ignore +// @ts-expect-error import _ from "lodash"; const config = { @@ -11,7 +11,7 @@ const config = { entry: "./src/index.ts", output: { - path: path.resolve(__dirname, "dist.es5"), + path: path.resolve(__dirname, "dist.es5+umd"), library: "MessagePack", libraryTarget: "umd", globalObject: "this", From bc3af803d4ec1fc731e072ab0c845d7ffa8e598d Mon Sep 17 00:00:00 2001 From: FUJI Goro Date: Sun, 4 Oct 2020 22:57:03 +0900 Subject: [PATCH 4/6] changes for v2.2.0 --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57152ffe..4d6350bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ This is the revision history of @msgpack/msgpack +## v2.2.0 2020/10/04 + +https://github.com/msgpack/msgpack-javascript/compare/v2.1.1...v2.2.0 + +* Now `package.json` has a `module` field to support ES modules + ## v2.1.1 2020/10/04 https://github.com/msgpack/msgpack-javascript/compare/v2.1.0...v2.1.1 From b8e51c2849f1c11525b546e715c58b3d616797c0 Mon Sep 17 00:00:00 2001 From: FUJI Goro Date: Sun, 4 Oct 2020 23:05:43 +0900 Subject: [PATCH 5/6] fix ESM support --- README.md | 9 +++++---- example/amd-example.html | 2 +- example/fetch-example.html | 2 +- package.json | 2 +- tsconfig.test-dist-es5-purejs.json | 2 +- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index a44e45c6..900e193f 100644 --- a/README.md +++ b/README.md @@ -481,7 +481,7 @@ If you support IE11, import `core-js` in your application entrypoints, as this l NodeJS v10 is required, but NodeJS v12 or later is recommended because it includes the V8 feature of [Improving DataView performance in V8](https://v8.dev/blog/dataview). -NodeJS before v10 will work by importing `@msgpack/msgpack/dist.es5/msgpack`. +NodeJS before v10 will work by importing `@msgpack/msgpack/dist.es5+umd/msgpack`. ### TypeScript @@ -522,9 +522,10 @@ Note that `Buffer.from()` for `JSON.stringify()` is necessary to emulate I/O whe The NPM package distributed in npmjs.com includes both ES2015+ and ES5 files: * `dist/` is compiled into ES2015+ -* `dist.es5/` is compiled into ES5 and bundled to singile file - * `dist.es5/msgpack.min.js` - the default, minified file (UMD) - * `dist.es5/msgpack.js` - an optional, non-minified file (UMD) +* `dist.es5+umd/` is compiled into ES5 and bundled to singile file + * `dist.es5+umd/msgpack.min.js` - the default, minified file (UMD) + * `dist.es5+umd/msgpack.js` - an optional, non-minified file (UMD) +* `dist.es5+esm/` is compiled into ES5 and placed as ES modules If you use NodeJS and/or webpack, their module resolvers use the suitable one automatically. diff --git a/example/amd-example.html b/example/amd-example.html index efd963e2..181bb902 100644 --- a/example/amd-example.html +++ b/example/amd-example.html @@ -1,7 +1,7 @@ - +