Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Javascript -- v1.2.0: Reimplement esm transpilation, and swap to it as the default. #479

Merged
merged 149 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
149 commits
Select commit Hold shift + click to select a range
e0ce087
Add links to the devlog
Skenvy Sep 26, 2022
acbb5e5
Add the default package json
Skenvy Oct 3, 2022
d5bb46c
Merge branch 'main' into javascript
Skenvy Oct 12, 2022
1874a07
Merge branch 'main' into javascript
Skenvy Oct 12, 2022
58bd634
Add more details to the package.json
Skenvy Oct 15, 2022
a6f7b90
A lot of comments in the devlog
Skenvy Oct 16, 2022
bc115c2
Update the workflows and add some basic recipes and remove the exit 1…
Skenvy Oct 30, 2022
96ebaed
Add cache dependency path to subdir
Skenvy Oct 30, 2022
9ab638e
Remove caching in setup-node
Skenvy Oct 30, 2022
b82f5a5
Add dist download and single publish recipe
Skenvy Oct 30, 2022
da8652d
Uncomment publish-npm
Skenvy Oct 30, 2022
ae6bad9
Remove the NA placeholder password in the java-test workflow, codeQL …
Skenvy Oct 30, 2022
41d9c76
Merge branch 'main' into javascript
Skenvy Oct 31, 2022
9b5ee75
Fix the missing permssions for the nested codeql step and add them to…
Skenvy Oct 31, 2022
5ab3c1d
Merge branch 'main' into javascript
Skenvy Oct 31, 2022
b36cc65
Remove the trailing slash on the path, and add a release upload target
Skenvy Oct 31, 2022
165d125
Add a word to the package
Skenvy Oct 31, 2022
2867401
Merge branch 'main' into javascript
Skenvy Oct 31, 2022
5255579
Whoops, remove the clean off publish
Skenvy Oct 31, 2022
d80db40
Bump the package.json to trigger package file changed
Skenvy Oct 31, 2022
e71f36c
Merge branch 'main' into javascript
Skenvy Oct 31, 2022
ff21c00
Add missing permission for publishing a package to github
Skenvy Oct 31, 2022
d046f7c
Merge branch 'main' into javascript
Skenvy Oct 31, 2022
3118378
Add npm-gh reference in main readme
Skenvy Oct 31, 2022
8d4938a
Add module dichotomy paragraph to devlog
Skenvy Nov 5, 2022
e9632eb
Merge branch 'main' into javascript
Skenvy Nov 6, 2022
8711eda
Merge branch 'main' into javascript
Skenvy Jan 15, 2023
b5a2d7a
Add mention of typescript
Jan 15, 2023
99ec339
Added some typescript settings to run tsc for cjs and mjs
Jan 16, 2023
da486a0
Added mocha-chai test setup for unit test framework.
Mar 27, 2023
aacb7d9
Bump emojis
Skenvy Mar 27, 2023
b9bd9f5
Some further comments regarding the snyk guide
Skenvy Mar 27, 2023
ae56b7a
Bump version
Mar 27, 2023
20ad21c
Run pack to have the library up to date.
Mar 27, 2023
474a0a4
Merge branch 'main' into javascript
Mar 27, 2023
932d538
Merge branch 'main' into javascript
Skenvy Apr 7, 2023
9f521d5
WIP fuse together snippets from the julia, java, and python implement…
Apr 8, 2023
64d01b9
Print the first layer of deps installed in ci
Apr 8, 2023
d86d59d
Change the minimum node version to 14.
Apr 8, 2023
48822a6
Update engine requires and install eslint
Apr 9, 2023
fb3dbe8
Add a linting step to CI (this push will fail on linting)
Apr 9, 2023
3dc78fd
Try adding AirBnB eslint config
Apr 9, 2023
24f9b9a
Fix test issue (lint will fail)
Apr 9, 2023
08340a1
Fewer linting errors
Apr 10, 2023
3b0eb60
Linter only warning now.
Apr 10, 2023
dd008ae
Format devlog
Apr 10, 2023
d508e6a
Add the reverse function
Apr 10, 2023
9d2793c
Tested and linted the reverse function
Apr 10, 2023
c2602fd
Add a recipe to fail if the checked in JS mismatches with the checked…
Apr 10, 2023
440b71b
Clean before the new verify
Apr 10, 2023
ebd3d00
Comment about docs gene
Apr 10, 2023
3674010
Install typedoc and tsdoc lint
Apr 10, 2023
ac99b63
Add tsdoc linter and try to make comments tsdoc-ish
Apr 10, 2023
70bb9ff
Recipe for docs
Apr 10, 2023
5eeae0a
Less cleaning when doccing
Apr 10, 2023
4516a92
Comment on attempt at JSDoc
Skenvy Apr 10, 2023
d685d32
Comment about setting up the docs target branch
Apr 10, 2023
f3e7d35
Bump up the version to test the whole CICD for javascript, with the f…
Apr 10, 2023
0288570
Retranspile the js files
Apr 10, 2023
2b87172
Add the devDependencies to the docs step
Skenvy Apr 10, 2023
515fec8
Stop windows from corrupting the line endings that eslint requires
Skenvy Apr 10, 2023
c971d61
Merge branch 'main' into javascript
Skenvy Apr 11, 2023
c078d06
Merge branch 'main' into javascript
Skenvy Apr 25, 2023
c7c279e
Add words to the readme
Apr 25, 2023
d1edc65
yike the lock
Apr 25, 2023
2944619
Up the node version in CI to one that bumps npm over 8.6
Apr 25, 2023
0ba765c
version
Apr 25, 2023
57aa219
Merge branch 'main' into javascript
Skenvy Apr 25, 2023
a49b99c
Merge branch 'main' into javascript
Jun 22, 2023
b3a8d9e
npm up
Jun 22, 2023
9085688
Add stoppingTimeTerminus
Jun 22, 2023
086bf17
transpile
Jun 22, 2023
5770593
wip adding hailstone
Jun 22, 2023
4fbecdf
transpile
Jun 22, 2023
35dc605
Add make for http-server and comment regarding __namedParameters
Jun 24, 2023
57c82b0
Swap from a named interface to anonymous interfaces i.e. object liter…
Jun 25, 2023
2bb5004
line length
Jun 25, 2023
b5cbbfb
WIP tests for hailstone sequence
Jun 25, 2023
2153c0f
WIP tests for hailstone sequence
Jun 25, 2023
70f380c
Finished testing hailstones
Jun 25, 2023
d0ee908
Bump to 0.4.0
Skenvy Jun 25, 2023
d9b6e33
Merge branch 'main' into javascript
Skenvy Jun 25, 2023
982d1c4
Stopping Time
Jun 26, 2023
21bb328
split into multiple files
Jun 26, 2023
9de2615
cutover to multiple files, tests and lints work
Jun 26, 2023
9e02a65
Add coverage reports being generated into docs
Jun 26, 2023
c14422f
coverage report handled
Jun 26, 2023
b5d22e8
pkg and lib
Jun 26, 2023
7576741
Merge branch 'main' into javascript
Skenvy Jun 27, 2023
e068ede
WIP tree graph
Jun 27, 2023
5a8a23e
assign the provided empty map in the test tgn's
Jun 27, 2023
89d7fcb
swap tgn to subtree
Skenvy Jun 27, 2023
93c102b
Set up the deep equals test for tree graph
Skenvy Jun 28, 2023
5a8904f
test, lint, and verify
Skenvy Jun 28, 2023
a63ba77
bump checkout
Skenvy Jun 28, 2023
b2c3de6
Add cache, add a basic stub for demos, and add comments regarding deno
Skenvy Jun 28, 2023
dfdb501
fix wf default and add node types to demo
Skenvy Jun 28, 2023
2d953d3
fix name of needed wf job
Skenvy Jun 28, 2023
ddf96f1
dot the demo folder
Skenvy Jun 28, 2023
8ef25e5
pkg
Jun 28, 2023
6275e19
Does setting up node with the development version fix npm not running…
Skenvy Jun 28, 2023
113113a
ok, ls the dir first, but also see if strip version manually works
Skenvy Jun 28, 2023
4a43ef9
but without another thing running first it doesn't run preinstall??
Skenvy Jun 29, 2023
f554f19
Does changing from preinstall to prepare let it know to run before th…
Skenvy Jun 29, 2023
10c6ea3
Does using the full name 'install' fix it?
Skenvy Jun 29, 2023
4ec9dd8
ok clearly lifecycle scripts are fucked
Skenvy Jun 29, 2023
8066106
Add ts demos
Jun 29, 2023
fe3ffbc
remove spaces
Jun 29, 2023
7f6dcc9
bump package to first major version
Jun 29, 2023
34fde4c
Merge branch 'main' into javascript
Skenvy Jun 29, 2023
002838f
Merge branch 'main' into javascript
Skenvy Jul 5, 2023
1f2a3f1
Stop testing node on version 17 and 19
Jul 5, 2023
cbd0b4a
Merge branch 'main' into javascript
Nov 2, 2023
022eb13
Set new minor release to update allowed engines
Nov 2, 2023
db578bc
Add wraps in the make to preempt nvm use on every npm command
Nov 2, 2023
b0c0425
Add overwrite of the nvmrc for the test steps
Nov 2, 2023
3d963bf
put install recipes in conditional make vars to not use nvm on windows
Nov 2, 2023
bf0d331
Merge branch 'main' into javascript
Skenvy Nov 2, 2023
8dd8255
Install nvm and npm versions before using make publish
Nov 2, 2023
ce7066b
Merge branch 'main' into javascript
Skenvy Mar 15, 2024
bfc5f87
Merge branch 'main' into javascript
Skenvy Mar 15, 2024
6fb3992
Merge branch 'main' into javascript
Skenvy Mar 15, 2024
19d82a9
Ran npm i on the unchanged package
Skenvy Mar 16, 2024
420f022
Bump mocha, @types/mocha, @types/node
Skenvy Mar 16, 2024
9c820c4
Bump everything except chai
Skenvy Mar 16, 2024
62b89d6
Bump chai to latest 4.x
Skenvy Mar 16, 2024
616dd0d
Merge branch 'main' into javascript
Skenvy Mar 16, 2024
248f85a
Bump chai to latest
Skenvy Mar 16, 2024
9dbd209
Add a node-options loader to use esm with mocha
Skenvy Mar 16, 2024
a91a610
wip trying different things to let chai v5
Skenvy Mar 16, 2024
00a11a8
Remove the attempt to make chai v5 work
Skenvy Mar 16, 2024
61297bf
very WIP, writing about this ridiculous ecosystem
Skenvy Mar 16, 2024
197dea7
demo to assert imports of the project work
Skenvy Mar 16, 2024
31b144e
Add back the previous copy of the transpiled result so there's less d…
Skenvy Mar 16, 2024
034408a
Remove the src and tests changes to record what the errors were
Skenvy Mar 16, 2024
e65684f
Revert all the things done to fix issues to replay the errors
Skenvy Mar 16, 2024
1dd284a
make build works
Skenvy Mar 16, 2024
e3f112f
added more specificity to the demo steps, wip mjs'ing and cjs'ing the…
Skenvy Mar 17, 2024
ce86c20
the project is buildable and the demo runs!
Skenvy Mar 17, 2024
007574f
use esm tsconf for docs
Skenvy Mar 17, 2024
65a93ef
fix ems -> esm and add use of mocha tsconf in covr
Skenvy Mar 17, 2024
7326064
Update setup-node action
Skenvy Mar 17, 2024
8cc3f60
update the version
Skenvy Mar 17, 2024
56a0ea7
fixed the ems -> esm typo in the demo package
Skenvy Mar 17, 2024
c377197
Change the demo test to use the new npm script names
Skenvy Mar 17, 2024
2f1294e
Add cross-env to let the test script continue to work on windows
Skenvy Mar 17, 2024
b1164e3
Add eol spec to gitattr to stop windows runners from not liking the n…
Skenvy Mar 17, 2024
277773d
Does changing package json to autocrlf fix the windows runner not lik…
Skenvy Mar 17, 2024
10f3f1a
Just don't bother with verifying the lib checkin on the matrix of run…
Skenvy Mar 17, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
# our GitHub Actions, which would otherwise change the line ending and fail.
*.js text eol=lf
*.ts text eol=lf
*.cjs text eol=lf
*.mjs text eol=lf
package.json text eol=lf
8 changes: 4 additions & 4 deletions .github/workflows/javascript-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ jobs:
- name: 🏁 Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: 🟨🟦🟩🟥 Set up Node
uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # v3.5.1
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
with:
node-version-file: 'javascript/.nvmrc'
cache: 'npm'
Expand Down Expand Up @@ -116,7 +116,7 @@ jobs:
name: Package
path: javascript/
- name: 🟨🟦🟩🟥 Set up Node
uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # v3.5.1
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
with:
node-version-file: 'javascript/.nvmrc'
registry-url: 'https://npm.pkg.github.com'
Expand All @@ -142,7 +142,7 @@ jobs:
name: Package
path: javascript/
- name: 🟨🟦🟩🟥 Set up Node
uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # v3.5.1
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
with:
node-version-file: 'javascript/.nvmrc'
registry-url: 'https://registry.npmjs.org'
Expand All @@ -165,7 +165,7 @@ jobs:
- name: 🏁 Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: 🟨🟦🟩🟥 Set up Node
uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # v3.5.1
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
with:
node-version-file: 'javascript/.nvmrc'
cache: 'npm'
Expand Down
27 changes: 13 additions & 14 deletions .github/workflows/javascript-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- name: 🏁 Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: 🟨🟦🟩🟥 Set up Node
uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # v3.5.1
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
with:
node-version-file: 'javascript/.nvmrc'
cache: 'npm'
Expand Down Expand Up @@ -62,7 +62,7 @@ jobs:
- name: 🏁 Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: 🟨🟦🟩🟥 Set up Node ${{ matrix.version }}
uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # v3.5.1
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
with:
node-version: '${{ matrix.version }}'
cache: 'npm'
Expand All @@ -75,10 +75,9 @@ jobs:
run: make test
- name: 🧹 Lint
run: make lint
# I'm not sure what benefit there is to doing this check in the whole matrix
# but it'd be interesting if it shook something loose I didn't expect!
- name: ⚖ Does the checked in JS match the transpiled TS?
run: make verify_transpiled_checkin
# We probably don't need to do this on all runners, and the windows CRLF echos are annoying
# - name: ⚖ Does the checked in JS match the transpiled TS?
# run: make verify_transpiled_checkin
built-example:
name: JavaScript 🟨🟦🟩🟥 Build Example 🦛
if: >-
Expand All @@ -89,7 +88,7 @@ jobs:
- name: 🏁 Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: 🟨🟦🟩🟥 Set up Node
uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # v3.5.1
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
with:
node-version-file: 'javascript/.nvmrc'
cache: 'npm'
Expand Down Expand Up @@ -119,7 +118,7 @@ jobs:
- name: 🏁 Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: 🟨🟦🟩🟥 Set up Node
uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # v3.5.1
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
with:
node-version-file: 'javascript/.nvmrc'
- name: 🆒 Download dists
Expand All @@ -128,11 +127,11 @@ jobs:
name: Package
path: javascript/.demo
- name: 🦛 Install this package
run: npm run strip-version && npm install
- name: 🦏 Transpile demo
run: npm run build
- name: 🦏 Run usage demonstration
run: npm run demo
run: npm run setup
- name: 🦏 Transpile and run demonstration; 🦑 CJS
run: npm run everything:cjs
- name: 🦏 Transpile and run demonstration; 🐙 ESM
run: npm run everything:esm
codeql:
name: JavaScript 🟨🟦🟩🟥 CodeQL 🛡👨‍💻🛡
if: >-
Expand All @@ -152,7 +151,7 @@ jobs:
- name: 🏁 Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: 🟨🟦🟩🟥 Set up Node
uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # v3.5.1
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
with:
node-version-file: 'javascript/.nvmrc'
cache: 'npm'
Expand Down
2 changes: 2 additions & 0 deletions javascript/.demo/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
package-lock.json
_/*.js
_/*.mjs
_/*.cjs
1 change: 0 additions & 1 deletion javascript/.demo/_/stoppingTime.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as Collatz from '@skenvy/collatz';


// Default (P,a,b); 0 trap [as b is not a multiple of a]
// 'should return the Default (P,a,b); 0 trap'
// // Test 0's immediated termination.
Expand Down
27 changes: 19 additions & 8 deletions javascript/.demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,29 @@
"license": "Apache-2.0",
"main": "index.js",
"dependencies": {
"@skenvy/collatz": "file:./skenvy-collatz.tgz"
"@skenvy/collatz": "file:skenvy-collatz.tgz"
},
"scripts": {
"clean": "rm **/*.js",
"clean:modules": "rm -rf node_modules",
"clean:js": "rm -f _/**.js",
"clean:cjs": "rm -f _/**.cjs",
"clean:esm": "rm -f _/**.mjs",
"clean": "npm run clean:modules && npm run clean:js && npm run clean:cjs && npm run clean:esm",
"strip-version": "[ -f skenvy-collatz.tgz ] || mv skenvy-collatz-*.tgz skenvy-collatz.tgz",
"build": "tsc --module CommonJS --target ES2020 --lib ES2020 --types node ./_/*.ts",
"demo": "find ./_/*.js -maxdepth 1 -type f | xargs -L1 node",
"everything": "npm run clean && npm run strip-version && npm i && npm run build && npm run demo"
"build:cjs": "tsc --module CommonJS --moduleResolution node --target ES2020 --lib ES2020 --types node ./_/*.ts",
"build:esm": "tsc --module ESNext --moduleResolution node --target ES2022 --lib ES2022 --types node ./_/*.ts",
"ftype:cjs": "for f in `find ./_ -iname '*.js' -type f -print`;do mv \"$f\" ${f%.js}.cjs; done",
"ftype:esm": "for f in `find ./_ -iname '*.js' -type f -print`;do mv \"$f\" ${f%.js}.mjs; done",
"demo:cjs": "find ./_/*.cjs -maxdepth 1 -type f | xargs -L1 node",
"demo:esm": "find ./_/*.mjs -maxdepth 1 -type f | xargs -L1 node",
"setup": "npm run clean && npm run strip-version && npm i -f && npm i file:skenvy-collatz.tgz",
"everything:cjs": "npm run build:cjs && npm run ftype:cjs && npm run demo:cjs && npm run clean:cjs",
"everything:esm": "npm run build:esm && npm run ftype:esm && npm run demo:esm && npm run clean:esm",
"everything": "npm run setup && npm run everything:cjs && npm run everything:esm"
},
"devDependencies": {
"@types/node": "^20.3.2",
"async": "^3.2.4",
"typescript": "^5.1.5"
"@types/node": "^20.11.28",
"async": "^3.2.5",
"typescript": "^5.4.2"
}
}
4 changes: 3 additions & 1 deletion javascript/.mocharc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"extension": ["ts"],
"spec": "./**/*.spec.ts",
"require": "ts-node/register"
"require": "ts-node/register",
"loader": "ts-node/esm",
"es-module-specifier-resolution": "node"
}
14 changes: 11 additions & 3 deletions javascript/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ NPM=$(_) npm
INSTALL_NODE=$(NVM) install $$(cat ./.nvmrc)
INSTALL_NPM=$(NPM) i -g npm@$$(cat ./.npm-version)
endif
.PHONY: bump_major bump_minor bump_patch install_node install_npm setup update clean docs docs_server test lint verify_transpiled_checkin build publish g
PKG_NAME=skenvy-collatz
.PHONY: bump_major bump_minor bump_patch install_node install_npm setup update clean docs docs_server test lint verify_transpiled_checkin build publish demo _demo
SHELL:=/bin/bash

# Versions can be manually changed where they appear in the package json and
Expand Down Expand Up @@ -52,7 +53,7 @@ update:
$(NPM) install

clean:
rm -f skenvy-collatz-*.tgz
rm -f $(PKG_NAME)-*.tgz
rm -rf docs
rm -rf .nyc_output
$(NPM) run clean
Expand Down Expand Up @@ -87,4 +88,11 @@ build: clean test lint verify_transpiled_checkin

# https://docs.npmjs.com/cli/v8/commands/npm-publish
publish:
$(NPM) publish --access=public skenvy-collatz-*.tgz
$(NPM) publish --access=public $(PKG_NAME)-*.tgz

demo: build
mv $(PKG_NAME)-*.tgz .demo/$(PKG_NAME).tgz
cd .demo && npm run everything

_demo: # packless demo to rerun it while manually tweaking it
cd .demo && npm run everything
35 changes: 35 additions & 0 deletions javascript/devlog.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,38 @@ Well, at least the webhook worked, and it should deploy to deno on the next tag.
# NVM
[NVM](https://github.com/nvm-sh/nvm), "Node version manager", is an extremely useful tool for managing multiple versions of node installed at the same time.
Coming back in here to say that, while updating the supported engines to remove 14 and 16 and add 20, that I'm surprised I hadn't mentioned nvm in here before.
## Another exercise in absurdity.
Yet again, a tumble down a rabbit-worm-hole has transpired from very little shaking the node tree. A dependabot PR to suggest upgrading `chai` from `v4` to `v5` led to quite a while trying to investigate why it was difficult to run `mocha` tests with it, to learn that [chai v5 dropped cjs support](https://github.com/chaijs/chai/issues/1597), and that [mocha's support for esm is still expiremental](https://github.com/mochajs/mocha/issues/4900) [[2:GH](https://github.com/mochajs/mocha-examples/issues/47)] [[3:SO](https://stackoverflow.com/questions/40635956/overriding-tsconfig-json-for-ts-node-in-mocha)]. Also see [this SO RE 'TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"'](https://stackoverflow.com/questions/71893906/run-mocha-with-typescript-throws-typeerror-err-unknown-file-extension-unknow). The rabbit-worm-hole also involved trying to figure out if our use of `nyc` had any influence / impact on this error, see [[1](https://github.com/istanbuljs/nyc/issues/1287)] [[2](https://github.com/istanbuljs/esm-loader-hook#readme)].

After deciding it would probably just be best to give up on it and say we're sticking with chai v4, I had a look in to the compiled "esm" output that changed last year when setting the `esm` `tsconfig`'s `module` and `moduleResolution` to `nodenext` last year to let through a typescript update. But it would appear now that I should have looked closer at the change in the compiled "esm" code back then, as this change to "nodenext" also changed the output of the "esm" build to "cjs". So the output at the moment is just _two copies of cjs_.

We definitely want to support esm, as the primary target. We might be able to support it optionally, and getting back to that would be a priority (as well as adding a check for this working in the demo, which was only checking that the cjs result was valid), but it would be nice if we could jump straight to esm as our default. We can edit the `package.json` to include the changes;
```diff
@@ -27,8 +27,9 @@
"files": [
"./lib/**/*"
],
- "main": "./lib/cjs/index.js",
- "types": "./lib/cjs/types/index.d.ts",
+ "main": "./lib/esm/index.mjs",
+ "types": "./lib/esm/types/index.d.ts",
+ "type": "module",
"exports": {
".": {
"import": {
```
But this still yeilds a `TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /<>/Collatz/javascript/tests/collatzFunction.spec.ts` during the `nyc mocha` execution. Following up with specifying `TS_NODE_PROJECT='./tsconfig.esm.json'` before the `nyc mocha` gets the same error. If we go ahead and add the `.mocharc.json` differences recommended by [a comment at the end of this](https://github.com/mochajs/mocha/issues/4900);
```diff
@@ -1,7 +1,5 @@
{
"extension": ["ts"],
"spec": "./**/*.spec.ts",
- "require": "ts-node/register"
+ "require": "ts-node/register",
+ "loader": "ts-node/esm",
+ "es-module-specifier-resolution": "node"
}
```
We get the error `TS2835: Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean '../src/index.js'`. Addressing this by adding `.ts` in all relative imports in the tests yields a different error `TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.`. Adding the requested `"allowImportingTsExtensions": true` to our base `tsconfig` now yields another 10 or so `TS2835: Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './XYZ.js'?`. Doing the same thing with all relative imports in the src yields passing tests, but the subsequent `tsc` gives us `npm ERR! error TS5096: Option 'allowImportingTsExtensions' can only be used when either 'noEmit' or 'emitDeclarationOnly' is set.`.

Whilst googling to try and solve this often yielded very thoroughly answered SO posts such as [this one about modules and moduleResolution](https://stackoverflow.com/questions/71463698/why-we-need-nodenext-typescript-compiler-option-when-we-have-esnext), the overall wholistic answer came from [here](https://dev.to/ayc0/typescript-50-new-mode-bundler-esm-1jic). The gist being that typescript wont ever change the name of a module, which includes not renaming a relative import of a `.ts` file to a `.js` file, but the relative link will only have any meaning in the context of having already been transpiled, so the suggestion to rename things to `.js` is not as misleading as it seemed initially, which did a lot of heavy lifting the bury the lead. But yes, relative imports with `.js`, for what they will be when they are transpiled, lets it work! However we still need the relative links to `.ts` files in our mocha spec files. So we can get around both of these by having a new tsconfig that extends our ems set of options, and add the necessary `allowImportingTsExtensions` setting to that one, so that mocha sees it's allowed to import `.ts`, our `tsc` can still emit `ems` code, and wont complain that it can't emit anything with `allowImportingTsExtensions` set. Now it's just a matter of getting the demo to be happy with both import and require.
24 changes: 12 additions & 12 deletions javascript/lib/cjs/HailstoneSequence.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.hailstoneSequence = exports.HailstoneSequence = void 0;
const utilities_1 = require("./utilities");
const function_1 = require("./function");
const utilities_js_1 = require("./utilities.js");
const function_js_1 = require("./function.js");
/** Contains the results of computing a hailstone sequence. */
class HailstoneSequence {
/**
Expand All @@ -20,17 +20,17 @@
* Thrown if either P or a are 0.
*/
constructor(initialValue, P, a, b, maxTotalStoppingTime, totalStoppingTime) {
this.terminate = (0, utilities_1.stoppingTimeTerminus)(initialValue, totalStoppingTime);
this.terminate = (0, utilities_js_1.stoppingTimeTerminus)(initialValue, totalStoppingTime);
if (initialValue === 0n) {
// 0 is always an immediate stop.
this.values = [0n];
this.terminalCondition = utilities_1.SequenceState.ZERO_STOP;
this.terminalCondition = utilities_js_1.SequenceState.ZERO_STOP;
this.terminalStatus = 0;
}
else if (initialValue === 1n) {
// 1 is always an immediate stop, with 0 stopping time.
this.values = [1n];
this.terminalCondition = utilities_1.SequenceState.TOTAL_STOPPING_TIME;
this.terminalCondition = utilities_js_1.SequenceState.TOTAL_STOPPING_TIME;
this.terminalStatus = 0;
}
else {
Expand All @@ -39,16 +39,16 @@
this.values = [initialValue];
let next;
for (let k = 1; k <= minMaxTotalStoppingTime; k += 1) {
next = (0, function_1.collatzFunction)({ n: this.values[k - 1], P: P, a: a, b: b });
next = (0, function_js_1.collatzFunction)({ n: this.values[k - 1], P: P, a: a, b: b });
// Check if the next hailstone is either the stopping time, total
// stopping time, the same as the initial value, or stuck at zero.
if (this.terminate(next)) {
this.values.push(next);
if (next === 1n) {
this.terminalCondition = utilities_1.SequenceState.TOTAL_STOPPING_TIME;
this.terminalCondition = utilities_js_1.SequenceState.TOTAL_STOPPING_TIME;
}
else {
this.terminalCondition = utilities_1.SequenceState.STOPPING_TIME;
this.terminalCondition = utilities_js_1.SequenceState.STOPPING_TIME;
}
this.terminalStatus = k;
return;
Expand All @@ -62,19 +62,19 @@
break;
}
}
this.terminalCondition = utilities_1.SequenceState.CYCLE_LENGTH;
this.terminalCondition = utilities_js_1.SequenceState.CYCLE_LENGTH;
this.terminalStatus = cycleInit;
return;
}
if (next === 0n) {
this.values.push(0n);
this.terminalCondition = utilities_1.SequenceState.ZERO_STOP;
this.terminalCondition = utilities_js_1.SequenceState.ZERO_STOP;
this.terminalStatus = -k;
return;
}
this.values.push(next);
}
this.terminalCondition = utilities_1.SequenceState.MAX_STOP_OUT_OF_BOUNDS;
this.terminalCondition = utilities_js_1.SequenceState.MAX_STOP_OUT_OF_BOUNDS;
this.terminalStatus = minMaxTotalStoppingTime;
}
}
Expand All @@ -96,7 +96,7 @@
*/
function hailstoneSequence({ initialValue, P = 2n, a = 3n, b = 1n, maxTotalStoppingTime = 1000, totalStoppingTime = true }) {
// Call out the function before any magic returns to trap bad values.
const throwaway = (0, function_1.collatzFunction)({ n: initialValue, P: P, a: a, b: b });
const throwaway = (0, function_js_1.collatzFunction)({ n: initialValue, P: P, a: a, b: b });

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused variable throwaway.
// Return the hailstone sequence.
return new HailstoneSequence(initialValue, P, a, b, maxTotalStoppingTime, totalStoppingTime);
}
Expand Down
4 changes: 2 additions & 2 deletions javascript/lib/cjs/TreeGraph.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.treeGraph = exports.TreeGraph = void 0;
const TreeGraphNode_1 = require("./TreeGraphNode");
const TreeGraphNode_js_1 = require("./TreeGraphNode.js");
/**
* Contains the results of computing the Tree Graph via Collatz.treeGraph(~).
* Contains the root node of a tree of TreeGraphNode's.
Expand All @@ -19,7 +19,7 @@ class TreeGraph {
* Thrown if either P or a are 0.
*/
constructor(nodeValue, maxOrbitDistance, P, a, b) {
this.root = TreeGraphNode_1.TreeGraphNode.new(nodeValue, maxOrbitDistance, P, a, b);
this.root = TreeGraphNode_js_1.TreeGraphNode.new(nodeValue, maxOrbitDistance, P, a, b);
}
}
exports.TreeGraph = TreeGraph;
Expand Down
Loading
Loading