Skip to content

Commit

Permalink
Convert messages into a hybrid cjs/esm package (#1756)
Browse files Browse the repository at this point in the history
* Convert cucumber-expressions into a module

* Use a root-level tsconfig.esm.json file

That way we will avoid duplication of ts-node section in packages
tsconfig.json.

Once all module have been converted, we will be able to move the ts-node
configuration into the root level tsconfig.json.

* Update TypeScript builds to consider legacy and new esm-compatible ones

* Add a dedicated template for javascript esm-compatible packages

* Add the specific cjs package.json to javascript-esm template

* Fix 'npm run clean' scripts to avoid deleting the 'javascript/dist/cjs/package.json' files

* Add an entry in the CHANGELOG after merge of main

* Update javascript-esm/default.mk with the last version of javascript/default.mk

* Add tests to make sure the CJS build works well

* Fix make clean targets for hybrid esm/cjs packages

* Convert messages into a hybrid cjs/esm package

* Fix tsconfig build files

* Fix make clean for messages javascript

* Fix Makefile clean target

* Convert message-streams to hybrid cjs/esm: rsync

* Convert message-streams to hybrid cjs/esm: convert the package

* Use lib: es2019 in tsconfig

* [skip ci]Update changelogs

* Fix json-formatter package.json

* Manage dist/cjs/package.json another way

* Fix templates for esm package

* Remove jsnext:main from package.json

Co-authored-by: Aslak Hellesøy <[email protected]>
  • Loading branch information
aurelien-reeves and aslakhellesoy authored Sep 24, 2021
1 parent 23404c7 commit f1570e2
Show file tree
Hide file tree
Showing 37 changed files with 2,212 additions and 3,018 deletions.
5 changes: 1 addition & 4 deletions .templates/javascript-esm/.gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
dist/*
!dist/cjs
dist/cjs/*
!dist/cjs/package.json
dist/
.idea/
.nyc_output/
coverage/
Expand Down
1 change: 0 additions & 1 deletion .templates/javascript-esm/default.mk
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ clean: clean-javascript

clean-javascript:
rm -rf .deps .codegen .tested* coverage dist acceptance
git checkout dist
.PHONY: clean-javascript

clobber: clean
Expand Down
2 changes: 1 addition & 1 deletion json-formatter/javascript-testdata/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"test": "echo 'no tests'"
},
"devDependencies": {
"@cucumber/fake-cucumber": "^13.0.0",
"@cucumber/fake-cucumber": "^14.0.0",
"@types/stream-buffers": "3.0.4",
"stream-buffers": "3.0.2"
},
Expand Down
3 changes: 3 additions & 0 deletions message-streams/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added

* Support for EcmaScript modules (aka ESM)
([#1756](https://github.com/cucumber/common/pull/1756))

### Changed

### Deprecated
Expand Down
2 changes: 1 addition & 1 deletion message-streams/javascript/.mocharc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"require": ["tsconfig-paths/register", "ts-node/register", "source-map-support/register"],
"loader": "ts-node/esm",
"extension": ["ts", "tsx"],
"recursive": true,
"timeout": 10000
Expand Down
2 changes: 1 addition & 1 deletion message-streams/javascript/.rsync
Original file line number Diff line number Diff line change
@@ -1 +1 @@
../../.templates/javascript/ .
../../.templates/javascript-esm/ .
File renamed without changes.
25 changes: 21 additions & 4 deletions message-streams/javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,34 @@
"name": "@cucumber/message-streams",
"version": "3.0.0",
"description": "Streams for reading/writing messages",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
"type": "module",
"main": "dist/cjs/src/index.js",
"types": "dist/cjs/src/index.d.ts",
"files": [
"dist/cjs",
"dist/esm"
],
"module": "dist/esm/src/index.js",
"exports": {
".": {
"import": "./dist/esm/src/index.js",
"require": "./dist/cjs/src/index.js"
}
},
"repository": {
"type": "git",
"url": "git://github.com/cucumber/cucumber.git"
},
"author": "Cucumber Limited <[email protected]>",
"license": "MIT",
"scripts": {
"test": "mocha",
"prepublishOnly": "tsc --build tsconfig.build.json"
"build:cjs": "tsc --build tsconfig.build-cjs.json",
"build:esm": "tsc --build tsconfig.build-esm.json",
"build": "npm run build:cjs && npm run build:esm",
"postbuild:cjs": "cp package.cjs.json dist/cjs/package.json",
"test": "mocha && npm run test:cjs",
"test:cjs": "npm run build:cjs && mocha --no-config dist/cjs/test",
"prepublishOnly": "npm run build"
},
"dependencies": {
"@cucumber/messages": "^17.1.1"
Expand Down
4 changes: 2 additions & 2 deletions message-streams/javascript/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import MessageToNdjsonStream from './MessageToNdjsonStream'
import NdjsonToMessageStream from './NdjsonToMessageStream'
import MessageToNdjsonStream from './MessageToNdjsonStream.js'
import NdjsonToMessageStream from './NdjsonToMessageStream.js'

export { MessageToNdjsonStream, NdjsonToMessageStream }
8 changes: 4 additions & 4 deletions message-streams/javascript/test/NdjsonStreamTest.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as messages from '@cucumber/messages'
import { MessageToNdjsonStream } from '../src'
import { MessageToNdjsonStream } from '../src/index.js'
import assert from 'assert'
import NdjsonToMessageStream from '../src/NdjsonToMessageStream'
import verifyStreamContract from './verifyStreamContract'
import toArray from './toArray'
import NdjsonToMessageStream from '../src/NdjsonToMessageStream.js'
import verifyStreamContract from './verifyStreamContract.js'
import toArray from './toArray.js'
import { Envelope } from '@cucumber/messages'

describe('NdjsonStream', () => {
Expand Down
4 changes: 2 additions & 2 deletions message-streams/javascript/test/verifyStreamContract.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as messages from '@cucumber/messages'
import { Transform } from 'stream'
import toArray from './toArray'
import assert = require('assert')
import toArray from './toArray.js'
import assert from 'assert'

export default function verifyStreamContract(
makeFromMessageStream: () => Transform,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"extends": "../../tsconfig.build.json",
"extends": "../../tsconfig.build-cjs.json",
"compilerOptions": {
"rootDir": ".",
"outDir": "dist"
"outDir": "dist/cjs"
},
"include": [
"src",
"test"
]
],
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{
"extends": "../../tsconfig.build.json",
"extends": "../../tsconfig.build-esm.json",
"compilerOptions": {
"rootDir": ".",
"outDir": "dist"
"outDir": "dist/esm"
},
"include": [
"package.json",
"src",
"test"
]
],
}
2 changes: 1 addition & 1 deletion message-streams/javascript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": "../../tsconfig.json"
"extends": "../../tsconfig.esm.json"
}
3 changes: 3 additions & 0 deletions messages/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added

* Support for EcmaScript modules (aka ESM)
([#1756](https://github.com/cucumber/common/pull/1756))

### Changed

### Deprecated
Expand Down
2 changes: 1 addition & 1 deletion messages/javascript/.mocharc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"require": ["tsconfig-paths/register", "ts-node/register", "source-map-support/register"],
"loader": "ts-node/esm",
"extension": ["ts", "tsx"],
"recursive": true,
"timeout": 10000
Expand Down
2 changes: 1 addition & 1 deletion messages/javascript/.rsync
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
../../LICENSE LICENSE
../../.templates/github/ .github/
../../.templates/javascript/ .
../../.templates/javascript-esm/ .
7 changes: 5 additions & 2 deletions messages/javascript/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ include default.mk

JSONSCHEMAS = $(shell find ../jsonschema -name "*.json")

.codegen: src/messages.ts
.codegen: src/messages.ts src/version.ts

src/messages.ts: $(JSONSCHEMAS) ../jsonschema/scripts/codegen.rb
ruby ../jsonschema/scripts/codegen.rb TypeScript ../jsonschema > $@

src/version.ts: package.json
npm version --json | jq $$'"// This file is generated using `make src/version.ts` or `make .codegen`\nexport const version = \'" + (."@cucumber/messages") + "\'"' --raw-output > $@

clean:
rm -rf dist src/types/*.ts
rm -rf src/types/*.ts
3 changes: 3 additions & 0 deletions messages/javascript/package.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
29 changes: 23 additions & 6 deletions messages/javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,41 @@
"name": "@cucumber/messages",
"version": "17.1.1",
"description": "JSON schema-based messages for Cucumber's inter-process communication",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
"type": "module",
"main": "dist/cjs/src/index.js",
"types": "dist/cjs/src/index.d.ts",
"files": [
"dist/cjs",
"dist/esm"
],
"module": "dist/esm/src/index.js",
"exports": {
".": {
"import": "./dist/esm/src/index.js",
"require": "./dist/cjs/src/index.js"
}
},
"repository": {
"type": "git",
"url": "git://github.com/cucumber/messages-javascript.git"
},
"author": "Cucumber Limited <[email protected]>",
"license": "MIT",
"scripts": {
"build:cjs": "tsc --build tsconfig.build-cjs.json",
"build:esm": "tsc --build tsconfig.build-esm.json",
"build": "npm run build:cjs && npm run build:esm",
"postbuild:cjs": "cp package.cjs.json dist/cjs/package.json",
"prepare": "make src/messages.ts",
"test": "mocha",
"prepublishOnly": "tsc --build tsconfig.build.json"
"test": "mocha && npm run test:cjs",
"test:cjs": "npm run build:cjs && mocha --no-config dist/cjs/test",
"prepublishOnly": "npm run build"
},
"dependencies": {
"@types/uuid": "8.3.1",
"uuid": "8.3.2",
"class-transformer": "0.4.0",
"reflect-metadata": "0.1.13"
"reflect-metadata": "0.1.13",
"uuid": "8.3.2"
},
"devDependencies": {
"@types/mocha": "9.0.0",
Expand Down
2 changes: 1 addition & 1 deletion messages/javascript/src/TimeConversion.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as messages from './messages'
import * as messages from './messages.js'

const MILLISECONDS_PER_SECOND = 1e3
const NANOSECONDS_PER_MILLISECOND = 1e6
Expand Down
4 changes: 2 additions & 2 deletions messages/javascript/src/getWorstTestStepResult.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TestStepResult, TestStepResultStatus } from './messages'
import { millisecondsToDuration } from './TimeConversion'
import { TestStepResult, TestStepResultStatus } from './messages.js'
import { millisecondsToDuration } from './TimeConversion.js'

/**
* Gets the worst result
Expand Down
12 changes: 6 additions & 6 deletions messages/javascript/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as TimeConversion from './TimeConversion'
import * as IdGenerator from './IdGenerator'
import { version } from '../package.json'
import { parseEnvelope } from './parseEnvelope'
import { getWorstTestStepResult } from './getWorstTestStepResult'
import * as TimeConversion from './TimeConversion.js'
import * as IdGenerator from './IdGenerator.js'
import { parseEnvelope } from './parseEnvelope.js'
import { getWorstTestStepResult } from './getWorstTestStepResult.js'
import { version } from './version.js'

export * from './messages'
export * from './messages.js'

export { TimeConversion, IdGenerator, version, parseEnvelope, getWorstTestStepResult }
2 changes: 1 addition & 1 deletion messages/javascript/src/parseEnvelope.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Envelope } from './messages'
import { Envelope } from './messages.js'
import { plainToClass } from 'class-transformer'

/**
Expand Down
2 changes: 2 additions & 0 deletions messages/javascript/src/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// This file is generated using `make src/version.ts` or `make .codegen`
export const version = '17.1.0'
4 changes: 2 additions & 2 deletions messages/javascript/test/TimeConversionTest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from 'assert'
import { Duration, TimeConversion } from '../src'
import { addDurations } from '../src/TimeConversion'
import { Duration, TimeConversion } from '../src/index.js'
import { addDurations } from '../src/TimeConversion.js'

const {
durationToMilliseconds,
Expand Down
4 changes: 2 additions & 2 deletions messages/javascript/test/getWorstTestStepResultsTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getWorstTestStepResult } from '../src/getWorstTestStepResult'
import { TestStepResultStatus } from '../src/messages'
import { getWorstTestStepResult } from '../src/getWorstTestStepResult.js'
import { TestStepResultStatus } from '../src/messages.js'
import assert from 'assert'

describe('getWorstTestStepResult', () => {
Expand Down
2 changes: 1 addition & 1 deletion messages/javascript/test/messagesTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'assert'
import { Envelope, parseEnvelope } from '../src/index'
import { Envelope, parseEnvelope } from '../src/index.js'

describe('messages', () => {
it('defaults missing fields when deserialising from JSON', () => {
Expand Down
11 changes: 11 additions & 0 deletions messages/javascript/tsconfig.build-cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.build-cjs.json",
"compilerOptions": {
"rootDir": ".",
"outDir": "dist/cjs"
},
"include": [
"src",
"test"
],
}
11 changes: 11 additions & 0 deletions messages/javascript/tsconfig.build-esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.build-esm.json",
"compilerOptions": {
"rootDir": ".",
"outDir": "dist/esm"
},
"include": [
"src",
"test"
],
}
2 changes: 1 addition & 1 deletion messages/javascript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": "../../tsconfig.json"
"extends": "../../tsconfig.esm.json"
}
Loading

0 comments on commit f1570e2

Please sign in to comment.