Skip to content

Commit

Permalink
Improve browser compatibility
Browse files Browse the repository at this point in the history
Fixes #28
  • Loading branch information
sindresorhus committed Sep 27, 2022
1 parent d9d9195 commit dccdb49
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
3 changes: 3 additions & 0 deletions home-directory-browser.js
@@ -0,0 +1,3 @@
const homeDirectory = '';

export default homeDirectory;
5 changes: 5 additions & 0 deletions home-directory.js
@@ -0,0 +1,5 @@
import os from 'node:os';

const homeDirectory = os.homedir().replace(/\\/g, '/');

export default homeDirectory;
5 changes: 2 additions & 3 deletions index.js
@@ -1,9 +1,8 @@
import os from 'node:os';
import escapeStringRegexp from 'escape-string-regexp';
import homeDirectory from '#home-directory';

const extractPathRegex = /\s+at.*[(\s](.*)\)?/;
const pathRegex = /^(?:(?:(?:node|node:[\w/]+|(?:(?:node:)?internal\/[\w/]*|.*node_modules\/(?:babel-polyfill|pirates)\/.*)?\w+)(?:\.js)?:\d+:\d+)|native)/;
const homeDir = typeof os.homedir === 'undefined' ? '' : os.homedir().replace(/\\/g, '/');

export default function cleanStack(stack, {pretty = false, basePath} = {}) {
const basePathRegex = basePath && new RegExp(`(at | \\()(file://)?${escapeStringRegexp(basePath.replace(/\\/g, '/'))}/?`, 'g');
Expand Down Expand Up @@ -41,7 +40,7 @@ export default function cleanStack(stack, {pretty = false, basePath} = {}) {
}

if (pretty) {
line = line.replace(extractPathRegex, (m, p1) => m.replace(p1, p1.replace(homeDir, '~')));
line = line.replace(extractPathRegex, (m, p1) => m.replace(p1, p1.replace(homeDirectory, '~')));
}

return line;
Expand Down
10 changes: 9 additions & 1 deletion package.json
Expand Up @@ -12,6 +12,12 @@
},
"type": "module",
"exports": "./index.js",
"imports": {
"#home-directory": {
"node": "./home-directory.js",
"default": "./home-directory-browser.js"
}
},
"types": "./index.d.ts",
"engines": {
"node": ">=14.16"
Expand All @@ -21,7 +27,9 @@
},
"files": [
"index.js",
"index.d.ts"
"index.d.ts",
"home-directory.js",
"home-directory-browser.js"
],
"keywords": [
"clean",
Expand Down

0 comments on commit dccdb49

Please sign in to comment.