Skip to content

conditional require is replaced with empty if block #202

Closed
@tkalliom

Description

@tkalliom
  • Rollup Plugin Name: commonjs
  • Rollup Plugin Version: 11.0.2
  • Rollup Version: 1.31.0
  • Operating System (or Browser): Linux
  • Node Version: 10.15.2

How Do We Reproduce?

See https://repl.it/repls/SilkyOurStruct, or alternatively:

npm install --save continuation-local-storage
npm install --save-dev rollup @rollup/plugin-commonjs @rollup/plugin-node-resolve
mkdir src && echo 'import createNamespace from "continuation-local-storage";' > src/index.js
echo "import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
export default {input: 'src/index.js', output: {dir: '.', format: 'cjs'}, plugins: [commonjs(), resolve()]};" > rollup.config.js
npx rollup -c

Expected Behavior

Rollup produces a valid output, possibly with the oneliner if block replaced with just evaluating the condition.

Actual Behavior

Bundling fails with:

[!] Error: Unexpected token
../node_modules/continuation-local-storage/context.js (17:0)
15: if (!process.addAsyncListener) require('async-listener');
16: 
17: function Namespace(name) {
    ^
18:   this.name   = name;
19:   // changed in 2.7: no default context
Error: Unexpected token
    at error (/tmp/node_modules/rollup/dist/shared/node-entry.js:5400:30)
    at Module.error (/tmp/node_modules/rollup/dist/shared/node-entry.js:9820:16)
    at tryParse (/tmp/node_modules/rollup/dist/shared/node-entry.js:9713:23)
    at Module.setSource (/tmp/node_modules/rollup/dist/shared/node-entry.js:10076:33)
    at Promise.resolve.catch.then.then.then (/tmp/node_modules/rollup/dist/shared/node-entry.js:12362:20)

It seems that the conditional require is translated into an import at the start of the output, but an syntactically invalid if block is left where the import was

if (!process.addAsyncListener) // a require was removed from this position

function Namespace(name) { // This is now in place of the statement of the if block
``

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions