Files
lucide/packages/lucide-react/rollup.config.mjs
1802f33f6c 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 <175728472+Copilot@users.noreply.github.com>

* 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 <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Eric Fennis <eric.fennis@gmail.com>
2026-10-26 11:05:25 +01:00

184 lines
4.2 KiB
JavaScript

import plugins from '@lucide/rollup-plugins';
import preserveDirectives from 'rollup-plugin-preserve-directives';
import pkg from './package.json' with { type: 'json' };
import dts from 'rollup-plugin-dts';
import getAliasesEntryNames from './scripts/getAliasesEntryNames.mts';
const dtsOptions = {
includeExternal: ['@lucide/shared/types'],
tsconfig: './tsconfig.json',
};
const aliasesEntries = await getAliasesEntryNames();
const packageName = 'LucideReact';
const outputFileName = 'lucide-react';
const inputs = [`src/lucide-react.ts`];
const bundles = [
{
format: 'cjs',
inputs,
outputDir: 'dist/cjs',
extension: 'js',
},
{
format: 'esm',
inputs,
outputDir: 'dist/esm',
preserveModules: true,
extension: 'mjs',
},
{
format: 'esm',
inputs: ['src/dynamicIconImports.ts', 'src/DynamicIcon.ts', ...aliasesEntries],
outputDir: 'dist/esm',
external: [/src/],
preserveModules: true,
extension: 'mjs',
paths: (id) => {
if (id.match(/src/)) {
const [, modulePath] = id.match(/src\/(.*)\.ts/);
return `./${modulePath}.mjs`;
}
},
},
{
format: 'esm',
inputs: ['src/dynamic.ts'],
outputFile: 'dynamic.mjs',
external: [/src/],
extension: 'mjs',
paths: (id) => {
if (id.match(/src/)) {
const [, modulePath] = id.match(/src\/(.*)\.ts/);
return `dist/esm/${modulePath}.mjs`;
}
},
},
];
const configs = bundles
.map(
({
inputs,
outputDir,
outputFile,
format,
minify,
preserveModules,
entryFileNames,
extension = 'js',
external = [],
paths,
}) =>
inputs.map((input) => ({
input,
plugins: [
...plugins({ pkg, minify }),
// Make sure we emit "use client" directive to make it compatible with Next.js
preserveDirectives({
include: ['src/lucide-react.ts', 'src/DynamicIcon.ts', 'src/context.ts', 'src/Icon.ts'],
suppressPreserveModulesWarning: true,
}),
],
external: ['react', 'prop-types', ...external],
output: {
name: packageName,
...(preserveModules
? {
dir: outputDir,
entryFileNames: entryFileNames ?? `[name].${extension}`,
}
: {
file: outputFile ?? `${outputDir}/${outputFileName}.${extension}`,
}),
paths,
format,
sourcemap: true,
preserveModules,
preserveModulesRoot: 'src',
globals: {
react: 'react',
'prop-types': 'PropTypes',
},
},
})),
)
.flat();
export default [
{
input: 'src/types.ts',
output: { file: 'dist/lucide-types.d.ts', format: 'es' },
plugins: [dts(dtsOptions)],
},
{
input: 'src/lucide-icon.d.ts',
output: { file: 'dist/lucide-icon.d.mts', format: 'es' },
plugins: [dts(dtsOptions)],
},
{
input: 'src/dynamicIconImports.ts',
output: [
{
file: `dynamicIconImports.d.ts`,
format: 'es',
},
// Extra declaration file with .d.mts extension for better compatibility with ESM environments
{
file: `dynamicIconImports.d.mts`,
format: 'es',
},
],
plugins: [dts(dtsOptions)],
},
{
input: 'src/dynamic.ts',
output: [
{
file: `dynamic.d.ts`,
format: 'es',
},
// Extra declaration file with .d.mts extension for better compatibility with ESM environments
{
file: `dynamic.d.mts`,
format: 'es',
},
],
plugins: [dts(dtsOptions)],
},
{
input: inputs[0],
output: [
{
file: `dist/${outputFileName}.d.ts`,
format: 'es',
},
],
plugins: [dts(dtsOptions)],
},
{
input: `src/${outputFileName}.suffixed.ts`,
output: [
{
file: `dist/${outputFileName}.suffixed.d.ts`,
format: 'es',
},
],
plugins: [dts(dtsOptions)],
},
{
input: `src/${outputFileName}.prefixed.ts`,
output: [
{
file: `dist/${outputFileName}.prefixed.d.ts`,
format: 'es',
},
],
plugins: [dts(dtsOptions)],
},
...configs,
];