|
@@ -103,13 +103,14 @@ async function buildLib () {
|
|
|
visitor: {
|
|
|
// eslint-disable-next-line no-shadow
|
|
|
ImportDeclaration (path) {
|
|
|
- const { value } = path.node.source
|
|
|
+ let { value } = path.node.source
|
|
|
if (value.endsWith('.jsx') && value.startsWith('./')) {
|
|
|
// Rewrite .jsx imports to .js:
|
|
|
- path.node.source.value = value.slice(0, -1) // eslint-disable-line no-param-reassign
|
|
|
- } else if (PACKAGE_JSON_IMPORT.test(value)
|
|
|
- && path.node.specifiers.length === 1
|
|
|
- && path.node.specifiers[0].type === 'ImportDefaultSpecifier') {
|
|
|
+ value = path.node.source.value = value.slice(0, -1) // eslint-disable-line no-param-reassign,no-multi-assign
|
|
|
+ }
|
|
|
+ if (PACKAGE_JSON_IMPORT.test(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/')))
|
|
|
if (version != null) {
|
|
@@ -121,12 +122,34 @@ async function buildLib () {
|
|
|
]))]),
|
|
|
)
|
|
|
}
|
|
|
- } else if (!value.startsWith('.')
|
|
|
- && path.node.specifiers.length === 1
|
|
|
- && path.node.specifiers[0].type === 'ImportDefaultSpecifier'
|
|
|
- ) {
|
|
|
- // Replace default imports with straight require calls (CommonJS interop):
|
|
|
- const [{ local }] = path.node.specifiers
|
|
|
+ } else if (path.node.specifiers[0].type === 'ImportDefaultSpecifier') {
|
|
|
+ const [{ local }, ...otherSpecifiers] = path.node.specifiers
|
|
|
+ if (otherSpecifiers.length === 1 && otherSpecifiers[0].type === 'ImportNamespaceSpecifier') {
|
|
|
+ // import defaultVal, * as namespaceImport from '@uppy/package'
|
|
|
+ // is transformed into:
|
|
|
+ // const defaultVal = require('@uppy/package'); const namespaceImport = defaultVal
|
|
|
+ path.insertAfter(
|
|
|
+ t.variableDeclaration('const', [
|
|
|
+ t.variableDeclarator(
|
|
|
+ otherSpecifiers[0].local,
|
|
|
+ local,
|
|
|
+ ),
|
|
|
+ ]),
|
|
|
+ )
|
|
|
+ } else if (otherSpecifiers.length !== 0) {
|
|
|
+ // import defaultVal, { exportedVal as importedName, other } from '@uppy/package'
|
|
|
+ // is transformed into:
|
|
|
+ // const defaultVal = require('@uppy/package'); const { exportedVal: importedName, other } = defaultVal
|
|
|
+ path.insertAfter(t.variableDeclaration('const', [t.variableDeclarator(
|
|
|
+ t.objectPattern(
|
|
|
+ otherSpecifiers.map(specifier => t.objectProperty(
|
|
|
+ t.identifier(specifier.imported.name),
|
|
|
+ specifier.local,
|
|
|
+ )),
|
|
|
+ ),
|
|
|
+ local,
|
|
|
+ )]))
|
|
|
+ }
|
|
|
path.replaceWith(
|
|
|
t.variableDeclaration('const', [
|
|
|
t.variableDeclarator(
|