From 8fbce4ae3f39e732a655b10df4d2f24abec5fbad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20Kooi?= Date: Sun, 12 May 2019 11:03:07 +0200 Subject: [PATCH] Use the package.json "browser" field to toggle Node.js-specific functionality (#145) --- package.json | 3 +++ source/utils/infer-label.browser.ts | 2 ++ source/utils/infer-label.ts | 7 ++----- source/utils/node/fs.ts | 4 ---- source/utils/node/require.ts | 13 ------------- 5 files changed, 7 insertions(+), 22 deletions(-) create mode 100644 source/utils/infer-label.browser.ts delete mode 100644 source/utils/node/fs.ts delete mode 100644 source/utils/node/require.ts diff --git a/package.json b/package.json index 3dec915..454588f 100644 --- a/package.json +++ b/package.json @@ -72,6 +72,9 @@ "webpack": "^4.29.0", "webpack-cli": "^3.1.2" }, + "browser": { + "./dist/source/utils/infer-label.js": "./dist/source/utils/infer-label.browser.js" + }, "types": "dist", "sideEffects": false, "ava": { diff --git a/source/utils/infer-label.browser.ts b/source/utils/infer-label.browser.ts new file mode 100644 index 0000000..af3762f --- /dev/null +++ b/source/utils/infer-label.browser.ts @@ -0,0 +1,2 @@ +// Dummy in the browser. +export const inferLabel = () => {}; diff --git a/source/utils/infer-label.ts b/source/utils/infer-label.ts index 69f273e..d2e3b24 100644 --- a/source/utils/infer-label.ts +++ b/source/utils/infer-label.ts @@ -1,5 +1,5 @@ import {CallSite} from 'callsites'; -import lazyFS from './node/fs'; +import * as fs from 'fs'; import isValidIdentifier from './is-valid-identifier'; import isNode from './node/is-node'; @@ -18,9 +18,6 @@ export const inferLabel = (callsites: CallSite[]) => { return; } - // Load the lazy `fs` module - const fs = lazyFS(); - // Grab the stackframe with the `ow` function call const functionCallStackFrame = callsites[1]; @@ -35,7 +32,7 @@ export const inferLabel = (callsites: CallSite[]) => { let content: string[] = []; try { - content = (fs.readFileSync(fileName, 'utf8') as string).split('\n'); + content = fs.readFileSync(fileName, 'utf8').split('\n'); } catch { return; } diff --git a/source/utils/node/fs.ts b/source/utils/node/fs.ts deleted file mode 100644 index 81a2bc7..0000000 --- a/source/utils/node/fs.ts +++ /dev/null @@ -1,4 +0,0 @@ -import nodeRequire from './require'; - -// Re-export the Node.js `fs` module lazily to make sure it doesn't get bundled with front-end apps -export default () => nodeRequire('fs'); diff --git a/source/utils/node/require.ts b/source/utils/node/require.ts deleted file mode 100644 index 2f1c6f9..0000000 --- a/source/utils/node/require.ts +++ /dev/null @@ -1,13 +0,0 @@ -let customRequire: (packageName: string) => any; - -try { - // Export `__non_webpack_require__` in Webpack environments to make sure it doesn't bundle modules loaded via this method - customRequire = (global as any).__non_webpack_require__ === 'function' - ? (global as any).__non_webpack_require__ - : eval('require'); // tslint:disable-line:no-eval -} catch { - // Use a noop in case both `__non_webpack_require__` and `require` does not exist - customRequire = () => {}; // tslint:disable-line:no-empty -} - -export default customRequire;