I have as part of my application
"use strict";
exports.makeNodeCompatible = function() {
if (typeof(DOMParser) === 'undefined') {
(function(global) {
var jsdom = require('jsdom'),
dom = new jsdom.JSDOM('');
global.DOMParser = dom.window.DOMParser;
})(global);
}
};
The purpose of making the code like this is that it would work both in node and in the browser. I like to keep this code being compatible with both environments (please consider this in answer).
- Source for inspiration: https://github.com/jkutianski/d3-templating/blob/29366e4c9dda7b044eb12ee325e8ee99b9f26ced/src/template.js#L3-L10 (this is not my code, just where i found this !)
- Source for recommendation jsdom: https://stackoverflow.com/a/55668667/1833322
When i run my app directly from unbundled sources node index.js --help
everything works well. When i bundle with parcel first i get an error ReferenceError: window is not defined
. I went into dist/index.js
(the bundled file) and manually found my application code and moved it to the end of this file. After that the dist version worked. This indicates to me that parcel didn’t put the code of my app and the jsdom in the right order.
Question:
- Can i either rewrite this code somehow so that parcel can pick up on the order of dependency? (and still keep browser compatibility)
- Can i manually tell parcel about the order of dependency in some configuration?
- Maybe my findings are incorrect and there is a different cause for
ReferenceError: window is not defined
? - If parcel does not support this is there an alternative bundler that i can try?