Skip to content

Commit 1069b15

Browse files
authored
docs: update snowpack-plugin.rst on dynamic imports (#1026)
1 parent d7961a0 commit 1069b15

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

docs/ref/snowpack-plugin.rst

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ API Reference - Snowpack Plugin (@lingui/snowpack-plugin)
55
It's a good practice to use compiled message catalogs during development. However,
66
running :cli:`compile` everytime messages are changed soon becomes tedious.
77

8-
``@lingui/snowpack-plugin`` is a snowpack loader, which compiles messages on the fly:
8+
``@lingui/snowpack-plugin`` is a Snowpack plugin, which compiles messages on the fly:
99

1010
Installation
1111
============
@@ -32,15 +32,30 @@ Simply add ``@lingui/snowpack-plugin`` inside your ``snowpack.config.js``:
3232
],
3333
}
3434
35-
Then in your code all you need is to use dynamic() import. Extension is mandatory. In case of using po format, use ``.po``.
35+
Then in your code all you need is to use `dynamic imports <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#dynamic_imports>`_ to load only necessary catalog. Extension is mandatory. In case of using po format, use ``.po``.
3636

37-
.. code-block:: jsx
37+
.. code-block:: ts
3838
3939
export async function dynamicActivate(locale: string) {
40-
const { messages } = await import(`./locales/${locale}/messages.po`)
41-
i18n.load(locale, messages)
40+
let catalog: {messages: Messages}
41+
42+
switch (locale) {
43+
case 'cs':
44+
catalog = await import('./locales/cs/messages.po')
45+
break
46+
case 'en':
47+
default:
48+
catalog = await import('./locales/en/messages.po')
49+
break;
50+
}
51+
52+
i18n.load(locale, catalog.messages)
4253
i18n.activate(locale)
4354
}
4455
56+
.. note::
57+
Comparing to `Webpack instructions for dynamic loading <./loader.html>`_, code snippet above doesn't rely on variable ``locale`` to do the actual import. Instead, we manually check every possible value using ``switch/case`` and import final catalog by exact path. This is default behavior (or restriction?) of `esbuild <https://esbuild.github.io>`_ - *extremely fast JavaScript bundler* used by Snowpack under the hood. There is `an issue regarding this feature <https://github.com/evanw/esbuild/issues/700>`_
58+
Similar restrictions apply to Babel macros or other non-standard features - they won't work with ``esbuild``
59+
4560
See the `guide about dynamic loading catalogs <../guides/dynamic-loading-catalogs.html>`_
4661
for more info.

0 commit comments

Comments
 (0)