Skip to content

Instantly share code, notes, and snippets.

@jonathantneal
Created May 1, 2019 01:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathantneal/7ee2c441a4450eeebdf0fad7a90df32e to your computer and use it in GitHub Desktop.
Save jonathantneal/7ee2c441a4450eeebdf0fad7a90df32e to your computer and use it in GitHub Desktop.
babel-plugin-transform-globals: How I generate browserGlobals.js
  1. In each browser, I get a JSON-stringified array of every property name on window except itself. I place these names into files like name.chrome.json or name.firefox.json.
<!doctype html>
<body>
<script>
(function () {
	const windowNames = Object.getOwnPropertyNames(window).filter(name => name !== 'window');
	const textarea = document.createElement('textarea');
	textarea.value = JSON.stringify(windowNames);
	document.body.appendChild(textarea);
	textarea.focus();
	textarea.select();
})();
</script>
  1. In Node, I import and combine all of these names, only removing those names which also appear in Node’s global.
const nodeNames = Object.getOwnPropertyNames(global);
const names = [
	...new Set(
		[].concat(
			require('./names.chrome'),
			require('./names.firefox'),
			require('./names.safari')
		)
	)
].filter(
	name => !nodeNames.includes(name)
).sort();
  1. In Node, I write these combined names as a default export to browserGlobals.js.
require('fs').writeFileSync(
	'browserGlobals.js',
	`export default ${JSON.stringify(names, null, '\t').replace(/"/g, `'`)};\n`
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment