I'm creating a library that exports a lot of React Components and also some TypeScript utilities used for multiple projects within my company. However, I can’t seem to do anything other than everything exporting from the root of the library.
I have the following folder structure. The .tsx and utils.ts files only export named properties and the root index.ts file contains export * from './Button';
, etc.
Then src/index.ts
is export * from './ui'
, etc...
src/
ui/
Button/
Button.tsx
Input/
Input.tsx
index.ts
utils/
myUtilFunc1.ts
myUtilFunc2.ts
index.ts
Using the below Webpack configuration,
const path = require("path");
module.exports = {
entry: "./src/index.ts",
resolve: {
extensions: [".ts", ".tsx", ".js"],
},
output: {
path: path.resolve(__dirname, "dist"),
filename: "main.js",
library: "@company/library",
libraryTarget: "umd",
umdNamedDefine: true,
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: "ts-loader",
exclude: /node_modules/,
},
],
},
externals: {
react: "react",
"react-dom": "react-dom",
},
};
And the following package.json properties:
"main": "dist/main.js",
"module": "dist/main.js",
"types": "dist/types/index.d.ts",
When I consume this library in another project, I can import as I need like so:
import { Button, myUtilFunc2 } from "@company/library"
But I would like to import like this:
import { Button } from "@company/library/ui"
and import {myUtilFunc2 } from "@company/library/util"
I know this should be possible, as MUI and other libraries have this. I am using Webpack as I tried this will rollup and even set up a monolithic repository with Lerna (which works well, but I just feel like I shouldn’t need to build two npm packages for this and they can both be contained in one).