简体   繁体   中英

Is there a way to abstract the import from a node module using typescript config files in the same style as the 'paths' property?

I am creating private node modules which for now might change considerably in structure which could mean splitting existing code into multiple packages.

If I have 100 files importing from a package that no longer holds the import I can do a find and replace but it becomes more difficult when classes are imported from that package...

so something like:

import { thing1, thing2} from 'my-package';

in the future may need to be:

import { thing1} from 'my-package';
import { thing2} from 'my-package2';

You can abstract imports using tsconfig like so:

"paths": {
  "@shared/*": ["app/shared/*"]
}

But I cant figure out a way to do the same thing with node modules so that if there is a bigger change I only need to change 1 line. Is this possible?

Check https://github.com/nrwl/nx They can help you in using monorepo approach. You can split your system into Applications and Libraries.

Create index.ts file, import and export your modules:

import { assign, assignWith } from 'lodash';
import { addDays } from 'date-fns';
export { assign, assignWith, addDays };

and import modules from that index:

import { assign, addDays } from './index';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM