roughViz icon indicating copy to clipboard operation
roughViz copied to clipboard

Not working with Svelte = Cannot read property 'ownerDocument' of null

Open MintyMods opened this issue 6 years ago • 1 comments
trafficstars

The provided demo throws Cannot read property 'ownerDocument' of null when used as a Svelte component. See the following Repl showing the issue.

The reason seems to be (as covered in this video by Svelte Master) that the index.js is incorrect and contains the contents of what looks like a build / config issue:-

content of index.js as installed via npm

// modules are defined as an array
// [ module function, map of requires ]
//
// map of requires is short require name -> numeric require
//
// anything defined in a previous bundle is accessed via the
// orig method which is the require for previous bundles
parcelRequire = (function (modules, cache, entry, globalName) {
  // Save the require from previous bundle to this closure if any
  var previousRequire = typeof parcelRequire === 'function' && parcelRequire;
  var nodeRequire = typeof require === 'function' && require;

function newRequire(name, jumped) { if (!cache[name]) { if (!modules[name]) { // if we cannot find the module within our internal map or // cache jump to the current global require ie. the last bundle // that was added to the page. var currentRequire = typeof parcelRequire === 'function' && parcelRequire; if (!jumped && currentRequire) { return currentRequire(name, true); }

    // If there are other bundles on this page the require from the
    // previous one is saved to 'previousRequire'. Repeat this as
    // many times as there are bundles until the module is found or
    // we exhaust the require chain.
    if (previousRequire) {
      return previousRequire(name, true);
    }

    // Try the node require function if it exists.
    if (nodeRequire && typeof name === 'string') {
      return nodeRequire(name);
    }

    var err = new Error('Cannot find module \'' + name + '\'');
    err.code = 'MODULE_NOT_FOUND';
    throw err;
  }

  localRequire.resolve = resolve;
  localRequire.cache = {};

  var module = cache[name] = new newRequire.Module(name);

  modules[name][0].call(module.exports, localRequire, module, module.exports, this);
}

return cache[name].exports;

function localRequire(x){
  return newRequire(localRequire.resolve(x));
}

function resolve(x){
  return modules[name][1][x] || x;
}

}

function Module(moduleName) { this.id = moduleName; this.bundle = newRequire; this.exports = {}; }

newRequire.isParcelRequire = true; newRequire.Module = Module; newRequire.modules = modules; newRequire.cache = cache; newRequire.parent = previousRequire; newRequire.register = function (id, exports) { modules[id] = [function (require, module) { module.exports = exports; }, {}]; };

var error; for (var i = 0; i < entry.length; i++) { try { newRequire(entry[i]); } catch (e) { // Save first error but execute all entries if (!error) { error = e; } } }

if (entry.length) { // Expose entry point to Node, AMD or browser globals // Based on https://github.com/ForbesLindesay/umd/blob/master/template.js var mainExports = newRequire(entry[entry.length - 1]);

// CommonJS
if (typeof exports === "object" && typeof module !== "undefined") {
  module.exports = mainExports;

// RequireJS
} else if (typeof define === "function" && define.amd) {
 define(function () {
   return mainExports;
 });

// <script>
} else if (globalName) {
  this[globalName] = mainExports;
}

}

// Override the current require with this new one parcelRequire = newRequire;

if (error) { // throw error from earlier, after updating parcelRequire throw error; }

return newRequire; })({"../node_modules/parcel-bundler/src/builtins/bundle-url.js":[function(require,module,exports) { var bundleURL = null;

function getBundleURLCached() { if (!bundleURL) { bundleURL = getBundleURL(); }

return bundleURL; }

function getBundleURL() { // Attempt to find the URL of the current script and use that as the base URL try { throw new Error(); } catch (err) { var matches = ('' + err.stack).match(/(https?|file|ftp|chrome-extension|moz-extension)://[^)\n]+/g);

if (matches) {
  return getBaseURL(matches[0]);
}

}

return '/'; }

function getBaseURL(url) { return ('' + url).replace(/^((?:https?|file|ftp|chrome-extension|moz-extension)://.+)/[^/]+$/, '$1') + '/'; }

exports.getBundleURL = getBundleURLCached; exports.getBaseURL = getBaseURL; },{}],"../node_modules/parcel-bundler/src/builtins/css-loader.js":[function(require,module,exports) { var bundle = require('./bundle-url');

function updateLink(link) { var newLink = link.cloneNode();

newLink.onload = function () { link.remove(); };

newLink.href = link.href.split('?')[0] + '?' + Date.now(); link.parentNode.insertBefore(newLink, link.nextSibling); }

var cssTimeout = null;

function reloadCSS() { if (cssTimeout) { return; }

cssTimeout = setTimeout(function () { var links = document.querySelectorAll('link[rel="stylesheet"]');

for (var i = 0; i < links.length; i++) {
  if (bundle.getBaseURL(links[i].href) === bundle.getBundleURL()) {
    updateLink(links[i]);
  }
}

cssTimeout = null;

}, 50); }

module.exports = reloadCSS; },{"./bundle-url":"../node_modules/parcel-bundler/src/builtins/bundle-url.js"}],"../node_modules/parcel-bundler/src/builtins/hmr-runtime.js":[function(require,module,exports) { var global = arguments[3]; var OVERLAY_ID = 'parcel__error__overlay'; var OldModule = module.bundle.Module;

function Module(moduleName) { OldModule.call(this, moduleName); this.hot = { data: module.bundle.hotData, _acceptCallbacks: [], _disposeCallbacks: [], accept: function (fn) { this._acceptCallbacks.push(fn || function () {}); }, dispose: function (fn) { this._disposeCallbacks.push(fn); } }; module.bundle.hotData = null; }

module.bundle.Module = Module; var checkedAssets, assetsToAccept; var parent = module.bundle.parent;

if ((!parent || !parent.isParcelRequire) && typeof WebSocket !== 'undefined') { var hostname = "" || location.hostname; var protocol = location.protocol === 'https:' ? 'wss' : 'ws'; var ws = new WebSocket(protocol + '://' + hostname + ':' + "57962" + '/');

ws.onmessage = function (event) { checkedAssets = {}; assetsToAccept = []; var data = JSON.parse(event.data);

if (data.type === 'update') {
  var handled = false;
  data.assets.forEach(function (asset) {
    if (!asset.isNew) {
      var didAccept = hmrAcceptCheck(global.parcelRequire, asset.id);

      if (didAccept) {
        handled = true;
      }
    }
  }); // Enable HMR for CSS by default.

  handled = handled || data.assets.every(function (asset) {
    return asset.type === 'css' && asset.generated.js;
  });

  if (handled) {
    console.clear();
    data.assets.forEach(function (asset) {
      hmrApply(global.parcelRequire, asset);
    });
    assetsToAccept.forEach(function (v) {
      hmrAcceptRun(v[0], v[1]);
    });
  } else {
    window.location.reload();
  }
}

if (data.type === 'reload') {
  ws.close();

  ws.onclose = function () {
    location.reload();
  };
}

if (data.type === 'error-resolved') {
  console.log('[parcel] ✨ Error resolved');
  removeErrorOverlay();
}

if (data.type === 'error') {
  console.error('[parcel] 🚨  ' + data.error.message + '\n' + data.error.stack);
  removeErrorOverlay();
  var overlay = createErrorOverlay(data);
  document.body.appendChild(overlay);
}

}; }

function removeErrorOverlay() { var overlay = document.getElementById(OVERLAY_ID);

if (overlay) { overlay.remove(); } }

function createErrorOverlay(data) { var overlay = document.createElement('div'); overlay.id = OVERLAY_ID; // html encode message and stack trace

var message = document.createElement('div'); var stackTrace = document.createElement('pre'); message.innerText = data.error.message; stackTrace.innerText = data.error.stack; overlay.innerHTML = '

' + 'ERROR' + '🚨' + '
' + message.innerHTML + '
' + '
' + stackTrace.innerHTML + '
' + '
'; return overlay; }

function getParents(bundle, id) { var modules = bundle.modules;

if (!modules) { return []; }

var parents = []; var k, d, dep;

for (k in modules) { for (d in modules[k][1]) { dep = modules[k][1][d];

  if (dep === id || Array.isArray(dep) && dep[dep.length - 1] === id) {
    parents.push(k);
  }
}

}

if (bundle.parent) { parents = parents.concat(getParents(bundle.parent, id)); }

return parents; }

function hmrApply(bundle, asset) { var modules = bundle.modules;

if (!modules) { return; }

if (modules[asset.id] || !bundle.parent) { var fn = new Function('require', 'module', 'exports', asset.generated.js); asset.isNew = !modules[asset.id]; modules[asset.id] = [fn, asset.deps]; } else if (bundle.parent) { hmrApply(bundle.parent, asset); } }

function hmrAcceptCheck(bundle, id) { var modules = bundle.modules;

if (!modules) { return; }

if (!modules[id] && bundle.parent) { return hmrAcceptCheck(bundle.parent, id); }

if (checkedAssets[id]) { return; }

checkedAssets[id] = true; var cached = bundle.cache[id]; assetsToAccept.push([bundle, id]);

if (cached && cached.hot && cached.hot._acceptCallbacks.length) { return true; }

return getParents(global.parcelRequire, id).some(function (id) { return hmrAcceptCheck(global.parcelRequire, id); }); }

function hmrAcceptRun(bundle, id) { var cached = bundle.cache[id]; bundle.hotData = {};

if (cached) { cached.hot.data = bundle.hotData; }

if (cached && cached.hot && cached.hot._disposeCallbacks.length) { cached.hot._disposeCallbacks.forEach(function (cb) { cb(bundle.hotData); }); }

delete bundle.cache[id]; bundle(id); cached = bundle.cache[id];

if (cached && cached.hot && cached.hot._acceptCallbacks.length) { cached.hot._acceptCallbacks.forEach(function (cb) { cb(); });

return true;

} } },{}]},{},["../node_modules/parcel-bundler/src/builtins/hmr-runtime.js"], null) //# sourceMappingURL=/index.js.map

Work arounds:-

  • import roughViz from 'node_modules/rough-viz/dist/rough-viz.min.js';
  • rename the roughviz.min.js to index.js (which is probably related to issue #7)

Thanks for the great package. They are a great alternative to the standard charts and makes the charts fun :)

MintyMods avatar Oct 19 '19 14:10 MintyMods

Great information, thanks a bunch!

jwilber avatar Oct 22 '19 15:10 jwilber

Fixed in v2 import roughViz from "rough-viz";

jwilber avatar Oct 01 '23 04:10 jwilber