In the process of developing a TypeScript telemetry library using OpenTelemetry, I am exploring ways to incorporate tree-shaking to allow consumers to selectively import only the necessary modules and minimize the overall bundle size. The project directory structure is as follows:
lib
|
|__ trace
| |____ TraceClass.ts
| |____ index.ts
|
|__ metrics
| |____ MetricClass.ts
| |____ index.ts
|
|__ logs
| |____ LogClass.ts
| |____ index.ts
|
|__ index.ts
The library consists of three main modules - "trace," "log," and "metrics," each contained within separate folders. Each module includes its own "index.ts" file, along with a root "index.ts" file. For instance, if the root "index.ts" file exports all components from the sub-modules:
root index.ts
export * from './trace';
export * from './metrics';
export * from './logs';
If a consumer imports the TraceClass
from the root file as shown below:
import { TraceClass } from 'mytelemetrylib';
Does the above code automatically import all modules into their code?
Alternatively, would they need to specify the import more explicitly like this:
import { TraceClass } from 'mytelemetrylib/trace';