1.0.2 • Published 2 years ago

import-redirect v1.0.2

Weekly downloads
-
License
-
Repository
-
Last release
2 years ago

IMPORT REDIRECT

This is a tool to redirect the import specifier, it is used to transform the relative import path into a bare specifier.

BASIC USAGE

Suppose we have the directory structure below:

/PROJECT/index.ts        ->  import { vec2, vec3 } from './math/index';
        /math/.
        /math/index.ts    ->  export * from './vec';
        /math/vec.ts      ->  export const vec2, vec3;
const { ir } = require('import-redirect');

const code = ir.transformFileImport({
    file: '/PROJECT/index.ts',
    moduleIndex: '/PROJECT/math/index.ts',
    moduleName: 'MY_MATH_LIB',
});
console.log(code);  // import { vec2, vec3 } from 'MY_MATH_LIB';

FEATURE

  • mergeSpecifier: merge multiple import statement with the same specifier.
const { ir } = require('import-redirect');

const code = ir.transformFileImport({
    file: '/PROJECT/index.ts',
    moduleIndex: '/PROJECT/math/index.ts',
    moduleName: 'MY_MATH_LIB',
    mergeSpecifier: true,
});
console.log(code);
// PROJECT/index.ts
import { vec3 } from './math/index';
import { vec2 } from './math/index';

code in PROJECT/index.ts should be transformed into:

import { vec2, vec3 } from 'MY_MATH_LIB';