Skip to content

Commit

Permalink
build: imporve ESM support
Browse files Browse the repository at this point in the history
- Use thin ESM wrapper for CJS output instead of double tsc compilation.
- Replace webpack with esbuild
- Add bundled versions of ESM build.

fixes #1383
  • Loading branch information
bd82 committed Feb 27, 2021
1 parent efbc5da commit 767da78
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 564 deletions.
1 change: 1 addition & 0 deletions .idea/chevrotain.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/implementation_languages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ The following examples all implement the same JSON parser using a variety of imp

- [ECMAScript 5](https://github.com/chevrotain/chevrotain/blob/master/examples/implementation_languages/ecma5/ecma5_json.js)

- [ECMAScript 6/2015](https://github.com/chevrotain/chevrotain/blob/master/examples/implementation_languages/ecma6/ecma6_json.js)
- [Modern ECMAScript](https://github.com/chevrotain/chevrotain/blob/master/examples/implementation_languages/modern_ecmascript/modern_ecmascript_json.mjs)

- [TypeScript](https://github.com/chevrotain/chevrotain/blob/master/examples/implementation_languages/typescript/typescript_json.ts)

- [CoffeeScript](https://github.com/chevrotain/chevrotain/blob/master/examples/implementation_languages/coffeescript/coffeescript_json.coffee)

To run all the implementation languages examples's tests:
To run all the implementation languages examples tests:

- `npm install` (only once)
- `npm test`
1 change: 0 additions & 1 deletion examples/implementation_languages/impl_lang_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ function createSanityTest(languageName, parseJson) {

describe("The ability to use Chevrotain using different implementation languages", () => {
createSanityTest("ECMAScript 5", require("./ecma5/ecma5_json"))
createSanityTest("ECMAScript 6/2015", require("./ecma6/ecma6_json"))
createSanityTest(
"TypeScript",
require("./typescript/typescript_json").parseJson
Expand Down
12 changes: 12 additions & 0 deletions examples/implementation_languages/impl_lang_spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { parseJson } from "./modern_ecmascript/modern_ecmascript_json.mjs"
import assert from "assert"

describe("The ability to use Chevrotain using modern ECMAScript", () => {
it("works with ESM", () => {
const inputText = '{ "arr": [1,2,3], "obj": {"num":666}}'
const lexAndParseResult = parseJson(inputText)

assert.equal(lexAndParseResult.lexErrors.length, 0)
assert.equal(lexAndParseResult.parseErrors.length, 0)
})
})
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use strict"
const { createToken, Lexer, CstParser } = require("chevrotain")
import { createToken, Lexer, CstParser } from "chevrotain"

// ----------------- lexer -----------------
const True = createToken({ name: "True", pattern: /true/ })
Expand Down Expand Up @@ -114,7 +114,7 @@ class JsonParserES6 extends CstParser {
// reuse the same parser instance.
const parser = new JsonParserES6()

module.exports = function (text) {
export function parseJson (text) {
const lexResult = JsonLexer.tokenize(text)
// setting a new input will RESET the parser instance's state.
parser.input = lexResult.tokens
Expand Down
7 changes: 5 additions & 2 deletions examples/implementation_languages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"build": "npm-run-all build:ts build:coffee",
"build:ts": "tsc ./typescript/typescript_json.ts --types \" \"",
"build:coffee": "coffee -c ./coffeescript/coffeescript_json.coffee",
"test": "mocha \"*spec.js\""
"test": "npm-run-all test:*",
"test:cjs": "mocha \"*spec.js\"",
"test:esm": "if-node-version \">=12.17\" && mocha \"*spec.mjs\" || node -e \"process.exit(0)\""
},
"dependencies": {
"chevrotain": "^7.1.2"
Expand All @@ -14,7 +16,8 @@
"coffee-script": "^1.11.1",
"mocha": "^8.1.3",
"npm-run-all": "^4.1.5",
"typescript": "4.2.2"
"typescript": "4.2.2",
"if-node-version": "^1.1.1"
},
"private": true
}
7 changes: 7 additions & 0 deletions examples/implementation_languages/scripts/is-esm-supported.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { gte } = require("semver")

const nodeVersion = process.version

if (gte(nodeVersion, "12.17.0") === true) {
process.exit(1)
}
38 changes: 19 additions & 19 deletions packages/chevrotain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
"name": "Shahar Soel"
},
"files": [
"lib_esm/src/**/*.js",
"lib_esm/src/**/*.js.map",
"lib_esm/**/*.js",
"lib/src/**/*.js",
"lib/src/**/*.js.map",
"lib/chevrotain.d.ts",
Expand All @@ -36,36 +35,36 @@
"diagrams/**/*.*",
"CHANGELOG.md"
],
"main": "lib/src/api.js",
"module": "lib_esm/src/api.js",
"main": "./lib/src/api.js",
"exports": {
"require": "./lib/src/api.js",
"import": "./lib_esm/api_esm.mjs"
},
"repository": {
"type": "git",
"url": "git://github.com/Chevrotain/chevrotain.git"
},
"homepage": "https://chevrotain.io/docs/",
"scripts": {
"---------- CI FLOWS --------": "",
"build": "npm-run-all clean compile build:esm dts api-site:build bundle",
"build:esm": "npm-run-all clean:esm compile:esm",
"test": "npm-run-all test:esm compile:def coverage check-coverage",
"test:esm": "mocha \"./lib_esm/test/**/*spec.js\" --require esm",
"build": "npm-run-all clean compile dts api-site:build bundle",
"test": "npm-run-all compile:def coverage check-coverage",
"version": "tsc ./src/version.ts --outDir lib/src && node ./scripts/version-update.js",
"postversion": "npm-run-all website:build website:upload api-site:build api-site:upload",
"---------- DEV FLOWS --------": "",
"watch": "tsc -w",
"unit-tests": "mocha \"./lib/test/**/*spec.js\"",
"quick-build": "tsc && yarn run bundle",
"---------- BUILD STEPS --------": "",
"clean": "shx rm -rf coverage dev lib",
"clean:esm": "shx rm -rf lib_esm",
"compile": "tsc && node ./scripts/fix-coverage-report.js",
"compile:esm": "tsc --project tsconfig.esm.json",
"clean": "shx rm -rf coverage dev lib lib_esm",
"compile": "tsc && node ./scripts/fix-coverage-report.js && npm-run-all gen-esm-wrapper",
"compile:watch": "tsc -w",
"compile:def": "npm-run-all compile-def-api compile-def-modules compile-def-namespace",
"gen-esm-wrapper": "gen-esm-wrapper . ./lib_esm/api_esm.mjs",
"dts": "node scripts/process-docs.js",
"coverage": "nyc mocha \"./lib/test/**/*spec.js\"",
"check-coverage": "nyc check-coverage --lines 100 --branches 100 --statements 100 --functions 100",
"bundle": "npm-run-all bundle:regular bundle:min",
"bundle": "npm-run-all bundle:*",
"api-site:build": "typedoc api.d.ts --out dev/docs --excludeExternals --excludePrivate",
"api-site:upload": "./scripts/api-site-upload.sh",
"website:dev": "vuepress dev docs",
Expand All @@ -77,8 +76,10 @@
"compile-def-modules": "tsc --noImplicitAny test_integration/definitions/es6_modules.ts --outDir dev/garbage --lib \"es2015\"",
"compile-def-namespace": "tsc --noImplicitAny test_integration/definitions/namespaces.ts --module none --outDir dev/garbage --lib \"es2015\"",
"---------- BUNDLING --------": "",
"bundle:regular": "webpack --config webpack_release.config.js",
"bundle:min": "webpack --config webpack_release_min.config.js"
"bundle:regular": "esbuild ./lib/src/api.js --bundle --sourcemap --outfile=lib/chevrotain.js",
"bundle:min": "esbuild ./lib/src/api.js --bundle --minify --sourcemap --outfile=lib/chevrotain.min.js",
"bundle:esm:regular": "esbuild ./lib/src/api.js --bundle --sourcemap --format=esm --outfile=lib/chevrotain.mjs",
"bundle:esm:min": "esbuild ./lib/src/api.js --bundle --minify --format=esm --sourcemap --outfile=lib/chevrotain.min.mjs"
},
"dependencies": {
"regexp-to-ast": "0.5.0"
Expand All @@ -89,7 +90,6 @@
"@types/sinon-chai": "^3.2.0",
"chai": "^4.1.2",
"error-stack-parser": "^2.0.6",
"esm": "^3.2.25",
"gitty": "^3.6.0",
"jsdom": "16.4.0",
"jsonfile": "^6.0.1",
Expand All @@ -101,9 +101,9 @@
"typedoc": "^0.20.28",
"typescript": "4.2.2",
"vuepress": "^1.4.1",
"webpack": "5.24.2",
"webpack-cli": "^4.1.0",
"xregexp": "^5.0.1"
"xregexp": "^5.0.1",
"gen-esm-wrapper": "^1.0.4",
"esbuild": "^0.8.53"
},
"typings": "lib/chevrotain.d.ts",
"mocha": {
Expand Down
9 changes: 0 additions & 9 deletions packages/chevrotain/tsconfig.esm.json

This file was deleted.

2 changes: 1 addition & 1 deletion packages/chevrotain/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"module": "commonjs",
"module": "CommonJS",
"removeComments": false,
"target": "ES5",
"outDir": "lib",
Expand Down
29 changes: 0 additions & 29 deletions packages/chevrotain/webpack_release.config.js

This file was deleted.

29 changes: 0 additions & 29 deletions packages/chevrotain/webpack_release_min.config.js

This file was deleted.

29 changes: 0 additions & 29 deletions packages/chevrotain/webpack_specs.config.js

This file was deleted.

4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ as any other pure code without requiring any new tools or processes.
- Latest:
- `https://unpkg.com/chevrotain/lib/chevrotain.js`
- `https://unpkg.com/chevrotain/lib/chevrotain.min.js`
- `https://unpkg.com/chevrotain/lib_esm/chevrotain.mjs`
- `https://unpkg.com/chevrotain/lib_esm/chevrotain.min.mjs`
- Explicit version number:
- `https://unpkg.com/chevrotain@7.1.2/lib/chevrotain.js`
- `https://unpkg.com/chevrotain@7.1.2/lib/chevrotain.min.js`
- `https://unpkg.com/chevrotain@7.1.2/lib_esm/chevrotain.mjs`
- `https://unpkg.com/chevrotain@7.1.2/lib_esm/chevrotain.min.mjs`

## Documentation & Resources

Expand Down
Loading

0 comments on commit 767da78

Please sign in to comment.