Skip to content

Conversation

@Yang-33
Copy link
Owner

@Yang-33 Yang-33 commented Mar 20, 2024

https://efcl.info/2023/01/18/eslint-cjs-to-esm/

https://gist.github.com/azu/f383ba74c80d17806badd49745ce2129

やったこと

  • 記事のとおりやる
  • import exportで .js をつける(auto fixでできない部分)
  • require("./package.json") 、__dirname, __filename を置き換える(☆)
  • tsconfig.jsonでいろいろきびしすぎたのでコメントアウト
  • dual packageに対応した(?)package.jsonのパスがめちゃくちゃなので直した(libにoutputされてしまっている)

現状

compileが通らないが、テストは通るようになったっぽい

☆の部分で、requireをしている部分がだめ。CJSでは import.meta.urlが使えない

lib/channel-access-token/tests/api/ChannelAccessTokenClientTest.spec.ts:17:44 - error TS1343: The 'import.meta' meta-property is only allowed when the '--module' option is 'es2020', 'es2022', 'esnext', 'system', 'node16', or 'nodenext'.

17 const requireModule = module.createRequire(import.meta.url);
                                              ~~~~~~~~~~~

lib/http-axios.ts:11:44 - error TS1343: The 'import.meta' meta-property is only allowed when the '--module' option is 'es2020', 'es2022', 'esnext', 'system', 'node16', or 'nodenext'.

11 const requireModule = module.createRequire(import.meta.url);
                                              ~~~~~~~~~~~

formatが動かなくなった

めちゃくちゃになった

dual packageどうすんの

やり方がありすぎる一方でどれも一筋縄ではいかない

  • 今の方法 + esbuild挟む。
  • honoのまねをする。 https://github.com/honojs/hono/tree/main (でもesbuildはさむの避けられなさそう)
  • discord.jsのまねをする。
  • そもそも、user agenetのためにpackage.jsonを読むのをやっているが、別にファイルに書き出せばimport/exportできるので、その方法にする(publish前の手間だけで済む)。テストで__dirname取ってるのをどうにかできるかは知らない。できはすると信じてる。

@Yang-33
Copy link
Owner Author

Yang-33 commented Mar 20, 2024

https://t28.dev/blog/tsup-document-and-code-reading/

Inject cjs and esm shims
cjs 環境でのみ使える __dirname
esm 環境でのみ使える import.meta.url
それぞれが異なる環境でも機能するためのコード (cjs_shims.js, esm_shims.js) が tsup によって挿入される(esbuild/index.ts#L220)。便利。

あーできそう じゃあtsupバージョンもやってみよう。

@Yang-33 Yang-33 changed the title a dual package (tsconfig2種+tsconfig-to-dual-package) Mar 20, 2024
This was referenced Mar 24, 2024
@Yang-33
Copy link
Owner Author

Yang-33 commented Mar 24, 2024

ESM化 + tsconfig.cjs.jsonではrootDirsからtestを外す + package.jsonはcjsの方のpackage.json(./dist/cjs/package.json?)にcommonjsと書く
のがいい?

@Yang-33
Copy link
Owner Author

Yang-33 commented Mar 30, 2024

そのように作り変えたらうまく行った

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