Skip to content
Closed
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ Icon
coverage
build
docs
dist
lib
out-tsc
tmp
17 changes: 0 additions & 17 deletions .npmignore

This file was deleted.

36 changes: 11 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,31 @@


## Installation
You can install this package from [NPM](https://www.npmjs.com):
You can install this package from [NPM](https://www.npmjs.com/package/file-selector):
```bash
npm add file-selector
npm install file-selector
```

Or with [Yarn](https://yarnpkg.com/en):
Or with [Yarn](https://yarnpkg.com/package/file-selector):
```bash
yarn add file-selector
```

### CDN
For CDN, you can use [unpkg](https://unpkg.com):
If you need a CDN, you can use [Skypack](https://www.skypack.dev/view/file-selector):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Skypack is an optimized CDN for serving NPM packages that use the ECMAScript module system. This allows them to be imported in scripts as outlined below, no globals needed!

```html
<script type="module">
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's probably worth mentioning https://caniuse.com/es6-module.

import {fromEvent} from 'https://cdn.skypack.dev/file-selector';

[https://unpkg.com/file-selector/dist/bundles/file-selector.umd.min.js](https://unpkg.com/file-selector/dist/bundles/file-selector.umd.min.js)

The global namespace for file-selector is `fileSelector`:
```js
const {fromEvent} = fileSelector;
document.addEventListener('drop', async evt => {
document.addEventListener('drop', async evt => {
const files = await fromEvent(evt);
console.log(files);
});
});
</script>
```


## Usage

### ES6
Convert a [DragEvent](https://developer.mozilla.org/en-US/docs/Web/API/DragEvent) to File objects:
```ts
import {fromEvent} from 'file-selector';
Expand Down Expand Up @@ -80,25 +77,14 @@ console.log(files);
```
**NOTE** The above is experimental and subject to change.

### CommonJS
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Since CommonJS is no longer supported I have removed this section.

Copy link
Collaborator

Choose a reason for hiding this comment

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

When has this happened? And what will happen to clients that still want to use this lib but have not updated to modern JS?

Convert a `DragEvent` to File objects:
```ts
const {fromEvent} = require('file-selector');
document.addEventListener('drop', async evt => {
const files = await fromEvent(evt);
console.log(files);
});
```


## Browser Support
Most browser support basic File selection with drag 'n' drop or file input:
* [File API](https://developer.mozilla.org/en-US/docs/Web/API/File#Browser_compatibility)
* [Drag Event](https://developer.mozilla.org/en-US/docs/Web/API/DragEvent#Browser_compatibility)
* [DataTransfer](https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer#Browser_compatibility)
* [`<input type="file">`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#Browser_compatibility)

For folder drop we use the [FileSystem API](https://developer.mozilla.org/en-US/docs/Web/API/FileSystem) which has very limited support:
For folder drop we use the [FileSystem API](https://developer.mozilla.org/en-US/docs/Web/API/FileSystem):
* [DataTransferItem.getAsFile()](https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem/getAsFile#Browser_compatibility)
* [DataTransferItem.webkitGetAsEntry()](https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem/webkitGetAsEntry#Browser_compatibility)
* [FileSystemEntry](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemEntry#Browser_compatibility)
Expand Down
27 changes: 0 additions & 27 deletions jest.config.js

This file was deleted.

21 changes: 21 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { InitialOptionsTsJest } from 'ts-jest';
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The Jest configuration is now written in TypeScript as well!


const config: InitialOptionsTsJest = {
testEnvironment: 'jsdom',
// See: https://kulshekhar.github.io/ts-jest/docs/guides/esm-support
preset: 'ts-jest/presets/default-esm',
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1'
},
transform: {
'^.+\\.tsx?$': [
'ts-jest',
{
useESM: true,
tsconfig: 'tsconfig.test.json'
}
]
}
};

export default config;
58 changes: 18 additions & 40 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
"name": "file-selector",
"version": "0.0.0-development",
"description": "Convert DataTransfer object to a list of File objects",
"main": "./dist/index.js",
"module": "./dist/es5/index.js",
"es2015": "./dist/es2015/index.js",
"typings": "./dist/index.d.ts",
"type": "module",
"main": "./lib/index.js",
"exports": "./lib/index.js",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This allows more modern tooling such as Vite to optimize the package even further. More info in the Node.js docs.

Copy link
Collaborator

Choose a reason for hiding this comment

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

So to understand, the module, es2015 and typings have been deprecated and no longer in use by other build systems?

Copy link
Collaborator

Choose a reason for hiding this comment

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

My only concern, as mentioned previously, is that clients/builders that are not modern will stop working post this change.

"types": "./lib/index.d.ts",
"sideEffects": false,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This allows tools like WebPack to do more advanced tree shaking on the package.

"files": [
"dist/**/*",
"src/*",
"!*.spec.*",
"LICENSE"
"lib"
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've renamed the dist directory to lib as per conventions in publishing TypeScript packages.

],
"keywords": [
"drag-and-drop",
Expand All @@ -30,43 +28,23 @@
"url": "https://github.com/react-dropzone/file-selector.git"
},
"scripts": {
"prebuild": "yarn run clean",
"build": "npm-run-all -s compile build:umd",
"build:umd": "rollup -c ./rollup.config.js",
"compile": "npm-run-all -p compile:es2015 compile:es5 compile:cjs compile:types",
"compile:es2015": "tsc -p ./tsconfig.es2015.json",
"compile:es5": "tsc -p ./tsconfig.es5.json",
"compile:cjs": "tsc -p ./tsconfig.cjs.json",
"compile:types": "tsc -p ./tsconfig.types.json",
"clean": "rm -rf dist/*",
"lint": "tslint -c tslint.json -p ./tsconfig.spec.json -t stylish",
"lint:fix": "yarn run lint -- --fix",
"pretest:cov": "yarn run lint",
"build": "rm -rf lib && tsc -p tsconfig.build.json",
"lint": "tslint -c tslint.json -p tsconfig.lint.json -t stylish",
"lint:fix": "yarn lint --fix",
"test:cov": "jest --coverage",
"test": "jest --watch"
},
"dependencies": {
"tslib": "^2.4.0"
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Since we're compiling to modern JavaScript we no longer need the shims from tslib.

Copy link
Collaborator

Choose a reason for hiding this comment

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

So how about non-modern js?

"engines": {
"node": ">=14.16"
},
"devDependencies": {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I managed to remove quite a bit of dependencies as we are now only using TypeScript to compile things.

"@babel/core": "^7.17.10",
"@rollup/plugin-commonjs": "^22.0.0",
"@rollup/plugin-node-resolve": "^13.2.1",
"@types/jest": "^27.4.1",
"@types/node": "^17.0.30",
"babel-jest": "^28.0.3",
"camelcase": "^6.3.0",
"jest": "^28.0.3",
"jest-environment-jsdom": "^28.0.2",
"npm-run-all": "^4.1.5",
"rollup": "^2.70.2",
"rollup-plugin-sourcemaps": "^0.6.3",
"rollup-plugin-terser": "^7.0.2",
"ts-jest": "next",
"@jest/globals": "^29.0.3",
"@types/node": "^18.7.16",
"jest": "^29.0.3",
"jest-environment-jsdom": "^29.0.3",
"ts-jest": "^29.0.0",
"ts-node": "^10.9.1",
"tslint": "^6.1.3",
"typescript": "^4.6.4"
},
"engines": {
"node": ">= 12"
"typescript": "^4.8.3"
}
}
60 changes: 0 additions & 60 deletions rollup.config.js

This file was deleted.

12 changes: 6 additions & 6 deletions src/file-selector.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {FileWithPath} from './file';
import {fromEvent} from './file-selector';

import { expect, it } from '@jest/globals';
import { fromEvent } from './file-selector.js';
import type { FileWithPath } from './file.js';

it('returns a Promise', async () => {
const evt = new Event('test');
Expand Down Expand Up @@ -228,7 +228,7 @@ it('should throw if reading dir entries fails', done => {
]);

fromEvent(evt)
.then(() => done.fail('Getting the files should have failed'))
.then(() => done('Getting the files should have failed'))
.catch(() => done());
});

Expand All @@ -246,7 +246,7 @@ it('should throw if reading file entry fails', done => {
]);

fromEvent(evt)
.then(() => done.fail('Getting the files should have failed'))
.then(() => done('Getting the files should have failed'))
.catch(() => done());
});

Expand All @@ -255,7 +255,7 @@ it('should throw if DataTransferItem is not a File', done => {
const evt = dragEvtFromFilesAndItems([], [item]);

fromEvent(evt)
.then(() => done.fail('Getting the files should have failed'))
.then(() => done('Getting the files should have failed'))
.catch(() => done());
});

Expand Down
3 changes: 1 addition & 2 deletions src/file-selector.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {FileWithPath, toFileWithPath} from './file';

import {FileWithPath, toFileWithPath} from './file.js';

const FILES_TO_IGNORE = [
// Thumbnail cache files for macOS and Windows
Expand Down
9 changes: 5 additions & 4 deletions src/file.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// tslint:disable: forin
import {COMMON_MIME_TYPES, toFileWithPath} from './file';
import { describe, expect, it, test } from '@jest/globals';
import { COMMON_MIME_TYPES, toFileWithPath } from './file.js';

describe('toFile()', () => {
it('should be an instance of a File', () => {
Expand Down Expand Up @@ -40,7 +41,7 @@ describe('toFile()', () => {

expect(Object.keys(fileWithPath)).toContain('path');

const keys = [];
const keys: string[] = [];
for (const key in fileWithPath) {
keys.push(key);
}
Expand Down Expand Up @@ -83,7 +84,7 @@ describe('toFile()', () => {

expect(Object.keys(fileWithPath)).toContain('type');

const keys = [];
const keys: string[] = [];
for (const key in fileWithPath) {
keys.push(key);
}
Expand Down Expand Up @@ -117,7 +118,7 @@ describe('toFile()', () => {
expect(d).toEqual(data);
done();
} catch (e) {
done.fail(e as Error);
done(e as Error);
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export {fromEvent} from './file-selector';
export {FileWithPath} from './file';
export {fromEvent} from './file-selector.js';
export type {FileWithPath} from './file.js';
13 changes: 13 additions & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "./tsconfig.json",
"include": [
"./src"
],
"exclude": [
"**/*.spec.*",
],
"compilerOptions": {
"outDir": "./lib",
"declaration": true
}
}
8 changes: 0 additions & 8 deletions tsconfig.cjs.json

This file was deleted.

Loading