node_preamble.dart icon indicating copy to clipboard operation
node_preamble.dart copied to clipboard

Webpack and jsdom detection issue

Open emerikool opened this issue 3 years ago • 7 comments

We are facing an issue on dart sass due to environement detection. With jsdom environment is not detected as a Node one and imports are not resolved.

Jsdom emulate a web browser by adding a window object that makes failed node detection: https://github.com/mbullington/node_preamble.dart/blob/20c5bee0bbe6fefead1509bbdbbac06072b778e2/lib/preamble.js#L41

emerikool avatar Apr 08 '21 08:04 emerikool

@emerikool Perhaps typeof process != "undefined" && process.browser? Should be careful and test with all the different types of electron first though! (Context isolation=true, nodeIntegration=false, etc)

Mstrodl avatar Apr 08 '21 23:04 Mstrodl

What do you mean by process.browser ? We use mochapack that bundle with webpack and run mocha + jsdom. In our case process.browser is undefined, perhaps best approach is to add a custom property to process object to force detection ?

emerikool avatar Apr 09 '21 13:04 emerikool

@emerikool you can check: process.platform=undefined OR process.browser (both seem used?)

Mstrodl avatar Apr 09 '21 14:04 Mstrodl

process.platform is defined (win32) and process.browser is undefined

emerikool avatar Apr 09 '21 14:04 emerikool

@emerikool Seems like something you should figure out with the mochapack people maybe? process definitely isn't polyfilled by default...

Mstrodl avatar Apr 09 '21 14:04 Mstrodl

I don't think it's related to mochapack, same issue should occur with webpack and any emulated browser. In case of emulated browser, dartNodeIsActuallyNode needs to be overriden like electron: https://github.com/mbullington/node_preamble.dart/blob/20c5bee0bbe6fefead1509bbdbbac06072b778e2/lib/preamble.js#L50 by replacing process.versions.hasOwnProperty('electron') with something matching this particular case. I did not find a valid property in process object to fit this use case that's why I suggest to add a custom one like: if ("undefined" !== typeof process && process.hasOwnProperty('forceNode') && process.versions && process.versions.hasOwnProperty('node')) {

emerikool avatar Apr 09 '21 15:04 emerikool

Having encountered this problem, my current workaround is set process.versions.electron = '' to force dartNodeIsActuallyNode = true

fangbinwei avatar Apr 27 '21 11:04 fangbinwei