Skip to content

Commit 61093a4

Browse files
committed
feat: make lib browser compatible
1 parent 6b5cb1a commit 61093a4

File tree

5 files changed

+336
-36
lines changed

5 files changed

+336
-36
lines changed

lib/options.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Omit } from "ast-types/types";
2+
import { getLineTerminator } from "./util";
23

34
/**
45
* All Recast API functions take second parameter with configuration options,
@@ -169,7 +170,7 @@ const defaults: Options = {
169170
tabWidth: 4,
170171
useTabs: false,
171172
reuseWhitespace: true,
172-
lineTerminator: require("os").EOL || "\n",
173+
lineTerminator: getLineTerminator(),
173174
wrapColumn: 74, // Aspirational for now.
174175
sourceFileName: null,
175176
sourceMapName: null,

lib/util.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ const SourceMapConsumer = sourceMap.SourceMapConsumer;
66
const SourceMapGenerator = sourceMap.SourceMapGenerator;
77
const hasOwn = Object.prototype.hasOwnProperty;
88

9+
export function getLineTerminator() {
10+
return isBrowser() ? "\n" : require("os").EOL || "\n";
11+
}
12+
13+
export function isBrowser() {
14+
return (
15+
typeof window !== "undefined" && typeof window.document !== "undefined"
16+
);
17+
}
18+
919
export function getOption(options: any, key: any, defaultValue: any) {
1020
if (options && hasOwn.call(options, key)) {
1121
return options[key];

0 commit comments

Comments
 (0)