From 1c5f43110ea590ba78cd7fd25fe46c008d1f8a4b Mon Sep 17 00:00:00 2001 From: Tofandel Date: Thu, 10 Aug 2023 10:21:41 +0200 Subject: [PATCH 1/2] feat(compiler-core): add filename to TransformContext When using `nodeTransform`, I have found myself needing the filename to do filesystem operations, which well cannot be done without the filename of the current component in the context passed to the function --- .../compiler-core/__tests__/transform.spec.ts | 19 +++++++++++++++++++ packages/compiler-core/src/transform.ts | 3 ++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/compiler-core/__tests__/transform.spec.ts b/packages/compiler-core/__tests__/transform.spec.ts index 042865efb4e..fffe7dc7c35 100644 --- a/packages/compiler-core/__tests__/transform.spec.ts +++ b/packages/compiler-core/__tests__/transform.spec.ts @@ -200,6 +200,25 @@ 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', () => { + const ast = baseParse(`
`) + + 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' + }) + }) test('onError option', () => { const ast = baseParse(`
`) diff --git a/packages/compiler-core/src/transform.ts b/packages/compiler-core/src/transform.ts index 4d9e8c6ed49..e308accef79 100644 --- a/packages/compiler-core/src/transform.ts +++ b/packages/compiler-core/src/transform.ts @@ -84,7 +84,7 @@ export interface ImportItem { export interface TransformContext extends Required< - Omit + Omit >, CompilerCompatOptions { selfName: string | null @@ -152,6 +152,7 @@ export function createTransformContext( const nameMatch = filename.replace(/\?.*$/, '').match(/([^/\\]+)\.\w+$/) const context: TransformContext = { // options + filename, selfName: nameMatch && capitalize(camelize(nameMatch[1])), prefixIdentifiers, hoistStatic, From 59ab9aae632929f8563fd0c12fece94f4f652cb9 Mon Sep 17 00:00:00 2001 From: Tofandel Date: Thu, 10 Aug 2023 10:37:50 +0200 Subject: [PATCH 2/2] Also drop a test for context.selfName while we're there --- packages/compiler-core/__tests__/transform.spec.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/compiler-core/__tests__/transform.spec.ts b/packages/compiler-core/__tests__/transform.spec.ts index fffe7dc7c35..f316a671e07 100644 --- a/packages/compiler-core/__tests__/transform.spec.ts +++ b/packages/compiler-core/__tests__/transform.spec.ts @@ -201,7 +201,7 @@ describe('compiler: transform', () => { expect((ast as any).children[1].props[0].exp.content).toBe(`_hoisted_2`) }) - test('context.filename', () => { + test('context.filename and selfName', () => { const ast = baseParse(`
`) const calls: any[] = [] @@ -210,13 +210,14 @@ describe('compiler: transform', () => { } transform(ast, { - filename: '/the/filename.vue', + filename: '/the/fileName.vue', nodeTransforms: [plugin] }) expect(calls.length).toBe(2) expect(calls[1]).toMatchObject({ - filename: '/the/filename.vue' + filename: '/the/fileName.vue', + selfName: 'FileName' }) })