Skip to content

Commit 6ea9062

Browse files
committed
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
1 parent 3be4e3c commit 6ea9062

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

packages/compiler-core/__tests__/transform.spec.ts

+19
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,25 @@ describe('compiler: transform', () => {
200200
expect((ast as any).children[0].props[0].exp.content).toBe(`_hoisted_1`)
201201
expect((ast as any).children[1].props[0].exp.content).toBe(`_hoisted_2`)
202202
})
203+
204+
test('context.filename', () => {
205+
const ast = baseParse(`<div />`)
206+
207+
const calls: any[] = []
208+
const plugin: NodeTransform = (node, context) => {
209+
calls.push({ ...context })
210+
}
211+
212+
transform(ast, {
213+
filename: '/the/filename.vue',
214+
nodeTransforms: [plugin]
215+
})
216+
217+
expect(calls.length).toBe(1)
218+
expect(calls[0]).toMatchObject({
219+
filename: '/the/filename.vue'
220+
})
221+
})
203222

204223
test('onError option', () => {
205224
const ast = baseParse(`<div/>`)

packages/compiler-core/src/transform.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export interface ImportItem {
8484

8585
export interface TransformContext
8686
extends Required<
87-
Omit<TransformOptions, 'filename' | keyof CompilerCompatOptions>
87+
Omit<TransformOptions, keyof CompilerCompatOptions>
8888
>,
8989
CompilerCompatOptions {
9090
selfName: string | null
@@ -152,6 +152,7 @@ export function createTransformContext(
152152
const nameMatch = filename.replace(/\?.*$/, '').match(/([^/\\]+)\.\w+$/)
153153
const context: TransformContext = {
154154
// options
155+
filename,
155156
selfName: nameMatch && capitalize(camelize(nameMatch[1])),
156157
prefixIdentifiers,
157158
hoistStatic,

0 commit comments

Comments
 (0)