protobufjs / bytebuffer.js

A fast and complete ByteBuffer implementation using either ArrayBuffers in the browser or Buffers under node.js.
http://dcode.io
Apache License 2.0
721 stars 155 forks source link

Could not resolve memcpy when trying to roll-up a bundle #81

Open btgoodwin opened 7 years ago

btgoodwin commented 7 years ago

Part of this could be that I'm pretty new to these various module-generating tools that sprang up over the last two or three years. What I'm trying to do is roll up ProtoBuf and all of its dependencies along with my library using the rollup-plugin-commonjs and rollup-plugin-node-resolve plugins. The reason why I'm doing this is because there seems to be no way to use things like SystemJS or WebPack to deploy an app with my libraries, purely because ProtoBuf's requires of bytebuffer and a few other things. So I figured I would bundle everything into my library (ES2015) and move on. The reason I'm posting this issue here and not ProtoBuf is that when bundling it all up, the error I get is that 'memcpy' could not be resolved for bytebuffer-node.js.

The error I'm getting is:

Treating 'buffer' as external dependency Could not resolve 'memcpy' from /var/redhawk/sdr/rest-python/apps/angular-redhawk/node_modules/bytebuffer/dist/bytebuffer-node.js Error: Could not resolve 'memcpy' from /var/redhawk/sdr/rest-python/apps/angular-redhawk/node_modules/bytebuffer/dist/bytebuffer-node.js at Error (native) at /var/redhawk/sdr/rest-python/apps/angular-redhawk/node_modules/rollup-plugin-node-resolve/dist/rollup-plugin-node-resolve.cjs.js:78:21 at /var/redhawk/sdr/rest-python/apps/angular-redhawk/node_modules/resolve/lib/async.js:46:14 at process (/var/redhawk/sdr/rest-python/apps/angular-redhawk/node_modules/resolve/lib/async.js:173:43) at ondir (/var/redhawk/sdr/rest-python/apps/angular-redhawk/node_modules/resolve/lib/async.js:188:17) at load (/var/redhawk/sdr/rest-python/apps/angular-redhawk/node_modules/resolve/lib/async.js:69:43) at onex (/var/redhawk/sdr/rest-python/apps/angular-redhawk/node_modules/resolve/lib/async.js:92:31) at /var/redhawk/sdr/rest-python/apps/angular-redhawk/node_modules/resolve/lib/async.js:22:47 at FSReqWrap.oncomplete (fs.js:123:15)

The configuration I'm using is:

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

export default {
  entry: `dist/index.js`,
  context: 'this',
  sourceMap: (process.env.NODE_ENV === 'test') ? 'inline' : true,
  moduleId,
  moduleName,
  plugins: [
    nodeResolve({ main: true }),
    commonjs({
      include: 'node_modules/**',
      sourceMap: false
    })
  ],
  targets: [
    { dest: `dist/bundles/${moduleId}.umd.js`, format: 'umd' },
    { dest: `dist/bundles/${moduleId}.mjs`, format: 'es' },
  ],
  globals: {
    '@angular/core': '_angular_core',
    '@angular/common': '_angular_common',
    '@angular/http': '_angular_http',
    'rxjs/Observable': '_rxjs_Observable',
    'rxjs/Subject': '_rxjs_Subject',
    'rxjs/add/operator/map': '_rxjs_add_operator_map',
    'rxjs/add/operator/catch': '_rxjs_add_operator_catch'
  },
  external: [
    '@angular/core',
    '@angular/common',
    '@angular/http',
    'rxjs/add/operator/map',
    'rxjs/add/operator/catch',
    'rxjs/Observable',
    'rxjs/Subject'
  ]
};
sjonany commented 7 years ago

I got around this error by inspecting this line in bytebuffer-node.js “ memcpy = null; try { memcpy = require("memcpy"); } catch (e) {}” Looks like memcpy is an optional dep, but rollup might be harvesting all the require statements statically.

When I remove the try statement, the error disappears -- I still have other problems from getting aot + rollup + protobufjs to work ;), but at least this particular error has a workaround.

EDIT: This error also disappeared once I added rollup-plugin-node-globals and rollup-plugin-node-builtins to my rollup plugin, following this: https://github.com/rollup/rollup/issues/1051#issuecomment-255105214

Again, got a different error, but I think it's a rollup issue instead of bytebuffer.