9.0.0-beta.67 • Published 1 day ago

@fluentui/babel-preset-global-context v9.0.0-beta.67

Weekly downloads
-
License
MIT
Repository
github
Last release
1 day ago

@fluentui/babel-preset-global-context

Babel Preset Global Context for Fluent UI React

Babel preset that transforms createContext calls to use global context shims

Install

yarn add --dev @fluentui/babel-preset-global-context
# or
npm install --dev @fluentui/babel-preset-global-context

When to use it?

This preset is mainly inteded for application developers to target specific libraries that use React context and suffer from the context duplication problem. React contexts are not real singletons and are scoped by module level. This can cause problems when the context is duplicated in node_modules.

This is a known issue that is by design according to facebook/react#13346 that is illustrated in the below example.

// my-context-package
import * as React from 'react';

export const MyContext = React.createContext({ foo: 'foo' });

// provider.ts

// node_modules/my-context-package/lib/index.js
import { MyContext } from 'my-context-package';

function Provider({ children }) {
  const ctx = { foo: 'bar' };

  // 1. Instantiate context with value foo='bar'
  return <MyContext.Provider value={ctx}>{children}</MyContext.Provider>;
}

// consumer.ts

// node_modules/nested-dep/node_modules/my-context-package/lib/index.js
import * as React from 'react';
import { MyContext } from 'my-context-package';

function Consumer() {
  // 2. Consume the 'same' context from different module
  const ctx = React.useContext(MyContext);

  // 3. ⚠️ Actual value would be the default 'foo'
  return <div>{ctx.foo}</div>;
}

⚠️ The recommended solution to the above problem is to make sure that the affected dependency only exists once in node_modules. You can do this by upgrading your dependencies or using resolutions.

This library should only be used as a workaround in the cases where it might not be feasible to deduplicate node_modules.

Usage

Install the shims for createContext that will this Babel preset will use to replace original React.createContext calls.

yarn add @fluentui/global-context
# or
npm install @fluentui/global-context

We recommend using this preset with Webpack and babel-loader while scoping the transforms to the affected packages.

// weboack.config.js
module.exports = {
  module: {
    rules: [
      {
        test: /\.(ts|js|tsx|jsx)$/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: [['@fluentui/babel-preset-global-context']],
          },
        },
        // Targets all @fluentui scoped packages and replaces `createContext` calls with global context
        // Can be setup for other packages
        // /node_modules\/(<packageName>|<pacakgeName>)\/*/
        include: [/node_modules\/@fluentui\/*/],
      },
      {
        test: /\.(ts|tsx)$/i,
        use: ['ts-loader'],
        exclude: ['/node_modules/'],
      },
    ],
  },
};
9.0.0-beta.67

1 day ago

9.0.0-beta.66

15 days ago

9.0.0-beta.65

28 days ago

9.0.0-beta.64

2 months ago

9.0.0-beta.63

2 months ago

9.0.0-beta.62

2 months ago

9.0.0-beta.61

2 months ago

9.0.0-beta.60

3 months ago

9.0.0-beta.59

3 months ago

9.0.0-beta.58

4 months ago

9.0.0-beta.57

4 months ago

9.0.0-beta.56

4 months ago

9.0.0-beta.55

4 months ago

9.0.0-beta.54

4 months ago

9.0.0-beta.53

4 months ago

9.0.0-beta.52

5 months ago

9.0.0-beta.51

5 months ago

9.0.0-beta.50

5 months ago

9.0.0-beta.39

9 months ago

9.0.0-beta.37

9 months ago

9.0.0-beta.38

9 months ago

9.0.0-beta.35

9 months ago

9.0.0-beta.36

9 months ago

9.0.0-beta.33

11 months ago

9.0.0-beta.34

10 months ago

9.0.0-beta.48

7 months ago

9.0.0-beta.49

6 months ago

9.0.0-beta.46

7 months ago

9.0.0-beta.47

7 months ago

9.0.0-beta.44

8 months ago

9.0.0-beta.45

8 months ago

9.0.0-beta.43

8 months ago

9.0.0-beta.40

9 months ago

9.0.0-beta.41

9 months ago

9.0.0-beta.32

11 months ago

9.0.0-beta.31

11 months ago

9.0.0-beta.30

11 months ago

9.0.0-beta.19

1 year ago

9.0.0-beta.18

1 year ago

9.0.0-beta.28

12 months ago

9.0.0-beta.29

12 months ago

9.0.0-beta.26

1 year ago

9.0.0-beta.27

1 year ago

9.0.0-beta.24

1 year ago

9.0.0-beta.25

1 year ago

9.0.0-beta.22

1 year ago

9.0.0-beta.23

1 year ago

9.0.0-beta.20

1 year ago

9.0.0-beta.21

1 year ago

9.0.0-beta.17

1 year ago

9.0.0-beta.16

1 year ago

9.0.0-beta.15

1 year ago

9.0.0-beta.13

1 year ago

9.0.0-beta.14

1 year ago

9.0.0-beta.9

2 years ago

9.0.0-beta.8

2 years ago

9.0.0-beta.7

2 years ago

9.0.0-beta.6

2 years ago

9.0.0-beta.11

1 year ago

9.0.0-beta.12

1 year ago

9.0.0-beta.10

1 year ago

9.0.0-beta.5

2 years ago

9.0.0-beta.4

2 years ago

9.0.0-beta.3

2 years ago

9.0.0-beta.2

2 years ago

9.0.0-beta.1

2 years ago