Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Cut deprecated exports/methods to save a few kilobytes #250

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 36 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,22 +1,50 @@
build/*
dist/*
dist-esm/*
dist-es6/*
dist-min/*
dist-buble/*
**/dist/*
website*/public/*
dist.min.js

node_modules/
coverage/
_docs/
.nyc_output/
.reify-cache/

coverage/
node_modules/
npm-debug.log
stats.json
.reify-cache
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this from and why do we need it?

.cache
jscpd-report.md

tsconfig.tsbuildinfo

test/data/test.png

package-lock.json
lerna-debug.log

*/**/yarn.lock
yarn-error.log
!website/yarn.lock

.yarn/*
!.yarn/cache
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions

tsconfig.tsbuildinfo

# editor files
.project
.DS_Store
.idea
*.zip
*.rar
TODO

.docusaurus

# editor files
.idea

1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodeLinker: node-modules
15 changes: 10 additions & 5 deletions modules/env/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
export {VERSION} from './utils/globals';
// Extract injected version from package.json (injected by babel plugin)
// @ts-expect-error
export const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'untranspiled source';

// ENVIRONMENT
export {self, window, global, document, process, console} from './lib/globals';
export {default as isBrowser, isBrowserMainThread} from './lib/is-browser';
export {default as getBrowser, isMobile} from './lib/get-browser';
export {default as isElectron} from './lib/is-electron';
export {isBrowser} from './lib/is-browser';
export {getBrowser, isMobile} from './lib/get-browser';
export {isElectron} from './lib/is-electron';

// ENVIRONMENT'S ASSERT IS 5-15KB, SO WE PROVIDE OUR OWN
export {default as assert} from './utils/assert';
export {assert} from './utils/assert';

// TODO - wish we could just export a constant
// export const isBrowser = checkIfBrowser();
25 changes: 8 additions & 17 deletions modules/env/src/lib/get-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
// This function is needed in initialization stages,
// make sure it can be imported in isolation

import isBrowser from './is-browser';
import isElectron from './is-electron';
import {isBrowser} from './is-browser';
import {isElectron} from './is-electron';
import {navigator} from './globals';

declare global {
Expand All @@ -31,45 +31,36 @@ declare global {
var mozInnerScreenX: number; // eslint-disable-line no-var
}

const window = globalThis;

export function isMobile(): boolean {
return typeof window.orientation !== 'undefined';
return typeof globalThis.orientation !== 'undefined';
}

// Simple browser detection
// `mockUserAgent` parameter allows user agent to be overridden for testing
/* eslint-disable complexity */
export default function getBrowser(
export function getBrowser(
mockUserAgent?: string
): 'Node' | 'Electron' | 'Chrome' | 'Firefox' | 'Safari' | 'Edge' | 'IE' | 'Unknown' {
): 'Node' | 'Electron' | 'Chrome' | 'Firefox' | 'Safari' | 'Edge' | 'Unknown' {
if (!mockUserAgent && !isBrowser()) {
return 'Node';
}

if (isElectron(mockUserAgent)) {
return 'Electron';
}

const userAgent = mockUserAgent || navigator.userAgent || '';
// const appVersion = navigator_.appVersion || '';

// NOTE: Order of tests matter, as many agents list Chrome etc.
if (userAgent.indexOf('Edge') > -1) {
return 'Edge';
}
const isMSIE = userAgent.indexOf('MSIE ') !== -1;
const isTrident = userAgent.indexOf('Trident/') !== -1;
if (isMSIE || isTrident) {
return 'IE';
}
if (window.chrome) {
if (globalThis.chrome) {
return 'Chrome';
}
if (window.safari) {
if (globalThis.safari) {
return 'Safari';
}
if (window.mozInnerScreenX) {
if (globalThis.mozInnerScreenX) {
return 'Firefox';
}
return 'Unknown';
Expand Down
6 changes: 2 additions & 4 deletions modules/env/src/lib/globals.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
// Do not name these variables the same as the global objects - will break bundling
const global_ = globalThis;
// eslint-disable-next-line consistent-this
const self_ = globalThis.self || globalThis.window || globalThis.global;
const window_ = (globalThis.window || globalThis.self || globalThis.global) as unknown as Window;
const window_ = globalThis as unknown as Window;
const document_ = globalThis.document || ({} as Document);
const process_ = globalThis.process || {};
const console_ = globalThis.console;
const navigator_ = globalThis.navigator || ({} as Navigator);

export {
global_ as global,
self_ as self,
global_ as self,
window_ as window,
document_ as document,
process_ as process,
Expand Down
17 changes: 5 additions & 12 deletions modules/env/src/lib/is-browser.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
// This function is needed in initialization stages,
// make sure it can be imported in isolation

import isElectron from './is-electron';

export default function isBrowser(): boolean {
// Check if in browser by duck-typing Node context
const isNode =
// @ts-expect-error
typeof process === 'object' && String(process) === '[object process]' && !process.browser;
import {isElectron} from './is-electron';

/** Check if in browser by duck-typing Node context */
export function isBrowser(): boolean {
// @ts-expect-error
const isNode = typeof process === 'object' && !process?.browser;
return !isNode || isElectron();
}

// document does not exist on worker thread
export function isBrowserMainThread(): boolean {
return isBrowser() && typeof document !== 'undefined';
}
26 changes: 7 additions & 19 deletions modules/env/src/lib/is-electron.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
// based on https://github.com/cheton/is-electron
// https://github.com/electron/electron/issues/2288
/* eslint-disable complexity */
export default function isElectron(mockUserAgent?: string): boolean {
export function isElectron(mockUserAgent?: string): boolean {
// Renderer process
if (
typeof window !== 'undefined' &&
typeof window.process === 'object' &&
// @ts-expect-error
window.process.type === 'renderer'
) {
// @ts-expect-error
if (typeof window !== 'undefined' && window.process?.type === 'renderer') {
return true;
}
// Main process
if (
typeof process !== 'undefined' &&
typeof process.versions === 'object' &&
// eslint-disable-next-line
Boolean(process.versions['electron'])
) {
// eslint-disable-next-line
if (typeof process !== 'undefined' && Boolean(process.versions?.['electron'])) {
return true;
}
// Detect the user agent when the `nodeIntegration` option is set to true
const realUserAgent =
typeof navigator === 'object' && typeof navigator.userAgent === 'string' && navigator.userAgent;
const realUserAgent = typeof navigator !== 'undefined' && navigator.userAgent;
const userAgent = mockUserAgent || realUserAgent;
if (userAgent && userAgent.indexOf('Electron') >= 0) {
return true;
}
return false;
return Boolean(userAgent && userAgent.indexOf('Electron') >= 0);
}
2 changes: 1 addition & 1 deletion modules/env/src/utils/assert.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function assert(condition: unknown, message?: string) {
export function assert(condition: unknown, message?: string) {
if (!condition) {
throw new Error(message || 'Assertion failed');
}
Expand Down
9 changes: 0 additions & 9 deletions modules/env/src/utils/globals.ts

This file was deleted.

12 changes: 0 additions & 12 deletions modules/env/test/lib/get-browser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@ import test from 'tape-promise/tape';
import {getBrowser} from '@probe.gl/env';

test('getBrowser', t => {
t.equal(
getBrowser('Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)'),
'IE',
'should return IE for IE 10'
);

t.equal(
getBrowser('Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko'),
'IE',
'should return IE for IE 11'
);

t.equal(
getBrowser(
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0'
Expand Down
58 changes: 4 additions & 54 deletions modules/log/src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* eslint-disable no-console */
import {VERSION, isBrowser} from '@probe.gl/env';
import {LocalStorage} from './utils/local-storage';
import {formatImage, formatTime, leftPad} from './utils/formatters';
import {formatTime, leftPad} from './utils/formatters';
import {addColor} from './utils/color';
import {autobind} from './utils/autobind';
import assert from './utils/assert';
Expand Down Expand Up @@ -156,7 +156,9 @@ export class Log {
// Unconditional logging

assert(condition: unknown, message?: string): asserts condition {
assert(condition, message);
if (!condition) {
throw new Error(message || 'Assertion failed');
}
}

/** Warn, but only once, no console flooding */
Expand Down Expand Up @@ -233,28 +235,6 @@ in a later version. Use \`${newUsage}\` instead`);
return noop;
}

/** logs an image under Chrome */
image({
logLevel,
priority,
image,
message = '',
scale = 1
}: {
logLevel?: number;
priority?: number;
image: any;
message?: string;
scale?: number;
}): LogFunction {
if (!this._shouldLog(logLevel || priority)) {
return noop;
}
return isBrowser()
? logImageInBrowser({image, message, scale})
: logImageInNode({image, message, scale});
}

time(logLevel, message) {
return this._getLogFunction(logLevel, message, console.time ? console.time : console.info);
}
Expand Down Expand Up @@ -457,36 +437,6 @@ function decorateMessage(id, message, opts) {
return message;
}

/** @deprecated Function removed */
function logImageInNode({image, message = '', scale = 1}) {
console.warn('removed');
return noop;
}

function logImageInBrowser({image, message = '', scale = 1}) {
if (typeof image === 'string') {
const img = new Image();
img.onload = () => {
const args = formatImage(img, message, scale);
console.log(...args);
};
img.src = image;
return noop;
}
const element = image.nodeName || '';
if (element.toLowerCase() === 'img') {
console.log(...formatImage(image, message, scale));
return noop;
}
if (element.toLowerCase() === 'canvas') {
const img = new Image();
img.onload = () => console.log(...formatImage(img, message, scale));
img.src = image.toDataURL();
return noop;
}
return noop;
}

function getTableHeader(table: Table): string {
for (const key in table) {
for (const title in table[key]) {
Expand Down
26 changes: 0 additions & 26 deletions modules/log/src/utils/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,29 +74,3 @@ function formatArrayValue(v: any, options: FormatValueOptions) {
const terminator = v.length > maxElts ? '...' : ']';
return `${string}${terminator}`;
}

/**
* Log an image to the console (uses browser specific console formatting styles)
* Inspired by https://github.com/hughsk/console-image (MIT license)
*/
export function formatImage(image: any, message: string, scale: number, maxWidth: number = 600) {
const imageUrl = image.src.replace(/\(/g, '%28').replace(/\)/g, '%29');

if (image.width > maxWidth) {
scale = Math.min(scale, maxWidth / image.width);
}

const width = image.width * scale;
const height = image.height * scale;

const style = [
'font-size:1px;',
`padding:${Math.floor(height / 2)}px ${Math.floor(width / 2)}px;`,
`line-height:${height}px;`,
`background:url(${imageUrl});`,
`background-size:${width}px ${height}px;`,
'color:transparent;'
].join('');

return [`${message} %c+`, style];
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@types/tape-promise": "^4.0.1",
"jsdoc-to-markdown": "^3.0.0",
"jsdom": "^16.5.0",
"ocular-dev-tools": "2.0.0-alpha.26",
"ocular-dev-tools": "2.0.0-alpha.28",
"pre-commit": "^1.2.2",
"puppeteer": "^22.0.0",
"react-dom": "^16.13.1",
Expand All @@ -44,5 +44,8 @@
"pre-commit": [
"test-fast"
],
"dependencies": {}
"volta": {
"node": "20.11.1",
"yarn": "4.1.1"
}
}
Loading
Loading