-
Notifications
You must be signed in to change notification settings - Fork 320
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update export structure, change bundler, move tests to ESM (#749)
* Put Cordova- and React Native-specific code into separate folders * First attempt at using new `exports` structure in `package.json` * Unify exported interface between browser and node variant * Disable unused imports * Rename files to better match their exported classes * Add exports for node-specific APIs * Turn node demo into ESM * Move tests to ESM and build using rollup Browserify does not support `exports` in `package.json`, so we replace it with Rollup. In addition, the tests are moved to ESM. * Let bundle be generated by rollup as well * Bundle TypeScript directly to use in sourcemaps * Explicitly change case of NoopUrlStorage file * Avoid resolving ESM types to CJS files * Add fallback methods for bundler that don't support `exports` * Add `publint` and `arethetypeswrong` as linters
- Loading branch information
Showing
43 changed files
with
881 additions
and
1,168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ lib.esm | |
dist | ||
.DS_Store | ||
yarn-error.log | ||
tus-js-client-*.tgz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
lib/browser/isReactNative.ts → lib/reactnative/isReactNative.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"//": "Generated by scripts/setup-exports.js", | ||
"main": "../../lib.cjs/node/FileUrlStorage.js", | ||
"types": "../../lib.cjs/node/FileUrlStorage.d.ts" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"//": "Generated by scripts/setup-exports.js", | ||
"main": "../../lib.cjs/node/NodeHttpStack.js", | ||
"types": "../../lib.cjs/node/NodeHttpStack.d.ts" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"//": "Generated by scripts/setup-exports.js", | ||
"main": "../../../lib.cjs/node/sources/StreamFileSource.js", | ||
"types": "../../../lib.cjs/node/sources/StreamFileSource.d.ts" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import path, { relative } from 'path' | ||
import { mkdir, readFile, writeFile } from 'fs/promises' | ||
|
||
/** | ||
* Each entry is a "subpackage" that users should be able to import as `tus-js-client/${entry}`. | ||
*/ | ||
const EXPORTS = ['node/sources/StreamFileSource', 'node/FileUrlStorage', 'node/NodeHttpStack'] | ||
|
||
const root = path.join(import.meta.dirname, '..') | ||
|
||
/** | ||
* addExportsToPackageJson adds all entries from EXPORTS to the exports field of package.json, | ||
* so loaders and bundlers supporting the exports field can resolve them properly to either ESM or CommonJS. | ||
*/ | ||
async function addExportsToPackageJson() { | ||
const packageJsonPath = path.join(root, 'package.json') | ||
const packageJson = JSON.parse(await readFile(packageJsonPath, 'utf8')) | ||
|
||
for (const destination of EXPORTS) { | ||
packageJson.exports ??= {} | ||
packageJson.exports[`./${destination}`] = { | ||
import: { | ||
types: `./lib.esm/${destination}.d.ts`, | ||
default: `./lib.esm/${destination}.js`, | ||
}, | ||
require: { | ||
types: `./lib.cjs/${destination}.d.ts`, | ||
default: `./lib.cjs/${destination}.js`, | ||
}, | ||
} | ||
} | ||
|
||
await writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2)) | ||
} | ||
|
||
/** | ||
* createExportsFallbacks setups fallbacks for loaders and bundlers not supporting the exports field. | ||
* It creates subdirectories with a package.json pointing to the CommonJS file and its type definition, | ||
* as explained in https://github.com/andrewbranch/example-subpath-exports-ts-compat/tree/main/examples/node_modules/package-json-redirects. | ||
* Therefore, bundlers without `exports` support get at least the CommonJS file and types. | ||
*/ | ||
async function createExportsFallbacks() { | ||
for (const entry of EXPORTS) { | ||
const fallbackPath = path.join(root, entry) | ||
await mkdir(fallbackPath, { recursive: true }) | ||
|
||
const destinationMain = path.join(root, 'lib.cjs', `${entry}.js`) | ||
const destinationTypes = path.join(root, 'lib.cjs', `${entry}.d.ts`) | ||
|
||
const packageJson = { | ||
'//': 'Generated by scripts/setup-exports.js', | ||
main: relative(fallbackPath, destinationMain), | ||
types: relative(fallbackPath, destinationTypes), | ||
} | ||
|
||
const packageJsonPath = path.join(fallbackPath, 'package.json') | ||
await writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2)) | ||
} | ||
} | ||
|
||
await addExportsToPackageJson() | ||
await createExportsFallbacks() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.