Skip to content

Commit

Permalink
Make sure node related imports can be tree shaken
Browse files Browse the repository at this point in the history
  • Loading branch information
twojtasz committed May 23, 2019
1 parent 6a85f68 commit ed0ba18
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 13 deletions.
3 changes: 2 additions & 1 deletion docs/api-reference/io/overview-provider.md
Expand Up @@ -12,7 +12,8 @@ the factory.
## Example

```js
import {FileSource, XVIZProviderFactory} from '@xviz/io';
import {XVIZProviderFactory} from '@xviz/io';
import {FileSource} from '@xviz/io/node';

const root = '.';
const source = new FileSource(root);
Expand Down
3 changes: 2 additions & 1 deletion docs/api-reference/io/overview-writer.md
Expand Up @@ -6,7 +6,8 @@ XVIZ Writers provide an interface to write XVIZ Metadata and Frames.

```js
import {XVIZMetadataBuilder, XVIZBuilder} from '@xviz/builder';
import {XVIZBinaryWriter, FileSink} from '@xviz/io';
import {XVIZBinaryWriter}
import {FileSink} from '@xviz/io/node';

const sink = new FileSink('output-dir');
const xvizWriter = new XVIZBinaryWriter(sink);
Expand Down
6 changes: 4 additions & 2 deletions docs/api-reference/io/xviz-binary-writer.md
Expand Up @@ -6,7 +6,8 @@ The class `XVIZBinaryWriter` will output the GLB based binary format of XVIZ dat

```js
import {XVIZMetadataBuilder, XVIZBuilder} from '@xviz/builder';
import {XVIZBinaryWriter, FileSink} from '@xviz/io';
import {XVIZBinaryWriter} from '@xviz/io';
import {FileSink} from '@xviz/io/node';

const sink = new FileSink('output-dir');
const xvizWriter = new XVIZBinaryWriter(sink);
Expand All @@ -27,7 +28,8 @@ xvizWriter.close();
### Constructor

```js
import {FileSink, XVIZBinaryWriter} from '@xviz/io';
import {XVIZBinaryWriter} from '@xviz/io';
import {FileSink} from '@xviz/io/node';

const sink = new FileSink('output-dir');
const xvizWriter = new XVIZBinaryWriter(sink);
Expand Down
6 changes: 4 additions & 2 deletions docs/api-reference/io/xviz-format-writer.md
Expand Up @@ -9,7 +9,8 @@ The XVIZFormatWriter currently expects XVIZData and not a raw object.

```js
import {XVIZMetadataBuilder, XVIZBuilder} from '@xviz/builder';
import {XVIZFormatWriter, FileSink, XVIZFormat} from '@xviz/io';
import {XVIZFormatWriter, XVIZFormat} from '@xviz/io';
import {FileSink} from '@xviz/io/node';

const sink = new FileSink('output-dir');
const xvizWriter = new XVIZFormatWriter(sink, {format: XVIZFormat.BINARY_GLB});
Expand All @@ -30,7 +31,8 @@ xvizWriter.close();
### Constructor

```js
import {FileSink, XVIZFormatWriter, XVIZFormat} from '@xviz/io';
import {XVIZFormatWriter, XVIZFormat} from '@xviz/io';
import {FileSink} from '@xviz/io/node';

const sink = new FileSink('output-dir');
const xvizWriter = new XVIZBinaryWriter(sink, {format: XVIZFormat.BINARY_GLB});
Expand Down
6 changes: 4 additions & 2 deletions docs/api-reference/io/xviz-json-writer.md
Expand Up @@ -6,7 +6,8 @@ The class `XVIZJSONWriter` will output the JSON format of XVIZ data.

```js
import {XVIZMetadataBuilder, XVIZBuilder} from '@xviz/builder';
import {XVIZJSONWriter, FileSink} from '@xviz/io';
import {XVIZJSONWriter} from '@xviz/io';
import {FileSink} from '@xviz/io/node';

const sink = new FileSink('output-dir');
const xvizWriter = new XVIZJSONWriter(sink);
Expand All @@ -27,7 +28,8 @@ xvizWriter.close();
### Constructor

```js
import {FileSink, XVIZJSONWriter} from '@xviz/io';
import {XVIZJSONWriter} from '@xviz/io';
import {FileSink} from '@xviz/io/node';

const sink = new FileSink('output-dir');
const xvizWriter = new XVIZJSONWriter(sink);
Expand Down
3 changes: 2 additions & 1 deletion docs/api-reference/io/xviz-provider-factory.md
Expand Up @@ -6,7 +6,8 @@ implementation that handles the underlying data source.
## Example

```js
import {FileSource, XVIZProviderFactory} from '@xviz/io';
import {XVIZProviderFactory} from '@xviz/io';
import {FileSource} from '@xviz/io/node';

const root = '.';
const source = new FileSource(root);
Expand Down
14 changes: 14 additions & 0 deletions modules/io/node.js
@@ -0,0 +1,14 @@
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
module.exports = require('./dist/es5/node');
7 changes: 6 additions & 1 deletion modules/io/package.json
Expand Up @@ -11,7 +11,8 @@
"module": "dist/esm/index.js",
"esnext": "dist/es6/index.js",
"files": [
"dist"
"dist",
"node.js"
],
"dependencies": {
"@loaders.gl/draco": "^0.7.1",
Expand All @@ -29,6 +30,10 @@
"build": "npm run clean && npm run build-es6 && npm run build-esm && npm run build-es5",
"cover": "NODE_ENV=test BABEL_ENV=cover npx nyc node ../../test/start.js cover"
},
"browser": {
"./src/io/fs-sink.js": false,
"./src/io/fs-source.js": false
},
"engines": {
"node": ">= 8",
"npm": ">= 5",
Expand Down
2 changes: 0 additions & 2 deletions modules/io/src/index.js
Expand Up @@ -18,8 +18,6 @@ export {XVIZFormatWriter} from './writers/xviz-format-writer';
export {XVIZJSONReader} from './readers/xviz-json-reader';
export {XVIZBinaryReader} from './readers/xviz-binary-reader';

export {FileSink} from './io/fs-sink';
export {FileSource} from './io/fs-source';
export {MemorySourceSink} from './io/memory-source-sink';

export {TextEncoder, TextDecoder} from './common/text-encoding';
Expand Down
15 changes: 15 additions & 0 deletions modules/io/src/node.js
@@ -0,0 +1,15 @@
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
export {FileSink} from './io/fs-sink';
export {FileSource} from './io/fs-source';
2 changes: 1 addition & 1 deletion modules/server/src/server/xviz-provider-handler.js
Expand Up @@ -14,7 +14,7 @@
import {default as path} from 'path';

import {XVIZProviderSession} from './xviz-provider-session';
import {FileSource} from '@xviz/io';
import {FileSource} from '@xviz/io/node';

// Setup the source and return an XVIZSession or null
export class XVIZProviderHandler {
Expand Down
1 change: 1 addition & 0 deletions test/modules/io/index.js
Expand Up @@ -15,3 +15,4 @@ import './writers';
import './common';
import './readers';
import './providers';
import './io';
15 changes: 15 additions & 0 deletions test/modules/io/io/index.js
@@ -0,0 +1,15 @@
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import './node-only.spec.js';
31 changes: 31 additions & 0 deletions test/modules/io/io/node-only.spec.js
@@ -0,0 +1,31 @@
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/* eslint-disable camelcase */
import test from 'tape-catch';
import {FileSink, FileSource} from '@xviz/io/node';

const isBrowser = typeof window !== 'undefined';

test('Node-Only imports#file system related', t => {
if (isBrowser) {
t.equals(FileSink, undefined, 'FileSink is undefined in the browser');
t.equals(FileSource, undefined, 'FileSource is undefined in the browser');
} else {
t.notEquals(FileSink, undefined, 'FileSink is defined in node');
t.notEquals(FileSource, undefined, 'FileSource is defined node');
}

t.end();
});

0 comments on commit ed0ba18

Please sign in to comment.