Skip to content

Commit b01a52e

Browse files
authored
meta: remove vite-plugin-jsx-commonjs plugin on dev env (#3749)
Now that we removed all JSX and CJS from `.js` files, there's no need to run Babel on every files, which speeds up greatly the time it takes for Vite to get ready.
1 parent ae9745d commit b01a52e

File tree

5 files changed

+27
-190
lines changed

5 files changed

+27
-190
lines changed

Diff for: .yarn/patches/babel-plugin-transform-commonjs-npm-1.1.6-0007fa2809

-47
This file was deleted.

Diff for: package.json

-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@
203203
"@types/react": "^17",
204204
"@types/webpack-dev-server": "^4",
205205
"[email protected]": "patch:npm-auth-to-token@npm:1.0.0#.yarn/patches/npm-auth-to-token-npm-1.0.0-c288ce201f",
206-
"[email protected]": "patch:babel-plugin-transform-commonjs@npm:1.1.6#.yarn/patches/babel-plugin-transform-commonjs-npm-1.1.6-0007fa2809",
207206
"exifr": "patch:exifr@npm:7.1.3#.yarn/patches/exifr-npm-7.1.3-e3f1c7a57d"
208207
}
209208
}

Diff for: private/dev/package.json

-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111
},
1212
"devDependencies": {
1313
"@babel/core": "^7.4.4",
14-
"@babel/plugin-transform-react-jsx": "^7.10.4",
1514
"@babel/types": "^7.17.0",
1615
"autoprefixer": "^10.2.6",
17-
"babel-plugin-transform-commonjs": "1.1.6",
1816
"postcss-dir-pseudo-class": "^5.0.0",
1917
"postcss-logical": "^4.0.2",
2018
"vite": "^2.7.1"

Diff for: private/dev/vite.config.js

+27-116
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { fileURLToPath } from 'node:url'
2-
import { createRequire } from 'node:module'
32
import { transformAsync } from '@babel/core'
43
import t from '@babel/types'
54
import autoprefixer from 'autoprefixer'
@@ -9,50 +8,11 @@ import postcssDirPseudoClass from 'postcss-dir-pseudo-class'
98
const ROOT = new URL('../../', import.meta.url)
109
const PACKAGES_ROOT = fileURLToPath(new URL('./packages/', ROOT))
1110

12-
// To enable the plugin, it looks like we need to interact with the resolution
13-
// algorithm, but we need to stop afterwards otherwise it messes up somewhere
14-
// else. This hack can be removed when we get rid of JSX inside of .js files.
15-
let counter = 0
16-
17-
const moduleTypeCache = new Map()
18-
function isTypeModule (file) {
19-
const packageFolder = file.slice(0, file.indexOf('/src/') + 1)
20-
21-
const cachedValue = moduleTypeCache.get(packageFolder)
22-
if (cachedValue != null) return cachedValue
23-
24-
// eslint-disable-next-line import/no-dynamic-require, global-require
25-
const { type } = createRequire(packageFolder)('./package.json')
26-
const typeModule = type === 'module'
27-
moduleTypeCache.set(packageFolder, typeModule)
28-
return typeModule
29-
}
30-
const packageLibImport = /^@uppy\/([^/]+)\/lib\/(.+)$/
31-
const packageEntryImport = /^@uppy\/([^/]+)$/
32-
function isSpecifierTypeModule (specifier) {
33-
const packageLib = packageLibImport.exec(specifier)
34-
if (packageLib != null) {
35-
return isTypeModule(`${PACKAGES_ROOT}@uppy/${packageLib[1]}/src/${packageLib[2]}`)
36-
}
37-
const packageEntry = packageEntryImport.exec(specifier)
38-
if (packageEntry != null) {
39-
return isTypeModule(`${PACKAGES_ROOT}@uppy/${packageEntry[1]}/src/index.js`)
40-
}
41-
return false
42-
}
43-
44-
const JS_FILE_EXTENSION = /\.jsx?$/
45-
4611
/**
4712
* @type {import('vite').UserConfig}
4813
*/
4914
const config = {
5015
envDir: fileURLToPath(ROOT),
51-
build: {
52-
commonjsOptions: {
53-
defaultIsModuleExports: true,
54-
},
55-
},
5616
css: {
5717
postcss: {
5818
plugins: [
@@ -87,98 +47,49 @@ const config = {
8747
],
8848
},
8949
plugins: [
90-
// TODO: remove plugin when we switch to ESM and get rid of JSX inside .js files.
50+
// TODO: remove plugin when we remove the socket.io require call in @uppy/transloadit/src/Assembly.
9151
{
92-
name: 'vite-plugin-jsx-commonjs',
93-
// TODO: remove this hack when we get rid of JSX inside .js files.
94-
enforce: 'pre',
52+
name: 'vite-plugin-rewrite-dynamic-socketIo-require',
9553
// eslint-disable-next-line consistent-return
9654
resolveId (id) {
97-
if (id.startsWith(PACKAGES_ROOT) && JS_FILE_EXTENSION.test(id)) {
98-
return id
99-
}
100-
// TODO: remove this hack when we get rid of JSX inside .js files.
101-
if (counter++ < 2) {
55+
if (id.startsWith(PACKAGES_ROOT) && id.endsWith('transloadit/src/Assembly.js')) {
10256
return id
10357
}
10458
},
10559
transform (code, id) {
106-
if (id.startsWith(PACKAGES_ROOT) && JS_FILE_EXTENSION.test(id)) {
107-
return transformAsync(code, isTypeModule(id) ? {
60+
if (id.startsWith(PACKAGES_ROOT) && id.endsWith('transloadit/src/Assembly.js')) {
61+
return transformAsync(code, {
10862
plugins: [
109-
id.endsWith('.jsx') ? ['@babel/plugin-transform-react-jsx', { pragma: 'h' }] : {},
11063
{
111-
// On type: "module" packages, we still want to rewrite import
112-
// statements that tries to access a named export from a CJS
113-
// module to using only the default import.
11464
visitor: {
115-
ImportDeclaration (path) {
116-
const { specifiers, source: { value } } = path.node
117-
if (value.startsWith('@uppy/') && !isSpecifierTypeModule(value)
118-
&& specifiers.some(node => node.type !== 'ImportDefaultSpecifier')) {
119-
const oldSpecifiers = specifiers[0].type === 'ImportDefaultSpecifier'
120-
// If there's a default import, it must come first.
121-
? specifiers.splice(1)
122-
// If there's no default import, we create one from a random identifier.
123-
: specifiers.splice(0, specifiers.length, t.importDefaultSpecifier(t.identifier(`_import_${counter++}`)))
124-
if (oldSpecifiers[0].type === 'ImportNamespaceSpecifier') {
125-
// import defaultVal, * as namespaceImport from '@uppy/package'
126-
// is transformed into:
127-
// import defaultVal from '@uppy/package'; const namespaceImport = defaultVal
128-
path.insertAfter(
129-
t.variableDeclaration('const', [t.variableDeclarator(
130-
oldSpecifiers[0].local,
131-
specifiers[0].local,
132-
)]),
133-
)
134-
} else {
135-
// import defaultVal, { exportedVal as importedName, other } from '@uppy/package'
136-
// is transformed into:
137-
// import defaultVal from '@uppy/package'; const { exportedVal: importedName, other } = defaultVal
138-
path.insertAfter(t.variableDeclaration('const', [t.variableDeclarator(
139-
t.objectPattern(
140-
oldSpecifiers.map(specifier => t.objectProperty(
141-
t.identifier(specifier.imported.name),
142-
specifier.local,
143-
)),
144-
),
145-
specifiers[0].local,
146-
)]))
65+
FunctionDeclaration (path) {
66+
if (path.node.id.name === 'requireSocketIo') {
67+
const prevSibling = path.getPrevSibling()
68+
if (t.isImportDeclaration(prevSibling) && prevSibling.node.specifiers?.length === 1
69+
&& t.isImportDefaultSpecifier(prevSibling.node.specifiers[0])
70+
&& prevSibling.node.specifiers[0].local.name === 'socketIo') {
71+
// The require call has already been rewritten to an import statement.
72+
return
73+
}
74+
if (!t.isVariableDeclaration(prevSibling)) {
75+
const { type, loc } = prevSibling.node
76+
throw new Error(`Unexpected ${type} at line ${loc.start.line}, cannot apply requireSocketIo hack`)
14777
}
148-
}
149-
},
150-
151-
// Very specific hack to avoid a breaking change when the file was refactored to ESM.
152-
// TODO: remove this hack in the next release.
153-
...(id.endsWith('transloadit/src/Assembly.js') ? {
154-
FunctionDeclaration (path) {
155-
if (path.node.id.name === 'requireSocketIo') {
156-
const prevSibling = path.getPrevSibling()
157-
if (!t.isVariableDeclaration(prevSibling) || prevSibling.node.declarations[0].id.name !== 'socketIo') {
158-
// The require call has already been rewritten to an import statement.
159-
return
160-
}
16178

162-
const { id:socketIoIdentifier } = prevSibling.node.declarations[0]
79+
const { id:socketIoIdentifier } = prevSibling.node.declarations[0]
16380

164-
prevSibling.replaceWith(t.importDeclaration(
165-
[t.importDefaultSpecifier(socketIoIdentifier)],
166-
t.stringLiteral('socket.io-client'),
167-
))
168-
path.replaceWith(t.functionDeclaration(path.node.id, path.node.params, t.blockStatement([
169-
t.returnStatement(socketIoIdentifier),
170-
])))
171-
}
172-
},
173-
} : null),
81+
prevSibling.replaceWith(t.importDeclaration(
82+
[t.importDefaultSpecifier(socketIoIdentifier)],
83+
t.stringLiteral('socket.io-client'),
84+
))
85+
path.replaceWith(t.functionDeclaration(path.node.id, path.node.params, t.blockStatement([
86+
t.returnStatement(socketIoIdentifier),
87+
])))
88+
}
89+
},
17490
},
17591
},
17692
],
177-
} : {
178-
plugins: [
179-
['@babel/plugin-transform-react-jsx', { pragma: 'h' }],
180-
'transform-commonjs',
181-
],
18293
})
18394
}
18495
return code

Diff for: yarn.lock

-24
Original file line numberDiff line numberDiff line change
@@ -9250,11 +9250,9 @@ __metadata:
92509250
resolution: "@uppy-dev/dev@workspace:private/dev"
92519251
dependencies:
92529252
"@babel/core": ^7.4.4
9253-
"@babel/plugin-transform-react-jsx": ^7.10.4
92549253
"@babel/types": ^7.17.0
92559254
"@uppy/companion": "workspace:^"
92569255
autoprefixer: ^10.2.6
9257-
babel-plugin-transform-commonjs: 1.1.6
92589256
postcss-dir-pseudo-class: ^5.0.0
92599257
postcss-logical: ^4.0.2
92609258
vite: ^2.7.1
@@ -13004,28 +13002,6 @@ __metadata:
1300413002
languageName: node
1300513003
linkType: hard
1300613004

13007-
"babel-plugin-transform-commonjs@npm:1.1.6":
13008-
version: 1.1.6
13009-
resolution: "babel-plugin-transform-commonjs@npm:1.1.6"
13010-
dependencies:
13011-
"@babel/helper-plugin-utils": ^7.0.0
13012-
peerDependencies:
13013-
"@babel/core": ">=7"
13014-
checksum: fc3f938b5d593457726c53e92305a3f1a3119524f057dedbe5adf16bc85b5b2bb376f8d50deb96bbf1aa168c46ba1c4a6a1cab35837d27049d2ab9fc402aeb9e
13015-
languageName: node
13016-
linkType: hard
13017-
13018-
"babel-plugin-transform-commonjs@patch:babel-plugin-transform-commonjs@npm:1.1.6#.yarn/patches/babel-plugin-transform-commonjs-npm-1.1.6-0007fa2809::locator=%40uppy-dev%2Fbuild%40workspace%3A.":
13019-
version: 1.1.6
13020-
resolution: "babel-plugin-transform-commonjs@patch:babel-plugin-transform-commonjs@npm%3A1.1.6#.yarn/patches/babel-plugin-transform-commonjs-npm-1.1.6-0007fa2809::version=1.1.6&hash=f83dbd&locator=%40uppy-dev%2Fbuild%40workspace%3A."
13021-
dependencies:
13022-
"@babel/helper-plugin-utils": ^7.0.0
13023-
peerDependencies:
13024-
"@babel/core": ">=7"
13025-
checksum: 5995b2641a8551fcc8d0f9d24222ae15698100917edffc8d98f8033cc65b1e577e6b346ac4dbf2456425e494bd1caf1f6646002163953a88290e901f611d83ad
13026-
languageName: node
13027-
linkType: hard
13028-
1302913005
"babel-preset-current-node-syntax@npm:^1.0.0":
1303013006
version: 1.0.1
1303113007
resolution: "babel-preset-current-node-syntax@npm:1.0.1"

0 commit comments

Comments
 (0)