rollup-ns

1.1.1 • Public • Published

rollup-ns

The ES6 to global namespace code transformation utility.

Rollup-ns is a node command producing a single (rolled-up) Namespace'd source code from the source ES6 modules.

Why?

To support single-file namespace organized module output from the ES6 modularized solution. While transpiling and module bundling can solve much but this is not addressable by any of these tools. This scenario is not supported by the typescript compiler, and bundle tools like babel or rollup.

rollup-ns solves this scenario with custom source file transformation and module imports aware target namespace source code building.

How it works?

  • rollup-ns reads all the ES6 modules in the given directory and subdirectories.
  • changes original ES6 imports to namespace type imports.
  • generates one single file containing the namespace organized modules.

Note: rollup-ns never changes the original sources but always rewrite the single output file. If you are customizing the generated namespaces use declaration merging and never make changes to the generated file.

Configuration

Rollup-ns supports configuring the source folder, the target file and namespace. It also has configuration option for ignoring files (such as test files, or other support files) from the generated output.

rollup-ns.config.js

/*
 * rollup-ns configuration.
 */
module.exports = {
    //set the target file 
    target: './generated/out.ts',
 
    //sets the target namespace 
    targetNs: 'Target.Namespace',
 
    //sets the source directory where the source es6 modules are located
    src: '../source/directory/',
 
    //enable/disable the target prettier.
    pretty: true,
 
    //excluded files...
    exclude: [
        //ignore the ES6 module index file (exports)
        '**/**/index.ts',
 
        //ignore the polyfill module.
        '**/**/polyfills.ts',
 
        //exclude all spec
        '**/*.[s|S]pec.ts',
 
        //exclude all test
        '**/*.[t|T]est.ts',
 
        //exclude all js files.
        '**/*.js',
    ],
    
    //the external types...
    externalTypes: [
        'Error',
    ]
};

Source Limitations

  1. import * as 'es6-namespace-name' module import is not supported.

rollup-ns needs named imports like this:

import { TypeName } from 'module';

where the imported module defines the type like this:

export type TypeName = 'a' | 'b' | 'c';
  1. imports from re-exporting modules are not supported.

Example

Basic source files

./ClassA.ts

import { EnumA } from './EnumA';
 
export class ClassA {
    enumField: EnumA;
}

./EnumA.ts

export enum EnumA {
    A,
    B,
    C
}

./SubFolder/ClassB.ts

import { ClassA } from '../ClassA';
import { EnumB } from './EnumB';
 
export class ClassB extends ClassA {
    subEnum: EnumB;
}

./SubFolder/EnumB.ts

export enum EnumB {
    A,
    B,
    C
}

Generated

The generated single output file using Samples.Basic target namespace.

//this file has been generated by rollup-ns
namespace Samples.Basic
{
export enum EnumA {
    A,
    B,
    C
}
}
namespace Samples.Basic
{
export class ClassA {
    enumField: EnumA;
}
}
namespace Samples.Basic.SubFolder
{
export enum EnumB {
    A,
    B,
    C
}
}
namespace Samples.Basic.SubFolder
{
export class ClassB extends ClassA {
    subEnum: EnumB;
}
}

Versions

  • 1.0 : Initial release
  • 1.1 : Add support for Global augmentations (such as extending Window or Array)
    Add support for External Packages.

Package Sidebar

Install

npm i rollup-ns

Weekly Downloads

10

Version

1.1.1

License

BSD-3-Clause

Unpacked Size

169 kB

Total Files

82

Last publish

Collaborators

  • csizo