2

I'm using Angular 14 and building a shared library. I have this project structure

+ projects  
    + my-lib
        - package.json
        + src
            - public-api.ts
            + lib
                + helpers
                    - index.ts
                    - my-first-helper.ts
                    - my-second-helper.ts
                    ...

In "public_api.ts" I have

export * from './lib/helpers';

and in helpers/index.ts I have

export * from './my-first-helper';
export * from './my-second-helper';

My question is, every time I add a new file to the "helpers" directory, I have to edit my "index.ts" to explicitly include it. Is there a way I can author my "index.ts" file to just export everything in each file in my "helpers" directory?

Dave
  • 15,639
  • 133
  • 442
  • 830
  • 1
    Someone correct me if I'm wrong, but I think you can do: `export * from '.';` – Brandon Taylor Mar 09 '23 at 15:48
  • I gave that a whirl, but it didn't seem to work. – Dave Mar 09 '23 at 16:40
  • Hmm. You may just have to add each and every file you want to export from. – Brandon Taylor Mar 09 '23 at 17:29
  • may want to look into something like [barrelsby](https://github.com/bencoveney/barrelsby) or [plugin](https://marketplace.visualstudio.com/items?itemName=eliostruyf.vscode-typescript-exportallmodules) – Alex Voloshyn Mar 17 '23 at 16:29
  • One possible solution is to use _moduleAliases to include files and which is better than adding every new file in the code. Just use the plugin to package JSON files and enjoy the code https://www.npmjs.com/package/module-alias – Mahesh S Mar 20 '23 at 11:23

1 Answers1

0

Remember that Typescript is just an superset language. When compiled it will be Javascript, and Javascript needs an entry point for all modules that needs to be interpreted by Node.js. When compiling, Node first searchs for package.json, if not finds, will lookup for index.js for default.
A module can be a single or multiple files, Node.js doesnt have to known about your software internal structure, Node only needs to knows about the imports (using the require keyword) on modules.

BUT

You can automate the process. Its possible to create a script, that manage your index.ts always when a file is added ou removed from your directory. Or use libraries, such as (create-ts-index)

Riheldo Melo
  • 114
  • 5