I've been working on creating a small library of React components for use in several other projects. I am publishing the package internally (using a private GitHub repository) and then including in another project. However, when I go to import from a subdirectory of the package I am not able to do so as the paths don't match.
The projects using the package all utilize webpack to bundle/transpile code as I am trying to avoid doing any building in the component library if possible.
Directory Structure
- package.json
- src/
- index.js
- Button/
- index.js
- Button.jsx
- ButtonGroup.jsx
- Header/
- index.js
- Header.jsx (default export)
package.json
...
"main": "./src/index.js",
"scripts": "",
...
src/Button/index.js
import Button from './Button';
import ButtonGroup from './ButtonGroup';
export default Button;
export { Button, ButtonGroup};
src/index.js
Is this file actually necessary if only importing from subdirectories?
import Button from './Button';
import ButtonGroup from './Button/ButtonGroup';
import Header from './Header';
export { Button, ButtonGroup, Header };
Other Project
// This project is responsible for building/transpiling after importing
import { Button, ButtonGroup } from 'components-library/Button';
Example
Material-UI is a library of React components that is used by requiring in the following fashion: import { RadioButtonGroup } from 'material-ui/RadioButton
. I've tried to figure out how this works for them but to no avail yet.
Similar Questions
- How would I import a module within an npm package subfolder with webpack?
- This is very nearly the correct approach I require, except that the import path used there involved the
src/
directory, which I am trying to avoid (should becomponent-library/item
, notcomponent-library/src/item
(which does work currently though))
- This is very nearly the correct approach I require, except that the import path used there involved the
- Publishing Flat NPM Packages
- This is exactly what I want except that I was hoping to not have a "build" phase in the package (rely on importing locations to build/transpile)
Questions
- Can I skip the
src/
directory somehow in the import path? - Can I skip any type of build phase in the package (so developers don't have to build before committing)?
- How does a package similar to material-ui handle this?
via Kendall Roth
No comments:
Post a Comment