Files
lucide/packages/solid/rollup.config.mjs
T
Davlatjon Sh d8b5ee75e8 fix(packages): ship ESM type declarations for Solid packages (#5263)
`lucide-solid` and `@lucide/solid` have no `"type": "module"` and point every
`types` export condition at `.d.ts` files, so TypeScript's Node16/NodeNext
resolution reads the declarations as CommonJS. ESM consumers then get the
module object instead of the component for per-icon default imports
(`import Camera from '@lucide/solid/icons/camera'`), failing with TS2604/TS2322.

Emit `.d.mts` declarations next to the existing `.d.ts` files (rollup rewrites
their relative imports to `.mjs`) and select them via a nested `import`
condition under `types`, as lucide-react already does with `.d.mts` files.
The `.d.ts` files, top-level `types` field and runtime conditions are unchanged.
2026-11-23 16:03:02 +01:00

141 lines
4.2 KiB
JavaScript

import path from 'path';
import { babel } from '@rollup/plugin-babel';
import esbuild from 'esbuild';
import plugins from '@lucide/rollup-plugins';
import dts from 'rollup-plugin-dts';
import pkg from './package.json' with { type: 'json' };
const dtsOptions = {
includeExternal: ['@lucide/shared/types'],
tsconfig: './tsconfig.json',
};
const packageName = '@lucide/solid';
const outputFileName = 'lucide-solid';
const outputDir = 'dist';
const inputs = ['src/lucide-solid.ts'];
const bundles = [
{
format: 'cjs',
inputs,
outputDir,
preserveModules: true,
},
{
format: 'esm',
inputs,
outputDir,
preserveModules: true,
extension: 'mjs',
},
];
const configs = bundles
.map(({ inputs, outputDir, format, preserveModules, extension = 'js' }) =>
inputs.map((input) => ({
input,
plugins: [
babel({
extensions: ['.ts', '.tsx', '.js', '.jsx'],
babelHelpers: 'bundled',
presets: [
'babel-preset-solid',
'@babel/preset-typescript',
['@babel/preset-env', { targets: 'last 2 years' }],
],
// Babel 8's @babel/preset-typescript no longer enables JSX parsing for .tsx files,
// and babel-preset-solid's Babel 7 JSX syntax plugin skips it when TypeScript is enabled.
overrides: [{ test: /\.[jt]sx$/, parserOpts: { plugins: ['jsx'] } }],
}),
...plugins({
pkg,
withEsbuild: false,
}),
format === 'esm'
? {
name: 'ts',
buildEnd() {
// Transpile typescript to './dist/source'
esbuild.build({
entryPoints: ['./src/**/*.tsx', './src/**/*.ts'],
outdir: './dist/source',
outExtension: {
'.js': '.jsx',
},
loader: {
'.js': 'jsx',
},
jsx: 'preserve',
jsxImportSource: '@solidjs/web',
bundle: true,
format: 'esm',
sourcemap: true,
target: ['esnext'],
banner: {
js: `/**
* @license ${pkg.name} v${pkg.version} - ${pkg.license}
*
* This source code is licensed under the ${pkg.license} license.
* See the LICENSE file in the root directory of this source tree.
*/`,
},
plugins: [
{
name: 'externalize-everything-except-own-dependencies',
setup(build) {
build.onResolve({ filter: /(.*)/ }, (args) => {
const modulePath = path.join(args.resolveDir, args.path);
if (
args.kind === 'import-statement' &&
args.path !== '@lucide/shared' &&
!modulePath.includes('packages/shared')
) {
return { path: args.path, external: true };
}
});
},
},
],
external: ['solid-js', '@solidjs/web'],
});
},
}
: null,
],
external: ['solid-js', '@solidjs/web'],
output: {
name: packageName,
...(preserveModules
? {
dir: `${outputDir}/${format}`,
exports: 'auto',
entryFileNames: `[name].${extension}`,
}
: {
file: `${outputDir}/${format}/${outputFileName}.${extension}`,
}),
format: format === 'source' ? 'esm' : format,
preserveModules,
preserveModulesRoot: 'src',
sourcemap: true,
},
})),
)
.flat();
const typesConfig = {
input: inputs[0],
output: ['d.ts', 'd.mts'].map((extension) => ({
dir: `${outputDir}/types`,
format: 'es',
preserveModules: true,
preserveModulesRoot: 'src',
entryFileNames: `[name].${extension}`,
})),
plugins: [dts(dtsOptions)],
};
export default [...configs, typesConfig];