Skip to content

Commit 96ca0bc

Browse files
authored
refactor: [REL-11292] separate imports to allow for better tree shaking (#432)
* fix: separate imports to allow for better tree shaking * chore: remove comment
1 parent 43a1198 commit 96ca0bc

File tree

10 files changed

+141
-9
lines changed

10 files changed

+141
-9
lines changed

packages/demo/src/AppWrapper.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ReactNode } from 'react';
22
import './App.css';
33
import { useLDClient } from 'launchdarkly-react-client-sdk';
4-
import type { ToolbarPosition } from '@launchdarkly/toolbar';
4+
import type { ToolbarPosition } from '@launchdarkly/toolbar/types';
55

66
interface AppWrapperProps {
77
children: ReactNode;

packages/demo/src/pages/DevServerMode.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useState } from 'react';
22
import { AppWrapper } from '../AppWrapper';
3-
import type { ToolbarPosition } from '@launchdarkly/toolbar';
4-
import { useLaunchDarklyToolbar } from '@launchdarkly/toolbar';
3+
import type { ToolbarPosition } from '@launchdarkly/toolbar/types';
4+
import { useLaunchDarklyToolbar } from '@launchdarkly/toolbar/react';
55
import { eventInterceptionPlugin } from '../plugins';
66
import { useLaunchDarklyProvider } from '../hooks/useLaunchDarklyProvider';
77
import { LoadingScreen } from '../components/LoadingScreen';

packages/demo/src/pages/SDKMode.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { useState } from 'react';
22
import { AppWrapper } from '../AppWrapper';
33
import { flagOverridePlugin, eventInterceptionPlugin } from '../plugins';
4-
import type { ToolbarPosition } from '@launchdarkly/toolbar';
5-
import { useLaunchDarklyToolbar } from '@launchdarkly/toolbar';
4+
import type { ToolbarPosition } from '@launchdarkly/toolbar/types';
5+
import { useLaunchDarklyToolbar } from '@launchdarkly/toolbar/react';
66
import { useLaunchDarklyProvider } from '../hooks/useLaunchDarklyProvider';
77
import { LoadingScreen } from '../components/LoadingScreen';
88
import { MockModeIndicator } from '../components/MockModeIndicator';

packages/demo/src/plugins.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EventInterceptionPlugin, FlagOverridePlugin } from '@launchdarkly/toolbar';
1+
import { EventInterceptionPlugin, FlagOverridePlugin } from '@launchdarkly/toolbar/plugins';
22

33
export const flagOverridePlugin = new FlagOverridePlugin({
44
storageNamespace: 'ld-demo-overrides',

packages/demo/vite.config.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import { dirname, resolve } from 'node:path';
77
const __dirname = dirname(fileURLToPath(import.meta.url));
88
const rootDir = resolve(__dirname, '..');
99
const toolbarSrc = resolve(rootDir, 'toolbar', 'src', 'index.ts');
10+
const toolbarPluginsSrc = resolve(rootDir, 'toolbar', 'src', 'plugins.ts');
11+
const toolbarReactSrc = resolve(rootDir, 'toolbar', 'src', 'react.ts');
12+
const toolbarTypesSrc = resolve(rootDir, 'toolbar', 'src', 'types-entry.ts');
1013

1114
// https://vitejs.dev/config/
1215
export default defineConfig(({ command }) => {
@@ -17,6 +20,20 @@ export default defineConfig(({ command }) => {
1720
resolve: {
1821
alias: isDev
1922
? [
23+
// More specific paths must come before the base path
24+
{
25+
find: '@launchdarkly/toolbar/plugins',
26+
replacement: toolbarPluginsSrc,
27+
},
28+
{
29+
find: '@launchdarkly/toolbar/react',
30+
replacement: toolbarReactSrc,
31+
},
32+
{
33+
find: '@launchdarkly/toolbar/types',
34+
replacement: toolbarTypesSrc,
35+
},
36+
// Base path last (catches @launchdarkly/toolbar without subpath)
2037
{
2138
find: '@launchdarkly/toolbar',
2239
replacement: toolbarSrc,
@@ -33,7 +50,12 @@ export default defineConfig(({ command }) => {
3350
: undefined,
3451
optimizeDeps: isDev
3552
? {
36-
exclude: ['@launchdarkly/toolbar'],
53+
exclude: [
54+
'@launchdarkly/toolbar',
55+
'@launchdarkly/toolbar/plugins',
56+
'@launchdarkly/toolbar/react',
57+
'@launchdarkly/toolbar/types',
58+
],
3759
}
3860
: undefined,
3961
};

packages/toolbar/package.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@
2828
"module": "./dist/js/index.js",
2929
"types": "./dist/index.d.ts",
3030
"typings": "./dist/index.d.ts",
31+
"typesVersions": {
32+
"*": {
33+
"plugins": [
34+
"./dist/plugins.d.ts"
35+
],
36+
"react": [
37+
"./dist/react.d.ts"
38+
],
39+
"types": [
40+
"./dist/types-entry.d.ts"
41+
]
42+
}
43+
},
3144
"sideEffects": false,
3245
"files": [
3346
"dist",
@@ -45,6 +58,36 @@
4558
"types": "./dist/index.d.ts",
4659
"default": "./dist/index.cjs"
4760
}
61+
},
62+
"./plugins": {
63+
"import": {
64+
"types": "./dist/plugins.d.ts",
65+
"default": "./dist/js/plugins.js"
66+
},
67+
"require": {
68+
"types": "./dist/plugins.d.ts",
69+
"default": "./dist/plugins.cjs"
70+
}
71+
},
72+
"./react": {
73+
"import": {
74+
"types": "./dist/react.d.ts",
75+
"default": "./dist/js/react.js"
76+
},
77+
"require": {
78+
"types": "./dist/react.d.ts",
79+
"default": "./dist/react.cjs"
80+
}
81+
},
82+
"./types": {
83+
"import": {
84+
"types": "./dist/types-entry.d.ts",
85+
"default": "./dist/js/types-entry.js"
86+
},
87+
"require": {
88+
"types": "./dist/types-entry.d.ts",
89+
"default": "./dist/types-entry.cjs"
90+
}
4891
}
4992
},
5093
"scripts": {

packages/toolbar/rslib.config.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ export default defineConfig({
99
source: {
1010
entry: {
1111
index: './src/index.ts',
12+
plugins: './src/plugins.ts',
13+
react: './src/react.ts',
14+
'types-entry': './src/types-entry.ts',
1215
},
1316
include: ['./src/**/*.ts', './src/**/*.tsx'],
1417
exclude: ['./src/**/*.test.*', './src/**/*.stories.*'],
@@ -27,7 +30,7 @@ export default defineConfig({
2730
js: 'js',
2831
},
2932
filename: {
30-
js: 'index.js',
33+
js: '[name].js',
3134
},
3235
},
3336
},
@@ -42,7 +45,7 @@ export default defineConfig({
4245
js: '.',
4346
},
4447
filename: {
45-
js: 'index.cjs',
48+
js: '[name].cjs',
4649
},
4750
},
4851
},

packages/toolbar/src/plugins.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Plugin exports for the LaunchDarkly Toolbar
3+
*
4+
* This entry point allows importing plugins separately from the main toolbar
5+
* for better bundle size optimization when using lazy loading.
6+
*
7+
* @example
8+
* ```ts
9+
* import { FlagOverridePlugin, EventInterceptionPlugin } from '@launchdarkly/toolbar/plugins';
10+
*
11+
* const flagOverridePlugin = new FlagOverridePlugin({
12+
* storageNamespace: 'my-app-overrides',
13+
* });
14+
*
15+
* const eventInterceptionPlugin = new EventInterceptionPlugin({
16+
* eventCapacity: 150,
17+
* });
18+
* ```
19+
*/
20+
export { FlagOverridePlugin, EventInterceptionPlugin } from './types/plugins';
21+
22+
export type {
23+
FlagOverridePluginConfig,
24+
EventInterceptionPluginConfig,
25+
IFlagOverridePlugin,
26+
IEventInterceptionPlugin,
27+
} from './types/plugins';

packages/toolbar/src/react.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* React-specific exports for the LaunchDarkly Toolbar
3+
*
4+
* This entry point provides React hooks and utilities for integrating
5+
* the toolbar with React applications.
6+
*
7+
* @example
8+
* ```tsx
9+
* import { useLaunchDarklyToolbar } from '@launchdarkly/toolbar/react';
10+
* import { FlagOverridePlugin } from '@launchdarkly/toolbar/plugins';
11+
*
12+
* // Create plugins outside the component (only once)
13+
* const flagOverridePlugin = new FlagOverridePlugin();
14+
*
15+
* function App() {
16+
* useLaunchDarklyToolbar({
17+
* flagOverridePlugin,
18+
* });
19+
*
20+
* return <YourApp />;
21+
* }
22+
* ```
23+
*/
24+
export { default as useLaunchDarklyToolbar } from './react/useLaunchDarklyToolbar';
25+
export { default as lazyLoadToolbar } from './react/lazyLoadToolbar';
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Type-only exports for the LaunchDarkly Toolbar
3+
*
4+
* This entry point provides TypeScript types without any runtime code,
5+
* useful for type-only imports in your application.
6+
*
7+
* @example
8+
* ```ts
9+
* import type { InitializationConfig, ToolbarConfig } from '@launchdarkly/toolbar/types';
10+
* ```
11+
*/
12+
export type * from './types';

0 commit comments

Comments
 (0)