-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document how to build a library for SolidJS #97
Comments
It would be great to have some documentation on this, because I cannot get my solidjs component library to work when bundled. If I copy the source code into my project, it works, if I import the bundled version, it shows very weird behaviour. |
I could figure out how to bundle my library, but it turned out to be pretty complicated imo: import { defineConfig } from 'vite';
import solid from 'vite-plugin-solid';
import dts from 'vite-plugin-dts';
import { glob } from 'glob';
import { readFile, writeFile } from 'fs/promises';
import { sep } from 'path';
const getEntries = async () => {
const entries = (await glob('src/**/index.@(ts|tsx)')).reduce(
(acc, entry) => {
const name = entry
.substring(0, entry.lastIndexOf('.'))
.replace(`src${sep}`, '')
.replace(`stories${sep}`, '');
return { ...acc, [name]: entry };
},
{} as Record<string, string>,
);
return entries;
};
interface Metadata {
main: string;
module: string;
types: string;
browser: {};
exports: {
[key: string]: {
import: {
types: string;
default: string;
};
};
};
typesVersions: {
'*': {
[key: string]: string[];
};
};
}
const entries = await getEntries();
export default defineConfig({
plugins: [
solid(),
dts({
include: 'src/**/index.@(ts|tsx)',
beforeWriteFile: (filePath, content) => {
return { filePath: filePath.replace('stories/', ''), content };
},
afterBuild: async () => {
const metadata: Metadata = {
main: './dist/index.js',
module: './dist/index.js',
types: './dist/index.d.ts',
browser: {},
exports: {
'.': {
import: {
types: './dist/index.d.ts',
default: './dist/index.js',
},
},
},
typesVersions: { '*': {} },
};
for (const key of Object.keys(entries)) {
if (key === 'index') continue;
const name = key.replace(`${sep}index`, '');
metadata.exports[`./${name}`] = {
import: {
types: `./dist/${name}/index.d.ts`,
default: `./dist/${name}/index.js`,
},
};
metadata.typesVersions['*'][name] = [`./dist/${name}/index.d.ts`];
}
const packageJson = JSON.parse(await readFile('package.json', 'utf-8'));
await writeFile(
'package.json',
`${JSON.stringify({ ...packageJson, ...metadata }, null, 2)}\n`,
'utf-8',
);
},
}),
],
server: { port: 3000 },
build: {
target: 'esnext',
lib: { formats: ['es'], entry: entries },
rollupOptions: { external: ['solid-js'] },
},
}); This includes everything, presenting a fully end-to-end build solution:
|
I had the same experience at work. The amount of work you have to do to get the build right including typings, CJS/ESM options and client/server bundles is quite heavy. |
My team had difficulties in building a component library for SolidJS because we couldn't find any documentation for it.
We have a monorepo where one package is an application which should use components from another package in the repo.
Each has their own build. We use Vite and
vite-plugin-solid
to build.vite-plugin-solid
seems to use the package.jsonexports
field with a custom import conditionsolid
.We couldn't find any documentation about this behavior (if there is one please let me know). Only after scouring through this plugin's source code and the NodeJs docs and looking at existing SolidJS component libs we got some idea about how this works.
IMO it the documentation should answer the following questions:
exports
to export SolidJS codeThe text was updated successfully, but these errors were encountered: