|
1 | 1 | import MagicString from 'magic-string'
|
2 | 2 | import { shouldTransform as shouldTransformRefSugar, transform as transformRef } from '@vue/ref-transform'
|
3 |
| -import { ScriptSetupTransformOptions, TransformResult } from '../types' |
| 3 | +import { ResolvedOptions, ScriptSetupTransformOptions, TransformResult } from '../types' |
4 | 4 | import { parseSFC } from './parseSFC'
|
5 | 5 | import { transformScriptSetup } from './transformScriptSetup'
|
6 | 6 | import { transformSfcRefSugar } from './transformSfcRefSugar'
|
| 7 | +import { resolveOptions } from './options' |
7 | 8 |
|
8 |
| -export const importHelpersFrom = '@vue/composition-api' |
9 | 9 | const scriptSetupRE = /<script\s(.*\s)?setup(\s.*)?>/
|
10 | 10 |
|
11 | 11 | export function shouldTransform(code: string, id: string, options?: ScriptSetupTransformOptions): boolean {
|
12 |
| - if (options?.refTransform) |
13 |
| - return true |
14 |
| - return scriptSetupRE.test(code) |
| 12 | + return (options?.refTransform && shouldTransformRefSugar(code)) || scriptSetupRE.test(code) |
15 | 13 | }
|
16 | 14 |
|
17 | 15 | export function transform(input: string, id: string, options?: ScriptSetupTransformOptions): TransformResult {
|
| 16 | + if (!shouldTransform(input, id, options)) |
| 17 | + return null |
| 18 | + const resolved = resolveOptions(options) |
18 | 19 | if (!id.endsWith('.vue'))
|
19 |
| - return transformNonVue(input, id, options) |
| 20 | + return transformNonVue(input, id, resolved) |
20 | 21 | else
|
21 |
| - return transformVue(input, id, options) |
| 22 | + return transformVue(input, id, resolved) |
22 | 23 | }
|
23 | 24 |
|
24 |
| -export function transformNonVue(input: string, id: string, options: ScriptSetupTransformOptions | undefined): TransformResult { |
25 |
| - if (options?.refTransform && shouldTransformRefSugar(input)) { |
| 25 | +function transformNonVue(input: string, id: string, options: ResolvedOptions): TransformResult { |
| 26 | + if (options.refTransform && shouldTransformRefSugar(input)) { |
26 | 27 | return transformRef(input, {
|
27 | 28 | filename: id,
|
28 |
| - sourceMap: true, |
29 |
| - importHelpersFrom, |
| 29 | + sourceMap: options.sourceMap, |
| 30 | + importHelpersFrom: options.importHelpersFrom, |
30 | 31 | })
|
31 | 32 | }
|
32 | 33 | return null
|
33 | 34 | }
|
34 | 35 |
|
35 |
| -export function transformVue(input: string, id: string, options: ScriptSetupTransformOptions | undefined): TransformResult { |
| 36 | +function transformVue(input: string, id: string, options: ResolvedOptions): TransformResult { |
36 | 37 | const s = new MagicString(input)
|
37 | 38 |
|
38 | 39 | const sfc = parseSFC(input, id)
|
39 | 40 |
|
40 | 41 | if (options?.refTransform)
|
41 |
| - transformSfcRefSugar(sfc) |
| 42 | + transformSfcRefSugar(sfc, options) |
42 | 43 |
|
43 | 44 | const { code } = transformScriptSetup(sfc, options)
|
44 | 45 |
|
@@ -68,6 +69,8 @@ export function transformVue(input: string, id: string, options: ScriptSetupTran
|
68 | 69 | }
|
69 | 70 | return {
|
70 | 71 | code: s.toString(),
|
71 |
| - get map() { return s.generateMap() }, |
| 72 | + map: options.sourceMap |
| 73 | + ? s.generateMap() |
| 74 | + : null, |
72 | 75 | }
|
73 | 76 | }
|
0 commit comments