How to pack Typescript module into one js module

167 views Asked by At

My current situation is that I have a Node package composed by a number of Typescript files, transpiling each to a single JavaScript file.

What I want to achieve is to pack all these JavaScript files into one file, that should be a single module named after the package name in the package.json file, that, with the proper typing file, could be referenced in another project.

Here's the tsconfig.json for the package that I want to export:

{
    "compilerOptions": {
        "target": "es5",
        "module": "amd",
        "moduleResolution": "node",
        "noImplicitAny": false,
        "noEmitOnError": true,
        "removeComments": true,
        "sourceMap": false,
        "experimentalDecorators": true,
        "outFile": "dist/bundle.js"
    },
    "include": [
        "index.ts",
        "src/**/*.ts"
    ]
}

And this is an example of index.ts in the main folder of the project:

export { BasePrinter, PrintResult, ResultType } from './src/base-printer';
export { PrinterOne } from './src/printer-one';
export { PrinterTwo } from './src/printer-two';

Each line exports a class that's contained in the related path.

What I want to achieve is to install the local file in another importer Node project like this:

import { PrinterOne } from 'exporter';

Having this lines of code in the importer package.json

"dependencies": {
    "exporter": "file:../packages/exporter/exporter", ...
}

Anyway, if I try to execute through Node any code in the importer project JavaScript file referencing the exporter project, I get this error:

Error: Cannot find module 'exporter'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (C:\Users\Massimiliano\Projects\node-packaging\importer\index.js:3:18)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)

Is there any way to obtain a single javascript bundle file that will allow me to import Typescript classes the way I described above?

Thanks!

0

There are 0 answers