プロジェクトの構造、ライフサイクル、バンドル

ミニアプリ プロジェクトの構造

先ほどのマークアップ言語、スタイル設定言語、コンポーネントと同様、ミニアプリのプロジェクト構造でも、ファイル拡張子やデフォルト名などの詳細は異なります。ただし、全体的な考え方は、すべてのスーパー アプリ プロバイダで共通です。プロジェクト構造は常に以下で構成されます。

  • mini アプリを初期化するルートファイル app.js
  • ウェブアプリ マニフェストおおむね対応する構成ファイル app.json
  • 共有のデフォルト スタイルを含む、省略可能な共通のスタイルシート ファイル app.css
  • ビルド情報を含む project.config.json ファイル。

すべてのページは、pages フォルダ内の個々のサブフォルダに保存されます。各ページ サブフォルダには、CSS ファイル、JS ファイル、HTML ファイル、オプションの構成 JSON ファイルが含まれます。すべてのファイルには、ファイル拡張子を除く、格納されているフォルダと同じ名前を付ける必要があります。このように、ミニアプリに必要なのは app.json ファイル内のディレクトリ(マニフェストに似たファイル)へのポインタのみで、すべてのサブリソースを動的に検索できます。ウェブ デベロッパーの観点から見ると、ミニアプリはマルチページ アプリです。

├── app.js               # Initialization logic
├── app.json             # Common configuration
├── app.css              # Common style sheet
├── project.config.json  # Project configuration
└── pages                # List of pages
   ├── index               # Home page
   │   ├── index.css         # Page style sheet
   │   ├── index.js          # Page logic
   │   ├── index.json        # Page configuration
   │   └── index.html        # Page markup
   └── other               # Other page
       ├── other.css         # Page style sheet
       ├── other.js          # Page logic
       ├── other.json        # Page configuration
       └── other.html        # Page markup

ミニアプリのライフサイクル

グローバルに定義された App() メソッドを呼び出して、ミニアプリをスーパーアプリに登録する必要があります。に概説したプロジェクト構造を参照すると、これは app.js で行われます。ミニアプリのライフサイクルは、基本的に 4 つのイベント(launchshowhideerror)で構成されます。これらのイベントのハンドラは、構成オブジェクトの形式で App() メソッドに渡すことができます。構成オブジェクトには、すべてのページでグローバルに利用できるデータを保持する globalData プロパティを含めることもできます。

/* app.js */
App({
  onLaunch(options) {
    // Do something when the app is launched initially.
  },
  onShow(options) {
    // Do something when the app is shown.
  },
  onHide() {
    // Do something when the app is hidden.
  },
  onError(msg) {
    console.log(msg);
  },
  globalData: "I am global data",
});

通常、個々の詳細は異なりますが、コンセプトは WeChatAlipayBaiduByteDanceQuick App と同じです。

ページのライフサイクル

アプリのライフサイクルと同様に、ページのライフサイクルにも、デベロッパーがリッスンして対応できるライフサイクル イベントがあります。該当するコアイベントは、loadshowreadyhideunload です。プラットフォームによっては、pulldownrefresh などの追加イベントも用意されています。イベント ハンドラの設定は、各ページで定義されている Page() メソッドで行われます。以前のプロジェクト構造の index ページまたは other ページの場合、それぞれ index.js または other.js で行われます。

/* index.js */
Page({
  data: {
    text: "This is page data.",
  },
  onLoad: function (options) {
    // Do something when the page is initially loaded.
  },
  onShow: function () {
    // Do something when the page is shown.
  },
  onReady: function () {
    // Do something when the page is ready.
  },
  onHide: function () {
    // Do something when the page is hidden.
  },
  onUnload: function () {
    // Do something when the page is closed.
  },
  onPullDownRefresh: function () {
    // Do something when the user pulls down to refresh.
  },
  onReachBottom: function () {
    // Do something when the user scrolls to the bottom.
  },
  onShareAppMessage: function () {
    // Do something when the user shares the page.
  },
  onPageScroll: function () {
    // Do something when the user scrolls the page.
  },
  onResize: function () {
    // Do something when the user resizes the page.
  },
  onTabItemTap(item) {
    // Do something when the user taps the page's tab.
  },
  customData: {
    foo: "bar",
  },
});

ビルドプロセス

デベロッパーがミニアプリのビルドプロセスを抽象化する。内部では、トランスパイルと圧縮用の Babel コンパイラや postcss CSS トランスフォーマーなどの業界ツールを使用しています。ビルド エクスペリエンスは、Next.jscreate-react-app に匹敵します。デベロッパーがビルド パラメータに明示的にアクセスしないことを明示的に選択した場合です。最終的に処理されたファイルは署名、暗号化、1 つまたは複数の(サブ)パッケージにパッケージ化され、スーパーアプリ プロバイダのサーバーにアップロードされます。サブパッケージは遅延読み込みを目的としているため、ミニアプリを一度にすべてダウンロードする必要はありません。パッケージングの詳細は非公開であり、文書化されていませんが、WeChat の wxapkg 形式などの一部のパッケージ形式はリバース エンジニアリングされています。

謝辞

この記事は、Joe MedleyKayce BasquesMilica MihajlijaAlan Kent、Keith Gu によってレビューされました。