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

meta: remove vite-plugin-jsx-commonjs plugin on dev env #3749

Merged
merged 1 commit into from
May 23, 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
47 changes: 0 additions & 47 deletions .yarn/patches/babel-plugin-transform-commonjs-npm-1.1.6-0007fa2809

This file was deleted.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@
"@types/react": "^17",
"@types/webpack-dev-server": "^4",
"[email protected]": "patch:npm-auth-to-token@npm:1.0.0#.yarn/patches/npm-auth-to-token-npm-1.0.0-c288ce201f",
"[email protected]": "patch:babel-plugin-transform-commonjs@npm:1.1.6#.yarn/patches/babel-plugin-transform-commonjs-npm-1.1.6-0007fa2809",
"exifr": "patch:exifr@npm:7.1.3#.yarn/patches/exifr-npm-7.1.3-e3f1c7a57d"
}
}
2 changes: 0 additions & 2 deletions private/dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
},
"devDependencies": {
"@babel/core": "^7.4.4",
"@babel/plugin-transform-react-jsx": "^7.10.4",
"@babel/types": "^7.17.0",
"autoprefixer": "^10.2.6",
"babel-plugin-transform-commonjs": "1.1.6",
"postcss-dir-pseudo-class": "^5.0.0",
"postcss-logical": "^4.0.2",
"vite": "^2.7.1"
Expand Down
143 changes: 27 additions & 116 deletions private/dev/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { fileURLToPath } from 'node:url'
import { createRequire } from 'node:module'
import { transformAsync } from '@babel/core'
import t from '@babel/types'
import autoprefixer from 'autoprefixer'
Expand All @@ -9,50 +8,11 @@ import postcssDirPseudoClass from 'postcss-dir-pseudo-class'
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

const moduleTypeCache = new Map()
function isTypeModule (file) {
const packageFolder = file.slice(0, file.indexOf('/src/') + 1)

const cachedValue = moduleTypeCache.get(packageFolder)
if (cachedValue != null) return cachedValue

// eslint-disable-next-line import/no-dynamic-require, global-require
const { type } = createRequire(packageFolder)('./package.json')
const typeModule = type === 'module'
moduleTypeCache.set(packageFolder, typeModule)
return typeModule
}
const packageLibImport = /^@uppy\/([^/]+)\/lib\/(.+)$/
const packageEntryImport = /^@uppy\/([^/]+)$/
function isSpecifierTypeModule (specifier) {
const packageLib = packageLibImport.exec(specifier)
if (packageLib != null) {
return isTypeModule(`${PACKAGES_ROOT}@uppy/${packageLib[1]}/src/${packageLib[2]}`)
}
const packageEntry = packageEntryImport.exec(specifier)
if (packageEntry != null) {
return isTypeModule(`${PACKAGES_ROOT}@uppy/${packageEntry[1]}/src/index.js`)
}
return false
}

const JS_FILE_EXTENSION = /\.jsx?$/

/**
* @type {import('vite').UserConfig}
*/
const config = {
envDir: fileURLToPath(ROOT),
build: {
commonjsOptions: {
defaultIsModuleExports: true,
},
},
css: {
postcss: {
plugins: [
Expand Down Expand Up @@ -87,98 +47,49 @@ const config = {
],
},
plugins: [
// TODO: remove plugin when we switch to ESM and get rid of JSX inside .js files.
// TODO: remove plugin when we remove the socket.io require call in @uppy/transloadit/src/Assembly.
{
name: 'vite-plugin-jsx-commonjs',
// TODO: remove this hack when we get rid of JSX inside .js files.
enforce: 'pre',
name: 'vite-plugin-rewrite-dynamic-socketIo-require',
// eslint-disable-next-line consistent-return
resolveId (id) {
if (id.startsWith(PACKAGES_ROOT) && JS_FILE_EXTENSION.test(id)) {
return id
}
// TODO: remove this hack when we get rid of JSX inside .js files.
if (counter++ < 2) {
if (id.startsWith(PACKAGES_ROOT) && id.endsWith('transloadit/src/Assembly.js')) {
return id
}
},
transform (code, id) {
if (id.startsWith(PACKAGES_ROOT) && JS_FILE_EXTENSION.test(id)) {
return transformAsync(code, isTypeModule(id) ? {
if (id.startsWith(PACKAGES_ROOT) && id.endsWith('transloadit/src/Assembly.js')) {
return transformAsync(code, {
plugins: [
id.endsWith('.jsx') ? ['@babel/plugin-transform-react-jsx', { pragma: 'h' }] : {},
{
// On type: "module" packages, we still want to rewrite import
// statements that tries to access a named export from a CJS
// module to using only the default import.
visitor: {
ImportDeclaration (path) {
const { specifiers, source: { value } } = path.node
if (value.startsWith('@uppy/') && !isSpecifierTypeModule(value)
&& specifiers.some(node => node.type !== 'ImportDefaultSpecifier')) {
const oldSpecifiers = specifiers[0].type === 'ImportDefaultSpecifier'
// If there's a default import, it must come first.
? specifiers.splice(1)
// If there's no default import, we create one from a random identifier.
: specifiers.splice(0, specifiers.length, t.importDefaultSpecifier(t.identifier(`_import_${counter++}`)))
if (oldSpecifiers[0].type === 'ImportNamespaceSpecifier') {
// import defaultVal, * as namespaceImport from '@uppy/package'
// is transformed into:
// import defaultVal from '@uppy/package'; const namespaceImport = defaultVal
path.insertAfter(
t.variableDeclaration('const', [t.variableDeclarator(
oldSpecifiers[0].local,
specifiers[0].local,
)]),
)
} else {
// import defaultVal, { exportedVal as importedName, other } from '@uppy/package'
// is transformed into:
// import defaultVal from '@uppy/package'; const { exportedVal: importedName, other } = defaultVal
path.insertAfter(t.variableDeclaration('const', [t.variableDeclarator(
t.objectPattern(
oldSpecifiers.map(specifier => t.objectProperty(
t.identifier(specifier.imported.name),
specifier.local,
)),
),
specifiers[0].local,
)]))
FunctionDeclaration (path) {
if (path.node.id.name === 'requireSocketIo') {
const prevSibling = path.getPrevSibling()
if (t.isImportDeclaration(prevSibling) && prevSibling.node.specifiers?.length === 1
&& t.isImportDefaultSpecifier(prevSibling.node.specifiers[0])
&& prevSibling.node.specifiers[0].local.name === 'socketIo') {
// The require call has already been rewritten to an import statement.
return
}
if (!t.isVariableDeclaration(prevSibling)) {
const { type, loc } = prevSibling.node
throw new Error(`Unexpected ${type} at line ${loc.start.line}, cannot apply requireSocketIo hack`)
}
}
},

// Very specific hack to avoid a breaking change when the file was refactored to ESM.
// TODO: remove this hack in the next release.
...(id.endsWith('transloadit/src/Assembly.js') ? {
FunctionDeclaration (path) {
if (path.node.id.name === 'requireSocketIo') {
const prevSibling = path.getPrevSibling()
if (!t.isVariableDeclaration(prevSibling) || prevSibling.node.declarations[0].id.name !== 'socketIo') {
// The require call has already been rewritten to an import statement.
return
}

const { id:socketIoIdentifier } = prevSibling.node.declarations[0]
const { id:socketIoIdentifier } = prevSibling.node.declarations[0]

prevSibling.replaceWith(t.importDeclaration(
[t.importDefaultSpecifier(socketIoIdentifier)],
t.stringLiteral('socket.io-client'),
))
path.replaceWith(t.functionDeclaration(path.node.id, path.node.params, t.blockStatement([
t.returnStatement(socketIoIdentifier),
])))
}
},
} : null),
prevSibling.replaceWith(t.importDeclaration(
[t.importDefaultSpecifier(socketIoIdentifier)],
t.stringLiteral('socket.io-client'),
))
path.replaceWith(t.functionDeclaration(path.node.id, path.node.params, t.blockStatement([
t.returnStatement(socketIoIdentifier),
])))
}
},
},
},
],
} : {
plugins: [
['@babel/plugin-transform-react-jsx', { pragma: 'h' }],
'transform-commonjs',
],
})
}
return code
Expand Down
24 changes: 0 additions & 24 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9250,11 +9250,9 @@ __metadata:
resolution: "@uppy-dev/dev@workspace:private/dev"
dependencies:
"@babel/core": ^7.4.4
"@babel/plugin-transform-react-jsx": ^7.10.4
"@babel/types": ^7.17.0
"@uppy/companion": "workspace:^"
autoprefixer: ^10.2.6
babel-plugin-transform-commonjs: 1.1.6
postcss-dir-pseudo-class: ^5.0.0
postcss-logical: ^4.0.2
vite: ^2.7.1
Expand Down Expand Up @@ -12999,28 +12997,6 @@ __metadata:
languageName: node
linkType: hard

"babel-plugin-transform-commonjs@npm:1.1.6":
version: 1.1.6
resolution: "babel-plugin-transform-commonjs@npm:1.1.6"
dependencies:
"@babel/helper-plugin-utils": ^7.0.0
peerDependencies:
"@babel/core": ">=7"
checksum: fc3f938b5d593457726c53e92305a3f1a3119524f057dedbe5adf16bc85b5b2bb376f8d50deb96bbf1aa168c46ba1c4a6a1cab35837d27049d2ab9fc402aeb9e
languageName: node
linkType: hard

"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.":
version: 1.1.6
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."
dependencies:
"@babel/helper-plugin-utils": ^7.0.0
peerDependencies:
"@babel/core": ">=7"
checksum: 5995b2641a8551fcc8d0f9d24222ae15698100917edffc8d98f8033cc65b1e577e6b346ac4dbf2456425e494bd1caf1f6646002163953a88290e901f611d83ad
languageName: node
linkType: hard

"babel-preset-current-node-syntax@npm:^1.0.0":
version: 1.0.1
resolution: "babel-preset-current-node-syntax@npm:1.0.1"
Expand Down