Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

reactjs - How to import local copy of library in app built with create-react-app

I have a library I am writing. My library is build using rollup.

I want to include it in my main application. The app is built using create-react-app.

I would like to use the dev version of the library in the app without publishing it to the npm registry. It becomes more so painful to publish and use. (even with local repositories like local-npm) because a version can be published only once. I have seen the hack provided here - Using SNAPSHOT in private NPM like in Maven

I tried to use the npm link. Apparently, CRA will not use the dep if the real path is outside of the base folder even if a symlink is present.

What is the best way of handling the situation? I want to set this up as a precedent for all my future app work and am looking for a solution as well as a best practice I can adhere to.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I do not know the best way to do it but your problem is the same with mine when I have to include a js SDK file to my CRA project.

Inside the index.js file, just import it.

import "./libs/latest.sdk.bundle.min";

const something = new ClassFromSDK();

something.func();

Generally

  • You write a class from your js file and inside this class, you should have some functions that you need to use.

  • Store this file in your project like src/libs/mylib.js

  • Import it to the root CRA file (index.js) like above.

  • After initiating a instance of this class, just use the function inside as usual.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...