Skip to content

Commit 095ed0a

Browse files
committed
fix(transform): don't parse vue template blocks
1 parent 866b8d6 commit 095ed0a

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/transform.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const MagicRegExpTransformPlugin = createUnplugin(() => {
2020
const { type } = parseQuery(search)
2121

2222
// vue files
23-
if (pathname.endsWith('.vue') && (type === 'template' || type === 'script' || !search)) {
23+
if (pathname.endsWith('.vue') && (type === 'script' || !search)) {
2424
return true
2525
}
2626

test/transform.test.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,26 @@ import { parse } from 'acorn'
44
import { MagicRegExpTransformPlugin } from '../src/transform'
55

66
describe('transformer', () => {
7+
const couldTransform = [
8+
"import { createRegExp, exactly, anyOf } from 'magic-regexp'",
9+
"const re1 = createRegExp(exactly('bar').notBefore('foo'))",
10+
]
11+
12+
it('ignores non-JS files', () => {
13+
expect(transform(couldTransform, 'test.css')).toBeUndefined()
14+
})
15+
16+
it('transforms vue script blocks', () => {
17+
expect(transform(couldTransform, 'test.vue?type=script')).toBeDefined()
18+
expect(transform(couldTransform, 'test.vue')).toBeDefined()
19+
expect(transform(couldTransform, 'test.vue?type=template')).toBeUndefined()
20+
})
21+
22+
it('ignores code without imports from magic-regexp', () => {
23+
expect(transform(couldTransform[1])).toBeUndefined()
24+
expect(transform(['// magic-regexp', couldTransform[1]])).toBeUndefined()
25+
})
26+
727
it('preserves context for dynamic regexps', () => {
828
expect(
929
transform([
@@ -15,6 +35,7 @@ describe('transformer', () => {
1535

1636
it('statically replaces regexps where possible', () => {
1737
const code = transform([
38+
"import { something } from 'other-module'",
1839
"import { createRegExp, exactly, anyOf } from 'magic-regexp'",
1940
'//', // this lets us tree-shake the import for use in our test-suite
2041
"const re1 = createRegExp(exactly('bar').notBefore('foo'))",
@@ -24,7 +45,8 @@ describe('transformer', () => {
2445
"re3.test('/foo/bar')",
2546
])
2647
expect(code).toMatchInlineSnapshot(`
27-
"import { createRegExp, exactly, anyOf } from 'magic-regexp'
48+
"import { something } from 'other-module'
49+
import { createRegExp, exactly, anyOf } from 'magic-regexp'
2850
//
2951
const re1 = /bar(?!foo)/
3052
const re2 = /(bar|foo)/
@@ -55,11 +77,11 @@ describe('transformer', () => {
5577
})
5678
})
5779

58-
const transform = (code: string | string[]) => {
80+
const transform = (code: string | string[], id = 'some-id.js') => {
5981
const plugin = MagicRegExpTransformPlugin.vite()
6082
return plugin.transform.call(
6183
{ parse: (code: string) => parse(code, { ecmaVersion: 2022, sourceType: 'module' }) },
6284
Array.isArray(code) ? code.join('\n') : code,
63-
'some-id.js'
85+
id
6486
)?.code
6587
}

0 commit comments

Comments
 (0)