Imported variable referenced before assignment in browser
simonvadee opened this issue · 2 comments
I'm having issue to build a browser-compatible library. I have dependencies which have to be babel-ified but they are supposed to be browser-compatible.
There is no build error when running rollup to build the library, but the generated file throws an error when loaded in a browser due to an unresolved import (actually, the import is resolved after the imported variable is referenced).
I have the following config:
const babel = require("rollup-plugin-babel");
const babelrc = require("babelrc-rollup").default;
const commonjs = require("rollup-plugin-commonjs");
const nodeResolve = require("rollup-plugin-node-resolve");
const json = require("rollup-plugin-json");
const builtins = require("rollup-plugin-node-builtins");
const globals = require("rollup-plugin-node-globals");
const config = {
sourcemap: true,
plugins: [
json(),
nodeResolve({
browser: true,
preferBuiltins: true
}),
builtins(),
commonjs(),
babel(
Object.assign(
{
include: [
"node_modules/libp2p-crypto/**",
"node_modules/libp2p-crypto-secp256k1/**",
"node_modules/asn1.js/**",
"node_modules/pem-jwk/**",
"node_modules/multihashing-async/**",
"node_modules/multihashes/**",
"src/**"
]
},
babelrc()
)
),
globals()
],
output: {
format: "umd",
file: "dist/index.js"
}
};
I created a minimal repository to reproduce the error here:
https://github.com/simonvadee/rollup-libp2p/
When launching tests, the script is loaded in a browser (using karma) and the following error gets displayed
Uncaught TypeError: Cannot read property 'Reporter' of undefined
at /Users/simonvadee/test-rollup-libp2p/node_modules/pem-jwk/node_modules/asn1.js/lib/asn1/base/buffer.js:2:0 <- index.js:46811
What happens is that the script attempts to instantiate a variable from a module which has not yet been instantiated.
My guess is that rollup does not handle well this kind of imports ( from node_modules/asn1.js./lib/asn1/buffer.js
):
const Reporter = require('../base').Reporter;
when the directory structure is the following
lib
asn1
base
buffer.js
index.js
reporter.js
Is this a rollup issue or am I doing something wrong ?
@simonvadee can you update your repro? I see unresolved deps, missing karma, old versions of all packages.
yes well it has been more than a year now, so I moved to an other library instead. I'm closing this.