Skip to content

Commit 1802f33

Browse files
huuyafwwwCopilotericfennis
authored
feat(lucide-react): add package exports for per-icon imports without changing how Node resolves the package (#4822)
* feat(lucide-react): add package exports for per-icon imports without changing how Node resolves the package Makes `lucide-react/icons/<name>` and `lucide-react/types` public so a single icon can be imported with a small shared declaration instead of the 2.2 MB `dist/lucide-react.d.ts`, and without loading the CJS barrel on the server. The root entry only lists `module` and `default`, so bundlers keep the ESM barrel and Node keeps resolving to `dist/cjs/lucide-react.js` as it does today through `main`. The map reverted in #2814 sent Node to the ESM barrel, which is what #2743 hit. `./dist/*` keeps existing deep imports working. * Modify getting started guide for icon imports Co-authored-by: Copilot Autofix powered by AI <[email protected]> * fix(lucide-react): add node, import and require conditions after module and keep the extension-specific and src subpaths exported Resolvers that are called with a custom condition list drop `module` and fell through to `default`, the CJS bundle; esbuild with `conditions: ['workerd', 'worker']` bundled 1.3 MB instead of the 58 KB tree-shaken output it gets on main. The root and `./icons` entries now list `node`, `import` and `require` after `module`, with `node` ahead of `import` so Node keeps resolving to `dist/cjs/lucide-react.js` for both `import` and `require`. `lucide-react/dynamic.mjs`, `lucide-react/dynamic.js` and `lucide-react/dynamicIconImports.mjs` are imported by consumers and threw ERR_PACKAGE_PATH_NOT_EXPORTED; they have explicit entries again. The docs API routes import `lucide-react/src/createLucideIcon` and `lucide-react/src/Icon`, which Nitro left as external imports without a `./src/*` entry, so the entry from the 0.470.0 map is back. * test(lucide-react): resolve the package entry points with Node itself in the React Router fixture The browser suite resolves lucide-react through Vite, so nothing checked the `exports` map the way a server runtime does. `tests/exports.node.test.ts` runs under `node --test` from the fixture's `check` script and asserts where `import.meta.resolve` and `require.resolve` land for the root, `./icons`, a single icon, the dynamic entries with and without an extension, the `dist/*` passthrough and `package.json`, that `./types` is declaration-only, and that the resolved modules load. Vitest is told to leave that file to Node. * docs(lucide-react): note that per-icon imports are ESM only `require()` of `lucide-react/icons/<name>` needs Node.js 20.19 or 22.12; 20.18 throws ERR_REQUIRE_ESM. --------- Co-authored-by: Copilot Autofix powered by AI <[email protected]> Co-authored-by: Eric Fennis <[email protected]>
1 parent 0523275 commit 1802f33

12 files changed

Lines changed: 164 additions & 6 deletions

File tree

‎docs/guide/react/getting-started.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ const App = () => {
5050
export default App;
5151
```
5252

53+
### Importing a single icon
54+
55+
You can also import one icon at a time. This skips the icon barrel, which matters when modules are resolved at runtime (server-side rendering, for example), and only loads a small type declaration instead of the full one.
56+
57+
```tsx
58+
import Camera from 'lucide-react/icons/camera';
59+
import type { LucideProps } from 'lucide-react/types';
60+
```
61+
62+
These per-icon entry points are ES modules only. `require()` of one works on Node.js 20.19 and 22.12 or newer; on older versions, import the icon from the package root instead.
63+
5364
## Props
5465

5566
To customize the appearance of an icon, you can use the following props:

‎integrations/lucide-react/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Every fixture runs three checks:
88
- TypeScript with `noEmit`;
99
- Vitest Browser Mode in Chromium.
1010

11+
The React Router fixture also resolves the package entry points with Node's own resolver (`node --test`), since the browser suite only sees them through Vite.
12+
1113
From the repository root, install Chromium once and run the complete suite:
1214

1315
```sh

‎integrations/lucide-react/react-router/app/IconShowcase.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Camera, Droplet, Edit2, House, LucideProvider, Pen } from 'lucide-react';
1+
import { Camera, Edit2, House, LucideProvider, Pen } from 'lucide-react';
2+
import Droplet from 'lucide-react/icons/droplet';
23
import { DynamicIcon } from 'lucide-react/dynamic';
34

45
export default function IconShowcase() {

‎integrations/lucide-react/react-router/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"build": "react-router build",
88
"typecheck": "react-router typegen && tsc --noEmit",
99
"test:browser": "vitest run",
10-
"check": "pnpm build && pnpm typecheck && pnpm test:browser"
10+
"test:node": "node --test tests/exports.node.test.ts",
11+
"check": "pnpm build && pnpm typecheck && pnpm test:node && pnpm test:browser"
1112
},
1213
"dependencies": {
1314
"@react-router/node": "8.3.1",
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import assert from 'node:assert/strict';
2+
import { existsSync } from 'node:fs';
3+
import { createRequire } from 'node:module';
4+
import { test } from 'node:test';
5+
import { fileURLToPath } from 'node:url';
6+
7+
// The browser suite resolves lucide-react through Vite. These checks use Node's own resolver,
8+
// which is what a server runtime sees, so the `exports` map is verified the way #2743 hit it.
9+
10+
const require = createRequire(import.meta.url);
11+
const packageRoot = fileURLToPath(new URL('./', import.meta.resolve('lucide-react/package.json')));
12+
13+
const inPackage = (file: string) => {
14+
assert.ok(file.startsWith(packageRoot), `${file} is outside ${packageRoot}`);
15+
assert.ok(existsSync(file), `${file} does not exist`);
16+
return file.slice(packageRoot.length);
17+
};
18+
19+
const resolveImport = (specifier: string) =>
20+
inPackage(fileURLToPath(import.meta.resolve(specifier)));
21+
const resolveRequire = (specifier: string) => inPackage(require.resolve(specifier));
22+
23+
test('the root entry resolves to the CommonJS bundle for both import and require', () => {
24+
assert.equal(resolveImport('lucide-react'), 'dist/cjs/lucide-react.js');
25+
assert.equal(resolveRequire('lucide-react'), 'dist/cjs/lucide-react.js');
26+
assert.equal(resolveImport('lucide-react/icons'), 'dist/cjs/lucide-react.js');
27+
assert.equal(resolveRequire('lucide-react/icons'), 'dist/cjs/lucide-react.js');
28+
});
29+
30+
test('a single icon resolves to its ESM module', () => {
31+
assert.equal(resolveImport('lucide-react/icons/camera'), 'dist/esm/icons/camera.mjs');
32+
assert.equal(resolveRequire('lucide-react/icons/camera'), 'dist/esm/icons/camera.mjs');
33+
});
34+
35+
test('the dynamic entries resolve with and without an extension', () => {
36+
assert.equal(resolveImport('lucide-react/dynamic'), 'dynamic.mjs');
37+
assert.equal(resolveRequire('lucide-react/dynamic'), 'dynamic.js');
38+
assert.equal(resolveImport('lucide-react/dynamic.mjs'), 'dynamic.mjs');
39+
assert.equal(resolveImport('lucide-react/dynamic.js'), 'dynamic.js');
40+
assert.equal(resolveImport('lucide-react/dynamicIconImports'), 'dynamicIconImports.mjs');
41+
assert.equal(resolveImport('lucide-react/dynamicIconImports.mjs'), 'dynamicIconImports.mjs');
42+
});
43+
44+
test('deep imports into dist and package.json keep resolving', () => {
45+
assert.equal(
46+
resolveImport('lucide-react/dist/esm/icons/camera.mjs'),
47+
'dist/esm/icons/camera.mjs',
48+
);
49+
assert.equal(resolveRequire('lucide-react/dist/cjs/lucide-react.js'), 'dist/cjs/lucide-react.js');
50+
assert.equal(resolveImport('lucide-react/package.json'), 'package.json');
51+
});
52+
53+
test('the types entry is declaration-only', () => {
54+
assert.throws(() => import.meta.resolve('lucide-react/types'), {
55+
code: 'ERR_PACKAGE_PATH_NOT_EXPORTED',
56+
});
57+
});
58+
59+
test('the resolved entries load and export icon components', async () => {
60+
const root = await import('lucide-react');
61+
const camera = await import('lucide-react/icons/camera');
62+
63+
assert.equal(root.Camera.displayName, 'Camera');
64+
assert.equal(camera.default.displayName, 'Camera');
65+
assert.equal(require('lucide-react').Camera.displayName, 'Camera');
66+
});

‎integrations/lucide-react/react-router/vitest.config.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import react from '@vitejs/plugin-react';
22
import { playwright } from '@vitest/browser-playwright';
3-
import { defineConfig } from 'vitest/config';
3+
import { configDefaults, defineConfig } from 'vitest/config';
44

55
export default defineConfig({
66
plugins: [react()],
77
test: {
8+
exclude: [...configDefaults.exclude, 'tests/*.node.test.ts'],
89
browser: {
910
enabled: true,
1011
headless: true,

‎integrations/lucide-react/vite/src/IconShowcase.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { Camera, Droplet, Edit2, House, LucideProvider, Pen } from 'lucide-react';
1+
import { Camera, House, LucideProvider, Pen } from 'lucide-react';
2+
import Droplet from 'lucide-react/icons/droplet';
3+
import Edit2 from 'lucide-react/dist/esm/icons/edit-2.mjs';
24
import { DynamicIcon } from 'lucide-react/dynamic';
35

46
export default function IconShowcase() {

‎integrations/lucide-react/vite/tests/icons.browser.spec.tsx‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
import { expect, test } from 'vitest';
1+
import Edit2 from 'lucide-react/dist/esm/icons/edit-2.mjs';
2+
import Droplet from 'lucide-react/icons/droplet';
3+
import type { LucideIcon } from 'lucide-react/types';
4+
import { expect, expectTypeOf, test } from 'vitest';
25
import { render } from 'vitest-browser-react';
36
import App from '../src/App';
47

58
test('renders Lucide public APIs in a real browser', async () => {
9+
expectTypeOf(Droplet).toEqualTypeOf<LucideIcon>();
10+
expectTypeOf(Edit2).toEqualTypeOf<LucideIcon>();
11+
612
const screen = await render(<App />);
713

814
const staticIcon = screen.getByTestId('static-icon');

‎packages/lucide-react/dynamic.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// eslint-disable
22
// This file is make sure that we can import from "lucide-react/dynamic" without having to specify the extension. This is a workaround for the fact that some bundlers (like Webpack) don't support importing from "lucide-react/dynamic.mjs" without specifying the extension.
3-
// Unfortunately, we can't use exports field in package.json because it breaks some runtime environments. See https://github.com/lucide-icons/lucide/issues/2743#issuecomment-2626784300
3+
// It is also the "default" target of the "./dynamic" entry in the package.json exports map, and it stays in place for resolvers that ignore "exports".
44
export * from './dynamic.mjs';

‎packages/lucide-react/package.json‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,59 @@
2626
"main": "dist/cjs/lucide-react.js",
2727
"module": "dist/esm/lucide-react.mjs",
2828
"typings": "dist/lucide-react.d.ts",
29+
"exports": {
30+
".": {
31+
"types": "./dist/lucide-react.d.ts",
32+
"module": "./dist/esm/lucide-react.mjs",
33+
"node": "./dist/cjs/lucide-react.js",
34+
"import": "./dist/esm/lucide-react.mjs",
35+
"require": "./dist/cjs/lucide-react.js",
36+
"default": "./dist/cjs/lucide-react.js"
37+
},
38+
"./icons": {
39+
"types": "./dist/lucide-react.d.ts",
40+
"module": "./dist/esm/lucide-react.mjs",
41+
"node": "./dist/cjs/lucide-react.js",
42+
"import": "./dist/esm/lucide-react.mjs",
43+
"require": "./dist/cjs/lucide-react.js",
44+
"default": "./dist/cjs/lucide-react.js"
45+
},
46+
"./icons/*": {
47+
"types": "./dist/lucide-icon.d.mts",
48+
"default": "./dist/esm/icons/*.mjs"
49+
},
50+
"./types": {
51+
"types": "./dist/lucide-types.d.ts"
52+
},
53+
"./dynamic": {
54+
"types": "./dynamic.d.ts",
55+
"import": "./dynamic.mjs",
56+
"default": "./dynamic.js"
57+
},
58+
"./dynamic.mjs": {
59+
"types": "./dynamic.d.mts",
60+
"default": "./dynamic.mjs"
61+
},
62+
"./dynamic.js": {
63+
"types": "./dynamic.d.ts",
64+
"default": "./dynamic.js"
65+
},
66+
"./dynamicIconImports": {
67+
"types": "./dynamicIconImports.d.ts",
68+
"default": "./dynamicIconImports.mjs"
69+
},
70+
"./dynamicIconImports.mjs": {
71+
"types": "./dynamicIconImports.d.mts",
72+
"default": "./dynamicIconImports.mjs"
73+
},
74+
"./dist/esm/icons/*.mjs": {
75+
"types": "./dist/lucide-icon.d.mts",
76+
"default": "./dist/esm/icons/*.mjs"
77+
},
78+
"./dist/*": "./dist/*",
79+
"./src/*": "./src/*.ts",
80+
"./package.json": "./package.json"
81+
},
2982
"sideEffects": false,
3083
"files": [
3184
"dist",

0 commit comments

Comments
 (0)