Skip to content

Commit

Permalink
@uppy/transloadit: remove IE 10 hack (#3777)
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed May 24, 2022
1 parent 62b2cbd commit 6f42104
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 69 deletions.
15 changes: 2 additions & 13 deletions packages/@uppy/transloadit/src/Assembly.js
@@ -1,21 +1,10 @@
import Emitter from 'component-emitter'
import io from 'socket.io-client'
import has from '@uppy/utils/lib/hasProperty'
import NetworkError from '@uppy/utils/lib/NetworkError'
import fetchWithNetworkError from '@uppy/utils/lib/fetchWithNetworkError'
import parseUrl from './parseUrl.js'

// We used to lazy load socket.io to avoid a console error
// in IE 10 when the Transloadit plugin is not used.
// (The console.error call comes from `buffer`. I
// think we actually don't use that part of socket.io
// at all…)
// TODO: remove this hack in the next release.
let socketIo
function requireSocketIo () {
// eslint-disable-next-line no-return-assign, no-restricted-globals, global-require, no-undef
return socketIo ??= require('socket.io-client')
}

const ASSEMBLY_UPLOADING = 'ASSEMBLY_UPLOADING'
const ASSEMBLY_EXECUTING = 'ASSEMBLY_EXECUTING'
const ASSEMBLY_COMPLETED = 'ASSEMBLY_COMPLETED'
Expand Down Expand Up @@ -75,7 +64,7 @@ class TransloaditAssembly extends Emitter {

#connectSocket () {
const parsed = parseUrl(this.status.websocket_url)
const socket = requireSocketIo().connect(parsed.origin, {
const socket = io(parsed.origin, {
transports: ['websocket'],
path: parsed.pathname,
})
Expand Down
2 changes: 0 additions & 2 deletions private/dev/package.json
Expand Up @@ -10,8 +10,6 @@
"@uppy/companion": "workspace:^"
},
"devDependencies": {
"@babel/core": "^7.4.4",
"@babel/types": "^7.17.0",
"autoprefixer": "^10.2.6",
"postcss-dir-pseudo-class": "^5.0.0",
"postcss-logical": "^4.0.2",
Expand Down
52 changes: 0 additions & 52 deletions private/dev/vite.config.js
@@ -1,6 +1,4 @@
import { fileURLToPath } from 'node:url'
import { transformAsync } from '@babel/core'
import t from '@babel/types'
import autoprefixer from 'autoprefixer'
import postcssLogical from 'postcss-logical'
import postcssDirPseudoClass from 'postcss-dir-pseudo-class'
Expand Down Expand Up @@ -46,56 +44,6 @@ const config = {
// },
],
},
plugins: [
// TODO: remove plugin when we remove the socket.io require call in @uppy/transloadit/src/Assembly.
{
name: 'vite-plugin-rewrite-dynamic-socketIo-require',
// eslint-disable-next-line consistent-return
resolveId (id) {
if (id.startsWith(PACKAGES_ROOT) && id.endsWith('transloadit/src/Assembly.js')) {
return id
}
},
transform (code, id) {
if (id.startsWith(PACKAGES_ROOT) && id.endsWith('transloadit/src/Assembly.js')) {
return transformAsync(code, {
plugins: [
{
visitor: {
FunctionDeclaration (path) {
if (path.node.id.name === 'requireSocketIo') {
const prevSibling = path.getPrevSibling()
if (t.isImportDeclaration(prevSibling) && prevSibling.node.specifiers?.length === 1
&& t.isImportDefaultSpecifier(prevSibling.node.specifiers[0])
&& prevSibling.node.specifiers[0].local.name === 'socketIo') {
// The require call has already been rewritten to an import statement.
return
}
if (!t.isVariableDeclaration(prevSibling)) {
const { type, loc } = prevSibling.node
throw new Error(`Unexpected ${type} at line ${loc.start.line}, cannot apply requireSocketIo hack`)
}

const { id:socketIoIdentifier } = prevSibling.node.declarations[0]

prevSibling.replaceWith(t.importDeclaration(
[t.importDefaultSpecifier(socketIoIdentifier)],
t.stringLiteral('socket.io-client'),
))
path.replaceWith(t.functionDeclaration(path.node.id, path.node.params, t.blockStatement([
t.returnStatement(socketIoIdentifier),
])))
}
},
},
},
],
})
}
return code
},
},
],
}

export default config
2 changes: 0 additions & 2 deletions yarn.lock
Expand Up @@ -9239,8 +9239,6 @@ __metadata:
version: 0.0.0-use.local
resolution: "@uppy-dev/dev@workspace:private/dev"
dependencies:
"@babel/core": ^7.4.4
"@babel/types": ^7.17.0
"@uppy/companion": "workspace:^"
autoprefixer: ^10.2.6
postcss-dir-pseudo-class: ^5.0.0
Expand Down

0 comments on commit 6f42104

Please sign in to comment.