Skip to content

Commit

Permalink
meta: use Vite for examples/dev (#3361)
Browse files Browse the repository at this point in the history
It's a bit hacky, but it works :)

We should be getting even more performance when we switch to ESM and
stop using JSX in `.js` files.
  • Loading branch information
aduh95 committed Dec 13, 2021
1 parent 367dd4c commit 0ab304c
Show file tree
Hide file tree
Showing 10 changed files with 424 additions and 54 deletions.
47 changes: 47 additions & 0 deletions .yarn/patches/babel-plugin-transform-commonjs-npm-1.1.6-0007fa2809
@@ -0,0 +1,47 @@
diff --git a/dist/index.js b/dist/index.js
index 0f9a4b342c75d75309b78a36473fb5c68f7b89b5..57effed56c190a946756d191cb3d390dbc84aee3 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -92,18 +92,32 @@ exports.default = helper_plugin_utils_1.declare((api, options) => {
const specifiers = [];
// Convert to named import.
if (core_1.types.isObjectPattern(path.parentPath.node.id)) {
- path.parentPath.node.id.properties.forEach(prop => {
- specifiers.push(core_1.types.importSpecifier(prop.value, prop.key));
- state.globals.add(prop.value.name);
- });
- const decl = core_1.types.importDeclaration(specifiers, core_1.types.stringLiteral(str.value));
- // @ts-ignore
- decl.__replaced = true;
- path.scope.getProgramParent().path.unshiftContainer('body', decl);
- path.parentPath.remove();
+ if(node.arguments[0].value.startsWith('.') || node.arguments[0].value.startsWith('@uppy/')) {
+ const id = path.scope.generateUidIdentifier(node.arguments[0].value)
+ const destructuring = core_1.types.variableDeclarator(path.parentPath.node.id, id)
+ const decl = core_1.types.importDeclaration([core_1.types.importDefaultSpecifier(id)], core_1.types.stringLiteral(str.value));
+ // @ts-ignore
+ decl.__replaced = true;
+ path.scope.getProgramParent().path.unshiftContainer('body', decl);
+ path.parentPath.replaceWith(destructuring)
+ } else {
+ path.parentPath.node.id.properties.forEach(prop => {
+ specifiers.push(core_1.types.importSpecifier(prop.value, prop.key));
+ state.globals.add(prop.value.name);
+ });
+ const decl = core_1.types.importDeclaration(specifiers, core_1.types.stringLiteral(str.value));
+ // @ts-ignore
+ decl.__replaced = true;
+ path.scope.getProgramParent().path.unshiftContainer('body', decl);
+ path.parentPath.remove()
+ }
}
+ else if (node.arguments[0].value === 'tus-js-client' && str) {
+ const decl = core_1.types.importDeclaration([core_1.types.importNamespaceSpecifier(path.parentPath.node.id)], core_1.types.stringLiteral(str.value))
+ path.scope.getProgramParent().path.unshiftContainer('body', decl);
+ path.parentPath.remove()
// Convert to default import.
- else if (str) {
+ } else if (str) {
const { parentPath } = path;
const { left } = parentPath.node;
// @ts-ignore
2 changes: 1 addition & 1 deletion babel.config.js
Expand Up @@ -20,7 +20,7 @@ module.exports = (api) => {
],
plugins: [
['@babel/plugin-transform-react-jsx', { pragma: 'h' }],
'babel-plugin-inline-package-json',
process.env.NODE_ENV !== 'dev' && 'babel-plugin-inline-package-json',
].filter(Boolean),
}
}
50 changes: 25 additions & 25 deletions examples/dev/Dashboard.js
@@ -1,28 +1,28 @@
// The @uppy/ dependencies are resolved using aliasify
// The @uppy/ dependencies are resolved from source
/* eslint-disable import/no-extraneous-dependencies */
const Uppy = require('@uppy/core/src')
const Dashboard = require('@uppy/dashboard/src')
const Instagram = require('@uppy/instagram/src')
const Facebook = require('@uppy/facebook/src')
const OneDrive = require('@uppy/onedrive/src')
const Dropbox = require('@uppy/dropbox/src')
const Box = require('@uppy/box/src')
const GoogleDrive = require('@uppy/google-drive/src')
const Unsplash = require('@uppy/unsplash/src')
const Zoom = require('@uppy/zoom/src')
const Url = require('@uppy/url/src')
const Webcam = require('@uppy/webcam/src')
const ScreenCapture = require('@uppy/screen-capture/src')
const GoldenRetriever = require('@uppy/golden-retriever/src')
const Tus = require('@uppy/tus/src')
const AwsS3 = require('@uppy/aws-s3/src')
const AwsS3Multipart = require('@uppy/aws-s3-multipart/src')
const XHRUpload = require('@uppy/xhr-upload/src')
const Transloadit = require('@uppy/transloadit/src')
const Form = require('@uppy/form/src')
const ImageEditor = require('@uppy/image-editor/src')
const DropTarget = require('@uppy/drop-target/src')
const Audio = require('@uppy/audio/src')
import Uppy from '@uppy/core'
import Dashboard from '@uppy/dashboard'
import Instagram from '@uppy/instagram'
import Facebook from '@uppy/facebook'
import OneDrive from '@uppy/onedrive'
import Dropbox from '@uppy/dropbox'
import Box from '@uppy/box'
import GoogleDrive from '@uppy/google-drive'
import Unsplash from '@uppy/unsplash'
import Zoom from '@uppy/zoom'
import Url from '@uppy/url'
import Webcam from '@uppy/webcam'
import ScreenCapture from '@uppy/screen-capture'
import GoldenRetriever from '@uppy/golden-retriever'
import Tus from '@uppy/tus'
import AwsS3 from '@uppy/aws-s3'
import AwsS3Multipart from '@uppy/aws-s3-multipart'
import XHRUpload from '@uppy/xhr-upload'
import Transloadit from '@uppy/transloadit'
import Form from '@uppy/form'
import ImageEditor from '@uppy/image-editor'
import DropTarget from '@uppy/drop-target'
import Audio from '@uppy/audio'
/* eslint-enable import/no-extraneous-dependencies */

// DEV CONFIG: pick an uploader
Expand Down Expand Up @@ -55,7 +55,7 @@ const RESTORE = false

// Rest is implementation! Obviously edit as necessary...

module.exports = () => {
export default () => {
const uppyDashboard = new Uppy({
logger: Uppy.debugLogger,
meta: {
Expand Down
12 changes: 6 additions & 6 deletions examples/dev/DragDrop.js
@@ -1,12 +1,12 @@
// The @uppy/ dependencies are resolved using aliasify
// The @uppy/ dependencies are resolved from source
/* eslint-disable import/no-extraneous-dependencies */
const Uppy = require('@uppy/core/src')
const Tus = require('@uppy/tus/src')
const DragDrop = require('@uppy/drag-drop/src')
const ProgressBar = require('@uppy/progress-bar/src')
import Uppy from '@uppy/core'
import Tus from '@uppy/tus'
import DragDrop from '@uppy/drag-drop'
import ProgressBar from '@uppy/progress-bar'
/* eslint-enable import/no-extraneous-dependencies */

module.exports = () => {
export default () => {
const uppyDragDrop = new Uppy({
debug: true,
autoProceed: true,
Expand Down
3 changes: 1 addition & 2 deletions examples/dev/Dashboard.html → examples/dev/index.html
Expand Up @@ -51,7 +51,6 @@ <h1>Dashboard is here</h1>
</form>
</main>

<link href="uppy.min.css" rel="stylesheet">
<script src="output/index.js"></script>
<script src="./index.js" type="module"></script>
</body>
</html>
7 changes: 5 additions & 2 deletions examples/dev/index.js
@@ -1,5 +1,8 @@
const DragDrop = require('./DragDrop.js')
const Dashboard = require('./Dashboard.js')
// eslint-disable-next-line import/no-extraneous-dependencies
import 'uppy/src/style.scss'

import DragDrop from './DragDrop.js'
import Dashboard from './Dashboard.js'

switch (window.location.pathname.toLowerCase()) {
case '/':
Expand Down
17 changes: 8 additions & 9 deletions examples/dev/package.json
Expand Up @@ -6,18 +6,17 @@
"@uppy": "../../packages/@uppy"
}
},
"dependencies": {
"@babel/core": "^7.4.4",
"aliasify": "^2.1.0",
"babelify": "^10.0.0",
"watchify": "^3.11.0"
},
"devDependencies": {
"browser-sync": "^2.27.4"
"@babel/core": "^7.4.4",
"@babel/plugin-transform-react-jsx": "^7.10.4",
"babel-plugin-transform-commonjs": "1.1.6",
"vite": "^2.7.1"
},
"private": true,
"type": "module",
"scripts": {
"start": "browser-sync start --no-open --no-ghost-mode false --server . --index Dashboard.html --port 3452 --serveStatic ../../packages/uppy/dist --files \"./output/*.js, ../../packages/uppy/dist/uppy.min.css, ../../packages/uppy/lib/**/*\"",
"watch:sandbox": "watchify -vd -t [ babelify --cwd ../../ ] -g aliasify index.js -o output/index.js"
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
}
}
76 changes: 76 additions & 0 deletions examples/dev/vite.config.js
@@ -0,0 +1,76 @@
import { fileURLToPath } from 'node:url'
import { transformAsync } from '@babel/core'

const ROOT = new URL('../../', import.meta.url)
const PACKAGES_ROOT = fileURLToPath(new URL('./packages/', ROOT))

// To enable the plugin, it looks like we need to interact with the resolution
// algorithm, but we need to stop afterwards otherwise it messes up somewhere
// else. This hack can be removed when we get rid of JSX inside of .js files.
let counter = 0

/**
* @type {import('vite').UserConfig}
*/
const config = {
build: {
commonjsOptions: {
defaultIsModuleExports: true,
},
},
esbuild: {
jsxFactory: 'h',
jsxFragment: 'Fragment',
},
resolve: {
alias: [
{
find: /^uppy\/(.+)$/,
replacement: `${PACKAGES_ROOT}uppy/$1`,
},
{
find: /^@uppy\/([^/]+)$/,
replacement: `${PACKAGES_ROOT}@uppy/$1/src/index.js`,
},
{
find: /^@uppy\/([^/]+)\/lib\/(.+)$/,
replacement: `${PACKAGES_ROOT}@uppy/$1/src/$2`,
},
// {
// find: /^@uppy\/([^/]+)\/(.+)$/,
// replacement: PACKAGES_ROOT + "@uppy/$1/src/$2",
// },
],
},
plugins: [
// TODO: remove plugin when we switch to ESM and get rid of JSX inside .js files.
{
name: 'vite-plugin-jsx-commonjs',
// TODO: remove this hack when we get rid of JSX inside .js files.
enforce: 'pre',
// eslint-disable-next-line consistent-return
resolveId (id) {
if (id.startsWith(PACKAGES_ROOT) && id.endsWith('.js')) {
return id
}
// TODO: remove this hack when we get rid of JSX inside .js files.
if (counter++ < 2) {
return id
}
},
transform (code, id) {
if (id.startsWith(PACKAGES_ROOT) && id.endsWith('.js')) {
return transformAsync(code, {
plugins: [
['@babel/plugin-transform-react-jsx', { pragma: 'h' }],
'transform-commonjs',
],
})
}
return code
},
},
],
}

export default config
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -138,7 +138,7 @@
"dev:browsersync": "yarn workspace @uppy-example/dev start",
"dev:watch-sandbox": "yarn workspace @uppy-example/dev watch:sandbox",
"dev:with-companion": "npm-run-all --parallel start:companion dev:watch-sandbox watch:js:lib watch:css dev:browsersync",
"dev": "npm-run-all --parallel dev:watch-sandbox watch:js:lib watch:css dev:browsersync",
"dev": "yarn workspace @uppy-example/dev dev",
"example": "yarn node bin/run-example",
"lint:fix": "yarn run lint -- --fix",
"lint:markdown": "remark -f -q . -i .remarkignore",
Expand Down Expand Up @@ -192,6 +192,7 @@
"resolutions": {
"@types/redis": "2",
"@types/eslint@^7.2.13": "^8.2.0",
"npm-auth-to-token@1.0.0": "patch:npm-auth-to-token@npm:1.0.0#.yarn/patches/npm-auth-to-token-npm-1.0.0-c288ce201f"
"npm-auth-to-token@1.0.0": "patch:npm-auth-to-token@npm:1.0.0#.yarn/patches/npm-auth-to-token-npm-1.0.0-c288ce201f",
"babel-plugin-transform-commonjs@1.1.6": "patch:babel-plugin-transform-commonjs@npm:1.1.6#.yarn/patches/babel-plugin-transform-commonjs-npm-1.1.6-0007fa2809"
}
}

0 comments on commit 0ab304c

Please sign in to comment.