mbullington / node_preamble.dart

:memo: Better node.js preamble for dart2js, use it in your build system.
Other
19 stars 12 forks source link

Webpack and jsdom detection issue #26

Open emerikool opened 3 years ago

emerikool commented 3 years ago

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

Mstrodl commented 3 years ago

@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)

emerikool commented 3 years ago

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 ?

Mstrodl commented 3 years ago

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

emerikool commented 3 years ago

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

Mstrodl commented 3 years ago

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

emerikool commented 3 years ago

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')) {

fangbinwei commented 3 years ago

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