Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,13 @@ export class Compiler {
return new Function(...args, template) as CompiledTemplate
}

/**
* The function registered using "claimTag", if any
*/
get claimTagFn(): ClaimTagFn | undefined {
return this.#claimTagFn
}

/**
* Define a function to claim tags
*/
Expand Down
14 changes: 14 additions & 0 deletions src/edge/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ export class Edge {
this.loader = new Loader()
}

const previousCompiler = this.compiler
const previousAsyncCompiler = this.asyncCompiler

this.compiler = new Compiler(this.loader, this.tags, this.processor, {
cache: !!options.cache,
async: false,
Expand All @@ -132,6 +135,17 @@ export class Edge {
cache: !!options.cache,
async: true,
})

/**
* Keep the tags claimed on the previous compilers. Plugins run
* once and do not claim them again on the new compilers
*/
if (previousCompiler?.claimTagFn) {
this.compiler.claimTag(previousCompiler.claimTagFn)
}
if (previousAsyncCompiler?.claimTagFn) {
this.asyncCompiler.claimTag(previousAsyncCompiler.claimTagFn)
}
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/supercharged.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ class SuperChargedComponents {
* The superCharged plugin converts components stored within the
* components directory of all the disk to Edge tags.
*/
let superCharged: SuperChargedComponents
const instances = new WeakMap<Edge, SuperChargedComponents>()
export const pluginSuperCharged: PluginFn<{ recurring: boolean }> = (edge, firstRun) => {
if (firstRun) {
superCharged = new SuperChargedComponents(edge)
instances.set(edge, new SuperChargedComponents(edge))
}
superCharged.refreshComponents()
instances.get(edge)!.refreshComponents()
}
35 changes: 35 additions & 0 deletions tests/edge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,41 @@ test.group('Edge', () => {
assert.equal(output.trim(), 'Hello virk')
})

test('refresh components list of each edge instance', async ({ assert, fs }) => {
const edgeA = new Edge()
edgeA.mount(join(fs.basePath, 'a'))
await fs.create('a/foo.edge', '@!foo({ username })')
await fs.create('a/components/foo.edge', 'Hello {{ username }}')
await fs.create('a/bar.edge', '@!bar({ username })')

const edgeB = new Edge()
edgeB.mount(join(fs.basePath, 'b'))
await fs.create('b/foo.edge', '@!foo({ username })')

const output1 = await edgeA.render('foo', { username: 'virk' })
assert.equal(output1.trim(), 'Hello virk')
const output2 = await edgeB.render('foo', { username: 'virk' })
assert.equal(output2.trim(), '@!foo({ username })')

await fs.create('a/components/bar.edge', 'Bye {{ username }}')
const output3 = await edgeA.render('bar', { username: 'virk' })
assert.equal(output3.trim(), 'Bye virk')
})

test('render components as tags after configure', async ({ assert, fs }) => {
const edge = Edge.create()
await fs.create('foo.edge', '@!foo({ username })')
await fs.create('components/foo.edge', 'Hello {{ username }}')
edge.mount(fs.basePath)

const output = await edge.render('foo', { username: 'virk' })
assert.equal(output.trim(), 'Hello virk')

edge.configure({ cache: true })
const output1 = await edge.render('foo', { username: 'virk' })
assert.equal(output1.trim(), 'Hello virk')
})

test('refresh components list on each render', async ({ assert, fs }) => {
const edge = new Edge()
await fs.create('foo.edge', '@!foo({ username })')
Expand Down
Loading