1 | # `@shopify/async`
|
2 |
|
3 | [![Build Status](https://travis-ci.org/Shopify/quilt.svg?branch=master)](https://travis-ci.org/Shopify/quilt)
|
4 | [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE.md) [![npm version](https://badge.fury.io/js/%40shopify%2Fasync.svg)](https://badge.fury.io/js/%40shopify%2Fasync.svg)
|
5 |
|
6 | Primitives for loading parts of an application asynchronously.
|
7 |
|
8 | ## Installation
|
9 |
|
10 | ```bash
|
11 | $ yarn add @shopify/async
|
12 | ```
|
13 |
|
14 | ## Usage
|
15 |
|
16 | This package provides a wrapper for asynchronous import statements that allows for synchronous resolution and cacheing. This wrapper can be created using the `createResolver` function. The resulting `Resolver` object exposes a number of methods for interacting with the loaded module.
|
17 |
|
18 | ```ts
|
19 | const resolver = createResolver({
|
20 | load: () => import('./expensive'),
|
21 | });
|
22 |
|
23 | // Access the resolved module, if available. If an `id` option is provided
|
24 | // to `createResolver`, the resolver will attempt to synchronously
|
25 | // resolve the module based on the environment and the passed identifier.
|
26 | resolver.resolved;
|
27 |
|
28 | // If you provide an `id` option to `createResolver`, it will be
|
29 | // reflected here
|
30 | resolver.id;
|
31 |
|
32 | // Force the module to resolve. Returns a promise for the resolved value.
|
33 | resolver.resolve();
|
34 | ```
|
35 |
|
36 | This package also contains a few types that are useful for creating async components:
|
37 |
|
38 | - `Import` represents a value that could be default or non-default export
|
39 | - `DeferTiming` is an enum of defer timing values; has values for component `Mount`, browser `Idle`, or element `InViewport`
|
40 |
|
41 | As well as the following types related to `window.requestIdleCallback`:
|
42 |
|
43 | - `RequestIdleCallbackHandle`
|
44 | - `RequestIdleCallbackOptions`
|
45 | - `RequestIdleCallbackDeadline`
|
46 | - `RequestIdleCallback`
|
47 | - `WindowWithRequestIdleCallback`
|
48 |
|
49 | Finally, this package includes a plugin for Babel that allows the module IDs that are asynchronously imported to be exposed to the application.
|
50 |
|
51 | ### Babel
|
52 |
|
53 | The Babel plugin is exported from the `@shopify/async/babel` entrypoint. This plugin will look for any functions imported from the specified packages. It will then iterate over each reference to that function and, if the reference is a call expression where the first argument is an object, and that object has a `load` function, will mark it for processing. The adjustment is simple: it simply adds an `id` method that returns a Webpack-specific identifier based on the first dynamic import in the `load` method, allowing the function to use this identifier to mark the module as used.
|
54 |
|
55 | ```js
|
56 | import {createAsyncComponent} from 'my-package';
|
57 |
|
58 | createAsyncComponent({
|
59 | load: () => import('../SomeComponent'),
|
60 | });
|
61 |
|
62 | // Becomes:
|
63 |
|
64 | createAsyncComponent({
|
65 | load: () => import('../SomeComponent'),
|
66 | id: () => require.resolveWeak('../SomeComponent'),
|
67 | });
|
68 | ```
|
69 |
|
70 | #### Options
|
71 |
|
72 | ##### `packages`
|
73 |
|
74 | `packages` should be an object where the keys are the names of packages, and the values are an array of functions that should be processed. This option defaults to an object containing a few Shopify-specific async packages (`createAsyncComponent` and `createAsyncContext` from `@shopify/react-async`, and `createAsyncQueryComponent` from `@shopify/react-graphql`).
|
75 |
|
76 | ```js
|
77 | // babel.config.js
|
78 | {
|
79 | plugins: [
|
80 | ['@shopify/async/babel', {
|
81 | packages: {
|
82 | 'my-package': ['createAsyncComponent'],
|
83 | },
|
84 | }],
|
85 | ],
|
86 | }
|
87 | ```
|
88 |
|
89 | ##### `webpack`
|
90 |
|
91 | If you are using your components in a non-Webpack environment (for example, in Jest), you can pass the `webpack: false` option to this plugin. This disables the Webpack-specific `require.resolveWeak` addition, and instead uses `require.resolve`. The resulting `id` can then be used to synchronously require the module in Node.js.
|
92 |
|
93 | ### Webpack
|
94 |
|
95 | In order to make use of the `id` property added by the Babel plugin, you will need to create a manifest of assets keyed by this ID. An example of doing so can be found in [sewing kit’s `webpac-asset-metadata-plugin` package](https://github.com/Shopify/sewing-kit/tree/master/packages/webpack-asset-metadata-plugin).
|