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
base: develop
Are you sure you want to change the base?
Conversation
|
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') |
There was a problem hiding this comment.
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.
@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 |
There was a problem hiding this comment.
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}';` |
There was a problem hiding this comment.
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.
// Remove leading dot slash (https://github.com/micromatch/picomatch/issues/77) | ||
const importTargetPath = importTarget.replace(/^\.\//, '') |
There was a problem hiding this comment.
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
@jennifer-shehane The tests are passing now. WDYT? :) |
@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)) { |
There was a problem hiding this comment.
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
We are using this plugin with a large react application and ran into issues. These changes address some of them.
Additional details
vite-plugin-mock-esm
. The debug string mentioned in the README now actually works:DEBUG=cypress:vite-plugin-cypress-esm
.import { from } from 'myPackage'
). This caused problems. The updated regex now handles this case.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 pathfolder/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.