Search code examples
javascriptnode.jsecmascript-6rollupluxon

Use ES6 library's ES5 module without compiling it on my own project with rollup


I need to use an ES6 library (Luxon) and want to compile down the files to ES5, but Rollup adds the files as ES6.

The library has a special /build folder with different output formats.

How can I configure Rollup to make use of that instead of doing nothing with the library?


Solution

  • First of all, you have two options here:

    • Either compiling the library on your project with Rollup and @rollup/plugin-babel
    • Or referencing the /build directory of the package instead of the ES6 version of the package, using an alias with @rollup/plugin-alias

    I'll go with the second approach because is the one you asked for:

    1. Install the plugin npm i @rollup/plugin-alias
    2. At rollup.config.js import it import alias from '@rollup/plugin-alias';
    3. Finally, add it to plugins:

    const path = require('path');
    module.exports = {
      input: 'src/index.js',
      output: {
        dir: 'output',
        format: 'cjs'
      },
      plugins: [
        alias({
          entries: [
            { find: 'luxon', replacement: path.resolve(process.cwd(), 'node_modules/luxon/build') },
    
          ]
        })
      ]
    };