Skip to content

Commit

Permalink
Compile to ES2015 modules instead of commonjs
Browse files Browse the repository at this point in the history
Caveat: I have not tested whether this package will build correctly or
not. I was only able to test it locally using `npm run build` and then
using `npm link ../$THIS_REPOSITORY` bundling it into a vanilla-ts Vite
test page.

Using ES modules (ESM) increases compatibility with modern tooling.
Using commonjs regularly introduced issues when working various tools.

One of those issues was directly using this package with unpkg.com or
the `npm link` command, e.g. `npm link ../$THIS_REPO`, resulting in
build errors complaining about a default export missing (presumably
since ES modules are expected).

Not being able to load this package using unpkg.com reduces testability,
e.g., when wanting to try Sarus in a jsfiddle.

Furthermore:

It would be good to investigate at this point whether checking the dist/
folder into the repository makes sense at all. When building an NPM
package, the dist folder can be packaged then, and since it can be
derived using `npm run build` any time, it should be left out of the
repository.

Being able to load this package using unpkg.com makes it easier for
developers to evaluate whether this package is usable for their needs.
Previously one needed to install it with NPM and use a build system that
explicitly supports common js modules.

Every modern supported version of Node.js (the introducer of commonjs)
supports ES modules as well. (See https://nodejs.org/api/esm.html)

Bundlers that support ESM:
- Webpack: https://webpack.js.org/guides/ecma-script-modules/
- Rollup (used by Vite): https://rollupjs.org/es-module-syntax/

Further note:

Consider switching back to >= ES2015 build target.
- https://caniuse.com/websockets: 97.17%
- https://caniuse.com/es6: 97.15
Unless there is an argument to keep ES5, such as a specific client's use
case, it would be good to update. Furthermore, core-js has polyfills for
all es6 features: https://github.com/zloirock/core-js#ecmascript
  • Loading branch information
justuswilhelm committed Oct 8, 2023
1 parent 78a547e commit af18dd0
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "ES5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
"module": "ES2015" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
"allowJs": false /* Allow javascript files to be compiled. */,
"checkJs": false /* Report errors in .js files. */,
"outDir": "./dist" /* Redirect output structure to the directory. */,
Expand Down

0 comments on commit af18dd0

Please sign in to comment.