Skip to content

Commit e3cb42a

Browse files
committed
migration to ESM
1 parent d59e1af commit e3cb42a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+139
-137
lines changed

.mocharc.js renamed to .mocharc.cjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ require("tsconfig-paths/register");
55

66
module.exports = {
77
diff: true,
8-
extension: ['ts'],
8+
extension: ['mts'],
99
package: '../package.json',
1010
timeout: 10000,
11+
12+
"node-option": [
13+
"loader=ts-node/esm",
14+
],
1115
};

example/webpack-example/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
4545
"baseUrl": "./", /* Base directory to resolve non-absolute module names. */
4646
"paths": {
47-
"@msgpack/msgpack": ["../../"]
47+
"@msgpack/msgpack": ["../../index.mjs"]
4848
}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
4949
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
5050
// "typeRoots": [], /* List of folders to include type definitions from. */

karma.conf.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
// import * as path from "path";
12
const path = require("path");
23
// const webpack = require("webpack");
34

45
// eslint-disable-next-line import/no-default-export
5-
export default function configure(config: any) {
6+
module.exports = function configure(config: any) {
67
config.set({
78
customLaunchers: {
89
// To debug it wih IE11,
@@ -18,10 +19,10 @@ export default function configure(config: any) {
1819

1920
basePath: "",
2021
frameworks: ["mocha"],
21-
files: ["./test/karma-run.ts"],
22+
files: ["./test/karma-run.mts"],
2223
exclude: [],
2324
preprocessors: {
24-
"**/*.ts": ["webpack", "sourcemap"],
25+
"**/*.mts": ["webpack", "sourcemap"],
2526
},
2627
reporters: ["dots"],
2728
port: 9876,
@@ -36,7 +37,7 @@ export default function configure(config: any) {
3637
mode: "production",
3738

3839
resolve: {
39-
extensions: [".ts", ".tsx", ".mjs", ".js", ".json", ".wasm"],
40+
extensions: [".ts", ".tsx", ".mts", ".mjs", ".js", ".json", ".wasm"],
4041
alias: {
4142
"@msgpack/msgpack": path.resolve(__dirname, "src"),
4243
},
@@ -64,7 +65,7 @@ export default function configure(config: any) {
6465
devtool: "inline-source-map",
6566
},
6667
mime: {
67-
"text/x-typescript": ["ts", "tsx"],
68+
"text/x-typescript": ["ts", "tsx", "mts"],
6869
},
6970
client: {
7071
mocha: {

package.json

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
"description": "MessagePack for ECMA-262/JavaScript/TypeScript",
55
"author": "The MessagePack community",
66
"license": "ISC",
7+
"type": "module",
8+
"exports": {
9+
"import": "./dist.es5+esm/index.mjs",
10+
"require": "./dist/index.js"
11+
},
712
"main": "./dist/index.js",
813
"module": "./dist.es5+esm/index.mjs",
914
"cdn": "./dist.es5+umd/msgpack.min.js",
@@ -12,12 +17,12 @@
1217
"sideEffects": false,
1318
"scripts": {
1419
"build": "npm publish --dry-run",
15-
"prepare": "npm run clean && webpack --bail && tsc --build tsconfig.dist.json tsconfig.dist.es5+esm.json && ts-node tools/esmify.ts dist.es5+esm/*.js dist.es5+esm/*/*.js",
20+
"prepare": "npm run clean && webpack --bail && tsc --build tsconfig.dist.json tsconfig.dist.es5+esm.json",
1621
"prepublishOnly": "run-p 'test:dist:*' && npm run test:browser",
1722
"clean": "rimraf build dist dist.*",
18-
"test": "mocha 'test/**/*.test.ts'",
19-
"test:purejs": "TEXT_ENCODING=never mocha 'test/**/*.test.ts'",
20-
"test:te": "TEXT_ENCODING=force mocha 'test/**/*.test.ts'",
23+
"test": "mocha 'test/**/*.test.mts'",
24+
"test:purejs": "TEXT_ENCODING=never mocha 'test/**/*.test.mts'",
25+
"test:te": "TEXT_ENCODING=force mocha 'test/**/*.test.mts'",
2126
"test:dist:purejs": "TS_NODE_PROJECT=tsconfig.test-dist-es5-purejs.json npm run test:purejs -- --reporter=dot",
2227
"test:cover": "npm run cover:clean && npm-run-all 'test:cover:*' && npm run cover:report",
2328
"test:cover:purejs": "npx nyc --no-clean npm run test:purejs",
@@ -30,9 +35,9 @@
3035
"test:browser:firefox": "karma start --single-run --browsers FirefoxHeadless",
3136
"test:browser:chrome": "karma start --single-run --browsers ChromeHeadless",
3237
"test:watch:browser": "karma start --browsers ChromeHeadless,FirefoxHeadless",
33-
"test:watch:nodejs": "mocha -w 'test/**/*.test.ts'",
34-
"lint": "eslint --ext .ts src test",
35-
"lint:fix": "prettier --loglevel=warn --write 'src/**/*.ts' 'test/**/*.ts' && eslint --fix --ext .ts src test",
38+
"test:watch:nodejs": "mocha -w 'test/**/*.test.mts'",
39+
"lint": "eslint --ext .ts --ext .mts src test",
40+
"lint:fix": "prettier --loglevel=warn --write 'src/**/*.mts' 'test/**/*.mts' && eslint --fix --ext .ts --ext .mts src test",
3641
"lint:print-config": "eslint --print-config .eslintrc.js",
3742
"update-dependencies": "npx rimraf node_modules/ package-lock.json ; npm install ; npm audit fix --force ; git restore package.json ; npm install"
3843
},
@@ -51,7 +56,7 @@
5156
"universal"
5257
],
5358
"engines": {
54-
"node": ">= 10"
59+
"node": ">= 14"
5560
},
5661
"devDependencies": {
5762
"@bitjourney/check-es-version-webpack-plugin": "latest",

src/CachedKeyDecoder.ts renamed to src/CachedKeyDecoder.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { utf8DecodeJs } from "./utils/utf8";
1+
import { utf8DecodeJs } from "./utils/utf8.mjs";
22

33
const DEFAULT_MAX_KEY_LENGTH = 16;
44
const DEFAULT_MAX_LENGTH_PER_KEY = 16;
File renamed without changes.

src/Decoder.ts renamed to src/Decoder.mts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { prettyByte } from "./utils/prettyByte";
2-
import { ExtensionCodec, ExtensionCodecType } from "./ExtensionCodec";
3-
import { getInt64, getUint64, UINT32_MAX } from "./utils/int";
4-
import { utf8DecodeJs, TEXT_DECODER_THRESHOLD, utf8DecodeTD } from "./utils/utf8";
5-
import { createDataView, ensureUint8Array } from "./utils/typedArrays";
6-
import { CachedKeyDecoder, KeyDecoder } from "./CachedKeyDecoder";
7-
import { DecodeError } from "./DecodeError";
1+
import { prettyByte } from "./utils/prettyByte.mjs";
2+
import { ExtensionCodec, ExtensionCodecType } from "./ExtensionCodec.mjs";
3+
import { getInt64, getUint64, UINT32_MAX } from "./utils/int.mjs";
4+
import { utf8DecodeJs, TEXT_DECODER_THRESHOLD, utf8DecodeTD } from "./utils/utf8.mjs";
5+
import { createDataView, ensureUint8Array } from "./utils/typedArrays.mjs";
6+
import { CachedKeyDecoder, KeyDecoder } from "./CachedKeyDecoder.mjs";
7+
import { DecodeError } from "./DecodeError.mjs";
88

99
const STATE_ARRAY = "array";
1010
const STATE_MAP_KEY = "map_key";

src/Encoder.ts renamed to src/Encoder.mts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { utf8EncodeJs, utf8Count, TEXT_ENCODER_THRESHOLD, utf8EncodeTE } from "./utils/utf8";
2-
import { ExtensionCodec, ExtensionCodecType } from "./ExtensionCodec";
3-
import { setInt64, setUint64 } from "./utils/int";
4-
import { ensureUint8Array } from "./utils/typedArrays";
5-
import type { ExtData } from "./ExtData";
1+
import { utf8EncodeJs, utf8Count, TEXT_ENCODER_THRESHOLD, utf8EncodeTE } from "./utils/utf8.mjs";
2+
import { ExtensionCodec, ExtensionCodecType } from "./ExtensionCodec.mjs";
3+
import { setInt64, setUint64 } from "./utils/int.mjs";
4+
import { ensureUint8Array } from "./utils/typedArrays.mjs";
5+
import type { ExtData } from "./ExtData.mjs";
66

77
export const DEFAULT_MAX_DEPTH = 100;
88
export const DEFAULT_INITIAL_BUFFER_SIZE = 2048;
File renamed without changes.

src/ExtensionCodec.ts renamed to src/ExtensionCodec.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ExtensionCodec to handle MessagePack extensions
22

3-
import { ExtData } from "./ExtData";
4-
import { timestampExtension } from "./timestamp";
3+
import { ExtData } from "./ExtData.mjs";
4+
import { timestampExtension } from "./timestamp.mjs";
55

66
export type ExtensionDecoderType<ContextType> = (
77
data: Uint8Array,
File renamed without changes.

src/decode.ts renamed to src/decode.mts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Decoder } from "./Decoder";
2-
import type { ExtensionCodecType } from "./ExtensionCodec";
3-
import type { ContextOf, SplitUndefined } from "./context";
1+
import { Decoder } from "./Decoder.mjs";
2+
import type { ExtensionCodecType } from "./ExtensionCodec.mjs";
3+
import type { ContextOf, SplitUndefined } from "./context.mjs";
44

55
export type DecodeOptions<ContextType = undefined> = Readonly<
66
Partial<{

src/decodeAsync.ts renamed to src/decodeAsync.mts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { Decoder } from "./Decoder";
2-
import { ensureAsyncIterable } from "./utils/stream";
3-
import { defaultDecodeOptions } from "./decode";
4-
import type { ReadableStreamLike } from "./utils/stream";
5-
import type { DecodeOptions } from "./decode";
6-
import type { SplitUndefined } from "./context";
1+
import { Decoder } from "./Decoder.mjs";
2+
import { ensureAsyncIterable } from "./utils/stream.mjs";
3+
import { defaultDecodeOptions } from "./decode.mjs";
4+
import type { ReadableStreamLike } from "./utils/stream.mjs";
5+
import type { DecodeOptions } from "./decode.mjs";
6+
import type { SplitUndefined } from "./context.mjs";
77

88
/**
99
* @throws {@link RangeError} if the buffer is incomplete, including the case where the buffer is empty.

src/encode.ts renamed to src/encode.mts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Encoder } from "./Encoder";
2-
import type { ExtensionCodecType } from "./ExtensionCodec";
3-
import type { ContextOf, SplitUndefined } from "./context";
1+
import { Encoder } from "./Encoder.mjs";
2+
import type { ExtensionCodecType } from "./ExtensionCodec.mjs";
3+
import type { ContextOf, SplitUndefined } from "./context.mjs";
44

55
export type EncodeOptions<ContextType = undefined> = Partial<
66
Readonly<{

src/index.ts renamed to src/index.mts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
// Main Functions:
22

3-
import { encode } from "./encode";
3+
import { encode } from "./encode.mjs";
44
export { encode };
5-
import type { EncodeOptions } from "./encode";
5+
import type { EncodeOptions } from "./encode.mjs";
66
export type { EncodeOptions };
77

8-
import { decode, decodeMulti } from "./decode";
8+
import { decode, decodeMulti } from "./decode.mjs";
99
export { decode, decodeMulti };
10-
import type { DecodeOptions } from "./decode";
10+
import type { DecodeOptions } from "./decode.mjs";
1111
export { DecodeOptions };
1212

13-
import { decodeAsync, decodeArrayStream, decodeMultiStream, decodeStream } from "./decodeAsync";
13+
import { decodeAsync, decodeArrayStream, decodeMultiStream, decodeStream } from "./decodeAsync.mjs";
1414
export { decodeAsync, decodeArrayStream, decodeMultiStream, decodeStream };
1515

16-
import { Decoder, DataViewIndexOutOfBoundsError } from "./Decoder";
17-
import { DecodeError } from "./DecodeError";
16+
import { Decoder, DataViewIndexOutOfBoundsError } from "./Decoder.mjs";
17+
import { DecodeError } from "./DecodeError.mjs";
1818
export { Decoder, DecodeError, DataViewIndexOutOfBoundsError };
1919

20-
import { Encoder } from "./Encoder";
20+
import { Encoder } from "./Encoder.mjs";
2121
export { Encoder };
2222

2323
// Utilitiies for Extension Types:
2424

25-
import { ExtensionCodec } from "./ExtensionCodec";
25+
import { ExtensionCodec } from "./ExtensionCodec.mjs";
2626
export { ExtensionCodec };
27-
import type { ExtensionCodecType, ExtensionDecoderType, ExtensionEncoderType } from "./ExtensionCodec";
27+
import type { ExtensionCodecType, ExtensionDecoderType, ExtensionEncoderType } from "./ExtensionCodec.mjs";
2828
export type { ExtensionCodecType, ExtensionDecoderType, ExtensionEncoderType };
29-
import { ExtData } from "./ExtData";
29+
import { ExtData } from "./ExtData.mjs";
3030
export { ExtData };
3131

3232
import {
@@ -36,7 +36,7 @@ import {
3636
decodeTimestampToTimeSpec,
3737
encodeTimestampExtension,
3838
decodeTimestampExtension,
39-
} from "./timestamp";
39+
} from "./timestamp.mjs";
4040
export {
4141
EXT_TIMESTAMP,
4242
encodeDateToTimeSpec,

src/timestamp.ts renamed to src/timestamp.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// https://github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type
2-
import { DecodeError } from "./DecodeError";
3-
import { getInt64, setInt64 } from "./utils/int";
2+
import { DecodeError } from "./DecodeError.mjs";
3+
import { getInt64, setInt64 } from "./utils/int.mjs";
44

55
export const EXT_TIMESTAMP = -1;
66

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/utils/utf8.ts renamed to src/utils/utf8.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-unnecessary-condition */
2-
import { UINT32_MAX } from "./int";
2+
import { UINT32_MAX } from "./int.mjs";
33

44
const TEXT_ENCODING_AVAILABLE =
55
(typeof process === "undefined" || process?.env?.["TEXT_ENCODING"] !== "never") &&

test/CachedKeyDecoder.test.ts renamed to test/CachedKeyDecoder.test.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import assert from "assert";
2-
import { CachedKeyDecoder, KeyDecoder } from "../src/CachedKeyDecoder";
3-
import { utf8EncodeJs, utf8Count } from "../src/utils/utf8";
2+
import { CachedKeyDecoder, KeyDecoder } from "../src/CachedKeyDecoder.mjs";
3+
import { utf8EncodeJs, utf8Count } from "../src/utils/utf8.mjs";
44

55
function tryDecode(keyDecoder: KeyDecoder, str: string): string {
66
const byteLength = utf8Count(str);

test/ExtensionCodec.test.ts renamed to test/ExtensionCodec.test.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import assert from "assert";
22
import util from "util";
3-
import { encode, decode, ExtensionCodec, decodeAsync } from "../src";
3+
import { encode, decode, ExtensionCodec, decodeAsync } from "../src/index.mjs";
44

55
describe("ExtensionCodec", () => {
66
context("timestamp", () => {

test/codec-bigint.test.ts renamed to test/codec-bigint.test.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from "assert";
2-
import { encode, decode, ExtensionCodec, DecodeError } from "../src";
2+
import { encode, decode, ExtensionCodec, DecodeError } from "../src/index.mjs";
33

44
const extensionCodec = new ExtensionCodec();
55
extensionCodec.register({

test/codec-float.test.ts renamed to test/codec-float.test.mts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import assert from "assert";
2-
import { decode } from "../src";
32
import * as ieee754 from "ieee754";
43

4+
import { decode } from "../src/index.mjs";
5+
56
const FLOAT32_TYPE = 0xca;
67
const FLOAT64_TYPE = 0xcb;
78

test/codec-int.test.ts renamed to test/codec-int.test.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from "assert";
2-
import { setInt64, getInt64, getUint64, setUint64 } from "../src/utils/int";
2+
import { setInt64, getInt64, getUint64, setUint64 } from "../src/utils/int.mjs";
33

44
const INT64SPECS = {
55
ZERO: 0,

test/codec-timestamp.test.ts renamed to test/codec-timestamp.test.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
decodeTimestampExtension,
88
decodeTimestampToTimeSpec,
99
encodeTimestampExtension,
10-
} from "../src";
10+
} from "../src/index.mjs";
1111

1212
const TIME = 1556636810389;
1313

test/decode-blob.test.ts renamed to test/decode-blob.test.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from "assert";
2-
import { encode, decode, decodeAsync } from "@msgpack/msgpack";
2+
import { encode, decode, decodeAsync } from "../src/index.mjs";
33

44
(typeof Blob !== "undefined" ? describe : describe.skip)("Blob", () => {
55
it("decodes it with `decode()`", async function () {

test/decode-max-length.test.ts renamed to test/decode-max-length.test.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import assert from "assert";
2-
import { encode, decode, decodeAsync } from "../src";
3-
import type { DecodeOptions } from "../src/decode";
2+
import { encode, decode, decodeAsync } from "../src/index.mjs";
3+
import type { DecodeOptions } from "../src/index.mjs";
44

55
describe("decode with max${Type}Length specified", () => {
66
async function* createStream<T>(input: T) {

test/decodeArrayStream.test.ts renamed to test/decodeArrayStream.test.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from "assert";
2-
import { encode, decodeArrayStream } from "../src";
2+
import { encode, decodeArrayStream } from "../src/index.mjs";
33

44
describe("decodeArrayStream", () => {
55
const generateSampleObject = () => {

test/decodeAsync.test.ts renamed to test/decodeAsync.test.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from "assert";
2-
import { encode, decodeAsync } from "../src";
2+
import { encode, decodeAsync } from "../src/index.mjs";
33

44
describe("decodeAsync", () => {
55
function wrapWithNoisyBuffer(byte: number) {

test/decodeMulti.test.ts renamed to test/decodeMulti.test.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from "assert";
2-
import { encode, decodeMulti } from "@msgpack/msgpack";
2+
import { encode, decodeMulti } from "../src/index.mjs";
33

44
describe("decodeMulti", () => {
55
it("decodes multiple objects in a single binary", () => {

test/decodeMultiStream.test.ts renamed to test/decodeMultiStream.test.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from "assert";
2-
import { encode, decodeMultiStream } from "@msgpack/msgpack";
2+
import { encode, decodeMultiStream } from "../src/index.mjs";
33

44
describe("decodeStream", () => {
55
it("decodes stream", async () => {

test/deno_test.ts renamed to test/deno_test.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/* eslint-disable */
44
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
5-
import * as msgpack from "../mod.ts";
5+
import * as msgpack from "../src/index.mjs";
66

77
Deno.test("Hello, world!", () => {
88
const encoded = msgpack.encode("Hello, world!");

test/edge-cases.test.ts renamed to test/edge-cases.test.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// kind of hand-written fuzzing data
22
// any errors should not break Encoder/Decoder instance states
33
import assert from "assert";
4-
import { encode, decodeAsync, decode, Encoder, Decoder, decodeMulti, decodeMultiStream } from "../src";
5-
import { DataViewIndexOutOfBoundsError } from "../src/Decoder";
4+
import { encode, decodeAsync, decode, Encoder, Decoder, decodeMulti, decodeMultiStream, DataViewIndexOutOfBoundsError,
5+
} from "../src/index.mjs";
66

77
function testEncoder(encoder: Encoder): void {
88
const object = {

test/encode.test.ts renamed to test/encode.test.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from "assert";
2-
import { encode, decode } from "@msgpack/msgpack";
2+
import { encode, decode } from "../src/index.mjs";
33

44
describe("encode", () => {
55
context("sortKeys", () => {

0 commit comments

Comments
 (0)