Open Vanuan opened 6 years ago
To resolve this dependency cycle, inner modules should depend on inner modules directly, without index.js as intermediate:
// index.js
const buffer = require('./buffer');
const reporter = require('./reporter');
const Node = require('./node');
module.exports = {
Reporter: reporter.Reporter,
DecodeBuffer: buffer.DecodeBuffer,
EncoderBuffer: buffer.EncoderBuffer,
Node,
};
// buffer.js
const Reporter = require('./reporter').Reporter;
Are there any reasons this wasn't done this way?
I agree, this cause cycle requires in some environments which could potentially be error-prone
Rollup is another example environment where this breaks.
There's this
base
file which contains a hidden cyclical dependency. The problem isbuffer
depends onbase
whilebase
depends onbuffer
.https://github.com/indutny/asn1.js/blob/bbf14e0732b7d8a5addedb800c91da7bc2a17e83/lib/asn1/base/index.js#L1-L8
https://github.com/indutny/asn1.js/blob/master/lib/asn1/base/buffer.js#L4 It works in node (and probably in webpack), but it causes problems when deployed using https://unpkg.com