rollup-plugin-typescript-path-mapping

1.0.2 • Public • Published

rollup-plugin-typescript-path-mapping

Resolving module path which applies typescript's path mapping rule, see Path mapping in official doc

why?

When we configure some path mapping rules in tsconfig.json, typescript compiler can resolve module paths correctly using the rule you just configured, however, typescript does not rewrite module path in emitted .js files, thus rollup could not resolve modules correctly when you try packing those emitted .js files. To solve this, you have to write another corresponding rule that rollup recognizes using some plugins.
When using this plugin, it can read the path mapping rule directly in tsconfig.json and adapte it to rollup automatically, there is nothing else you need to do :)

Installation

$ npm install rollup-plugin-typescript-path-mapping

Usage

// rollup.config.js
const rollupPluginTypescriptPathMapping = require('rollup-plugin-typescript-path-mapping')
 
module.exports = {
    input: 'dist/main.js',
    output: {
        file: 'bundle.js',
        format: 'iife'
    },
    plugins: [rollupPluginTypescriptPathMapping({ tsconfig: true })]
}

there is some path mapping rules configured in tsconfig.json

{
    "compilerOptions": {
        "baseUrl": "./",
        "paths": {
            "base/*": ["src/base/*"],
            "util/*": ["src/common/util/*"],
        },
        "outDir": "dist",
        "rootDir": "src",
        "module": "es2015",
        "target": "es2015"
    }
}

then just rollup these files that typescript compiled
$ rollup -c

of course, you can manually configure rule

// rollup.config.js
const rollupPluginTypescriptPathMapping = require('rollup-plugin-typescript-path-mapping')
 
module.exports = {
    input: 'dist/main.js',
    output: {
        file: 'bundle.js',
        format: 'iife'
    },
    plugins: [rollupPluginTypescriptPathMapping({
        baseUrl: './',
        paths: {
            'base/*': ['src/base/*'],
            'util/*': ['src/common/util/*'],
        },
        outDir: 'dist',
        rootDir: 'src'
    })]
}

Package Sidebar

Install

npm i rollup-plugin-typescript-path-mapping

Weekly Downloads

0

Version

1.0.2

License

MIT

Last publish

Collaborators

  • mutefish0