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

ESmodulesに移行 #7658

Closed
9 of 10 tasks
syuilo opened this issue Aug 19, 2021 · 52 comments · Fixed by #8358
Closed
9 of 10 tasks

ESmodulesに移行 #7658

syuilo opened this issue Aug 19, 2021 · 52 comments · Fixed by #8358
Assignees
Labels
🔥high priority 💚Refactor Rewriting code without changing behavior

Comments

@syuilo
Copy link
Member

syuilo commented Aug 19, 2021

Summary

node向けのライブラリもes modulesを使用するものが増えてきて、commonjsだと使えないため

やることリスト

  • ディレクトリインポートを直す
  • 拡張子省略しない
  • requireをimportにする
  • jsonをimportしている箇所をどうにかする
    • --experimental-json-modules
  • __dirnameを置き換える
  • デコレータがなんか動かないのを何とかする
  • use the node: protocol for imports
  • Add "type": "module" to your package.json
  • autobind-decorator の代替を考える
  • テストが動かないのをどうにかする

ref. https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

@syuilo syuilo added the 💚Refactor Rewriting code without changing behavior label Aug 19, 2021
@syuilo
Copy link
Member Author

syuilo commented Aug 19, 2021

ESMだとディレクトリインポートができないみたい

例えば

import boot from './boot';

import boot from './boot/index.js';

に書き換える必要がある

@syuilo
Copy link
Member Author

syuilo commented Aug 19, 2021

(そんなのTypeScriptコンパイラーがよしなにやってほしい)

@syuilo syuilo self-assigned this Aug 19, 2021
@syuilo
Copy link
Member Author

syuilo commented Aug 19, 2021

あー、あとそもそも拡張子省略できない

@syuilo
Copy link
Member Author

syuilo commented Aug 19, 2021

死ぬほど大変じゃん

@syuilo
Copy link
Member Author

syuilo commented Aug 19, 2021

とても大変ということが分かったので、ESMに移行するのは一旦諦め、CJSに対応している古いバージョンのライブラリを使い続けていくことにする

@rinsuki
Copy link
Contributor

rinsuki commented Aug 19, 2021

なんか単純置換でいけそうな気もする

@syuilo
Copy link
Member Author

syuilo commented Aug 19, 2021

ふーむ

@syuilo
Copy link
Member Author

syuilo commented Aug 19, 2021

あるインポートがローカルのファイルを指しているのかパッケージを指しているのか区別が難しい場合もあるかもしれないけど、ほとんどは置換でいけるか…?

@syuilo
Copy link
Member Author

syuilo commented Aug 19, 2021

多分ほとんどのローカルのインポートは./とかで始まってる気がするからいけるかも

@rinsuki
Copy link
Contributor

rinsuki commented Aug 19, 2021

ローカルのファイルは基本 @/ or ./ or ../ から始まるはず

@mei23
Copy link
Contributor

mei23 commented Aug 19, 2021

node向けのライブラリもes modulesを使用するものが増えてきて、commonjsだと使えないため

例えばどれだわ?

@syuilo
Copy link
Member Author

syuilo commented Aug 19, 2021

pureimageとかis-rootとか

@syuilo
Copy link
Member Author

syuilo commented Aug 19, 2021

jsonをrequireしてる箇所ってどう置き換えれば良いんだろ

https://stackoverflow.com/questions/34944099/how-to-import-a-json-file-in-ecmascript-6

によると将来的には出来るようになるらしいけど

@syuilo
Copy link
Member Author

syuilo commented Aug 19, 2021

import * as foo from './foo.json';って書けばTypeScriptがよしなにやってくれるっぽい

@syuilo
Copy link
Member Author

syuilo commented Aug 19, 2021

いや嘘
やってくれなかった

@syuilo
Copy link
Member Author

syuilo commented Aug 19, 2021

現時点での移行は難しいかも

@mei23
Copy link
Contributor

mei23 commented Aug 19, 2021

あんまりCommonJSで動かないモジュールって記憶になくて

pureimageは 0.3.3 >= は本当にdistフォルダがないから単に壊れてるだけかも
Error: Cannot find module '.../node_modules/pureimage/dist/pureimage-umd.cjs'. Please verify that the package.json has a valid "main" entry

is-rootはESM要求してるけど、そもそもrootチェックっていらないのではと。

@rinsuki
Copy link
Contributor

rinsuki commented Aug 19, 2021

というか is-root 中身は process.getuid() === 0 だしわざわざパッケージ使わなくてもよさそう

@syuilo
Copy link
Member Author

syuilo commented Aug 19, 2021

is-rootたった3行しかなかった

@syuilo
Copy link
Member Author

syuilo commented Aug 19, 2021

--experimental-json-modules付ければいけた
https://nodejs.org/api/esm.html#esm_experimental_json_modules

@syuilo
Copy link
Member Author

syuilo commented Aug 19, 2021

__dirname は使えないっぽい

@syuilo
Copy link
Member Author

syuilo commented Aug 19, 2021

syuilo added a commit that referenced this issue Aug 19, 2021
将来ESMに移行しやすいように
Related: #7658

なんかmochaが起動しなくなってるけど理由不明
すぐ直したい
@syuilo
Copy link
Member Author

syuilo commented Aug 19, 2021

あー、拡張子を.jsにするとts-nodeで直接ts動かす時にエラーになるな

@syuilo
Copy link
Member Author

syuilo commented Aug 19, 2021

👀
TypeStrong/ts-node#783

@syuilo
Copy link
Member Author

syuilo commented Aug 19, 2021

うーんヨクワカラン

@marihachi
Copy link
Contributor

marihachi commented Aug 20, 2021

これ止めといたほうが良い気がする
よっぽど依存モジュールが少なくないと厳しそう

@syuilo
Copy link
Member Author

syuilo commented Feb 25, 2022

npmでインストールしたライブラリがCJSでもESMは使えるよね?そんなことない?

@syuilo
Copy link
Member Author

syuilo commented Feb 25, 2022

もし依存ライブラリもすべてESMでないといけないとしたらESM移行は不可能そう

@syuilo
Copy link
Member Author

syuilo commented Feb 25, 2022

npmでインストールしたライブラリがCJSでもESMは使えるよね?そんなことない?

ESMで出力したらTypeError: redisLock is not a functionが出るようになったため

@syuilo
Copy link
Member Author

syuilo commented Feb 25, 2022

npmでインストールしたライブラリがCJSでもESMは使えるよね?そんなことない?

ESMで出力したらTypeError: redisLock is not a functionが出るようになったため

import * as redisLock from 'redis-lock';import redisLock from 'redis-lock'; にしたら直った

@syuilo
Copy link
Member Author

syuilo commented Feb 25, 2022

C:\Users\ai\projects\misskey\packages\backend\node_modules\reflect-metadata\Reflect.js:553
                var decorated = decorator(target, propertyKey, descriptor);
                                ^

TypeError: decorator is not a function
    at DecorateProperty (C:\Users\ai\projects\misskey\packages\backend\node_modules\reflect-metadata\Reflect.js:553:33)
    at Reflect.decorate (C:\Users\ai\projects\misskey\packages\backend\node_modules\reflect-metadata\Reflect.js:123:24)
    at __decorate (file:///C:/Users/ai/projects/misskey/packages/backend/built/services/chart/core.js:8:92)
    at file:///C:/Users/ai/projects/misskey/packages/backend/built/services/chart/core.js:508:1
    at ModuleJob.run (node:internal/modules/esm/module_job:183:25)
    at async Loader.import (node:internal/modules/esm/loader:178:24)
    at async Object.loadESM (node:internal/process/esm_loader:68:5)
    at async handleMainPromise (node:internal/modules/run_main:63:12)

が出るわね

@syuilo
Copy link
Member Author

syuilo commented Feb 25, 2022

:tasukete:

@syuilo
Copy link
Member Author

syuilo commented Feb 25, 2022

npmでインストールしたライブラリがCJSでもESMは使えるよね?そんなことない?

ライブラリによっては型として正しいimport方法と実際に動くimport方法が異なる場合が発生するな

@syuilo
Copy link
Member Author

syuilo commented Feb 25, 2022

C:\Users\ai\projects\misskey\packages\backend\node_modules\reflect-metadata\Reflect.js:553
                var decorated = decorator(target, propertyKey, descriptor);
                                ^

TypeError: decorator is not a function
    at DecorateProperty (C:\Users\ai\projects\misskey\packages\backend\node_modules\reflect-metadata\Reflect.js:553:33)
    at Reflect.decorate (C:\Users\ai\projects\misskey\packages\backend\node_modules\reflect-metadata\Reflect.js:123:24)
    at __decorate (file:///C:/Users/ai/projects/misskey/packages/backend/built/services/chart/core.js:8:92)
    at file:///C:/Users/ai/projects/misskey/packages/backend/built/services/chart/core.js:508:1
    at ModuleJob.run (node:internal/modules/esm/module_job:183:25)
    at async Loader.import (node:internal/modules/esm/loader:178:24)
    at async Object.loadESM (node:internal/process/esm_loader:68:5)
    at async handleMainPromise (node:internal/modules/run_main:63:12)

が出るわね

autobind消したら出なくなった

@syuilo
Copy link
Member Author

syuilo commented Feb 25, 2022

起動しようとするとエラーは出なくなったけど起動しない

@syuilo
Copy link
Member Author

syuilo commented Feb 25, 2022

起動しようとするとエラーは出なくなったけど起動しない

直った

@syuilo
Copy link
Member Author

syuilo commented Feb 25, 2022

一応起動するとこまでは来た

@syuilo
Copy link
Member Author

syuilo commented Feb 25, 2022

autobind消したから、this周りの不具合が発生するはずなので何か対策考えないと

@syuilo
Copy link
Member Author

syuilo commented Feb 25, 2022

@syuilo
Copy link
Member Author

syuilo commented Feb 26, 2022

autobind消したから、this周りの不具合が発生するはずなので何か対策考えないと

おとなしくthis.foo = this.foo.bind(this)をコンストラクタに書くようにした

@syuilo syuilo unpinned this issue Feb 26, 2022
@syuilo syuilo pinned this issue Feb 26, 2022
@syuilo
Copy link
Member Author

syuilo commented Feb 26, 2022

テストが動かない問題を解決した人に¥3000進呈

@syuilo syuilo reopened this Feb 27, 2022
@Johann150
Copy link
Contributor

Johann150 commented Apr 30, 2022

A lot of calls to os.popup still use import(...).

@tamaina
Copy link
Member

tamaina commented May 17, 2022

A lot of calls to os.popup still use import(...).

Is this a standard feature of esmodules...? @Johann150

@tamaina tamaina closed this as completed May 17, 2022
@tamaina tamaina unpinned this issue May 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔥high priority 💚Refactor Rewriting code without changing behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants