Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rollup - incorrect path resolution

I want to use the library ebnf from NPM and create a bundle using rollup. Since the ebnf is installed to node_modules I also use the rollup plugin rollup-plugin-node-resolve.

The problem is that ebnf contains the code require('..') which - in my case - is resolved to dist in my case. Thus it seems .. is interpreted relative to the output file instead of being relative to the source file.

This is my rollup.config.js (taken from my test repo jneuendorf/rollup-broken-resolve):

import resolve from 'rollup-plugin-node-resolve'
import commonjs from 'rollup-plugin-commonjs'


export default {
    input: 'src/index.js',
    output: {
        file: 'dist/bundle.js',
        format: 'cjs'
    },
    // name: 'MyModule',
    plugins: [
        resolve(),
        commonjs(),
    ]
}

Is this a problem in rollup-plugin-node-resolve or am I doing something wrong?

like image 671
jneuendorf Avatar asked Nov 07 '22 11:11

jneuendorf


1 Answers

Since some of the external libraries needed will still be available only as Common.js modules, you could also convert them to ES-Modules:

"Since most packages in your node_modules folder are probably legacy CommonJS rather than JavaScript modules, you may need to use rollup-plugin-commonjs"

  • https://github.com/rollup/rollup-plugin-commonjs

    "Convert CommonJS modules to ES6, so they can be included in a Rollup bundle"

like image 104
alamadrid Avatar answered Dec 06 '22 09:12

alamadrid