-
-
Notifications
You must be signed in to change notification settings - Fork 33
feat: convert package to pure ESM #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,6 @@ Icon | |
| coverage | ||
| build | ||
| docs | ||
| dist | ||
| lib | ||
| out-tsc | ||
| tmp | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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): | ||
| ```html | ||
| <script type="module"> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'; | ||
|
|
@@ -80,25 +77,14 @@ console.log(files); | |
| ``` | ||
| **NOTE** The above is experimental and subject to change. | ||
|
|
||
| ### CommonJS | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since CommonJS is no longer supported I have removed this section.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import type { InitialOptionsTsJest } from 'ts-jest'; | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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", | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So to understand, the
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've renamed the |
||
| ], | ||
| "keywords": [ | ||
| "drag-and-drop", | ||
|
|
@@ -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" | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So how about non-modern js? |
||
| "engines": { | ||
| "node": ">=14.16" | ||
rolandjitsu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| "devDependencies": { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
| } | ||
| } | ||
This file was deleted.
| 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'; |
| 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 | ||
| } | ||
| } |
This file was deleted.
There was a problem hiding this comment.
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!