abstract-logging icon indicating copy to clipboard operation
abstract-logging copied to clipboard

Rollup fails to build when importing abstract-logging

Open freddi301 opened this issue 2 years ago • 2 comments

freddi301 avatar Mar 31 '22 09:03 freddi301

Would you like to send a Pull Request to address this issue? Remember to add unit tests.

jsumners avatar Mar 31 '22 12:03 jsumners

I encountered this issue as well when I tried to bundle fastify with rollup.

Code like:

const logger = require("abstract-logging");
logger.info(111);

works fine. But when we bundle it with rollup, it will crash with the following error:

logger.info(111);
       ^
TypeError: logger.info is not a function

The generated bundle will be something like this:

'use strict';

var rollupPlayground = {};

var abstractLoggingExports = {};
var abstractLogging = {
  get exports(){ return abstractLoggingExports; },
  set exports(v){ abstractLoggingExports = v; },
};

(function (module) {
	function noop () { }
	const proto = {
	  fatal: noop,
	  error: noop,
	  warn: noop,
	  info: noop,
	  debug: noop,
	  trace: noop
	};
	Object.defineProperty(module, 'exports', {
	  get () {
	    return Object.create(proto)
	  }
	});
} (abstractLogging));

const logger = abstractLoggingExports;
logger.info(111);

module.exports = rollupPlayground;

The abstractLogger.exports setter isn't triggered by Object.defineProperty(), which makes abstractLoggingExports empty and calls to its methods will fail.

Tried some of the rollup options but without luck.

evshiron avatar Jan 05 '23 07:01 evshiron