Skip to content

Commit

Permalink
fix: support multiple imports of one module with multiple lines
Browse files Browse the repository at this point in the history
e.g.
```
import {
  a,
  b,
  c,
} from 'x';
```
  • Loading branch information
wenfw committed Oct 1, 2024
1 parent 80e70b8 commit 1c23b91
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ export const export1 = 'export1'

export const export2 = 'export2'

export const export3 = 'export3'

export const export4 = 'export4'

export const export5 = 'export5'

// @ts-expect-error
window.sideEffect = 'Side Effect'

Expand Down
65 changes: 63 additions & 2 deletions npm/vite-plugin-cypress-esm/cypress/component/importSyntax.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ import { default as alias } from './fixtures/kitchenSink'
import defaultExport2, { export2 } from './fixtures/kitchenSink'
import defaultExport3, * as name2 from './fixtures/kitchenSink'
import { export1 as e1, export2 as e2 } from './fixtures/kitchenSink'
import {
export3,
export4,
} from './fixtures/kitchenSink'
import {
export3 as alias3,
export4 as alias4,
} from './fixtures/kitchenSink'
import defaultExport4, {
export5,
} from './fixtures/kitchenSink'
import './fixtures/kitchenSink'

// Examples for all syntax
Expand All @@ -19,6 +30,9 @@ describe('supports every combination of import syntax in a single file', () => {
expect(defaultExport1).to.deep.eq({
export1: 'export1',
export2: 'export2',
export3: 'export3',
export4: 'export4',
export5: 'export5',
default: {
export1: 'export1',
export2: 'export2',
Expand All @@ -30,6 +44,9 @@ describe('supports every combination of import syntax in a single file', () => {
expect(name1).to.deep.eq({
export1: 'export1',
export2: 'export2',
export3: 'export3',
export4: 'export4',
export5: 'export5',
default: {
export1: 'export1',
export2: 'export2',
Expand All @@ -38,7 +55,7 @@ describe('supports every combination of import syntax in a single file', () => {
})

it('Import { export1 } from "./kitchenSink"', () => {
expect(export1).to.deep.eq(export1)
expect(export1).to.deep.eq('export1')
})

it('Import { export1 as alias1 } from "./kitchenSink"', () => {
Expand All @@ -56,19 +73,25 @@ describe('supports every combination of import syntax in a single file', () => {
expect(defaultExport2).to.deep.eq({
export1: 'export1',
export2: 'export2',
export3: 'export3',
export4: 'export4',
export5: 'export5',
default: {
export1: 'export1',
export2: 'export2',
},
})

expect(export2).to.eq(export2)
expect(export2).to.eq('export2')
})

it('Import defaultExport3, * as name2 from "./kitchenSink"', () => {
expect(defaultExport3).to.deep.eq({
export1: 'export1',
export2: 'export2',
export3: 'export3',
export4: 'export4',
export5: 'export5',
default: {
export1: 'export1',
export2: 'export2',
Expand All @@ -80,6 +103,9 @@ describe('supports every combination of import syntax in a single file', () => {
expect(name2).to.deep.eq({
export1: 'export1',
export2: 'export2',
export3: 'export3',
export4: 'export4',
export5: 'export5',
default: {
export1: 'export1',
export2: 'export2',
Expand All @@ -92,6 +118,41 @@ describe('supports every combination of import syntax in a single file', () => {
expect(e2).to.deep.eq(export2)
})

it(`import {
export3,
export4,
} from './fixtures/kitchenSink'`, () => {
expect(export3).to.deep.eq('export3')
expect(export4).to.deep.eq('export4')
})

it(`import {
export3 as alias3,
export4 as alias4,
} from './fixtures/kitchenSink'`, () => {
expect(alias3).to.deep.eq(export3)
expect(alias4).to.deep.eq(export4)
})

it(`import defaultExport4, {
export5,
} from './fixtures/kitchenSink'`, () => {
console.log(defaultExport4)
expect(defaultExport4).to.deep.eq({
export1: 'export1',
export2: 'export2',
export3: 'export3',
export4: 'export4',
export5: 'export5',
default: {
export1: 'export1',
export2: 'export2',
},
})

expect(export5).to.eq('export5')
})

it('Import "./kitchenSink"', () => {
// @ts-expect-error
expect(window.sideEffect).to.eq('Side Effect')
Expand Down
2 changes: 1 addition & 1 deletion npm/vite-plugin-cypress-esm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

return code.replace(
importRegex,
Expand Down

0 comments on commit 1c23b91

Please sign in to comment.