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

fix: Some fixes and improvements to vite-plugin-cypress-esm #29368

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from

Conversation

M165437
Copy link

@M165437 M165437 commented Apr 19, 2024

We are using this plugin with a large react application and ran into issues. These changes address some of them.

Additional details

  • The plugin seemed to have been renamed in the past. I updated some occurrences of the previous name vite-plugin-mock-esm. The debug string mentioned in the README now actually works: DEBUG=cypress:vite-plugin-cypress-esm.
  • The regex capture group for the import target included quotes and semicolon. I updated it to only capture what is between the quotes. For example, in our application, we import a named export "from" (import { from } from 'myPackage'). This caused problems. The updated regex now handles this case.
  • Additionally, I'm now removing a possible leading dot slash from the import target which further improves picomatch behaviour (see It seems like ./ breaks matching. micromatch/picomatch#77).

How has the user experience changed?

I corrected the picomatch glob matching. It's not trying to match "./folder/file"; anymore but correctly (bash spec conform) the actual path folder/file. This might lead to matching failures for some people who wrote glob patterns for the previous string (that included the quotes and semicolon). Still, I believe this change is necessary as it's the correct way to match globs.

@CLAassistant
Copy link

CLAassistant commented Apr 19, 2024

CLA assistant check
All committers have signed the CLA.

@cypress-app-bot
Copy link
Collaborator

@M165437 M165437 changed the title Some fixes and improvements to vite-plugin-cypress-esm fix: Some fixes and improvements to vite-plugin-cypress-esm Apr 19, 2024
npm/vite-plugin-cypress-esm/src/index.ts Outdated Show resolved Hide resolved
npm/vite-plugin-cypress-esm/src/index.ts Outdated Show resolved Hide resolved
npm/vite-plugin-cypress-esm/src/index.ts Outdated Show resolved Hide resolved
import type { Plugin } from 'vite'
import fs from 'fs'
import path from 'path'
const debug = debugFn('cypress:vite-plugin-mock-esm')
const debug = debugFn('cypress:vite-plugin-cypress-esm')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to the debug string already mentioned in the README.

@jennifer-shehane
Copy link
Member

@M165437 Thanks for the contribution! There are some tests failing from these changes, can you take a look? https://app.circleci.com/pipelines/github/cypress-io/cypress/61103/workflows/bb62c8f0-bf9c-4838-aec4-556582a46fdf/jobs/2540478

@@ -120,7 +120,7 @@ export const CypressEsm = (options?: CypressEsmOptions): Plugin => {

// Ensure import comes at start of line *or* is prefixed by a space so we don't capture things like
// `Refresh.__hmr_import('')
const importRegex = /(?<=^|\s)import (.+?) from (.*)/g
const importRegex = /(?<=^|\s)import (.+?) from ['"](.*?)['"]/g
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only capture what's between the quotes.

if (!replacement.endsWith(';')) {
replacement += ';'
}
let replacement = `import * as cypress_${moduleIdentifier}_${++counter} from '${importTarget}';`
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add quotes and semicolon as they are not included in the import target anymore.

Comment on lines +78 to +79
// Remove leading dot slash (https://github.com/micromatch/picomatch/issues/77)
const importTargetPath = importTarget.replace(/^\.\//, '')
Copy link
Author

@M165437 M165437 Apr 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove possible leading dot slash. See micromatch/picomatch#77

@M165437
Copy link
Author

M165437 commented Apr 21, 2024

@jennifer-shehane The tests are passing now. WDYT? :)

@jennifer-shehane
Copy link
Member

@M165437 We'll have to assign some reviewers, hopefully this sprint.

@@ -209,6 +209,12 @@ export const CypressEsm = (options?: CypressEsmOptions): Plugin => {
return code.replace(
RE,
(match, importVars: string, importTarget: string) => {
if (isImportOnIgnoreList(importTarget)) {
Copy link
Author

@M165437 M165437 Apr 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last commit to also ignore dynamic imports present on the ignoreImportList

@mschile mschile requested a review from ryanthemanuel May 7, 2024 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants