Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
CJSとESMとnpmパッケージ / CommonJS and ES Modules and ...
Search
shimataro
June 29, 2018
Technology
870
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
CJSとESMとnpmパッケージ / CommonJS and ES Modules and npm package
Node学園 31時限目の発表資料です
https://nodejs.connpass.com/event/90936/
shimataro
June 29, 2018
Transcript
29 slides
CJSとESMとNPMパッケージ CJSとESMとNPMパッケージ (2018/06/29) (2018/06/29) 小田島 太郎 / @shimataro NODE学園 31時限目
NODE学園 31時限目
自己紹介 自己紹介 ウェブリオ株式会社所属(京都) サーバサイドエンジニア 趣味は手品 小田島 太郎 shimataro@GitHub odashima.taro@Facebook shimataro999@Twitter
この発表について この発表について 対象: 汎用 npmパッケージを開発している人 技術レベル: 中級 npmパッケージやBabelの基礎知識を前提 技術的に濃い話ではありません ↓スライドはこちら↓
https://speakerdeck.com/shimataro https://shimataro.github.io/slides/
目次 目次 背景 今回のゴール CJSとESM パッケージの構成 コード生成 まとめ
始める前に 始める前に
7/5 関西NODE学園 2時限目 7/5 関西NODE学園 2時限目
それでは始めます それでは始めます
背景 背景
背景 背景 JavaScript(ECMAScript)のモジュール require() / module.exports (CommonJS; CJS) import /
export (ES Modules; ESM) ネイティブESMはあまり使われていない (BabelでCJSに変 換するのが一般的) でもきっとそのうち普及する
今回のゴール 今回のゴール
今回のゴール 今回のゴール CJS / ESM / Babelの どれからでも使える パッケージモジ ュールを作りたい
↓ // パッケージ "foo" export default foo; export {bar}; // CJS const {default: foo, bar} = require("foo"); // ESM / Babel import foo, {bar} from "foo";
今回のゴール 今回のゴール パッケージの条件 ソースコードは単一 ビルド時に複数ファイルの生成はOK default export / named exports
両方対応 ソースコード内に変なハックは入れない おまじない NG 古い文法 NG 作る側にも使う側にも、極力負担をかけない
CJSとESM CJSとESM
CJSとESM CJSとESM 拡張子 .mjs はESM それ以外の拡張子はCJS export default foo; export
{bar}; module.exports = foo; // または exports.default = foo; exports.bar = bar;
CJSとESM CJSとESM 相互運用について Native ES Modules - something almost, but
not quite entirely unlike CommonJS by Gil Tayor
CJSとESM CJSとESM 1つのファイルで全部対応するのはややこしい .mjs と .js を両方用意する方が確実 最初にESMで書いて、CJS形式にも変換 Babelの出番
パッケージの構成 パッケージの構成
パッケージの構成 パッケージの構成 package.json 内の main を 拡張子なし で指定 2種類のindexを用意 ./index.js
(CJS/Babel用) ./index.mjs (ESM用) http://yosuke- furukawa.hatenablog.com/entry/2016/05/10/111102 { ... "main": "./index", // ".js" はつけない ... }
コード生成 コード生成
コード生成 コード生成 概要 index.js はこうする index.mjs はこのままでOK exports.default = foo;
exports.bar = bar; export default foo; export {bar};
コード生成 - CJS コード生成 - CJS index.js は普通にBabelで変換すればOK ↓ export
default foo; export {bar}; exports.default = foo; exports.bar = bar;
コード生成 - ESM コード生成 - ESM index.mjs は何もしなくてOK? Nodeがサポートしている文法だけを使うならOK デコレータとかstatic
propertiesとか使えない 汎用 パッケージなら、ある程度古いバージョンもサポー トしたい せめてLTSくらいは… そのために 古い文法 を使うのは嫌だ というわけで、 やっぱりBabelは使いたい
コード生成 - ESM コード生成 - ESM Babel使用時の注意 その1 babel-preset-env のデフォルトはCJS形式
import / export構文を変換しない場合はこうする // .babelrc { "presets": [ ["env", {"modules": false}] ] }
コード生成 - ESM コード生成 - ESM Babel使用時の注意 その2 拡張子が自動的に .js
になる ESMでは .mjs じゃないとダメ Babel7に --keep-file-extension が登場 拡張子を変更しないオプション 変換元の拡張子も .mjs にする必要あり まだbeta段階
コード生成(ESM) コード生成(ESM) 選択肢は2つ Babel単体で解決 Babel7 & --keep-file-extension 他のツールを導入 変換後に拡張子をリネーム 今回は後者(gulp)を採用
Babel6(安定版)でも使える 元の拡張子に制限なし
実際に作ってみた 実際に作ってみた
実際に作ってみた 実際に作ってみた ESM(.mjs) / Babel CJS https://github.com/shimataro/hell-word npm install @shimataro/hell-word
import foo, {bar} from "@shimataro/hell-word"; foo(); // "hell, word" bar(); // "地獄の言葉" const {default: foo, bar} = require("@shimataro/hell-word"); foo(); // "hell, word" bar(); // "地獄の言葉"
まとめ まとめ 単一ソースからCJS/ESM/Babel対応のパッケージを作 る方法 まずESMの export 構文で作る CJS用は exports.default に変換→
.js ESM用はそのまま→ .mjs package.json の "main" に拡張子をつけない サンプル作ってみたからよかったら参考にしてね 関西Node学園もよろしく! 手品に興味があったら声をかけてね!
ありがとうございました ありがとうございました
More Decks by shimataro
See All by shimataro
作って理解するPromise / Let's try implementing Promise
shimataro
0
75
Single Executable Applicationsについて / About Single Executable Applications
shimataro
0
2.1k
パッケージ開発者の苦悩 -JavaScriptランタイム群雄割拠- / distress of package developer
shimataro
0
940
An introduction to Node.js
shimataro
0
340
KFDのススメ / About KFD
shimataro
3
890
Node.js v12のES Modules / ES Modules on NodeJS v12
shimataro
1
1.2k
おまいらちゃんとリソース解放してますか / Remember to close resources!
shimataro
2
1.3k
BigInt あれこれ / overview about BigInt
shimataro
0
1k
dynamic import あれこれ / dynamic import - overview and pitfalls
shimataro
1
910
Other Decks in Technology
See All in Technology
社内アプリ基盤「Quartz」による、 非エンジニアとAIに本番デプロイを 任せても壊れないプラットフォームの設計
staka121
PRO
1
260
AI-DLCでソフトウェア開発の速さは 上がったのか?
rfdnxbro
0
190
プラガブルで宣言的なソフトウェアファクトリーを支えるTypeScript
timakin
5
2k
Is the Website Dying?
miriamschwab
0
260
Amazon Bedrock AgentCore の基礎とセキュリティ・ガバナンス
ren8k
0
290
AIが学べる子供の夏休みの自由研究教材を作ったが空振りだった話
yoshimura_datam
0
120
フロントエンドのテストカバレッジは 100%強制がいい
bengo4com
7
3.9k
20261119_AgentCore
takuyay0ne
2
340
Fukurou Labo_Engineer_Deck
fukuroulabo
0
120
リリースしたで終わらない。 観測できるフロントエンド開発
yamakenji24
4
1.7k
AIフレンドリーなGenerative UIをJSXで
uhyo
2
900
Amazon EFS のマルウェアスキャンをClamAV で自前実装したら苦労した話
mtmtfactory01
0
130
Featured
See All Featured
Leo the Paperboy
mayatellez
11
2.5k
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
350
Odyssey Design
rkendrick25
PRO
3
980
Speed Design
sergeychernyshev
33
2.3k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
560
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
370
Test your architecture with Archunit
thirion
2
2.5k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.7k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
470
Into the Great Unknown - MozCon
thekraken
41
2.8k
Ruling the World: When Life Gets Gamed
codingconduct
0
450