calvinmetcalf / rollup-plugin-node-builtins

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error importing one of the internal libs

darlanalves opened this issue · comments

I can't find a way around this error while bulding with rollup:

ERROR: 'import' and 'export' may only appear at the top level (24:0) in [...]/rollup-plugin-node-builtins/src/es6/util.js

I'm not sure why it's happening. Pheraphs is due to an import statement in the middle of the 'utils.js':

import inherit from 'inherits';

Any ideas? I got a working build by extracting the 'streams' lib from this plugin and putting into a file in my main project files.

I've been bashing my head on a wall for weeks to get the stream module working with rollup. Your plugin just saved me a lot of time on it. Would be great to have your ES6 versions of common modules as separated packages as well, like process-es6 :)

yeah you have to use the master version of commonjs plugin with the ignoreGlobals option

I have the same problem with commonjs plugin 3.1.0

+1 here

are you using the ignoreGlobals options ?

I am using commonjs plugin of verstion 3.3.0. Here is plugins part of rollup config:

plugins: [
    json(),
    babel({
      include: [
        'node_modules/rollup-plugin-node-builtins/**',
        'lib/**'
      ],
      babelrc: false,
      presets: ['react', 'es2015-rollup', 'stage-0']
    }),
    builtins(),
    nodeResolve({
      jsnext: false, // Default: false
      main: true, // Default: true
      skip: [], // Default: []
      browser: true, // Default: false
      extensions: [ '.js', '.json' ], // Default: ['.js']
      preferBuiltins: true // Default: true
    }),
    commonjs({
      include: 'node_modules/**',
      ignoreGlobal: true,
      namedExports: {
        'node_modules/immutable/dist/immutable.js': [
          'Iterable', 'Seq', 'Collection', 'Map', 'OrderedMap', 'List', 'Stack', 'Set',
          'OrderedSet', 'Record', 'Range', 'Repeat', 'is', 'fromJS'
        ],
        'node_modules/esrever/esrever.js': [ 'reverse' ]
      }
    }),
    replace({
      'process.env.NODE_ENV': JSON.stringify(NODE_ENV),
      'process.browser': JSON.stringify(true)
    })
  ]

And here is the full error:

Error parsing .../node_modules/rollup-plugin-node-builtins/src/es6/util.js: 'import' and 'export' may only appear at the top level (26:0) in .../node_modules/rollup-plugin-node-builtins/src/es6/util.js

try moving builtins() to be after commonjs, also try removing node_modules/rollup-plugin-node-builtins/** from the babel include

did. no effect.

OK. it seems that the error goes away if I comment out following lines in cjs dist file:

libs.set('process', require.resolve('process-es6'));
libs.set('buffer', require.resolve('rollup-plugin-node-globals/dist/buffer'));
libs.set('util', require.resolve(path.join('..', 'src', 'es6', 'util')));
libs.set('sys', libs.get('util'));
libs.set('events', require.resolve(path.join('..', 'src', 'es6', 'events')));
libs.set('stream', require.resolve(path.join('..', 'src', 'es6', 'stream')));
libs.set('path', require.resolve(path.join('..', 'src', 'es6', 'path')));
libs.set('querystring', require.resolve(path.join('..', 'src', 'es6', 'qs')));

Not sure about the exact consequences of doing so, I mean maybe there are some negative side effects. But the error is not thrown and final bundle does contain node builtins' shims.

Few thoughts on possible root causes of the problem:

  • those es6 modules seem to be extra there since that is a commonjs bundle file. I know that they are simple transpiled from src file, but at least from cjs dist file they seem to be extra.
  • don't they (those lines) over-write commonjs modules from node-libs-browser being loaded into the libs Map few lines earlier?
  • The paths to es6 modules resolve to es6 files, not commonjs ones, so can it be that they are not babelified during compilation?

so I've 'es6' ified some of the browserify core modules so you can do import {inherits} from 'util' and have it work, that's what those lines you're commenting out do. maybe they are getting picked up by bebal try remvoing 'node_modules/rollup-plugin-node-builtins/**',
from the babel config

Did. but that doesn't help

so yeah the gist of this is it's a weird interaction between the commonjs module and this one, I think the issue is that this file is being incorrectly registered as commonjs

I see.
But I in the context of plugin I still don't get this statement:

so I've 'es6' ified some of the browserify core modules so you can do import {inherits} from 'util'

I consume the plugin, not its internal stuff, so why would I need to import {inherits} from 'util' when using the plugin?

How to fix this issuse:
add to commonjs exclude "node_modules/rollup-plugin-node-buildins/**"
and add inherits lib to deps npm i inherits --save

ok looks like I forgot a dep so should be fixed

On Sat, Jul 23, 2016 at 3:45 PM Askar Yusupov notifications@github.com
wrote:

How to fix this issuse:
add to commonjs exclude "node_modules/rollup-plugin-node-buildins/**"
and add inherits lib to deps npm i inherits --save


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#5 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABE4n6ABMtgq0Ezs_Ahvd8o1Y_lapMUmks5qYm8-gaJpZM4H5l7z
.

@calvinmetcalf you can use "jsnext:main" in package.json and then jsnext: true in rollup config. https://github.com/rollup/rollup/wiki/jsnext:main

In my opinion its better than excluding manually from rollup-plugin-commonjs.

@SupremeTechnopriest I don't think that has anything to do with this current issue which had to do with me forgetting to include a file in my dependencies, feel free to open up a new issue with your particular problem