Skip to content

chore: set shared tsc target (ES6) - #179

Merged
keichi merged 2 commits into
keichi:masterfrom
manzt:manzt/es2018-target
Sep 27, 2021
Merged

keichi merged 2 commits into
keichi:masterfrom
manzt:manzt/es2018-target

Conversation

@manzt

@manzt manzt commented Sep 26, 2021 •

Copy link
Copy Markdown
Contributor

The commonjs & esm builds have different tsc targets ('es5' & 'esnext'). The javascript class was introduced in es6 (I believe), so the Parsers exported from the cjs & esm builds are different due to transpilation and have different characteristics. Example:

import { Parser } from 'binary-parser'; // esm
class MyParser extends Parser { ... } // works because Parser is a javascript class and can be extended
const { Parser } = require('binary-parser');
class MyParser extends Parser { ... } // Parser isn't a class for cjs output so this doesn't work correctly..

This PR sets the target output to es2018, which is a modern target and 100% compatible with node >= 10. This way the only difference between the CJS & ESM builds is the module system.

The target has been changed to ES6.

Comment thread tsconfig.json Outdated
"compilerOptions": {
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es2018", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why just touching this file led to such a large diff... sorry!

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not your fault, it seems this file was using CRLF line breaks for some reason.

@keichi

keichi commented Sep 27, 2021

Copy link
Copy Markdown
Owner

@manzt

manzt commented Sep 27, 2021

Copy link
Copy Markdown
Contributor Author

I don't have an issue with targeting a slightly older ES version. However, I would push for ES6 (ES2015) because it is well supported in Node and browsers, and this specifically includes class keyword. If needed, one can transpile the library to an earlier target (and would need since ESM was introduced in ES6), and additionally polyfill TextDecoder & BigInt for that environment.

Full disclosure, I am considering extending Parser in a personal project via class Parser extends BaseParser {}.

@manzt manzt changed the title chore: set shared tsc target (2018) chore: set shared tsc target (ES6) Sep 27, 2021
@manzt

manzt commented Sep 27, 2021 •

Copy link
Copy Markdown
Contributor Author

Additionally, as of TS 4.5 apparently the default for tsc --init is ES7 (ES2016): https://twitter.com/orta/status/1441468054689419265

@keichi

keichi commented Sep 27, 2021

Copy link
Copy Markdown
Owner

Ok, let's target ES6 then 👍

@keichi
keichi merged commit 9da9080 into keichi:master Sep 27, 2021
@manzt
manzt deleted the manzt/es2018-target branch September 27, 2021 04:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants