Browse Source
feat(compiler-core): add current filename to TransformContext (#8950)
pull/5953/head
Adrien Foulon
2 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with
22 additions and
1 deletions
-
packages/compiler-core/__tests__/transform.spec.ts
-
packages/compiler-core/src/transform.ts
|
|
@ -200,6 +200,26 @@ describe('compiler: transform', () => { |
|
|
|
expect((ast as any).children[0].props[0].exp.content).toBe(`_hoisted_1`) |
|
|
|
expect((ast as any).children[1].props[0].exp.content).toBe(`_hoisted_2`) |
|
|
|
}) |
|
|
|
|
|
|
|
test('context.filename and selfName', () => { |
|
|
|
const ast = baseParse(`<div />`) |
|
|
|
|
|
|
|
const calls: any[] = [] |
|
|
|
const plugin: NodeTransform = (node, context) => { |
|
|
|
calls.push({ ...context }) |
|
|
|
} |
|
|
|
|
|
|
|
transform(ast, { |
|
|
|
filename: '/the/fileName.vue', |
|
|
|
nodeTransforms: [plugin] |
|
|
|
}) |
|
|
|
|
|
|
|
expect(calls.length).toBe(2) |
|
|
|
expect(calls[1]).toMatchObject({ |
|
|
|
filename: '/the/fileName.vue', |
|
|
|
selfName: 'FileName' |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
test('onError option', () => { |
|
|
|
const ast = baseParse(`<div/>`) |
|
|
|
|
|
@ -84,7 +84,7 @@ export interface ImportItem { |
|
|
|
|
|
|
|
export interface TransformContext |
|
|
|
extends Required< |
|
|
|
Omit<TransformOptions, 'filename' | keyof CompilerCompatOptions> |
|
|
|
Omit<TransformOptions, keyof CompilerCompatOptions> |
|
|
|
>, |
|
|
|
CompilerCompatOptions { |
|
|
|
selfName: string | null |
|
|
@ -153,6 +153,7 @@ export function createTransformContext( |
|
|
|
const nameMatch = filename.replace(/\?.*$/, '').match(/([^/\\]+)\.\w+$/) |
|
|
|
const context: TransformContext = { |
|
|
|
// options
|
|
|
|
filename, |
|
|
|
selfName: nameMatch && capitalize(camelize(nameMatch[1])), |
|
|
|
prefixIdentifiers, |
|
|
|
hoistStatic, |
|
|
|