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

feat: Convert to ES6 #90

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft

feat: Convert to ES6 #90

wants to merge 7 commits into from

Conversation

ltfschoen
Copy link
Owner

@ltfschoen ltfschoen commented Mar 14, 2023

dev works, haven't tried in production yet.
zeitgeist sdk moved to server not sure why not console logs outputting for sdk:, and undefined value being exported from IIFE so i'm doing it wrong

zeitgeistpm/sdk-next#12 (comment)

@ltfschoen
Copy link
Owner Author

ltfschoen commented Mar 27, 2023

tried updating sdk-next repo to not use lodash-es...
see branch 'convert-es6-play2'

pool.ts

+// import { isEqual } from 'lodash-es'
+import { diff } from 'deep-object-diff';
-      weight => weight?.assetId && isEqual(parseAssetId(weight?.assetId).unwrap(), assetId),
+      weight => weight?.assetId && diff(parseAssetId(weight?.assetId).unwrap(), assetId),
-      [upperFirst(key)]: obj[key],
+      [key.charAt(0).toUpperCase() + key.slice(1)]: obj[key],
+// import { range } from 'lodash'
 
 function random_rgba() {
   var o = Math.round,
@@ -10,6 +10,11 @@ function random_rgba() {
   )
 }
 
+// https://stackoverflow.com/questions/3895478/does-javascript-have-a-method-like-range-to-generate-a-range-within-the-supp
+function range(start: number, end: number) {
+  return Array.apply(0, Array(end - 1))
+    .map((element, index) => index + start);
+}

tried `"pretest": "../node_modules/esbuild/bin/esbuild --platform=node ./api/deps.ts --bundle --outfile=./api/deps.bundle.js"

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/luke/code/github/ltfschoen/flappytips2/node_modules/lodash-es/lodash.js from /Users/luke/code/github/ltfschoen/flappytips2/node_modules/@zeitgeistpm/utility/dist/object/index.js not supported.
Instead change the require of lodash.js in /Users/luke/code/github/ltfschoen/flappytips2/node_modules/@zeitgeistpm/utility/dist/object/index.js to a dynamic import() which is available in all CommonJS modules.

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/luke/code/github/ltfschoen/flappytips2/node_modules/@zeitgeistpm/indexer/dist/index.js from /Users/luke/code/github/ltfschoen/flappytips2/node_modules/@zeitgeistpm/sdk/dist/model/markets/market.js not supported.
index.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /Users/luke/code/github/ltfschoen/flappytips2/node_modules/@zeitgeistpm/indexer/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /Users/luke/code/github/ltfschoen/flappytips2/node_modules/ipfs-http-client/package.json
code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'

    Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/luke/code/github/zeitgeistpm/sdk-next/packages/indexer/dist/index.js from /Users/luke/code/github/zeitgeistpm/sdk-next/packages/sdk/dist/model/markets/market.js not supported.
index.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /Users/luke/code/github/zeitgeistpm/sdk-next/packages/indexer/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

ran sdk-next with:

cd ~/code/github/zeitgeistpm/sdk-next
nvm use 16.8.1
yarn && yarn build
rm -rf /Users/luke/code/github/ltfschoen/flappytips2/node_modules/@zeitgeistpm
cp -r /Users/luke/code/github/zeitgeistpm/sdk-next/packages /Users/luke/code/github/ltfschoen/flappytips2/node_modules/@zeitgeistpm

removed `"type": "module",` from @zeitgeistpm/indexer

added the following to package.json of ipfs-http-client/package.json

"exports": {
...
"default": "./src/index.js"
}
"main": "./src/index.js",

removed `"type": "module",` from ipfs-http-client/package.json

temporarily add these dependencies so in node_modules (since zeitgeist has them but can't install its dependencies)

```
cd flappytips2

cp -rn /Users/luke/code/github/zeitgeistpm/sdk-next/node_modules/ /Users/luke/code/github/ltfschoen/flappytips2/node_modules/

ln -is /path/to/package/directory packagename

cd /Users/luke/code/github/ltfschoen/flappytips2/node_modules/
mkdir -p @zeitgeistpm/augment-api @zeitgeistpm/indexer @zeitgeistpm/rpc @zeitgeistpm/sdk @zeitgeistpm/utility @zeitgeistpm/web3.storage

ln -is /Users/luke/code/github/zeitgeistpm/sdk-next/packages/augment-api @zeitgeistpm/augment-api
ln -is /Users/luke/code/github/zeitgeistpm/sdk-next/packages/indexer @zeitgeistpm/indexer
ln -is /Users/luke/code/github/zeitgeistpm/sdk-next/packages/rpc @zeitgeistpm/rpc
ln -is /Users/luke/code/github/zeitgeistpm/sdk-next/packages/sdk @zeitgeistpm/sdk
ln -is /Users/luke/code/github/zeitgeistpm/sdk-next/packages/utility @zeitgeistpm/utility
ln -is /Users/luke/code/github/zeitgeistpm/sdk-next/packages/web3.storage @zeitgeistpm/web3.storage

cd ../
```
# disable running `yarn` since want to use sdk-next that was copied across
```
printf '\e[?2004l' && nvm use 19.7.0 && npm i -g yarn && corepack enable && corepack prepare yarn@stable --activate && yarn set version 3.4.1 && yarn && npm install -g nodemon && npm install -g concurrently && yarn add node-gyp && yarn add fs && . ./scripts/env_dev.sh && yarn run setup && cd server && (yarn run build-server && yarn run run-server)
```


```
find /Users/luke/code/github/ltfschoen/flappytips2/node_modules/@zeitgeistpm/sdk/dist/ -type f -name '*.js' -print0 | xargs -0 rename 's/.js$/.cjs/'

cd /Users/luke/code/github/ltfschoen/flappytips2/node_modules/@zeitgeistpm/sdk/dist/

find . -type f -name "*.cjs" -print0 | xargs -0 sed -i '' -e "s/.js\'/.cjs\'/g"

cd ~/code/github/ltfschoen/flappytips2


@zeitgeistpm/sdk/package.json, rename .js to .mjs

  "module": "./dist/index.mjs",
  ...
  "main": "./dist/index.mjs",
  ...
      "default": "./dist/index.mjs"
      ...


add to @zeitgeistpm/sdk/package.json

"type": "module"

replace decimal.cjs with decimal.js (since we have dependency `"decimal.js": "^10.4.3",` in package.json and shouldn't have been replaced 

nvm use 19.7.0 && npm i -g yarn && npm install -g nodemon concurrently && . ./scripts/env_dev.sh && DEBUG=* yarn run dev


still get error
```
[0] /Users/luke/code/github/ltfschoen/flappytips2/node_modules/@zeitgeistpm/sdk/dist/model/swaps/pool.cjs:9
[0] var lodashEs = require('lodash-es');
[0]                ^
[0] 
[0] Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/luke/code/github/ltfschoen/flappytips2/node_modules/lodash-es/lodash.js from /Users/luke/code/github/ltfschoen/flappytips2/node_modules/@zeitgeistpm/sdk/dist/model/swaps/pool.cjs not supported.
```
```

@ltfschoen
Copy link
Owner Author

ltfschoen commented Mar 27, 2023

will have to build basic client/server from scratch again that works then migrate

@ltfschoen
Copy link
Owner Author

would likely need to convert to ESM like i did here maciejhirsz/kobold@83229b3

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.

None yet

1 participant