Skip to content

Commit

Permalink
Merge branch 'main' into uploader-refactor
Browse files Browse the repository at this point in the history
* main:
  Abstract restriction logic in a new Restricter class (transloadit#3532)
  @uppy/companion - Fetch all Google Drive shared drives (transloadit#3553)
  website: add blog post 2.4-2.7 (transloadit#3557)
  meta: fix e2e (transloadit#3562)
  fix broken link (transloadit#3559)
  meta: fix support of export declaration in source files (transloadit#3558)
  Order Google Drive results by folder to show all folders first (transloadit#3546)
  add corsOrigins to docs (transloadit#3554)
  @uppy/audio: refactor to ESM (transloadit#3470)
  @uppy/locales: compressor cleanup (transloadit#3531)
  meta: fix CJS interop in Vite config (transloadit#3543)
  upgrade node-redis-pubsub (transloadit#3541)
  • Loading branch information
Murderlon committed Mar 14, 2022
2 parents 9a52329 + 490732f commit 4df08a6
Show file tree
Hide file tree
Showing 37 changed files with 755 additions and 1,591 deletions.
38 changes: 38 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,44 @@ module.exports = {
},
},
},
{
files: [
// Packages that have switched to ESM sources:
'packages/@uppy/audio/src/**/*.js',
'packages/@uppy/compressor/src/**/*.js',
],
parserOptions: {
sourceType: 'module',
ecmaFeatures: {
jsx: false,
},
},
rules: {
'no-restricted-globals': [
'error',
{
name: '__filename',
message: 'Use import.meta.url instead',
},
{
name: '__dirname',
message: 'Not available in ESM',
},
{
name: 'exports',
message: 'Not available in ESM',
},
{
name: 'module',
message: 'Not available in ESM',
},
{
name: 'require',
message: 'Use import instead',
},
],
},
},
{
files: ['./packages/@uppy/companion/**/*.js'],
rules: {
Expand Down
89 changes: 78 additions & 11 deletions bin/build-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ async function isTypeModule (file) {
return typeModule
}

// eslint-disable-next-line no-shadow
function transformExportDeclarations (path) {
const { 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
}

path.replaceWith(
t.assignmentExpression(
'=',
t.memberExpression(t.identifier('module'), t.identifier('exports')),
t.callExpression(t.identifier('require'), [path.node.source]),
),
)
}

async function buildLib () {
const metaMtimes = await Promise.all(META_FILES.map((filename) => lastModified(path.join(__dirname, '..', filename))))
const metaMtime = Math.max(...metaMtimes)
Expand Down Expand Up @@ -86,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) {
Expand All @@ -104,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(
Expand All @@ -122,6 +162,33 @@ async function buildLib () {
)
}
},
ExportAllDeclaration: transformExportDeclarations,
// eslint-disable-next-line no-shadow,consistent-return
ExportNamedDeclaration (path) {
if (path.node.source != null) return transformExportDeclarations(path)
},
// eslint-disable-next-line no-shadow
ExportDefaultDeclaration (path) {
const moduleExports = t.memberExpression(t.identifier('module'), t.identifier('exports'))
if (!t.isDeclaration(path.node.declaration)) {
path.replaceWith(
t.assignmentExpression('=', moduleExports, path.node.declaration),
)
} else if (path.node.declaration.id != null) {
const { id } = path.node.declaration
path.insertBefore(path.node.declaration)
path.replaceWith(
t.assignmentExpression('=', moduleExports, id),
)
} else {
const id = t.identifier('_default')
path.node.declaration.id = id // eslint-disable-line no-param-reassign
path.insertBefore(path.node.declaration)
path.replaceWith(
t.assignmentExpression('=', moduleExports, id),
)
}
},
},
}] : undefined
const { code, map } = await babel.transformFileAsync(file, { sourceMaps: true, plugins })
Expand Down
4 changes: 1 addition & 3 deletions e2e/clients/dashboard-compressor/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@
<title>dashboard-compressor</title>
<script defer type="module" src="app.js"></script>
</head>
<body>
<div id="app"></div>
</body>
<body></body>
</html>
6 changes: 3 additions & 3 deletions e2e/clients/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
<h1>Test apps</h1>
<nav>
<ul>
<li><a href="dashboard-tus/index.html">dashboard-tus</a></li>
<li><a href="dashboard-compressor/index.html">dashboard-compressor</a></li>
<li><a href="dashboard-react/index.html">dashboard-react</a></li>
<li><a href="dashboard-transloadit/index.html">dashboard-transloadit</a></li>
<li><a href="dashboard-tus/index.html">dashboard-tus</a></li>
<li><a href="dashboard-ui/index.html">dashboard-ui</a></li>
<li><a href="dashboard-react/index.html">dashboard-react</a></li>
<li><a href="dashboard-vue/index.html">dashboard-vue</a></li>
<li><a href="dashboard-compressor/index.html">dashboard-compressor</a></li>
</ul>
<nav>
</body>
Expand Down
1 change: 1 addition & 0 deletions packages/@uppy/audio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"record",
"mediarecorder"
],
"type": "module",
"homepage": "https://uppy.io",
"bugs": {
"url": "https://github.com/transloadit/uppy/issues"
Expand Down
Loading

0 comments on commit 4df08a6

Please sign in to comment.