Skip to content
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

meta: add support for TypeScript plugins #4640

Merged
merged 46 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
0124e35
meta: add support for TypeScript plugins
aduh95 Aug 22, 2023
d8a518a
fixup! meta: add support for TypeScript plugins
aduh95 Aug 30, 2023
4e8f732
fixup! meta: add support for TypeScript plugins
aduh95 Aug 30, 2023
f15b3e3
fixup! meta: add support for TypeScript plugins
aduh95 Aug 30, 2023
3985734
fix `yarn dev`
aduh95 Aug 30, 2023
f00dec9
resolve e2e build failure
aduh95 Aug 31, 2023
926effa
fixup! resolve e2e build failure
aduh95 Aug 31, 2023
ef5a87c
fixup! resolve e2e build failure
aduh95 Aug 31, 2023
7f8cda3
add `js2ts` script
aduh95 Sep 1, 2023
c1f69af
add `build:ts`
aduh95 Sep 1, 2023
da629a9
Merge branch 'main' of https://github.com/transloadit/uppy
aduh95 Sep 6, 2023
1ccdcfa
update tsconfig
aduh95 Sep 13, 2023
8ff4725
Merge branch 'main' of https://github.com/transloadit/uppy
aduh95 Sep 22, 2023
4980818
Update private/js2ts/index.mjs
mifi Sep 26, 2023
57341b6
Update private/js2ts/index.mjs
aduh95 Sep 26, 2023
f7c2e57
Update private/js2ts/index.mjs
aduh95 Sep 26, 2023
2a80377
Merge branch 'main' of https://github.com/transloadit/uppy
aduh95 Sep 26, 2023
6c9586e
Update private/js2ts/index.mjs
aduh95 Sep 26, 2023
123c98d
update p-q patch
aduh95 Sep 26, 2023
5cd4e9b
enforce use of explicit return types in library code
aduh95 Sep 27, 2023
9e3acf6
Merge branch 'main' into ts-packages-support
aduh95 Sep 27, 2023
996c2af
add comment
aduh95 Sep 27, 2023
0909fea
Merge branch 'main' into ts-packages-support
aduh95 Sep 28, 2023
3b19a37
prettier
aduh95 Sep 28, 2023
b685754
Merge branch 'main' of https://github.com/transloadit/uppy
aduh95 Sep 29, 2023
d10929b
@uppy/svelte: fix TS build command
aduh95 Oct 2, 2023
1f70266
`allowSyntheticDefaultImports`
aduh95 Oct 2, 2023
c7f50be
Merge branch 'main' into ts-packages-support
aduh95 Oct 2, 2023
ba73746
add support for TS in test files
aduh95 Oct 2, 2023
e4eb8ef
Merge branch 'main' into ts-packages-support
aduh95 Oct 3, 2023
d1c289a
extend config, and match more files
aduh95 Oct 3, 2023
d398bcf
fix imports
aduh95 Oct 3, 2023
40ebb91
fixup! fix imports
aduh95 Oct 3, 2023
887b173
fixup! fixup! fix imports
aduh95 Oct 3, 2023
6ccc518
uppy: fix types
aduh95 Oct 3, 2023
eb1949f
use two TSConfig files per package
aduh95 Oct 3, 2023
e71ce8e
fixup! use two TSConfig files per package
aduh95 Oct 3, 2023
f5940c0
prettier
aduh95 Oct 3, 2023
e167e22
remove outdated ignore entries
aduh95 Oct 3, 2023
9863186
`skipLibCheck`
aduh95 Oct 3, 2023
5bdbda7
remove debug line
aduh95 Oct 3, 2023
75e4bc0
Merge branch 'main' into ts-packages-support
aduh95 Oct 3, 2023
74e3dc0
Merge branch 'main' of https://github.com/transloadit/uppy
aduh95 Oct 15, 2023
0bfdf16
disable a rule that conflicts with Prettier
aduh95 Oct 15, 2023
759ecbb
exclude test files from mandatory function return type
aduh95 Oct 17, 2023
05a1266
throw when trying to convert package before its deps
aduh95 Oct 17, 2023
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
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ test/lib/**
test/endtoend/*/build
examples/svelte-example/public/build/
bundle-legacy.js
packages/@uppy/*/src/*.d.ts
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ module.exports = {
'postcss.config.js',
'.eslintrc.js',
'private/**/*.js',
'private/**/*.mjs',
],
rules: {
'no-console': 'off',
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,5 @@ jobs:
corepack yarn run build:locale-pack
- name: Run type tests
run: corepack yarn run test:type
- name: Attempt building TS packages
run: corepack yarn run build:ts
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ yarn-error.log

dist/
lib/
tsOut/
coverage/
examples/dev/bundle.js
examples/aws-php/vendor/*
test/endtoend/create-react-app/build/
test/endtoend/create-react-app/coverage/
uppy-*.tgz
generatedLocale.d.ts
packages/@uppy/*/src/*.d.ts

**/output/*
!output/.keep
Expand Down
12 changes: 12 additions & 0 deletions .yarn/patches/p-queue-npm-7.3.4-c746a7f1ae.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
diff --git a/package.json b/package.json
index 9724d458b80411c2fd145b2e9d6db718523ccff9..2902526f4c9170369263e60d3555d11cd9f7f32d 100644
--- a/package.json
+++ b/package.json
@@ -6,6 +6,7 @@
"repository": "sindresorhus/p-queue",
"funding": "https://github.com/sponsors/sindresorhus",
"type": "module",
+ "main": "./dist/index.js",
"exports": "./dist/index.js",
"engines": {
"node": ">=12"
45 changes: 23 additions & 22 deletions bin/build-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ const path = require('node:path')
const { mkdir, stat, writeFile } = fs.promises

const PACKAGE_JSON_IMPORT = /^\..*\/package.json$/
const SOURCE = 'packages/{*,@uppy/*}/src/**/*.js?(x)'
const SOURCE = 'packages/{*,@uppy/*}/src/**/*.{js,ts}?(x)'
// Files not to build (such as tests)
const IGNORE = /\.test\.js$|__mocks__|svelte|angular|companion\//
const IGNORE = /\.test\.[jt]s$|__mocks__|svelte|angular|companion\//
// Files that should trigger a rebuild of everything on change
const META_FILES = [
'babel.config.js',
Expand All @@ -32,32 +32,28 @@ function lastModified (file, createParentDir = false) {
})
}

const moduleTypeCache = new Map()
const versionCache = new Map()
async function isTypeModule (file) {
const packageFolder = file.slice(0, file.indexOf('/src/'))

const cachedValue = moduleTypeCache.get(packageFolder)
if (cachedValue != null) return cachedValue
async function preparePackage (file) {
const packageFolder = file.slice(0, file.indexOf('/src/'))
if (versionCache.has(packageFolder)) return

// eslint-disable-next-line import/no-dynamic-require, global-require
const { type, version } = require(path.join(__dirname, '..', packageFolder, 'package.json'))
const typeModule = type === 'module'
const { version } = require(path.join(__dirname, '..', packageFolder, 'package.json'))
if (process.env.FRESH) {
// in case it hasn't been done before.
await mkdir(path.join(packageFolder, 'lib'), { recursive: true })
}
moduleTypeCache.set(packageFolder, typeModule)
versionCache.set(packageFolder, version)
return typeModule
}

const nonJSImport = /^\.\.?\/.+\.([jt]sx|ts)$/
// eslint-disable-next-line no-shadow
function transformJSXImportsToJS (path) {
const { value } = path.node.source
if (value.endsWith('.jsx') && (value.startsWith('./') || value.startsWith('../'))) {
// Rewrite .jsx imports to .js:
path.node.source.value = value.slice(0, -1) // eslint-disable-line no-param-reassign
function rewriteNonJSImportsToJS (path) {
const match = nonJSImport.exec(path.node.source.value)
if (match) {
// eslint-disable-next-line no-param-reassign
path.node.source.value = `${match[0].slice(0, -match[1].length)}js`
}
}

Expand All @@ -71,7 +67,8 @@ async function buildLib () {
if (IGNORE.test(file)) {
continue
}
const libFile = file.replace('/src/', '/lib/').replace(/\.jsx$/, '.js')
await preparePackage(file)
const libFile = file.replace('/src/', '/lib/').replace(/\.[jt]sx?$/, '.js')

// on a fresh build, rebuild everything.
if (!process.env.FRESH) {
Expand All @@ -85,16 +82,17 @@ async function buildLib () {
}
}

const plugins = await isTypeModule(file) ? [{
const plugins = [{
visitor: {
// eslint-disable-next-line no-shadow
ImportDeclaration (path) {
transformJSXImportsToJS(path)
rewriteNonJSImportsToJS(path)
if (PACKAGE_JSON_IMPORT.test(path.node.source.value)
&& path.node.specifiers.length === 1
&& path.node.specifiers[0].type === 'ImportDefaultSpecifier') {
// Vendor-in version number from package.json files:
const version = versionCache.get(file.slice(0, file.indexOf('/src/')))
console.log({ version })
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
if (version != null) {
const [{ local }] = path.node.specifiers
path.replaceWith(
Expand All @@ -107,15 +105,18 @@ async function buildLib () {
}
},

ExportAllDeclaration: transformJSXImportsToJS,
ExportAllDeclaration: rewriteNonJSImportsToJS,
// eslint-disable-next-line no-shadow
ExportNamedDeclaration (path) {
if (path.node.source != null) {
transformJSXImportsToJS(path)
rewriteNonJSImportsToJS(path)
}
},
},
}] : undefined
}]
const isTSX = file.endsWith('.tsx')
if (isTSX || file.endsWith('.ts')) { plugins.push(['@babel/plugin-transform-typescript', { disallowAmbiguousJSXLike: true, isTSX, jsxPragma: 'h' }]) }

const { code, map } = await babel.transformFileAsync(file, { sourceMaps: true, plugins })
const [{ default: chalk }] = await Promise.all([
import('chalk'),
Expand Down
30 changes: 30 additions & 0 deletions bin/build-ts.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env node

import { spawn } from 'node:child_process'
import { once } from 'node:events'
import { existsSync } from 'node:fs'
import path from 'node:path'
import { stdin, env } from 'node:process'
import { createInterface as readLines } from 'node:readline'
import { fileURLToPath } from 'node:url'

const fromYarn = 'npm_execpath' in env
const exe = fromYarn ? env.npm_execpath : 'corepack'
const argv0 = fromYarn ? [] : ['yarn']

const cwd = fileURLToPath(new URL('../', import.meta.url))

for await (const line of readLines(stdin)) {
const { location, name } = JSON.parse(line)
if (existsSync(path.join(cwd, location, 'tsconfig.json'))) {
const cp = spawn(exe, [...argv0, 'tsc', '-p', location], {
stdio: 'inherit',
cwd,
})
await Promise.race([
once(cp, 'error').then(err => Promise.reject(err)),
await once(cp, 'exit')
.then(([code]) => (code && Promise.reject(new Error(`Non-zero exit code when building "${name}": ${code}`)))),
])
Murderlon marked this conversation as resolved.
Show resolved Hide resolved
}
}
22 changes: 22 additions & 0 deletions e2e/cypress/integration/dashboard-transloadit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ describe('Dashboard with Transloadit', () => {
cy.get('.uppy-StatusBar-actionBtn--upload').click()

cy.wait(['@createAssemblies']).then(() => {
// eslint-disable-next-line
// @ts-ignore fix me
expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).every((a: any) => a.pollInterval)).to.equal(true)

uppy.cancelAll()

cy.wait(['@delete']).then(() => {
// eslint-disable-next-line
// @ts-ignore fix me
expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).some((a: any) => a.pollInterval)).to.equal(false)
})
})
Expand All @@ -53,6 +57,8 @@ describe('Dashboard with Transloadit', () => {
const spy = cy.spy()

cy.window().then(({ uppy }) => {
// eslint-disable-next-line
// @ts-ignore fix me
uppy.on('transloadit:assembly-cancelled', spy)

cy.get('@file-input').selectFile(['cypress/fixtures/images/cat.jpg', 'cypress/fixtures/images/traffic.jpg'], { force:true })
Expand Down Expand Up @@ -89,6 +95,8 @@ describe('Dashboard with Transloadit', () => {
const spy = cy.spy()

cy.window().then(({ uppy }) => {
// eslint-disable-next-line
// @ts-ignore fix me
uppy.on('transloadit:assembly-cancelled', spy)

cy.get('@file-input').selectFile(['cypress/fixtures/images/cat.jpg', 'cypress/fixtures/images/traffic.jpg'], { force:true })
Expand All @@ -111,12 +119,18 @@ describe('Dashboard with Transloadit', () => {

cy.get('.uppy-StatusBar-actionBtn--upload').click()
cy.wait('@assemblyPolling')
// eslint-disable-next-line
// @ts-ignore fix me
expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).every((a: any) => a.pollInterval)).to.equal(true)

const { files } = uppy.getState()
// eslint-disable-next-line
// @ts-ignore fix me
uppy.removeFiles(Object.keys(files))

cy.wait('@assemblyDeletion').then(() => {
// eslint-disable-next-line
// @ts-ignore fix me
expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).some((a: any) => a.pollInterval)).to.equal(false)
expect(spy).to.be.calledOnce
})
Expand All @@ -128,12 +142,18 @@ describe('Dashboard with Transloadit', () => {
cy.get('.uppy-StatusBar-actionBtn--upload').click()

cy.window().then(({ uppy }) => {
// eslint-disable-next-line
// @ts-ignore fix me
expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).length).to.equal(0)

const { files } = uppy.getState()
// eslint-disable-next-line
// @ts-ignore fix me
uppy.removeFiles(Object.keys(files))

cy.wait('@createAssemblies').then(() => {
// eslint-disable-next-line
// @ts-ignore fix me
expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).some((a: any) => a.pollInterval)).to.equal(false)
})
})
Expand All @@ -145,6 +165,8 @@ describe('Dashboard with Transloadit', () => {
cy.get('.uppy-StatusBar-actionBtn--upload').click()

cy.window().then(({ uppy }) => {
// eslint-disable-next-line
// @ts-ignore fix me
expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).length).to.equal(0)

const { files } = uppy.getState()
Expand Down
2 changes: 2 additions & 0 deletions e2e/cypress/integration/react.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ describe('@uppy/react', () => {
it('should render Drag & Drop in React and create a thumbail with @uppy/thumbnail-generator', () => {
const spy = cy.spy()

// eslint-disable-next-line
// @ts-ignore fix me
cy.window().then(({ uppy }) => uppy.on('thumbnail:generated', spy))
cy.get('@dragdrop-input').selectFile(['cypress/fixtures/images/cat.jpg', 'cypress/fixtures/images/traffic.jpg'], { force:true })
// not sure how I can accurately wait for the thumbnail
Expand Down
10 changes: 5 additions & 5 deletions e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@
"@uppy/zoom": "workspace:^"
},
"devDependencies": {
"cypress": "^12.9.0",
"cypress-terminal-report": "^4.1.2",
"@parcel/transformer-vue": "2.9.3",
"cypress": "^13.0.0",
"cypress-terminal-report": "^5.0.0",
"deep-freeze": "^0.0.1",
"execa": "^6.1.0",
"parcel": "2.0.0-nightly.1278",
"parcel": "^2.0.0",
"prompts": "^2.4.2",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"typescript": "~4.8",
"typescript": "~5.1",
"vue": "^3.2.33"
}
}
49 changes: 34 additions & 15 deletions e2e/start-companion-with-load-balancer.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env node

import { execa } from 'execa'
import { spawn } from 'node:child_process'
import http from 'node:http'
import httpProxy from 'http-proxy'
import process from 'node:process'

const numInstances = 3
const lbPort = 3020
Expand Down Expand Up @@ -45,20 +46,38 @@ function createLoadBalancer (baseUrls) {
return server
}

const startCompanion = ({ name, port }) => execa('nodemon', [
'--watch', 'packages/@uppy/companion/src', '--exec', 'node', '-r', 'dotenv/config', './packages/@uppy/companion/src/standalone/start-server.js',
], {
cwd: new URL('../', import.meta.url),
stdio: 'inherit',
env: {
// Note: these env variables will override anything set in .env
COMPANION_PORT: port,
COMPANION_SECRET: 'development', // multi instance will not work without secret set
COMPANION_PREAUTH_SECRET: 'development', // multi instance will not work without secret set
COMPANION_ALLOW_LOCAL_URLS: 'true',
COMPANION_LOGGER_PROCESS_NAME: name,
},
})
const isWindows = process.platform === 'win32'
const isOSX = process.platform === 'darwin'

const startCompanion = ({ name, port }) => {
const cp = spawn(process.execPath, [
'-r', 'dotenv/config', ...(isWindows || isOSX ? ['--watch-path', 'packages/@uppy/companion/src', '--watch'] : []), './packages/@uppy/companion/src/standalone/start-server.js',
], {
cwd: new URL('../', import.meta.url),
stdio: 'inherit',
env: {
// Note: these env variables will override anything set in .env
...process.env,
COMPANION_PORT: port,
COMPANION_SECRET: 'development', // multi instance will not work without secret set
COMPANION_PREAUTH_SECRET: 'development', // multi instance will not work without secret set
COMPANION_ALLOW_LOCAL_URLS: 'true',
COMPANION_LOGGER_PROCESS_NAME: name,
},
})
return Object.defineProperty(cp, 'then', {
__proto__: null,
writable: true,
configurable: true,
value: Promise.prototype.then.bind(new Promise((resolve, reject) => {
cp.on('exit', (code) => {
if (code === 0) resolve(cp)
else reject(new Error(`Non-zero exit code: ${code}`))
})
cp.on('error', reject)
})),
})
}
Murderlon marked this conversation as resolved.
Show resolved Hide resolved

const hosts = Array.from({ length: numInstances }, (_, index) => {
const port = companionStartPort + index
Expand Down
2 changes: 2 additions & 0 deletions e2e/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"compilerOptions": {
"moduleResolution": "NodeNext",
"noEmit": true,
"target": "es2020",
"lib": ["es2020", "dom"],
"types": ["cypress"]
Expand Down
4 changes: 2 additions & 2 deletions examples/svelte-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
"rollup-plugin-terser": "^7.0.0",
"svelte": ">=3.24.0",
"svelte-check": "^1.6.0",
"svelte-preprocess": "^4.6.1",
"svelte-preprocess": "^5.0.0",
"tslib": "^2.0.0",
"typescript": "~4.8"
"typescript": "~5.1"
},
"dependencies": {
"@uppy/core": "workspace:*",
Expand Down
Loading