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

@uppy/form: refactor to ESM #3654

Merged
merged 1 commit into from
Apr 21, 2022
Merged
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
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ module.exports = {
'packages/@uppy/drag-drop/src/**/*.js',
'packages/@uppy/drop-target/src/**/*.js',
'packages/@uppy/compressor/src/**/*.js',
'packages/@uppy/form/src/**/*.js',
'packages/@uppy/vue/src/**/*.js',
],
parserOptions: {
Expand Down
17 changes: 14 additions & 3 deletions bin/build-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ const META_FILES = [
'bin/build-lib.js',
]

// Rollup uses get-form-data's ES modules build, and rollup-plugin-commonjs automatically resolves `.default`.
// So, if we are being built using rollup, this require() won't have a `.default` property.
const esPackagesThatNeedSpecialTreatmentForRollupInterop = [
'get-form-data',
]

function lastModified (file, createParentDir = false) {
return stat(file).then((s) => s.mtime, async (err) => {
if (err.code === 'ENOENT') {
Expand Down Expand Up @@ -150,13 +156,18 @@ async function buildLib () {
local,
)]))
}

let requireCall = t.callExpression(t.identifier('require'), [
t.stringLiteral(value),
])
if (esPackagesThatNeedSpecialTreatmentForRollupInterop.includes(value)) {
requireCall = t.logicalExpression('||', t.memberExpression(requireCall, t.identifier('default')), requireCall)
}
path.replaceWith(
t.variableDeclaration('const', [
t.variableDeclarator(
local,
t.callExpression(t.identifier('require'), [
t.stringLiteral(value),
]),
requireCall,
),
]),
)
Expand Down
1 change: 1 addition & 0 deletions packages/@uppy/form/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"version": "2.0.4",
"license": "MIT",
"main": "lib/index.js",
"type": "module",
"types": "types/index.d.ts",
"keywords": [
"file uploader",
Expand Down
23 changes: 12 additions & 11 deletions packages/@uppy/form/src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
const BasePlugin = require('@uppy/core/lib/BasePlugin')
const findDOMElement = require('@uppy/utils/lib/findDOMElement')
const toArray = require('@uppy/utils/lib/toArray')
// Rollup uses get-form-data's ES modules build, and rollup-plugin-commonjs automatically resolves `.default`.
// So, if we are being built using rollup, this require() won't have a `.default` property.
const getFormData = require('get-form-data').default || require('get-form-data')
import BasePlugin from '@uppy/core/lib/BasePlugin'
import findDOMElement from '@uppy/utils/lib/findDOMElement'
import toArray from '@uppy/utils/lib/toArray'

import getFormData from 'get-form-data'

import packageJson from '../package.json'

/**
* Form
*/
module.exports = class Form extends BasePlugin {
static VERSION = require('../package.json').version
export default class Form extends BasePlugin {
static VERSION = packageJson.version

constructor (uppy, opts) {
super(uppy, opts)
Expand Down Expand Up @@ -61,17 +62,17 @@ module.exports = class Form extends BasePlugin {
elements.forEach((el) => {
const isButton = el.tagName === 'BUTTON' || (el.tagName === 'INPUT' && el.type === 'submit')
if (isButton && !el.disabled) {
el.disabled = true
el.disabled = true // eslint-disable-line no-param-reassign
disabledByUppy.push(el)
}
})
this.uppy.upload().then(() => {
disabledByUppy.forEach((button) => {
button.disabled = false
button.disabled = false // eslint-disable-line no-param-reassign
})
}, (err) => {
disabledByUppy.forEach((button) => {
button.disabled = false
button.disabled = false // eslint-disable-line no-param-reassign
})
return Promise.reject(err)
}).catch((err) => {
Expand Down