Skip to content

Commit

Permalink
Refine Node.js and global check to also support service workers
Browse files Browse the repository at this point in the history
Use globalThis which is the standard way to access the global this

Check for Node.js explicitly to eliminate specific service worker
and Electron checks
  • Loading branch information
mnito committed Apr 9, 2022
1 parent d278def commit a3c9735
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 38 deletions.
23 changes: 4 additions & 19 deletions lib/preamble.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
library node_preamble;

final _minified = r"""var dartNodePreambleSelf="undefined"!=typeof global?Object.create(global):window,self=dartNodePreambleSelf;if(self.scheduleImmediate="undefined"!=typeof setImmediate?function(e){setImmediate(e)}:function(e){setTimeout(e,0)},"undefined"!=typeof require)self.require=require;if("undefined"!=typeof exports)self.exports=exports;if("undefined"!=typeof process)self.process=process;if("undefined"!=typeof __dirname)self.__dirname=__dirname;if("undefined"!=typeof __filename)self.__filename=__filename;if("undefined"!=typeof Buffer)self.Buffer=Buffer;var dartNodeIsActuallyNode=!dartNodePreambleSelf.window;try{if("undefined"!=typeof WorkerGlobalScope&&dartNodePreambleSelf instanceof WorkerGlobalScope)dartNodeIsActuallyNode=!1;if("undefined"!=typeof process&&process.versions&&process.versions.hasOwnProperty("electron")&&process.versions.hasOwnProperty("node"))dartNodeIsActuallyNode=!0}catch(e){}if(dartNodeIsActuallyNode){var url=("undefined"!=typeof __webpack_require__?__non_webpack_require__:require)("url");Object.defineProperty(self,"location",{value:{get href(){if(url.pathToFileURL)return url.pathToFileURL(process.cwd()).href+"/";else return"file://"+function(){var e=process.cwd();if("win32"!=process.platform)return e;else return"/"+e.replace(/\\/g,"/")}()+"/"}}}),function(){function e(){try{throw new Error}catch(f){var e=f.stack,r=new RegExp("^ *at [^(]*\\((.*):[0-9]*:[0-9]*\\)$","mg"),o=null;do{var t=r.exec(e);if(null!=t)o=t}while(null!=t);return o[1]}}var r=null;Object.defineProperty(self,"document",{value:{get currentScript(){if(null==r)r={src:e()};return r}}})}(),self.dartDeferredLibraryLoader=function(e,r,o){try{load(e),r()}catch(e){o(e)}}}""";
final _minified = r"""var dartNodeIsActuallyNode="undefined"!=typeof process&&(process.versions||{}).hasOwnProperty("node"),self=dartNodeIsActuallyNode?Object.create(globalThis):globalThis;if(self.scheduleImmediate="undefined"!=typeof setImmediate?function(e){setImmediate(e)}:function(e){setTimeout(e,0)},"undefined"!=typeof require)self.require=require;if("undefined"!=typeof exports)self.exports=exports;if("undefined"!=typeof process)self.process=process;if("undefined"!=typeof __dirname)self.__dirname=__dirname;if("undefined"!=typeof __filename)self.__filename=__filename;if("undefined"!=typeof Buffer)self.Buffer=Buffer;if(dartNodeIsActuallyNode){var url=("undefined"!=typeof __webpack_require__?__non_webpack_require__:require)("url");Object.defineProperty(self,"location",{value:{get href(){if(url.pathToFileURL)return url.pathToFileURL(process.cwd()).href+"/";else return"file://"+function(){var e=process.cwd();if("win32"!=process.platform)return e;else return"/"+e.replace(/\\/g,"/")}()+"/"}}}),function(){function e(){try{throw new Error}catch(n){var e=n.stack,r=new RegExp("^ *at [^(]*\\((.*):[0-9]*:[0-9]*\\)$","mg"),f=null;do{var t=r.exec(e);if(null!=t)f=t}while(null!=t);return f[1]}}var r=null;Object.defineProperty(self,"document",{value:{get currentScript(){if(null==r)r={src:e()};return r}}})}(),self.dartDeferredLibraryLoader=function(e,r,f){try{load(e),r()}catch(e){f(e)}}}""";

final _normal = r"""
var dartNodeIsActuallyNode = typeof process !== "undefined" && (process.versions || {}).hasOwnProperty('node');
// make sure to keep this as 'var'
// we don't want block scoping
var dartNodePreambleSelf = typeof global !== "undefined" ? Object.create(global) : window;
var self = dartNodePreambleSelf;
var self = dartNodeIsActuallyNode ? Object.create(globalThis) : globalThis;
self.scheduleImmediate = typeof setImmediate !== "undefined"
? function (cb) {
Expand Down Expand Up @@ -47,20 +46,6 @@ if (typeof Buffer !== "undefined") {
// if we're running in a browser, Dart supports most of this out of box
// make sure we only run these in Node.js environment
var dartNodeIsActuallyNode = !dartNodePreambleSelf.window
try {
// Check if we're in a Web Worker instead.
if ("undefined" !== typeof WorkerGlobalScope && dartNodePreambleSelf instanceof WorkerGlobalScope) {
dartNodeIsActuallyNode = false;
}
// Check if we're in Electron, with Node.js integration, and override if true.
if ("undefined" !== typeof process && process.versions && process.versions.hasOwnProperty('electron') && process.versions.hasOwnProperty('node')) {
dartNodeIsActuallyNode = true;
}
} catch(e) {}
if (dartNodeIsActuallyNode) {
// This line is to:
// 1) Prevent Webpack from bundling.
Expand Down
21 changes: 3 additions & 18 deletions lib/preamble.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
var dartNodeIsActuallyNode = typeof process !== "undefined" && (process.versions || {}).hasOwnProperty('node');

// make sure to keep this as 'var'
// we don't want block scoping

var dartNodePreambleSelf = typeof global !== "undefined" ? Object.create(global) : window;

var self = dartNodePreambleSelf;
var self = dartNodeIsActuallyNode ? Object.create(globalThis) : globalThis;

self.scheduleImmediate = typeof setImmediate !== "undefined"
? function (cb) {
Expand Down Expand Up @@ -42,20 +41,6 @@ if (typeof Buffer !== "undefined") {
// if we're running in a browser, Dart supports most of this out of box
// make sure we only run these in Node.js environment

var dartNodeIsActuallyNode = !dartNodePreambleSelf.window

try {
// Check if we're in a Web Worker instead.
if ("undefined" !== typeof WorkerGlobalScope && dartNodePreambleSelf instanceof WorkerGlobalScope) {
dartNodeIsActuallyNode = false;
}

// Check if we're in Electron, with Node.js integration, and override if true.
if ("undefined" !== typeof process && process.versions && process.versions.hasOwnProperty('electron') && process.versions.hasOwnProperty('node')) {
dartNodeIsActuallyNode = true;
}
} catch(e) {}

if (dartNodeIsActuallyNode) {
// This line is to:
// 1) Prevent Webpack from bundling.
Expand Down
2 changes: 1 addition & 1 deletion lib/preamble.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a3c9735

Please sign in to comment.